@@ -33,10 +33,52 @@ var (
3333 dflyUserGroup int64 = 999
3434)
3535
36- func generateProbeConfigMap (df * resourcesv1.Dragonfly , suffix , key , script string ) * corev1.ConfigMap {
36+ // returns the custom ConfigMap name if set, or "<dfName>-<suffix>" otherwise
37+ func probeConfigMapName (dfName , suffix string , custom * corev1.LocalObjectReference ) string {
38+ if custom != nil && custom .Name != "" {
39+ return custom .Name
40+ }
41+ return fmt .Sprintf ("%s-%s" , dfName , suffix )
42+ }
43+
44+ // append probe ConfigMap volumes and mounts into the StatefulSet
45+ func appendProbeVolumesAndMounts (sts * appsv1.StatefulSet , livenessCM , readinessCM , startupCM string ) {
46+ probes := []struct {
47+ volumeName string
48+ configMapName string
49+ scriptKey string
50+ }{
51+ {LivenessProbeVolumeName , livenessCM , LivenessScriptKey },
52+ {ReadinessProbeVolumeName , readinessCM , ReadinessScriptKey },
53+ {StartupProbeVolumeName , startupCM , StartupScriptKey },
54+ }
55+ for _ , p := range probes {
56+ sts .Spec .Template .Spec .Volumes = append (sts .Spec .Template .Spec .Volumes ,
57+ corev1.Volume {
58+ Name : p .volumeName ,
59+ VolumeSource : corev1.VolumeSource {
60+ ConfigMap : & corev1.ConfigMapVolumeSource {
61+ LocalObjectReference : corev1.LocalObjectReference {Name : p .configMapName },
62+ },
63+ },
64+ },
65+ )
66+ sts .Spec .Template .Spec .Containers [0 ].VolumeMounts = append (
67+ sts .Spec .Template .Spec .Containers [0 ].VolumeMounts ,
68+ corev1.VolumeMount {
69+ Name : p .volumeName ,
70+ MountPath : ProbeMountPath + "/" + p .scriptKey ,
71+ SubPath : p .scriptKey ,
72+ },
73+ )
74+ }
75+ }
76+
77+ // generateProbeConfigMap builds a ConfigMap owned by the Dragonfly instance for a single probe script.
78+ func generateProbeConfigMap (df * resourcesv1.Dragonfly , name , key , script string ) * corev1.ConfigMap {
3779 return & corev1.ConfigMap {
3880 ObjectMeta : metav1.ObjectMeta {
39- Name : fmt . Sprintf ( "%s-%s" , df . Name , suffix ) ,
81+ Name : name ,
4082 Namespace : df .Namespace ,
4183 OwnerReferences : []metav1.OwnerReference {
4284 {
@@ -428,77 +470,19 @@ func GenerateDragonflyResources(df *resourcesv1.Dragonfly, defaultDragonflyImage
428470 }
429471 }
430472
431- // Probe script volumes — the corresponding ConfigMaps are generated and appended to resources below.
432- livenessConfigMapName := fmt .Sprintf ("%s-%s" , df .Name , LivenessProbeConfigMapSuffix )
433- if df .Spec .CustomLivenessProbeConfigMap != nil && df .Spec .CustomLivenessProbeConfigMap .Name != "" {
434- livenessConfigMapName = df .Spec .CustomLivenessProbeConfigMap .Name
435- }
436- readinessConfigMapName := fmt .Sprintf ("%s-%s" , df .Name , ReadinessProbeConfigMapSuffix )
437- if df .Spec .CustomReadinessProbeConfigMap != nil && df .Spec .CustomReadinessProbeConfigMap .Name != "" {
438- readinessConfigMapName = df .Spec .CustomReadinessProbeConfigMap .Name
439- }
440- startupConfigMapName := fmt .Sprintf ("%s-%s" , df .Name , StartupProbeConfigMapSuffix )
441- if df .Spec .CustomStartupProbeConfigMap != nil && df .Spec .CustomStartupProbeConfigMap .Name != "" {
442- startupConfigMapName = df .Spec .CustomStartupProbeConfigMap .Name
443- }
473+ // Wire probe ConfigMap volumes and mounts into the StatefulSet.
474+ livenessConfigMapName := probeConfigMapName (df .Name , LivenessProbeConfigMapSuffix , df .Spec .CustomLivenessProbeConfigMap )
475+ readinessConfigMapName := probeConfigMapName (df .Name , ReadinessProbeConfigMapSuffix , df .Spec .CustomReadinessProbeConfigMap )
476+ startupConfigMapName := probeConfigMapName (df .Name , StartupProbeConfigMapSuffix , df .Spec .CustomStartupProbeConfigMap )
444477
445- statefulset .Spec .Template .Spec .Volumes = append (statefulset .Spec .Template .Spec .Volumes ,
446- corev1.Volume {
447- Name : LivenessProbeVolumeName ,
448- VolumeSource : corev1.VolumeSource {
449- ConfigMap : & corev1.ConfigMapVolumeSource {
450- LocalObjectReference : corev1.LocalObjectReference {Name : livenessConfigMapName },
451- },
452- },
453- },
454- corev1.Volume {
455- Name : ReadinessProbeVolumeName ,
456- VolumeSource : corev1.VolumeSource {
457- ConfigMap : & corev1.ConfigMapVolumeSource {
458- LocalObjectReference : corev1.LocalObjectReference {Name : readinessConfigMapName },
459- },
460- },
461- },
462- corev1.Volume {
463- Name : StartupProbeVolumeName ,
464- VolumeSource : corev1.VolumeSource {
465- ConfigMap : & corev1.ConfigMapVolumeSource {
466- LocalObjectReference : corev1.LocalObjectReference {Name : startupConfigMapName },
467- },
468- },
469- },
470- )
471-
472- statefulset .Spec .Template .Spec .Containers [0 ].VolumeMounts = append (
473- statefulset .Spec .Template .Spec .Containers [0 ].VolumeMounts ,
474- corev1.VolumeMount {
475- Name : LivenessProbeVolumeName ,
476- MountPath : ProbeMountPath + "/" + LivenessScriptKey ,
477- SubPath : LivenessScriptKey ,
478- },
479- corev1.VolumeMount {
480- Name : ReadinessProbeVolumeName ,
481- MountPath : ProbeMountPath + "/" + ReadinessScriptKey ,
482- SubPath : ReadinessScriptKey ,
483- },
484- corev1.VolumeMount {
485- Name : StartupProbeVolumeName ,
486- MountPath : ProbeMountPath + "/" + StartupScriptKey ,
487- SubPath : StartupScriptKey ,
488- },
478+ appendProbeVolumesAndMounts (& statefulset ,
479+ livenessConfigMapName , readinessConfigMapName , startupConfigMapName ,
489480 )
490481
491482 statefulset .Spec .Template .Spec .Containers = mergeNamedSlices (
492483 statefulset .Spec .Template .Spec .Containers , df .Spec .AdditionalContainers ,
493484 func (c corev1.Container ) string { return c .Name })
494485
495- // There are two ways to override probe scripts:
496- // 1. spec.customLivenessProbeConfigMap / customReadinessProbeConfigMap / customStartupProbeConfigMap
497- // — replaces the generated default ConfigMap for a specific probe.
498- // 2. spec.additionalVolumes with a matching volume name ("liveness-probe", "readiness-probe",
499- // "startup-probe") — wins over both the default and custom ConfigMap volumes because
500- // mergeNamedSlices appends additionalVolumes last, and Kubernetes uses the last definition.
501- // Do not use both mechanisms for the same probe simultaneously.
502486 statefulset .Spec .Template .Spec .Volumes = mergeNamedSlices (
503487 statefulset .Spec .Template .Spec .Volumes , df .Spec .AdditionalVolumes ,
504488 func (v corev1.Volume ) string { return v .Name })
@@ -507,13 +491,13 @@ func GenerateDragonflyResources(df *resourcesv1.Dragonfly, defaultDragonflyImage
507491 // and can mount the probe script volumes without getting stuck in Pending.
508492 // Skip generating a default when the user has pointed to their own ConfigMap.
509493 if df .Spec .CustomLivenessProbeConfigMap == nil || df .Spec .CustomLivenessProbeConfigMap .Name == "" {
510- resources = append (resources , generateProbeConfigMap (df , LivenessProbeConfigMapSuffix , LivenessScriptKey , defaultLivenessScript ))
494+ resources = append (resources , generateProbeConfigMap (df , livenessConfigMapName , LivenessScriptKey , defaultLivenessScript ))
511495 }
512496 if df .Spec .CustomReadinessProbeConfigMap == nil || df .Spec .CustomReadinessProbeConfigMap .Name == "" {
513- resources = append (resources , generateProbeConfigMap (df , ReadinessProbeConfigMapSuffix , ReadinessScriptKey , defaultReadinessScript ))
497+ resources = append (resources , generateProbeConfigMap (df , readinessConfigMapName , ReadinessScriptKey , defaultReadinessScript ))
514498 }
515499 if df .Spec .CustomStartupProbeConfigMap == nil || df .Spec .CustomStartupProbeConfigMap .Name == "" {
516- resources = append (resources , generateProbeConfigMap (df , StartupProbeConfigMapSuffix , StartupScriptKey , defaultStartupScript ))
500+ resources = append (resources , generateProbeConfigMap (df , startupConfigMapName , StartupScriptKey , defaultStartupScript ))
517501 }
518502
519503 resources = append (resources , & statefulset )
0 commit comments