From b7f0f503a965ff06d4b62d3ca8b07a5924b4dab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Ramon=20Ma=C3=B1es?= <32740567+jrmanes@users.noreply.github.com> Date: Thu, 18 Apr 2024 11:09:39 +0200 Subject: [PATCH] fix(scope): use SanitizeName to fix the name of the id (#277) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jose Ramon MaƱes --- pkg/knuu/instance_helper.go | 2 +- pkg/knuu/knuu.go | 5 +++-- pkg/knuu/preloader.go | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/knuu/instance_helper.go b/pkg/knuu/instance_helper.go index 6f50e5b..4f6e4cd 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": testScope, + "knuu.sh/scope": k8s.SanitizeName(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 4b0a6e2..7944231 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -34,6 +34,7 @@ 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) } @@ -202,7 +203,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", testScope, k8s.Namespace(), TimeoutHandlerInstance.String(), k8s.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), k8s.Namespace(), TimeoutHandlerInstance.String(), k8s.Namespace())) // Delete the namespace if it was created by knuu. if namespaceCreated { @@ -212,7 +213,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", testScope, k8s.Namespace())) + commands = append(commands, fmt.Sprintf("kubectl delete all,pvc,netpol,roles,serviceaccounts,rolebindings,configmaps -l knuu.sh/scope=%s -n %s", k8s.SanitizeName(testScope), k8s.Namespace())) finalCmd := strings.Join(commands, " && ") diff --git a/pkg/knuu/preloader.go b/pkg/knuu/preloader.go index c094636..31acc88 100644 --- a/pkg/knuu/preloader.go +++ b/pkg/knuu/preloader.go @@ -90,7 +90,7 @@ func (p *Preloader) preloadImages() error { labels := map[string]string{ "app": p.k8sName, "k8s.kubernetes.io/managed-by": "knuu", - "knuu.sh/scope": testScope, + "knuu.sh/scope": k8s.SanitizeName(testScope), "knuu.sh/test-started": startTime, }