diff --git a/pkg/knuu/instance_helper.go b/pkg/knuu/instance_helper.go index 27a6da3..e0cef81 100644 --- a/pkg/knuu/instance_helper.go +++ b/pkg/knuu/instance_helper.go @@ -66,7 +66,7 @@ func (i *Instance) getLabels() map[string]string { return map[string]string{ "app": i.k8sName, "k8s.kubernetes.io/managed-by": "knuu", - "knuu.sh/scope": k8s.SanitizeName(testScope), + "knuu.sh/scope": testScope, "knuu.sh/test-started": startTime, "knuu.sh/name": i.name, "knuu.sh/k8s-name": i.k8sName, diff --git a/pkg/knuu/knuu.go b/pkg/knuu/knuu.go index 7c503f0..773a45f 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -39,7 +39,6 @@ var ( func Initialize() error { t := time.Now() scope := fmt.Sprintf("%s-%03d", t.Format("20060102-150405"), t.Nanosecond()/1e6) - scope = k8s.SanitizeName(scope) return InitializeWithScope(scope) } @@ -60,7 +59,8 @@ func InitializeWithScope(scope string) error { return ErrCannotInitializeKnuuWithEmptyScope } - testScope = scope + // Sanitize the scope to ensure it is a valid Kubernetes name and match the namespace + testScope = k8s.SanitizeName(scope) t := time.Now() startTime = fmt.Sprintf("%s-%03d", t.Format("20060102-150405"), t.Nanosecond()/1e6) @@ -226,7 +226,7 @@ func handleTimeout() error { commands = append(commands, fmt.Sprintf("sleep %d", int64(timeout.Seconds()))) // Collects all resources (pods, services, etc.) within the specified namespace that match a specific label, excluding certain types, // and then deletes them. This is useful for cleaning up specific test resources before proceeding to delete the namespace. - commands = append(commands, fmt.Sprintf("kubectl get all,pvc,netpol,roles,serviceaccounts,rolebindings,configmaps -l knuu.sh/scope=%s -n %s -o json | jq -r '.items[] | select(.metadata.labels.\"knuu.sh/type\" != \"%s\") | \"\\(.kind)/\\(.metadata.name)\"' | xargs -r kubectl delete -n %s", k8s.SanitizeName(testScope), k8sClient.Namespace(), TimeoutHandlerInstance.String(), k8sClient.Namespace())) + commands = append(commands, fmt.Sprintf("kubectl get all,pvc,netpol,roles,serviceaccounts,rolebindings,configmaps -l knuu.sh/scope=%s -n %s -o json | jq -r '.items[] | select(.metadata.labels.\"knuu.sh/type\" != \"%s\") | \"\\(.kind)/\\(.metadata.name)\"' | xargs -r kubectl delete -n %s", testScope, k8sClient.Namespace(), TimeoutHandlerInstance.String(), k8sClient.Namespace())) // Delete the namespace as it was created by knuu. logrus.Debugf("The namespace generated [%s] will be deleted", k8sClient.Namespace()) @@ -234,7 +234,7 @@ func handleTimeout() error { // Delete all labeled resources within the namespace. // Unlike the previous command that excludes certain types, this command ensures that everything remaining is deleted. - commands = append(commands, fmt.Sprintf("kubectl delete all,pvc,netpol,roles,serviceaccounts,rolebindings,configmaps -l knuu.sh/scope=%s -n %s", k8s.SanitizeName(testScope), k8sClient.Namespace())) + commands = append(commands, fmt.Sprintf("kubectl delete all,pvc,netpol,roles,serviceaccounts,rolebindings,configmaps -l knuu.sh/scope=%s -n %s", testScope, k8sClient.Namespace())) finalCmd := strings.Join(commands, " && ") diff --git a/pkg/knuu/preloader.go b/pkg/knuu/preloader.go index eaacd6f..bd5b2c1 100644 --- a/pkg/knuu/preloader.go +++ b/pkg/knuu/preloader.go @@ -101,7 +101,7 @@ func (p *Preloader) preloadImages(ctx context.Context) error { labels := map[string]string{ "app": p.K8sName, "k8s.kubernetes.io/managed-by": "knuu", - "knuu.sh/scope": k8s.SanitizeName(testScope), + "knuu.sh/scope": testScope, "knuu.sh/test-started": startTime, }