Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support argo cd sync #197

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: support argo cd sync
  • Loading branch information
vsukhin committed Nov 14, 2023
commit 6cf3c0bc9928921969ec7e97a9286b22e9fbe5ff
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
ArgocdSync 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.ArgocdSync),
}).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.ArgocdSync),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TestSuite")
os.Exit(1)
Expand Down
7 changes: 4 additions & 3 deletions internal/controller/tests/test_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func (r *TestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.

options := cronjob.CronJobOptions{
Schedule: test.Spec.Schedule,
Resource: cronjob.TestResourceURI,
Resource: testsv3.Resource,
ResourceURI: cronjob.TestResourceURI,
Data: string(data),
Labels: test.Labels,
CronJobTemplate: jobTemplate,
Expand All @@ -128,7 +129,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 +139,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
7 changes: 4 additions & 3 deletions internal/controller/testsuite/testsuite_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (r *TestSuiteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

options := cronjob.CronJobOptions{
Schedule: testSuite.Spec.Schedule,
Resource: cronjob.TestSuiteResourceURI,
Resource: testsuitev3.Resource,
ResourceURI: cronjob.TestSuiteResourceURI,
Data: string(data),
Labels: testSuite.Labels,
CronJobTemplate: jobTemplate,
Expand All @@ -129,7 +130,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 +140,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
19 changes: 16 additions & 3 deletions pkg/cronjob/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ type Client struct {
servicePort int
cronJobTemplate string
registry string
argocdSync bool
}

type CronJobOptions struct {
Schedule string
Resource string
ResourceURI string
Data string
Labels map[string]string
CronJobTemplate string
Expand All @@ -49,21 +51,26 @@ type templateParameters struct {
ServicePort int
Schedule string
Resource string
ResourceURI string
CronJobTemplate string
CronJobTemplateExtensions string
Data string
Labels map[string]string
Registry string
ArgocdSync bool
UUID 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 +85,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, uuid string, options CronJobOptions) error {
template := c.cronJobTemplate
if options.CronJobTemplate != "" {
template = options.CronJobTemplate
Expand All @@ -92,11 +99,14 @@ func (c *Client) Create(ctx context.Context, id, name, namespace string, options
ServicePort: c.servicePort,
Schedule: options.Schedule,
Resource: options.Resource,
ResourceURI: options.ResourceURI,
CronJobTemplate: template,
CronJobTemplateExtensions: options.CronJobTemplateExtensions,
Data: options.Data,
Labels: options.Labels,
Registry: c.registry,
ArgocdSync: c.argocdSync,
UUID: uuid,
}

cronJobSpec, err := NewCronJobSpec(parameters)
Expand All @@ -112,7 +122,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, uuid string, options CronJobOptions) error {
template := c.cronJobTemplate
if options.CronJobTemplate != "" {
template = options.CronJobTemplate
Expand All @@ -126,11 +136,14 @@ func (c *Client) Update(ctx context.Context, cronJob *batchv1.CronJob, id, name,
ServicePort: c.servicePort,
Schedule: options.Schedule,
Resource: options.Resource,
ResourceURI: options.ResourceURI,
CronJobTemplate: template,
CronJobTemplateExtensions: options.CronJobTemplateExtensions,
Data: options.Data,
Labels: options.Labels,
Registry: c.registry,
ArgocdSync: c.argocdSync,
UUID: uuid,
}

cronJobSpec, err := NewCronJobSpec(parameters)
Expand Down
Loading