Skip to content

Commit

Permalink
Fix custome config enforcer
Browse files Browse the repository at this point in the history
  • Loading branch information
sergicastro committed Nov 19, 2024
1 parent af2af33 commit 75c23f6
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ func (r *CustomConfigSpecEnforcer) GetSpecName() string {

func (r *CustomConfigSpecEnforcer) CheckForSpecDifference(statefulSet *apps.StatefulSet) StatefulSetSpecDifference {

statefulSetCopy := *statefulSet
hasStatefulSetChanged, changesDetails := r.customConfigSpecHelper.ConfigureStatefulSet(&statefulSetCopy)
// We need to create a deep copy of the statefulSet to avoid modifying the original object, as we are only checking for differences.
// Original statefulSet will be modified by the EnforceSpec method.
statefulSetCopy := statefulSet.DeepCopy()
hasStatefulSetChanged, changesDetails := r.customConfigSpecHelper.ConfigureStatefulSet(statefulSetCopy)

if hasStatefulSetChanged {
return StatefulSetSpecDifference{
Expand All @@ -58,6 +60,6 @@ func (r *CustomConfigSpecEnforcer) EnforceSpec(statefulSet *apps.StatefulSet) (w
return wasSpecUpdated, nil
}

func (r *CustomConfigSpecEnforcer) OnSpecEnforcedSuccessfully(statefulSet *apps.StatefulSet) error {
func (r *CustomConfigSpecEnforcer) OnSpecEnforcedSuccessfully(*apps.StatefulSet) error {
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,22 @@ func extractCustom(src map[string]string) map[string]string {

func (r *CustomMetadataSpecEnforcer) EnforceSpec(statefulSet *apps.StatefulSet) (bool, error) {
md := getCustomMetadata(r.kubegresContext.Kubegres.GetObjectMeta())
merge(statefulSet.ObjectMeta.Labels, md.Labels)
merge(statefulSet.ObjectMeta.Annotations, md.Annotations)
merge(statefulSet.Spec.Template.ObjectMeta.Labels, md.Labels)
merge(statefulSet.Spec.Template.ObjectMeta.Annotations, md.Annotations)
statefulSet.ObjectMeta.Labels = merge(statefulSet.ObjectMeta.Labels, md.Labels)
statefulSet.ObjectMeta.Annotations = merge(statefulSet.ObjectMeta.Annotations, md.Annotations)
statefulSet.Spec.Template.ObjectMeta.Labels = merge(statefulSet.Spec.Template.ObjectMeta.Labels, md.Labels)
statefulSet.Spec.Template.ObjectMeta.Annotations = merge(statefulSet.Spec.Template.ObjectMeta.Annotations, md.Annotations)
return true, nil
}

func merge(dst map[string]string, src map[string]string) {
for key, value := range src {
dst[key] = value
func merge(a map[string]string, b map[string]string) map[string]string {
result := make(map[string]string, len(a)+len(b))
for key, value := range a {
result[key] = value
}
for key, value := range b {
result[key] = value
}
return result
}

func (r *CustomMetadataSpecEnforcer) OnSpecEnforcedSuccessfully(*apps.StatefulSet) error {
Expand Down
15 changes: 1 addition & 14 deletions controllers/spec/template/CustomConfigSpecHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ func (r *CustomConfigSpecHelper) ConfigureStatefulSet(statefulSet *v1.StatefulSe
hasStatefulSetChanged = true
}

if r.updateVolumeMountNameIfChanged(configMap.ConfigLocations.PromoteReplica, states.ConfigMapDataKeyPromoteReplica, statefulSet) {
differenceDetails += r.createDescriptionMsg(configMap.ConfigLocations.PromoteReplica, states.ConfigMapDataKeyPromoteReplica)
hasStatefulSetChanged = true
}
// No need to check for states.ConfigMapDataKeyPromoteReplica as this is only used by the failover enforcer

statefulSetTemplateSpec := &statefulSet.Spec.Template.Spec

Expand Down Expand Up @@ -127,16 +124,6 @@ func (r *CustomConfigSpecHelper) createDescriptionMsg(volumeMountName, configMap
return "VolumeMount with subPath: '" + configMapDataKey + "' was updated to name: '" + volumeMountName + "' - "
}

func (r *CustomConfigSpecHelper) getVolumeMountIndex(configMapDataKey string, statefulSet *v1.StatefulSet) int {
volumeMounts := statefulSet.Spec.Template.Spec.Containers[0].VolumeMounts
for i := 0; i < len(volumeMounts); i++ {
if volumeMounts[i].SubPath == configMapDataKey {
return i
}
}
return -1
}

func (r *CustomConfigSpecHelper) getCustomConfigMapVolume(volumes []core.Volume) *core.Volume {
for _, volume := range volumes {
if volume.Name == ctx.CustomConfigMapVolumeName {
Expand Down
3 changes: 3 additions & 0 deletions test/resourceConfigs/ConfigForTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@ const (

CustomConfigMapWithPromoteReplicaScriptResourceName = "config-with-promote-replica-script"
CustomConfigMapWithPromoteReplicaScriptYamlFile = "resourceConfigs/customConfig/configMap_with_promote_replica_script.yaml"

CustomConfigMapWithPrimaryCreateReplicationRoleResourceName = "config-with-primary-create-replication-role-script"
CustomConfigMapwithPrimaryCreateReplicationRoleYamlFile = "resourceConfigs/customConfig/configMap_with_primary_create_replication_role_script.yaml"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: config-with-primary-create-replication-role-script
namespace: default
labels:
environment: acceptancetesting

data:

postgres.conf: |
# Replication configs
listen_addresses = '*'
max_wal_senders = 10
max_connections = 100
shared_buffers = 128MB
# Logging
#log_destination = 'stderr,csvlog'
#logging_collector = on
#log_directory = 'pg_log'
#log_filename= 'postgresql-%Y-%m-%d_%H%M%S.log'
primary_create_replication_role.sh: |
#!/bin/bash
set -e
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt - Creating replication role...";
echo "$dt - Running: psql -v ON_ERROR_STOP=1 --username $POSTGRES_USER --dbname $POSTGRES_DB ... CREATE ROLE replication WITH REPLICATION PASSWORD ... GRANT EXECUTE ON FUNCTION pg_promote TO replication;";
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE ROLE replication WITH REPLICATION PASSWORD '$POSTGRES_REPLICATION_PASSWORD' LOGIN;
GRANT EXECUTE ON FUNCTION pg_promote TO replication;
EOSQL
echo "$dt - Replication role created";
42 changes: 41 additions & 1 deletion test/spec_customConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = Describe("Setting Kubegres specs 'customConfig'", Label("group:2"), func
test.resourceCreator.CreateConfigMapWithPgHbaConf()
test.resourceCreator.CreateConfigMapWithPostgresConf()
test.resourceCreator.CreateConfigMapWithPrimaryInitScript()
test.resourceCreator.CreateConfigMapWithPromoteReplicaScript()
test.resourceCreator.CreateConfigMapWithPrimaryCreateReplicationRoleScript()
})

AfterEach(func() {
Expand Down Expand Up @@ -345,6 +345,46 @@ var _ = Describe("Setting Kubegres specs 'customConfig'", Label("group:2"), func
log.Print("END OF: Test 'GIVEN new Kubegres is created with backUp enabled and spec 'customConfig' set to base-config AND later it is updated to a configMap containing data-key 'backup_database.sh''")
})
})

Context("GIVEN new Kubegres is created with spec 'customConfig' set to base-config AND later it is updated to a configMap containing data-key 'promote_replica_to_primary.sh'", func() {

It("THEN the value of 'promote_replica_to_primary.sh' is first set to 'ctx.BaseConfigMapVolumeName' and later it is set to 'ctx.CustomConfigMapVolumeName'", func() {

log.Print("START OF: Test 'GIVEN new Kubegres is created with spec 'customConfig' set to base-config AND later it is updated to a configMap containing data-key 'promote_replica_to_primary.sh''")

test.givenNewKubegresSpecIsSetTo(ctx.BaseConfigMapName, 3)

test.whenKubegresIsCreated()

test.thenPodsStatesShouldBe(1, 2)

test.dbQueryTestCases.ThenWeCanSqlQueryPrimaryDb()
test.dbQueryTestCases.ThenWeCanSqlQueryReplicaDb()

test.givenExistingKubegresSpecIsSetTo(resourceConfigs.CustomConfigMapWithPrimaryCreateReplicationRoleResourceName)

test.whenKubernetesIsUpdated()

test.thenPodsContainsCustomConfigWithResourceName(resourceConfigs.CustomConfigMapWithPrimaryCreateReplicationRoleResourceName)

test.thenPodsContainsConfigTypeAssociatedToFile(ctx.CustomConfigMapVolumeName, states.ConfigMapDataKeyPostgresConf, primary)
test.thenPodsContainsConfigTypeAssociatedToFile(ctx.CustomConfigMapVolumeName, states.ConfigMapDataKeyPostgresConf, replica)

test.thenPodsContainsConfigTypeAssociatedToFile(ctx.BaseConfigMapVolumeName, states.ConfigMapDataKeyPrimaryInitScript, primary)

test.thenPodsContainsConfigTypeAssociatedToFile(ctx.BaseConfigMapVolumeName, states.ConfigMapDataKeyPgHbaConf, primary)
test.thenPodsContainsConfigTypeAssociatedToFile(ctx.BaseConfigMapVolumeName, states.ConfigMapDataKeyPgHbaConf, replica)

test.thenPodsContainsConfigTypeAssociatedToFile(ctx.BaseConfigMapVolumeName, states.ConfigMapDataKeyCopyPrimaryDataToReplica, replica)

test.thenPodsContainsConfigTypeAssociatedToFile(ctx.CustomConfigMapVolumeName, states.ConfigMapDataKeyPrimaryCreateReplicaRole, primary)

test.dbQueryTestCases.ThenWeCanSqlQueryPrimaryDb()
test.dbQueryTestCases.ThenWeCanSqlQueryReplicaDb()

log.Print("END OF: Test 'GIVEN new Kubegres is created with spec 'customConfig' set to base-config AND later it is updated to a configMap containing data-key 'promote_replica_to_primary.sh''")
})
})
})

type SpecCustomConfigTest struct {
Expand Down
7 changes: 7 additions & 0 deletions test/util/TestResourceCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ func (r *TestResourceCreator) CreateConfigMapWithPromoteReplicaScript() {
r.createResourceFromYaml("Custom ConfigMap with promote replica script", resourceConfigs2.CustomConfigMapWithPromoteReplicaScriptResourceName, &existingResource, &resourceToCreate)
}

func (r *TestResourceCreator) CreateConfigMapWithPrimaryCreateReplicationRoleScript() {
existingResource := v1.ConfigMap{}
resourceToCreate := resourceConfigs2.LoadCustomConfigMapYaml(resourceConfigs2.CustomConfigMapwithPrimaryCreateReplicationRoleYamlFile)
resourceToCreate.Namespace = r.namespace
r.createResourceFromYaml("Custom ConfigMap with primary create replication role script", resourceConfigs2.CustomConfigMapWithPrimaryCreateReplicationRoleResourceName, &existingResource, &resourceToCreate)
}

func (r *TestResourceCreator) CreateNamespace() {
if r.namespace == resourceConfigs2.DefaultNamespace {
return
Expand Down

0 comments on commit 75c23f6

Please sign in to comment.