Skip to content

Commit

Permalink
feat: support argo cd sync (#197)
Browse files Browse the repository at this point in the history
* feat: support argo cd sync

* fix: rename config var

* fix: rename field

* fix: add vesion

* fix: rename field
  • Loading branch information
vsukhin authored Nov 15, 2023
1 parent 0a5b5ef commit 803ccbc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
19 changes: 11 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down Expand Up @@ -75,6 +76,7 @@ type config struct {
Fullname string
TemplateCronjob string `split_words:"true"`
Registry string
UseArgocdSync bool `split_words:"true"`
}

func init() {
Expand Down Expand Up @@ -128,7 +130,6 @@ func main() {
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{

Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
HealthProbeBindAddress: probeAddr,
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions internal/controller/tests/test_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
Expand All @@ -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
}

Expand Down
8 changes: 5 additions & 3 deletions internal/controller/testsuite/testsuite_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
Expand All @@ -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
}

Expand Down
23 changes: 20 additions & 3 deletions pkg/cronjob/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 803ccbc

Please sign in to comment.