Skip to content

Commit

Permalink
fix(scope): use SanitizeName to fix the name of the id (#277)
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <[email protected]>
  • Loading branch information
tty47 authored Apr 18, 2024
1 parent 3e59b40 commit b7f0f50
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/knuu/instance_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions pkg/knuu/knuu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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, " && ")

Expand Down
2 changes: 1 addition & 1 deletion pkg/knuu/preloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down

0 comments on commit b7f0f50

Please sign in to comment.