github.com/kubeshop/testkube@v1.17.23/pkg/tcl/testworkflowstcl/testworkflowcontroller/cleanup.go (about) 1 // Copyright 2024 Testkube. 2 // 3 // Licensed as a Testkube Pro file under the Testkube Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt 8 9 package testworkflowcontroller 10 11 import ( 12 "context" 13 "errors" 14 "fmt" 15 16 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 17 "k8s.io/client-go/kubernetes" 18 19 "github.com/kubeshop/testkube/internal/common" 20 "github.com/kubeshop/testkube/pkg/tcl/testworkflowstcl/testworkflowprocessor/constants" 21 ) 22 23 func cleanupConfigMaps(ctx context.Context, clientSet kubernetes.Interface, namespace, id string) error { 24 return clientSet.CoreV1().ConfigMaps(namespace).DeleteCollection(ctx, metav1.DeleteOptions{ 25 GracePeriodSeconds: common.Ptr(int64(0)), 26 PropagationPolicy: common.Ptr(metav1.DeletePropagationBackground), 27 }, metav1.ListOptions{ 28 LabelSelector: fmt.Sprintf("%s=%s", constants.ExecutionIdLabelName, id), 29 }) 30 } 31 32 func cleanupSecrets(ctx context.Context, clientSet kubernetes.Interface, namespace, id string) error { 33 return clientSet.CoreV1().Secrets(namespace).DeleteCollection(ctx, metav1.DeleteOptions{ 34 GracePeriodSeconds: common.Ptr(int64(0)), 35 PropagationPolicy: common.Ptr(metav1.DeletePropagationBackground), 36 }, metav1.ListOptions{ 37 LabelSelector: fmt.Sprintf("%s=%s", constants.ExecutionIdLabelName, id), 38 }) 39 } 40 41 func cleanupPods(ctx context.Context, clientSet kubernetes.Interface, namespace, id string) error { 42 return clientSet.CoreV1().Pods(namespace).DeleteCollection(ctx, metav1.DeleteOptions{ 43 GracePeriodSeconds: common.Ptr(int64(0)), 44 PropagationPolicy: common.Ptr(metav1.DeletePropagationBackground), 45 }, metav1.ListOptions{ 46 LabelSelector: fmt.Sprintf("%s=%s", constants.ExecutionIdLabelName, id), 47 }) 48 } 49 50 func cleanupJobs(ctx context.Context, clientSet kubernetes.Interface, namespace, id string) error { 51 return clientSet.BatchV1().Jobs(namespace).DeleteCollection(ctx, metav1.DeleteOptions{ 52 GracePeriodSeconds: common.Ptr(int64(0)), 53 PropagationPolicy: common.Ptr(metav1.DeletePropagationBackground), 54 }, metav1.ListOptions{ 55 LabelSelector: fmt.Sprintf("%s=%s", constants.ExecutionIdLabelName, id), 56 }) 57 } 58 59 func Cleanup(ctx context.Context, clientSet kubernetes.Interface, namespace, id string) error { 60 var errs []error 61 ops := []func(context.Context, kubernetes.Interface, string, string) error{ 62 cleanupJobs, 63 cleanupPods, 64 cleanupConfigMaps, 65 cleanupSecrets, 66 } 67 for _, op := range ops { 68 err := op(ctx, clientSet, namespace, id) 69 if err != nil { 70 errs = append(errs, err) 71 } 72 } 73 return errors.Join(errs...) 74 }