diff --git a/cmd/main.go b/cmd/main.go index 13fbb3ec..b7fa3b73 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -81,6 +81,7 @@ type config struct { TemplateCronjob string `split_words:"true"` Registry string UseArgocdSync bool `split_words:"true"` + PurgeExecutions bool `split_words:"true"` } func init() { @@ -163,21 +164,23 @@ func main() { os.Exit(1) } if err = (&testscontrollers.TestReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - CronJobClient: cronJobClient, - ServiceName: httpConfig.Fullname, - ServicePort: httpConfig.Port, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + CronJobClient: cronJobClient, + ServiceName: httpConfig.Fullname, + ServicePort: httpConfig.Port, + PurgeExecutions: httpConfig.PurgeExecutions, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Test") os.Exit(1) } if err = (&testsuitecontrollers.TestSuiteReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - CronJobClient: cronJobClient, - ServiceName: httpConfig.Fullname, - ServicePort: httpConfig.Port, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + CronJobClient: cronJobClient, + ServiceName: httpConfig.Fullname, + ServicePort: httpConfig.Port, + PurgeExecutions: httpConfig.PurgeExecutions, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "TestSuite") os.Exit(1) @@ -230,11 +233,12 @@ func main() { os.Exit(1) } if err = (&testworkflowscontrollers.TestWorkflowReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - CronJobClient: cronJobClient, - ServiceName: httpConfig.Fullname, - ServicePort: httpConfig.Port, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + CronJobClient: cronJobClient, + ServiceName: httpConfig.Fullname, + ServicePort: httpConfig.Port, + PurgeExecutions: httpConfig.PurgeExecutions, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "TestWorkflow") os.Exit(1) diff --git a/internal/controller/tests/test_controller.go b/internal/controller/tests/test_controller.go index 90bd2317..45835d28 100644 --- a/internal/controller/tests/test_controller.go +++ b/internal/controller/tests/test_controller.go @@ -39,10 +39,11 @@ import ( // TestReconciler reconciles a Test object type TestReconciler struct { client.Client - Scheme *runtime.Scheme - CronJobClient *cronjob.Client - ServiceName string - ServicePort int + Scheme *runtime.Scheme + CronJobClient *cronjob.Client + ServiceName string + ServicePort int + PurgeExecutions bool } //+kubebuilder:rbac:groups=tests.testkube.io,resources=tests,verbs=get;list;watch;create;update;patch;delete @@ -168,6 +169,10 @@ func (r *TestReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *TestReconciler) deleteTest(testName, namespace string) (out string, err error) { + if !r.PurgeExecutions { + return out, nil + } + request, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("http://%s.%s.svc.cluster.local:%d/v1/tests/%s?skipDeleteCRD=true", r.ServiceName, namespace, r.ServicePort, testName), nil) diff --git a/internal/controller/testsuite/testsuite_controller.go b/internal/controller/testsuite/testsuite_controller.go index 61afb2a3..1241a2cc 100644 --- a/internal/controller/testsuite/testsuite_controller.go +++ b/internal/controller/testsuite/testsuite_controller.go @@ -38,10 +38,11 @@ import ( // TestSuiteReconciler reconciles a TestSuite object type TestSuiteReconciler struct { client.Client - Scheme *runtime.Scheme - CronJobClient *cronjob.Client - ServiceName string - ServicePort int + Scheme *runtime.Scheme + CronJobClient *cronjob.Client + ServiceName string + ServicePort int + PurgeExecutions bool } //+kubebuilder:rbac:groups=tests.testkube.io,resources=testsuites,verbs=get;list;watch;create;update;patch;delete @@ -168,6 +169,10 @@ func (r *TestSuiteReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *TestSuiteReconciler) deleteTestSuite(testSuiteName, namespace string) (out string, err error) { + if !r.PurgeExecutions { + return out, nil + } + request, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("http://%s.%s.svc.cluster.local:%d/v1/test-suites/%s?skipDeleteCRD=true", r.ServiceName, namespace, r.ServicePort, testSuiteName), nil) diff --git a/internal/controller/testworkflows/testworkflow_controller.go b/internal/controller/testworkflows/testworkflow_controller.go index 9b3c3c61..fac25cee 100644 --- a/internal/controller/testworkflows/testworkflow_controller.go +++ b/internal/controller/testworkflows/testworkflow_controller.go @@ -37,10 +37,11 @@ import ( // TestWorkflowReconciler reconciles a TestWorkflow object type TestWorkflowReconciler struct { client.Client - Scheme *runtime.Scheme - CronJobClient *cronjob.Client - ServiceName string - ServicePort int + Scheme *runtime.Scheme + CronJobClient *cronjob.Client + ServiceName string + ServicePort int + PurgeExecutions bool } //+kubebuilder:rbac:groups=testworkflows.testkube.io,resources=testworkflows,verbs=get;list;watch;create;update;patch;delete @@ -225,6 +226,10 @@ func (r *TestWorkflowReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *TestWorkflowReconciler) deleteTestWorkflow(testWorkflowName, namespace string) (out string, err error) { + if !r.PurgeExecutions { + return out, nil + } + request, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("http://%s.%s.svc.cluster.local:%d/v1/test-workflows/%s?skipDeleteCRD=true", r.ServiceName, namespace, r.ServicePort, testWorkflowName), nil)