diff --git a/Dockerfile b/Dockerfile index 5a355c2..64af319 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM golang:1.18 as builder +FROM golang:1.18 AS builder WORKDIR /workspace # Copy the Go Modules manifests diff --git a/Makefile b/Makefile index 566a337..920dba6 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Image URL to use all building/pushing image targets #IMG_TAG ?= $(shell git rev-parse --short HEAD) -IMG_TAG ?= 0.2.3 +IMG_TAG ?= 0.2.5 IMG_NAME ?= rekuberate-io-sleepcycles DOCKER_HUB_NAME ?= $(shell docker info | sed '/Username:/!d;s/.* //') IMG ?= $(DOCKER_HUB_NAME)/$(IMG_NAME):$(IMG_TAG) diff --git a/charts/sleepcycles/Chart.yaml b/charts/sleepcycles/Chart.yaml index 2f87cff..5dcfafc 100644 --- a/charts/sleepcycles/Chart.yaml +++ b/charts/sleepcycles/Chart.yaml @@ -13,9 +13,9 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.2.4 +version: 0.2.5 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "0.2.3" \ No newline at end of file +appVersion: "0.2.5" \ No newline at end of file diff --git a/charts/sleepcycles/values.yaml b/charts/sleepcycles/values.yaml index 0098ac0..9338ba1 100644 --- a/charts/sleepcycles/values.yaml +++ b/charts/sleepcycles/values.yaml @@ -32,7 +32,7 @@ controllerManager: - ALL image: repository: akyriako78/rekuberate-io-sleepcycles - tag: 0.2.3 + tag: 0.2.5 resources: limits: cpu: 500m diff --git a/controllers/sleepcycle_runners_cronjobs.go b/controllers/sleepcycle_runners_cronjobs.go index da0e25d..d5b9e12 100644 --- a/controllers/sleepcycle_runners_cronjobs.go +++ b/controllers/sleepcycle_runners_cronjobs.go @@ -7,11 +7,11 @@ import ( corev1alpha1 "github.com/rekuberate-io/sleepcycles/api/v1alpha1" batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "strconv" + "strings" ) var ( @@ -26,17 +26,28 @@ const ( Replicas = "rekuberate.io/replicas" ) -func (r *SleepCycleReconciler) getCronJob(ctx context.Context, objKey client.ObjectKey) (*batchv1.CronJob, error) { - var job batchv1.CronJob - if err := r.Get(ctx, objKey, &job); err != nil { - if apierrors.IsNotFound(err) { - return nil, nil - } +func (r *SleepCycleReconciler) getCronJob(ctx context.Context, ownedBy, target, namespace, suffix string) (*batchv1.CronJob, error) { + var jobs batchv1.CronJobList + labelSelector := map[string]string{ + OwnedBy: ownedBy, + Target: target, + } + listOptions := []client.ListOption{ + client.InNamespace(namespace), + client.MatchingLabels(labelSelector), + } + if err := r.List(ctx, &jobs, listOptions...); err != nil { return nil, err } - return &job, nil + for _, job := range jobs.Items { + if strings.Contains(job.Name, suffix) { + return &job, nil + } + } + + return nil, nil } func (r *SleepCycleReconciler) createCronJob( @@ -206,18 +217,19 @@ func (r *SleepCycleReconciler) reconcileCronJob( suffix = "wakeup" } - cronObjectKey := client.ObjectKey{ - Name: fmt.Sprintf("%s-%s-%s", sleepcycle.Name, targetMeta.Name, suffix), - Namespace: sleepcycle.Namespace, - } - cronjob, err := r.getCronJob(ctx, cronObjectKey) + cronjob, err := r.getCronJob(ctx, sleepcycle.Name, targetMeta.Name, sleepcycle.Namespace, suffix) if err != nil { - logger.Error(err, "unable to fetch runner", "cronjob", cronObjectKey.Name) + logger.Error(err, "unable to fetch runner", "sleepcycle", sleepcycle.Name, "target", targetMeta.Namespace, "op", suffix) return err } if cronjob == nil { - _, err := r.createCronJob(ctx, logger, sleepcycle, cronObjectKey, targetKind, targetMeta, targetReplicas, isShutdownOp) + cronObjectKey := client.ObjectKey{ + Name: fmt.Sprintf("sleepcycle-runner-%s%s-%s", sleepcycle.ObjectMeta.UID[:4], targetMeta.UID[:4], suffix), + Namespace: sleepcycle.Namespace, + } + + _, err = r.createCronJob(ctx, logger, sleepcycle, cronObjectKey, targetKind, targetMeta, targetReplicas, isShutdownOp) if err != nil { return err } @@ -245,7 +257,7 @@ func (r *SleepCycleReconciler) reconcileCronJob( err := r.updateCronJob(ctx, logger, sleepcycle, cronjob, targetKind, schedule, *tz, suspend, targetReplicas) if err != nil { - logger.Error(err, "failed to update runner", "name", cronObjectKey.Name) + logger.Error(err, "failed to update runner", "sleepcycle", sleepcycle.Name, "target", targetMeta.Namespace, "op", suffix) return err } } diff --git a/go.mod b/go.mod index 1302384..0127ba6 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/hashicorp/go-multierror v1.0.0 github.com/onsi/ginkgo v1.16.5 github.com/onsi/gomega v1.18.1 + github.com/pkg/errors v0.9.1 go.uber.org/zap v1.19.1 k8s.io/api v0.24.2 k8s.io/apimachinery v0.24.2 @@ -52,7 +53,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/nxadm/tail v1.4.8 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.12.1 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.32.1 // indirect