diff --git a/cmd/main.go b/cmd/main.go index 2d860526..b6f24b97 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,8 +19,9 @@ package main import ( "encoding/base64" "flag" - testtriggersv1 "github.com/kubeshop/testkube-operator/api/testtriggers/v1" "os" + + testtriggersv1 "github.com/kubeshop/testkube-operator/api/testtriggers/v1" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) @@ -75,6 +76,7 @@ type config struct { Fullname string TemplateCronjob string `split_words:"true"` Registry string + UseArgocdSync bool `split_words:"true"` } func init() { @@ -128,7 +130,6 @@ func main() { } mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ - Scheme: scheme, Metrics: metricsserver.Options{BindAddress: metricsAddr}, HealthProbeBindAddress: probeAddr, @@ -155,17 +156,19 @@ func main() { os.Exit(1) } if err = (&testscontrollers.TestReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - CronJobClient: cronjob.NewClient(mgr.GetClient(), httpConfig.Fullname, httpConfig.Port, templateCronjob, httpConfig.Registry), + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + CronJobClient: cronjob.NewClient(mgr.GetClient(), httpConfig.Fullname, httpConfig.Port, + templateCronjob, httpConfig.Registry, httpConfig.UseArgocdSync), }).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: cronjob.NewClient(mgr.GetClient(), httpConfig.Fullname, httpConfig.Port, templateCronjob, httpConfig.Registry), + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + CronJobClient: cronjob.NewClient(mgr.GetClient(), httpConfig.Fullname, httpConfig.Port, + templateCronjob, httpConfig.Registry, httpConfig.UseArgocdSync), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "TestSuite") os.Exit(1) diff --git a/internal/controller/tests/test_controller.go b/internal/controller/tests/test_controller.go index 9d3090b7..d3f7b461 100644 --- a/internal/controller/tests/test_controller.go +++ b/internal/controller/tests/test_controller.go @@ -115,7 +115,9 @@ func (r *TestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. options := cronjob.CronJobOptions{ Schedule: test.Spec.Schedule, - Resource: cronjob.TestResourceURI, + Resource: testsv3.Resource, + Version: testsv3.Version, + ResourceURI: cronjob.TestResourceURI, Data: string(data), Labels: test.Labels, CronJobTemplate: jobTemplate, @@ -128,7 +130,7 @@ func (r *TestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. if err != nil { if errors.IsNotFound(err) { if err = r.CronJobClient.Create(ctx, test.Name, - cronjob.GetMetadataName(test.Name, cronjob.TestResourceURI), req.NamespacedName.Namespace, options); err != nil { + cronjob.GetMetadataName(test.Name, cronjob.TestResourceURI), req.NamespacedName.Namespace, string(test.UID), options); err != nil { return ctrl.Result{}, err } } @@ -138,7 +140,7 @@ func (r *TestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. // Update CronJob if it was created before provided Test schedule if err = r.CronJobClient.Update(ctx, cronJob, test.Name, - cronjob.GetMetadataName(test.Name, cronjob.TestResourceURI), req.NamespacedName.Namespace, options); err != nil { + cronjob.GetMetadataName(test.Name, cronjob.TestResourceURI), req.NamespacedName.Namespace, string(test.UID), options); err != nil { return ctrl.Result{}, err } diff --git a/internal/controller/testsuite/testsuite_controller.go b/internal/controller/testsuite/testsuite_controller.go index 92892df4..7d31db00 100644 --- a/internal/controller/testsuite/testsuite_controller.go +++ b/internal/controller/testsuite/testsuite_controller.go @@ -116,7 +116,9 @@ func (r *TestSuiteReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( options := cronjob.CronJobOptions{ Schedule: testSuite.Spec.Schedule, - Resource: cronjob.TestSuiteResourceURI, + Resource: testsuitev3.Resource, + Version: testsuitev3.Version, + ResourceURI: cronjob.TestSuiteResourceURI, Data: string(data), Labels: testSuite.Labels, CronJobTemplate: jobTemplate, @@ -129,7 +131,7 @@ func (r *TestSuiteReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( if err != nil { if errors.IsNotFound(err) { if err = r.CronJobClient.Create(ctx, testSuite.Name, - cronjob.GetMetadataName(testSuite.Name, cronjob.TestSuiteResourceURI), req.NamespacedName.Namespace, options); err != nil { + cronjob.GetMetadataName(testSuite.Name, cronjob.TestSuiteResourceURI), req.NamespacedName.Namespace, string(testSuite.UID), options); err != nil { return ctrl.Result{}, err } } @@ -139,7 +141,7 @@ func (r *TestSuiteReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( // Update CronJob if it was created before provided Test schedule if err = r.CronJobClient.Update(ctx, cronJob, testSuite.Name, - cronjob.GetMetadataName(testSuite.Name, cronjob.TestSuiteResourceURI), req.NamespacedName.Namespace, options); err != nil { + cronjob.GetMetadataName(testSuite.Name, cronjob.TestSuiteResourceURI), req.NamespacedName.Namespace, string(testSuite.UID), options); err != nil { return ctrl.Result{}, err } diff --git a/pkg/cronjob/client.go b/pkg/cronjob/client.go index 3c5ec856..b1171a80 100644 --- a/pkg/cronjob/client.go +++ b/pkg/cronjob/client.go @@ -30,11 +30,14 @@ type Client struct { servicePort int cronJobTemplate string registry string + argoCDSync bool } type CronJobOptions struct { Schedule string Resource string + Version string + ResourceURI string Data string Labels map[string]string CronJobTemplate string @@ -49,21 +52,27 @@ type templateParameters struct { ServicePort int Schedule string Resource string + Version string + ResourceURI string CronJobTemplate string CronJobTemplateExtensions string Data string Labels map[string]string Registry string + ArgoCDSync bool + UID string } // NewClient is a method to create new cron job client -func NewClient(cli client.Client, serviceName string, servicePort int, cronJobTemplate, registry string) *Client { +func NewClient(cli client.Client, serviceName string, servicePort int, cronJobTemplate, registry string, + argoCDSync bool) *Client { return &Client{ Client: cli, serviceName: serviceName, servicePort: servicePort, cronJobTemplate: cronJobTemplate, registry: registry, + argoCDSync: argoCDSync, } } @@ -78,7 +87,7 @@ func (c *Client) Get(ctx context.Context, name, namespace string) (*batchv1.Cron } // Create is a method to create a cron job -func (c *Client) Create(ctx context.Context, id, name, namespace string, options CronJobOptions) error { +func (c *Client) Create(ctx context.Context, id, name, namespace, uid string, options CronJobOptions) error { template := c.cronJobTemplate if options.CronJobTemplate != "" { template = options.CronJobTemplate @@ -92,11 +101,15 @@ func (c *Client) Create(ctx context.Context, id, name, namespace string, options ServicePort: c.servicePort, Schedule: options.Schedule, Resource: options.Resource, + Version: options.Version, + ResourceURI: options.ResourceURI, CronJobTemplate: template, CronJobTemplateExtensions: options.CronJobTemplateExtensions, Data: options.Data, Labels: options.Labels, Registry: c.registry, + ArgoCDSync: c.argoCDSync, + UID: uid, } cronJobSpec, err := NewCronJobSpec(parameters) @@ -112,7 +125,7 @@ func (c *Client) Create(ctx context.Context, id, name, namespace string, options } // Update is a method to update an existing cron job -func (c *Client) Update(ctx context.Context, cronJob *batchv1.CronJob, id, name, namespace string, options CronJobOptions) error { +func (c *Client) Update(ctx context.Context, cronJob *batchv1.CronJob, id, name, namespace, uid string, options CronJobOptions) error { template := c.cronJobTemplate if options.CronJobTemplate != "" { template = options.CronJobTemplate @@ -126,11 +139,15 @@ func (c *Client) Update(ctx context.Context, cronJob *batchv1.CronJob, id, name, ServicePort: c.servicePort, Schedule: options.Schedule, Resource: options.Resource, + Version: options.Version, + ResourceURI: options.ResourceURI, CronJobTemplate: template, CronJobTemplateExtensions: options.CronJobTemplateExtensions, Data: options.Data, Labels: options.Labels, Registry: c.registry, + ArgoCDSync: c.argoCDSync, + UID: uid, } cronJobSpec, err := NewCronJobSpec(parameters)