From 2bf125f43bcd99ae92b87c81cc0748c4b7c201c3 Mon Sep 17 00:00:00 2001 From: Tomas Pierantoni Date: Thu, 26 Dec 2024 13:56:40 +0100 Subject: [PATCH] modify configmap names to include instance identifier and avoid collisions --- config/manager/kustomization.yaml | 4 ++-- .../controller/reconcilers/configmap_reconciler.go | 8 +++++--- .../controller/reconcilers/statefulset_reconciler.go | 12 ++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 9c550cb..dbcd532 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization images: - name: controller - newName: ghcr.io/tomp21/yazio-challenge - newTag: main + newName: controller + newTag: local diff --git a/internal/controller/reconcilers/configmap_reconciler.go b/internal/controller/reconcilers/configmap_reconciler.go index 4f1913c..b3ab37a 100644 --- a/internal/controller/reconcilers/configmap_reconciler.go +++ b/internal/controller/reconcilers/configmap_reconciler.go @@ -2,8 +2,10 @@ package reconcilers import ( _ "embed" + "fmt" "context" + cachev1alpha1 "github.com/tomp21/yazio-challenge/api/v1alpha1" "github.com/tomp21/yazio-challenge/internal/util" corev1 "k8s.io/api/core/v1" @@ -77,7 +79,7 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, redis *cachev1alpha func (r *ConfigMapReconciler) ReconcileStartScripts(ctx context.Context, redis *cachev1alpha1.Redis) error { cmStartScripts := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "start-scripts", + Name: fmt.Sprintf("%s-start-scripts", redis.Name), Namespace: redis.Namespace, Labels: util.GetLabels(redis, nil), }, @@ -96,7 +98,7 @@ func (r *ConfigMapReconciler) ReconcileStartScripts(ctx context.Context, redis * func (r *ConfigMapReconciler) ReconcileConfigs(ctx context.Context, redis *cachev1alpha1.Redis) error { cmConfig := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "redis-conf", + Name: fmt.Sprintf("%s-conf", redis.Name), Namespace: redis.Namespace, Labels: util.GetLabels(redis, nil), }, @@ -116,7 +118,7 @@ func (r *ConfigMapReconciler) ReconcileConfigs(ctx context.Context, redis *cache func (r *ConfigMapReconciler) ReconcileHealthScripts(ctx context.Context, redis *cachev1alpha1.Redis) error { cmHealth := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "redis-health", + Name: fmt.Sprintf("%s-health", redis.Name), Namespace: redis.Namespace, Labels: util.GetLabels(redis, nil), }, diff --git a/internal/controller/reconcilers/statefulset_reconciler.go b/internal/controller/reconcilers/statefulset_reconciler.go index 0208e06..66b72eb 100644 --- a/internal/controller/reconcilers/statefulset_reconciler.go +++ b/internal/controller/reconcilers/statefulset_reconciler.go @@ -146,7 +146,7 @@ func generateReplicaStatefulSet(ss appsv1.StatefulSet, redis *cachev1alpha1.Redi } ss.Spec.Template.ObjectMeta.SetLabels(labels) ss.Spec.Template.Spec.Containers = getReplicaContainers(redis.Name, imageFullName) - ss.Spec.Template.Spec.Volumes = getVolumes() + ss.Spec.Template.Spec.Volumes = getVolumes(redis.Name) ss.Spec.VolumeClaimTemplates = getVolumeClaimTemplates(redis, util.GetLabels(redis, nil)) return ss } @@ -164,7 +164,7 @@ func generateMasterStatefulSet(ss appsv1.StatefulSet, redis *cachev1alpha1.Redis } ss.Spec.Template.ObjectMeta.SetLabels(labels) ss.Spec.Template.Spec.Containers = getMasterContainers(redis.Name, imageFullName) - ss.Spec.Template.Spec.Volumes = getVolumes() + ss.Spec.Template.Spec.Volumes = getVolumes(redis.Name) ss.Spec.VolumeClaimTemplates = getVolumeClaimTemplates(redis, util.GetLabels(redis, nil)) return ss } @@ -377,7 +377,7 @@ func getVolumeClaimTemplates(redis *cachev1alpha1.Redis, labels map[string]strin } } -func getVolumes() []corev1.Volume { +func getVolumes(instanceName string) []corev1.Volume { defaultMode := int32(0755) return []corev1.Volume{ { @@ -385,7 +385,7 @@ func getVolumes() []corev1.Volume { VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: "start-scripts", + Name: fmt.Sprintf("%s-start-scripts", instanceName), }, DefaultMode: &defaultMode, }, @@ -395,7 +395,7 @@ func getVolumes() []corev1.Volume { VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: "redis-conf", + Name: fmt.Sprintf("%s-conf", instanceName), }, DefaultMode: &defaultMode, }, @@ -405,7 +405,7 @@ func getVolumes() []corev1.Volume { VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: "redis-health", + Name: fmt.Sprintf("%s-health", instanceName), }, DefaultMode: &defaultMode, },