Skip to content

Commit 4cb4952

Browse files
committed
Ability to set pod_environment_secret and pod_environment_configmap on cluster resource
1 parent 654d22d commit 4cb4952

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

charts/postgres-operator/crds/postgresqls.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ spec:
196196
type: boolean
197197
enableShmVolume:
198198
type: boolean
199+
env:
200+
type: array
201+
nullable: true
202+
items:
203+
type: object
204+
x-kubernetes-preserve-unknown-fields: true
199205
init_containers:
200206
type: array
201207
description: deprecated

manifests/postgresql.crd.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ spec:
194194
type: boolean
195195
enableShmVolume:
196196
type: boolean
197+
env:
198+
type: array
199+
nullable: true
200+
items:
201+
type: object
202+
x-kubernetes-preserve-unknown-fields: true
197203
init_containers:
198204
type: array
199205
description: deprecated

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
311311
"enableShmVolume": {
312312
Type: "boolean",
313313
},
314+
"env": {
315+
Type: "array",
316+
Nullable: true,
317+
Items: &apiextv1.JSONSchemaPropsOrArray{
318+
Schema: &apiextv1.JSONSchemaProps{
319+
Type: "object",
320+
XPreserveUnknownFields: util.True(),
321+
},
322+
},
323+
},
314324
"init_containers": {
315325
Type: "array",
316326
Description: "deprecated",

pkg/apis/acid.zalan.do/v1/postgresql_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type PostgresSpec struct {
8080
TLS *TLSDescription `json:"tls,omitempty"`
8181
AdditionalVolumes []AdditionalVolume `json:"additionalVolumes,omitempty"`
8282
Streams []Stream `json:"streams,omitempty"`
83+
Env []v1.EnvVar `json:"env,omitempty"`
8384

8485
// deprecated json tags
8586
InitContainersOld []v1.Container `json:"init_containers,omitempty"`

pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/k8sres.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,10 @@ func deduplicateEnvVars(input []v1.EnvVar, containerName string, logger *logrus.
938938
func (c *Cluster) getPodEnvironmentConfigMapVariables() ([]v1.EnvVar, error) {
939939
configMapPodEnvVarsList := make([]v1.EnvVar, 0)
940940

941+
if len(c.Spec.Env) > 0 {
942+
configMapPodEnvVarsList = append(configMapPodEnvVarsList, c.Spec.Env...)
943+
}
944+
941945
if c.OpConfig.PodEnvironmentConfigMap.Name == "" {
942946
return configMapPodEnvVarsList, nil
943947
}
@@ -959,7 +963,9 @@ func (c *Cluster) getPodEnvironmentConfigMapVariables() ([]v1.EnvVar, error) {
959963
}
960964
}
961965
for k, v := range cm.Data {
962-
configMapPodEnvVarsList = append(configMapPodEnvVarsList, v1.EnvVar{Name: k, Value: v})
966+
if !isEnvVarExist(configMapPodEnvVarsList, k) {
967+
configMapPodEnvVarsList = append(configMapPodEnvVarsList, v1.EnvVar{Name: k, Value: v})
968+
}
963969
}
964970
return configMapPodEnvVarsList, nil
965971
}
@@ -968,6 +974,10 @@ func (c *Cluster) getPodEnvironmentConfigMapVariables() ([]v1.EnvVar, error) {
968974
func (c *Cluster) getPodEnvironmentSecretVariables() ([]v1.EnvVar, error) {
969975
secretPodEnvVarsList := make([]v1.EnvVar, 0)
970976

977+
if len(c.Spec.Env) > 0 {
978+
secretPodEnvVarsList = append(secretPodEnvVarsList, c.Spec.Env...)
979+
}
980+
971981
if c.OpConfig.PodEnvironmentSecret == "" {
972982
return secretPodEnvVarsList, nil
973983
}
@@ -999,20 +1009,31 @@ func (c *Cluster) getPodEnvironmentSecretVariables() ([]v1.EnvVar, error) {
9991009
}
10001010

10011011
for k := range secret.Data {
1002-
secretPodEnvVarsList = append(secretPodEnvVarsList,
1003-
v1.EnvVar{Name: k, ValueFrom: &v1.EnvVarSource{
1004-
SecretKeyRef: &v1.SecretKeySelector{
1005-
LocalObjectReference: v1.LocalObjectReference{
1006-
Name: c.OpConfig.PodEnvironmentSecret,
1012+
if !isEnvVarExist(secretPodEnvVarsList, k) {
1013+
secretPodEnvVarsList = append(secretPodEnvVarsList,
1014+
v1.EnvVar{Name: k, ValueFrom: &v1.EnvVarSource{
1015+
SecretKeyRef: &v1.SecretKeySelector{
1016+
LocalObjectReference: v1.LocalObjectReference{
1017+
Name: c.OpConfig.PodEnvironmentSecret,
1018+
},
1019+
Key: k,
10071020
},
1008-
Key: k,
1009-
},
1010-
}})
1021+
}})
1022+
}
10111023
}
10121024

10131025
return secretPodEnvVarsList, nil
10141026
}
10151027

1028+
func isEnvVarExist(envs []v1.EnvVar, key string) bool {
1029+
for _, env := range envs {
1030+
if env.Name == key {
1031+
return true
1032+
}
1033+
}
1034+
return false
1035+
}
1036+
10161037
func getSidecarContainer(sidecar acidv1.Sidecar, index int, resources *v1.ResourceRequirements) *v1.Container {
10171038
name := sidecar.Name
10181039
if name == "" {

0 commit comments

Comments
 (0)