Skip to content

Commit

Permalink
fix(knuu): testScope sanitized (#304)
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 May 6, 2024
1 parent 1012d39 commit 0bb8566
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 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": k8s.SanitizeName(testScope),
"knuu.sh/scope": testScope,
"knuu.sh/test-started": startTime,
"knuu.sh/name": i.name,
"knuu.sh/k8s-name": i.k8sName,
Expand Down
8 changes: 4 additions & 4 deletions pkg/knuu/knuu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down Expand Up @@ -226,15 +226,15 @@ 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())
commands = append(commands, fmt.Sprintf("kubectl delete namespace %s", k8sClient.Namespace()))

// 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, " && ")

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

Expand Down

0 comments on commit 0bb8566

Please sign in to comment.