From c756cb2f8afff1c6f0556ce616d76e69f8f0cc35 Mon Sep 17 00:00:00 2001 From: Felix Kunde Date: Thu, 5 Jan 2023 12:02:19 +0100 Subject: [PATCH] spec.env can override clone and standby variables (#2159) --- docs/administrator.md | 6 ++--- pkg/cluster/k8sres.go | 10 ++++---- pkg/cluster/k8sres_test.go | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/docs/administrator.md b/docs/administrator.md index cd56289b4..8fde634c4 100644 --- a/docs/administrator.md +++ b/docs/administrator.md @@ -631,9 +631,9 @@ order (e.g. a variable defined in 4. overrides a variable with the same name in 5.): 1. Assigned by the operator -2. Clone section (with WAL settings from operator config when `s3_wal_path` is empty) -3. Standby section -4. `env` section in cluster manifest +2. `env` section in cluster manifest +3. Clone section (with WAL settings from operator config when `s3_wal_path` is empty) +4. Standby section 5. Pod environment secret via operator config 6. Pod environment config map via operator config 7. WAL and logical backup settings from operator config diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index 902e6f873..451a189fd 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -962,6 +962,11 @@ func (c *Cluster) generateSpiloPodEnvVars( envVars = append(envVars, v1.EnvVar{Name: "KUBERNETES_USE_CONFIGMAPS", Value: "true"}) } + // fetch cluster-specific variables that will override all subsequent global variables + if len(spec.Env) > 0 { + envVars = appendEnvVars(envVars, spec.Env...) + } + if spec.Clone != nil && spec.Clone.ClusterName != "" { envVars = append(envVars, c.generateCloneEnvironment(spec.Clone)...) } @@ -970,11 +975,6 @@ func (c *Cluster) generateSpiloPodEnvVars( envVars = append(envVars, c.generateStandbyEnvironment(spec.StandbyCluster)...) } - // fetch cluster-specific variables that will override all subsequent global variables - if len(spec.Env) > 0 { - envVars = appendEnvVars(envVars, spec.Env...) - } - // fetch variables from custom environment Secret // that will override all subsequent global variables secretEnvVarsList, err := c.getPodEnvironmentSecretVariables() diff --git a/pkg/cluster/k8sres_test.go b/pkg/cluster/k8sres_test.go index bfdca7e59..175f6fd7f 100644 --- a/pkg/cluster/k8sres_test.go +++ b/pkg/cluster/k8sres_test.go @@ -602,6 +602,23 @@ func TestGenerateSpiloPodEnvVars(t *testing.T) { envVarValue: "s3.eu-central-1.amazonaws.com", }, } + expectedCloneEnvSpecEnv := []ExpectedValue{ + { + envIndex: 15, + envVarConstant: "CLONE_WAL_BUCKET_SCOPE_PREFIX", + envVarValue: "test-cluster", + }, + { + envIndex: 17, + envVarConstant: "CLONE_WALE_S3_PREFIX", + envVarValue: "s3://another-bucket", + }, + { + envIndex: 21, + envVarConstant: "CLONE_AWS_ENDPOINT", + envVarValue: "s3.eu-central-1.amazonaws.com", + }, + } expectedCloneEnvConfigMap := []ExpectedValue{ { envIndex: 16, @@ -821,6 +838,36 @@ func TestGenerateSpiloPodEnvVars(t *testing.T) { standbyDescription: &acidv1.StandbyDescription{}, expectedValues: expectedCloneEnvSpec, }, + { + subTest: "will set CLONE_ parameters from manifest `env` section, followed by other options", + opConfig: config.Config{ + Resources: config.Resources{ + PodEnvironmentConfigMap: spec.NamespacedName{ + Name: testPodEnvironmentConfigMapName, + }, + }, + WALES3Bucket: "global-s3-bucket", + }, + cloneDescription: &acidv1.CloneDescription{ + ClusterName: "test-cluster", + EndTimestamp: "somewhen", + UID: dummyUUID, + S3WalPath: "s3://another-bucket", + S3Endpoint: "s3.eu-central-1.amazonaws.com", + }, + standbyDescription: &acidv1.StandbyDescription{}, + expectedValues: expectedCloneEnvSpecEnv, + pgsql: acidv1.Postgresql{ + Spec: acidv1.PostgresSpec{ + Env: []v1.EnvVar{ + { + Name: "CLONE_WAL_BUCKET_SCOPE_PREFIX", + Value: "test-cluster", + }, + }, + }, + }, + }, { subTest: "will set CLONE_AWS_ENDPOINT parameter from pod environment config map", opConfig: config.Config{