From 0bb7695fd87ef71656961c454af49443d420267f Mon Sep 17 00:00:00 2001 From: Kyriakos Akriotis Date: Wed, 30 Oct 2024 10:28:07 +0100 Subject: [PATCH] fixed bug adding 12 rand chars #35 --- Makefile | 2 +- charts/sleepcycles/Chart.yaml | 4 ++-- charts/sleepcycles/values.yaml | 2 +- config/manager/kustomization.yaml | 2 +- controllers/sleepcycle_runners_cronjobs.go | 7 ++++++- controllers/sleepcycle_utils.go | 17 ++++++++++++++++- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 6440a21..a85d2d9 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Image URL to use all building/pushing image targets DOCKER_HUB_NAME ?= $(shell docker info | sed '/Username:/!d;s/.* //') # sleepcycles -IMG_TAG ?= 0.2.6 +IMG_TAG ?= 0.2.7 #IMG_TAG ?= $(shell git rev-parse --short HEAD) IMG_NAME ?= rekuberate-io-sleepcycles IMG ?= $(DOCKER_HUB_NAME)/$(IMG_NAME):$(IMG_TAG) diff --git a/charts/sleepcycles/Chart.yaml b/charts/sleepcycles/Chart.yaml index c06b3f9..09701bf 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.6 +version: 0.2.7 # 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.6" \ No newline at end of file +appVersion: "0.2.7" \ No newline at end of file diff --git a/charts/sleepcycles/values.yaml b/charts/sleepcycles/values.yaml index d190dcb..ffa1fc6 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.6 + tag: 0.2.7 resources: limits: cpu: 500m diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index fcc96b1..2f0a3e3 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -13,4 +13,4 @@ kind: Kustomization images: - name: controller newName: akyriako78/rekuberate-io-sleepcycles - newTag: 0.2.6 + newTag: 0.2.7 diff --git a/controllers/sleepcycle_runners_cronjobs.go b/controllers/sleepcycle_runners_cronjobs.go index d5b9e12..1387626 100644 --- a/controllers/sleepcycle_runners_cronjobs.go +++ b/controllers/sleepcycle_runners_cronjobs.go @@ -224,8 +224,13 @@ func (r *SleepCycleReconciler) reconcileCronJob( } if cronjob == nil { + cronJobRandomUID, err := r.generateSecureRandomString(12) + if err != nil { + return err + } + cronObjectKey := client.ObjectKey{ - Name: fmt.Sprintf("sleepcycle-runner-%s%s-%s", sleepcycle.ObjectMeta.UID[:4], targetMeta.UID[:4], suffix), + Name: fmt.Sprintf("sleepcycle-runner-%s-%s-%s", sleepcycle.ObjectMeta.UID[:4], strings.ToLower(cronJobRandomUID), suffix), Namespace: sleepcycle.Namespace, } diff --git a/controllers/sleepcycle_utils.go b/controllers/sleepcycle_utils.go index dda780b..3597ca3 100644 --- a/controllers/sleepcycle_utils.go +++ b/controllers/sleepcycle_utils.go @@ -1,14 +1,16 @@ package controllers import ( + "crypto/rand" "encoding/base64" corev1alpha1 "github.com/rekuberate-io/sleepcycles/api/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "math/rand" "strings" ) +const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + func (r *SleepCycleReconciler) hasLabel(obj *metav1.ObjectMeta, tag string) bool { val, ok := obj.GetLabels()[SleepCycleLabel] @@ -31,6 +33,19 @@ func (r *SleepCycleReconciler) generateToken() (string, error) { return base64EncodedToken, nil } +func (r *SleepCycleReconciler) generateSecureRandomString(length int) (string, error) { + result := make([]byte, length) + _, err := rand.Read(result) + if err != nil { + return "", err + } + + for i := range result { + result[i] = letters[int(result[i])%len(letters)] + } + return string(result), nil +} + func (r *SleepCycleReconciler) recordEvent(sleepCycle *corev1alpha1.SleepCycle, message string, isError bool) { eventType := corev1.EventTypeNormal reason := "SuccessfulSleepCycleReconcile"