Skip to content

Commit

Permalink
feat: add custom env to logical backup
Browse files Browse the repository at this point in the history
fixes #1473
  • Loading branch information
farodin91 committed Jul 25, 2022
1 parent 1c80ac0 commit 04cf023
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/apis/acid.zalan.do/v1/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/cluster/k8sres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions pkg/cluster/k8sres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 04cf023

Please sign in to comment.