Allow user env vars to override operator-generated ones

This change modifies appendEnvVars() to allow environment variables
defined in the PostgreSQL CRD spec.env to override operator-generated
environment variables (like SPILO_CONFIGURATION) instead of being
silently ignored.

Previously, if an env var already existed in the list, user-provided
values were skipped. Now, user values take precedence and replace
the operator-generated ones.

This enables users to customize SPILO_CONFIGURATION and other
operator-managed env vars through the CRD, which is useful for
adding custom Patroni DCS configuration like ignore_slots.
This commit is contained in:
Pavel Zaytsev 2025-11-12 14:53:33 -08:00
parent 2c57498e43
commit c5b3cc9c3a
1 changed files with 14 additions and 1 deletions

View File

@ -1088,7 +1088,20 @@ func (c *Cluster) generateSpiloPodEnvVars(
func appendEnvVars(envs []v1.EnvVar, appEnv ...v1.EnvVar) []v1.EnvVar {
collectedEnvs := envs
for _, env := range appEnv {
if !isEnvVarPresent(collectedEnvs, env.Name) {
// Check if env var already exists
existingIdx := -1
for i, existing := range collectedEnvs {
if strings.EqualFold(existing.Name, env.Name) {
existingIdx = i
break
}
}
if existingIdx >= 0 {
// Replace existing env var (user override takes precedence)
collectedEnvs[existingIdx] = env
} else {
// Add new env var
collectedEnvs = append(collectedEnvs, env)
}
}