@@ -20,11 +20,14 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"maps"
23
+ "os/exec"
24
+ "strings"
23
25
24
26
"github.com/onsi/ginkgo/v2"
25
27
"github.com/onsi/gomega"
26
28
corev1 "k8s.io/api/core/v1"
27
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
+ "sigs.k8s.io/controller-runtime/pkg/client"
28
31
)
29
32
30
33
const (
@@ -40,6 +43,7 @@ type Framework struct {
40
43
41
44
namespace * corev1.Namespace
42
45
namespacesToDelete []string
46
+ resourcesToDelete []client.Object
43
47
}
44
48
45
49
func NewFramework (namespacePrefix string ) * Framework {
@@ -73,12 +77,18 @@ func (f *Framework) Before() {
73
77
gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
74
78
ginkgo .By (fmt .Sprintf ("Created namespace %s" , ns .Name ))
75
79
f .namespace = ns
76
- f .AddNamespaceToDelete (ns .Name )
77
80
}
78
81
}
79
82
80
83
func (f * Framework ) After () {
81
84
ginkgo .GinkgoHelper ()
85
+
86
+ for _ , resource := range f .resourcesToDelete {
87
+ ginkgo .By (fmt .Sprintf ("Delete resource %s" , resource .GetName ()))
88
+ err := f .client .Delete (context .Background (), resource )
89
+ gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
90
+ }
91
+
82
92
for _ , ns := range f .namespacesToDelete {
83
93
ginkgo .By (fmt .Sprintf ("Delete namespace %s" , ns ))
84
94
err := f .KubeClient ().CoreV1 ().Namespaces ().Delete (context .Background (), ns , metav1.DeleteOptions {})
@@ -98,8 +108,8 @@ func (f *Framework) CreateNamespace(prefix string, labels map[string]string) (*c
98
108
APIVersion : corev1 .SchemeGroupVersion .String (),
99
109
},
100
110
ObjectMeta : metav1.ObjectMeta {
101
- GenerateName : fmt .Sprintf ("%s-%s-" , NamespaceBasePrefix , prefix ),
102
- Labels : nsLabels ,
111
+ Name : fmt .Sprintf ("%s-%s-%s " , NamespaceBasePrefix , prefix , GetCommitHash () ),
112
+ Labels : nsLabels ,
103
113
},
104
114
}
105
115
@@ -118,3 +128,24 @@ func (f *Framework) Namespace() *corev1.Namespace {
118
128
func (f * Framework ) AddNamespaceToDelete (name string ) {
119
129
f .namespacesToDelete = append (f .namespacesToDelete , name )
120
130
}
131
+
132
+ func (f * Framework ) AddResourceToDelete (obj client.Object ) {
133
+ f .resourcesToDelete = append (f .resourcesToDelete , obj )
134
+ }
135
+
136
+ // func (f *Framework) GetRunHash() string {
137
+ // parts := strings.Split(f.Namespace().Name, "-")
138
+ // if len(parts) == 0 {
139
+ // return ""
140
+ // }
141
+ // return parts[len(parts)-1]
142
+ // }
143
+
144
+ func GetCommitHash () string {
145
+ ginkgo .GinkgoHelper ()
146
+
147
+ cmd := exec .Command ("git" , "rev-parse" , "--short" , "HEAD" )
148
+ stdout , err := cmd .Output ()
149
+ gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
150
+ return strings .TrimSpace (string (stdout ))
151
+ }
0 commit comments