From 04cf023c33523c159a74af72d0c4af5cfdc2cafe Mon Sep 17 00:00:00 2001 From: Jan Jansen Date: Mon, 25 Jul 2022 16:31:59 +0200 Subject: [PATCH] feat: add custom env to logical backup fixes #1473 --- pkg/apis/acid.zalan.do/v1/postgresql_type.go | 1 + .../acid.zalan.do/v1/zz_generated.deepcopy.go | 7 ++ pkg/cluster/k8sres.go | 5 ++ pkg/cluster/k8sres_test.go | 74 +++++++++++++++++++ 4 files changed, 87 insertions(+) diff --git a/pkg/apis/acid.zalan.do/v1/postgresql_type.go b/pkg/apis/acid.zalan.do/v1/postgresql_type.go index 1e9245fb8..15c03ba65 100644 --- a/pkg/apis/acid.zalan.do/v1/postgresql_type.go +++ b/pkg/apis/acid.zalan.do/v1/postgresql_type.go @@ -74,6 +74,7 @@ type PostgresSpec struct { ShmVolume *bool `json:"enableShmVolume,omitempty"` EnableLogicalBackup bool `json:"enableLogicalBackup,omitempty"` LogicalBackupSchedule string `json:"logicalBackupSchedule,omitempty"` + LogicalBackupEnv []v1.EnvVar `json:"logicalBackupEnv,omitempty"` StandbyCluster *StandbyDescription `json:"standby,omitempty"` PodAnnotations map[string]string `json:"podAnnotations,omitempty"` ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"` diff --git a/pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go b/pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go index b338421a2..d7a5494e2 100644 --- a/pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go +++ b/pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go @@ -741,6 +741,13 @@ func (in *PostgresSpec) DeepCopyInto(out *PostgresSpec) { *out = new(bool) **out = **in } + if in.LogicalBackupEnv != nil { + in, out := &in.LogicalBackupEnv, &out.LogicalBackupEnv + *out = make([]corev1.EnvVar, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } if in.StandbyCluster != nil { in, out := &in.StandbyCluster, &out.StandbyCluster *out = new(StandbyDescription) diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index e84303b1e..47925df53 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -2197,6 +2197,11 @@ func (c *Cluster) generateLogicalBackupPodEnvVars() []v1.EnvVar { envVars = append(envVars, v1.EnvVar{Name: "AWS_SECRET_ACCESS_KEY", Value: c.OpConfig.LogicalBackup.LogicalBackupS3SecretAccessKey}) } + // fetch logical-backup-specific variables that will override all subsequent global variables + if len(c.Spec.LogicalBackupEnv) > 0 { + envVars = appendEnvVars(envVars, c.Spec.LogicalBackupEnv...) + } + c.logger.Debugf("Generated logical backup env vars") c.logger.Debugf("%v", envVars) return envVars diff --git a/pkg/cluster/k8sres_test.go b/pkg/cluster/k8sres_test.go index 36f23999c..299305d9a 100644 --- a/pkg/cluster/k8sres_test.go +++ b/pkg/cluster/k8sres_test.go @@ -864,6 +864,80 @@ func TestGenerateSpiloPodEnvVars(t *testing.T) { } } +func TestGenerateLogicalBackupPodEnvVars(t *testing.T) { + testName := "TestGenerateLogicalBackupPodEnvVars" + tests := []struct { + subTest string + opConfig config.Config + expectedValues []ExpectedValue + pgsql acidv1.Postgresql + }{ + { + subTest: "will set CLUSTER_NAME_LABEL env", + opConfig: config.Config{ + Resources: config.Resources{ + ClusterNameLabel: "test", + }, + }, + expectedValues: []ExpectedValue{ + { + envIndex: 1, + envVarConstant: "CLUSTER_NAME_LABEL", + envVarValue: "test", + }, + }, + }, + { + subTest: "will set CUSTOM_VALUE env by logicalBackupEnv", + pgsql: acidv1.Postgresql{ + Spec: acidv1.PostgresSpec{ + LogicalBackupEnv: []v1.EnvVar{ + { + Name: "CUSTOM_VALUE", + Value: "my-scope-label", + }, + }, + }, + }, + expectedValues: []ExpectedValue{ + { + envIndex: 17, + envVarConstant: "CUSTOM_VALUE", + envVarValue: "my-scope-label", + }, + }, + }, + } + + for _, tt := range tests { + c := newMockCluster(tt.opConfig) + c.Postgresql = tt.pgsql + actualEnvs := c.generateLogicalBackupPodEnvVars() + + for _, ev := range tt.expectedValues { + env := actualEnvs[ev.envIndex] + + if env.Name != ev.envVarConstant { + t.Errorf("%s %s: expected env name %s, have %s instead", + testName, tt.subTest, ev.envVarConstant, env.Name) + } + + if ev.envVarValueRef != nil { + if !reflect.DeepEqual(env.ValueFrom, ev.envVarValueRef) { + t.Errorf("%s %s: expected env value reference %#v, have %#v instead", + testName, tt.subTest, ev.envVarValueRef, env.ValueFrom) + } + continue + } + + if env.Value != ev.envVarValue { + t.Errorf("%s %s: expected env value %s, have %s instead", + testName, tt.subTest, ev.envVarValue, env.Value) + } + } + } +} + func TestGetNumberOfInstances(t *testing.T) { testName := "TestGetNumberOfInstances" tests := []struct {