Skip to content

Commit

Permalink
Make volumes and custom configs to apply to init containers too
Browse files Browse the repository at this point in the history
  • Loading branch information
sergicastro committed Nov 12, 2024
1 parent bf96217 commit 63e87cb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
30 changes: 26 additions & 4 deletions controllers/spec/template/CustomConfigSpecHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ func (r *CustomConfigSpecHelper) ConfigureStatefulSet(statefulSet *v1.StatefulSe
hasStatefulSetChanged = true
}

if r.updateVolumeMountNameIfChanged(configMap.ConfigLocations.CopyPrimaryDataToReplica, states.ConfigMapDataKeyCopyPrimaryDataToReplica, statefulSet) {
differenceDetails += r.createDescriptionMsg(configMap.ConfigLocations.CopyPrimaryDataToReplica, states.ConfigMapDataKeyCopyPrimaryDataToReplica)
hasStatefulSetChanged = true
}

if r.updateVolumeMountNameIfChanged(configMap.ConfigLocations.PrimaryCreateReplicaRole, states.ConfigMapDataKeyPrimaryCreateReplicaRole, statefulSet) {
differenceDetails += r.createDescriptionMsg(configMap.ConfigLocations.PrimaryCreateReplicaRole, states.ConfigMapDataKeyPrimaryCreateReplicaRole)
hasStatefulSetChanged = true
}

if r.updateVolumeMountNameIfChanged(configMap.ConfigLocations.PromoteReplica, states.ConfigMapDataKeyPromoteReplica, statefulSet) {
differenceDetails += r.createDescriptionMsg(configMap.ConfigLocations.PromoteReplica, states.ConfigMapDataKeyPromoteReplica)
hasStatefulSetChanged = true
}

statefulSetTemplateSpec := &statefulSet.Spec.Template.Spec

customConfigMapVolume := r.getCustomConfigMapVolume(statefulSetTemplateSpec.Volumes)
Expand Down Expand Up @@ -89,15 +104,22 @@ func (r *CustomConfigSpecHelper) ConfigureStatefulSet(statefulSet *v1.StatefulSe

func (r *CustomConfigSpecHelper) updateVolumeMountNameIfChanged(volumeName, configMapDataKey string, statefulSet *v1.StatefulSet) (updated bool) {

volumeMounts := statefulSet.Spec.Template.Spec.Containers[0].VolumeMounts

for i := 0; i < len(volumeMounts); i++ {
volumeMount := volumeMounts[i]
for i, volumeMount := range statefulSet.Spec.Template.Spec.Containers[0].VolumeMounts {
if volumeMount.SubPath == configMapDataKey && volumeMount.Name != volumeName {
statefulSet.Spec.Template.Spec.Containers[0].VolumeMounts[i].Name = volumeName
updated = true
}
}

if len(statefulSet.Spec.Template.Spec.InitContainers) > 0 {
for i, volume := range statefulSet.Spec.Template.Spec.InitContainers[0].VolumeMounts {
if volume.SubPath == configMapDataKey && volume.Name != volumeName {
statefulSet.Spec.Template.Spec.InitContainers[0].VolumeMounts[i].Name = volumeName
updated = true
}
}
}

return updated
}

Expand Down
3 changes: 3 additions & 0 deletions controllers/spec/template/ResourcesCreatorFromTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func (r *ResourcesCreatorFromTemplate) initStatefulSet(

if postgresSpec.Volume.VolumeMounts != nil {
statefulSetTemplate.Spec.Template.Spec.Containers[0].VolumeMounts = append(statefulSetTemplate.Spec.Template.Spec.Containers[0].VolumeMounts, r.kubegresContext.Kubegres.Spec.Volume.VolumeMounts...)
if len(statefulSetTemplate.Spec.Template.Spec.InitContainers) > 0 {
statefulSetTemplate.Spec.Template.Spec.InitContainers[0].VolumeMounts = append(statefulSetTemplate.Spec.Template.Spec.InitContainers[0].VolumeMounts, r.kubegresContext.Kubegres.Spec.Volume.VolumeMounts...)
}
}

if postgresSpec.SecurityContext != nil {
Expand Down
37 changes: 29 additions & 8 deletions controllers/states/ConfigStates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ import (
)

const (
ConfigMapDataKeyPostgresConf = "postgres.conf"
ConfigMapDataKeyPrimaryInitScript = "primary_init_script.sh"
ConfigMapDataKeyPgHbaConf = "pg_hba.conf"
ConfigMapDataKeyBackUpScript = "backup_database.sh"
ConfigMapDataKeyPostgresConf = "postgres.conf"
ConfigMapDataKeyPrimaryInitScript = "primary_init_script.sh"
ConfigMapDataKeyPgHbaConf = "pg_hba.conf"
ConfigMapDataKeyBackUpScript = "backup_database.sh"
ConfigMapDataKeyCopyPrimaryDataToReplica = "copy_primary_data_to_replica.sh"
ConfigMapDataKeyPrimaryCreateReplicaRole = "primary_create_replication_role.sh"
ConfigMapDataKeyPromoteReplica = "promote_replica_to_primary.sh"
)

type ConfigStates struct {
Expand All @@ -46,10 +49,13 @@ type ConfigStates struct {

// Stores as string the volume-name for each config-type which can be either 'base-config' or 'custom-config'
type ConfigLocations struct {
PostgreConf string
PrimaryInitScript string
BackUpScript string
PgHbaConf string
PostgreConf string
PrimaryInitScript string
BackUpScript string
PgHbaConf string
CopyPrimaryDataToReplica string
PrimaryCreateReplicaRole string
PromoteReplica string
}

func loadConfigStates(kubegresContext ctx.KubegresContext) (ConfigStates, error) {
Expand All @@ -69,6 +75,9 @@ func (r *ConfigStates) loadStates() (err error) {
r.ConfigLocations.PrimaryInitScript = ctx.BaseConfigMapVolumeName
r.ConfigLocations.BackUpScript = ctx.BaseConfigMapVolumeName
r.ConfigLocations.PgHbaConf = ctx.BaseConfigMapVolumeName
r.ConfigLocations.CopyPrimaryDataToReplica = ctx.BaseConfigMapVolumeName
r.ConfigLocations.PrimaryCreateReplicaRole = ctx.BaseConfigMapVolumeName
r.ConfigLocations.PromoteReplica = ctx.BaseConfigMapVolumeName

baseConfigMap, err := r.getBaseDeployedConfigMap()
if err != nil {
Expand Down Expand Up @@ -107,6 +116,18 @@ func (r *ConfigStates) loadStates() (err error) {
if customConfigMap.Data[ConfigMapDataKeyPgHbaConf] != "" {
r.ConfigLocations.PgHbaConf = ctx.CustomConfigMapVolumeName
}

if customConfigMap.Data[ConfigMapDataKeyCopyPrimaryDataToReplica] != "" {
r.ConfigLocations.CopyPrimaryDataToReplica = ctx.CustomConfigMapVolumeName
}

if customConfigMap.Data[ConfigMapDataKeyPrimaryCreateReplicaRole] != "" {
r.ConfigLocations.PrimaryCreateReplicaRole = ctx.CustomConfigMapVolumeName
}

if customConfigMap.Data[ConfigMapDataKeyPromoteReplica] != "" {
r.ConfigLocations.PromoteReplica = ctx.CustomConfigMapVolumeName
}
}

return nil
Expand Down

0 comments on commit 63e87cb

Please sign in to comment.