Skip to content

Commit

Permalink
fix: delete executions (#287) (#289)
Browse files Browse the repository at this point in the history
* fix: delete executions (#287)

Signed-off-by: Vladislav Sukhin <[email protected]>

* fix: add purge executions option (#291)

Signed-off-by: Vladislav Sukhin <[email protected]>

---------

Signed-off-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin authored Aug 6, 2024
1 parent 8eb900e commit f42711e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 15 deletions.
28 changes: 19 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -163,17 +164,23 @@ func main() {
os.Exit(1)
}
if err = (&testscontrollers.TestReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
CronJobClient: cronJobClient,
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,
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)
Expand Down Expand Up @@ -226,9 +233,12 @@ func main() {
os.Exit(1)
}
if err = (&testworkflowscontrollers.TestWorkflowReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
CronJobClient: cronJobClient,
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)
Expand Down
39 changes: 37 additions & 2 deletions internal/controller/tests/test_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package tests
import (
"context"
"encoding/json"
"fmt"
"net/http"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -37,8 +39,11 @@ import (
// TestReconciler reconciles a Test object
type TestReconciler struct {
client.Client
Scheme *runtime.Scheme
CronJobClient *cronjob.Client
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
Expand Down Expand Up @@ -66,6 +71,10 @@ func (r *TestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
return ctrl.Result{}, err
}

if _, err = r.deleteTest(req.NamespacedName.Name, req.NamespacedName.Namespace); err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -158,3 +167,29 @@ func (r *TestReconciler) SetupWithManager(mgr ctrl.Manager) error {
WithEventFilter(pred).
Complete(r)
}

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)
if err != nil {
return out, err
}

request.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(request)
if err != nil {
return out, err
}
defer resp.Body.Close()

if resp.StatusCode > 300 {
return out, fmt.Errorf("could not DELETE, statusCode: %d", resp.StatusCode)
}

return fmt.Sprintf("status: %d", resp.StatusCode), err
}
39 changes: 37 additions & 2 deletions internal/controller/testsuite/testsuite_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package testsuite
import (
"context"
"encoding/json"
"fmt"
"net/http"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -36,8 +38,11 @@ import (
// TestSuiteReconciler reconciles a TestSuite object
type TestSuiteReconciler struct {
client.Client
Scheme *runtime.Scheme
CronJobClient *cronjob.Client
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
Expand Down Expand Up @@ -65,6 +70,10 @@ func (r *TestSuiteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}

if _, err = r.deleteTestSuite(req.NamespacedName.Name, req.NamespacedName.Namespace); err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -158,3 +167,29 @@ func (r *TestSuiteReconciler) SetupWithManager(mgr ctrl.Manager) error {
WithEventFilter(pred).
Complete(r)
}

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)
if err != nil {
return out, err
}

request.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(request)
if err != nil {
return out, err
}
defer resp.Body.Close()

if resp.StatusCode > 300 {
return out, fmt.Errorf("could not DELETE, statusCode: %d", resp.StatusCode)
}

return fmt.Sprintf("status: %d", resp.StatusCode), err
}
39 changes: 37 additions & 2 deletions internal/controller/testworkflows/testworkflow_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package testworkflows

import (
"context"
"fmt"
"maps"
"net/http"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube-operator/pkg/cronjob"
Expand All @@ -35,8 +37,11 @@ import (
// TestWorkflowReconciler reconciles a TestWorkflow object
type TestWorkflowReconciler struct {
client.Client
Scheme *runtime.Scheme
CronJobClient *cronjob.Client
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
Expand Down Expand Up @@ -64,6 +69,10 @@ func (r *TestWorkflowReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, err
}

if _, err = r.deleteTestWorkflow(req.NamespacedName.Name, req.NamespacedName.Namespace); err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -215,3 +224,29 @@ func (r *TestWorkflowReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&testworkflowsv1.TestWorkflow{}).
Complete(r)
}

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)
if err != nil {
return out, err
}

request.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(request)
if err != nil {
return out, err
}
defer resp.Body.Close()

if resp.StatusCode > 300 {
return out, fmt.Errorf("could not DELETE, statusCode: %d", resp.StatusCode)
}

return fmt.Sprintf("status: %d", resp.StatusCode), err
}

0 comments on commit f42711e

Please sign in to comment.