Skip to content

Commit

Permalink
Merge pull request #23 from rekuberate-io/22-cronjob-name-has-to-be-l…
Browse files Browse the repository at this point in the history
…ess-than-52-characters

22 cronjob name has to be less than 52 characters
  • Loading branch information
akyriako authored Oct 24, 2024
2 parents 1fb8cfd + bcc0037 commit ce56df9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions charts/sleepcycles/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
appVersion: "0.2.5"
2 changes: 1 addition & 1 deletion charts/sleepcycles/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ controllerManager:
- ALL
image:
repository: akyriako78/rekuberate-io-sleepcycles
tag: 0.2.3
tag: 0.2.5
resources:
limits:
cpu: 500m
Expand Down
44 changes: 28 additions & 16 deletions controllers/sleepcycle_runners_cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce56df9

Please sign in to comment.