@@ -20,11 +20,14 @@ import (
2020 "context"
2121 "fmt"
2222 "maps"
23+ "os/exec"
24+ "strings"
2325
2426 "github.com/onsi/ginkgo/v2"
2527 "github.com/onsi/gomega"
2628 corev1 "k8s.io/api/core/v1"
2729 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+ "sigs.k8s.io/controller-runtime/pkg/client"
2831)
2932
3033const (
@@ -40,6 +43,7 @@ type Framework struct {
4043
4144 namespace * corev1.Namespace
4245 namespacesToDelete []string
46+ resourcesToDelete []client.Object
4347}
4448
4549func NewFramework (namespacePrefix string ) * Framework {
@@ -73,12 +77,18 @@ func (f *Framework) Before() {
7377 gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
7478 ginkgo .By (fmt .Sprintf ("Created namespace %s" , ns .Name ))
7579 f .namespace = ns
76- f .AddNamespaceToDelete (ns .Name )
7780 }
7881}
7982
8083func (f * Framework ) After () {
8184 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+
8292 for _ , ns := range f .namespacesToDelete {
8393 ginkgo .By (fmt .Sprintf ("Delete namespace %s" , ns ))
8494 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
98108 APIVersion : corev1 .SchemeGroupVersion .String (),
99109 },
100110 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 ,
103113 },
104114 }
105115
@@ -118,3 +128,24 @@ func (f *Framework) Namespace() *corev1.Namespace {
118128func (f * Framework ) AddNamespaceToDelete (name string ) {
119129 f .namespacesToDelete = append (f .namespacesToDelete , name )
120130}
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