Skip to content

Commit

Permalink
fix: revert env value when uninstalling (#1325)
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir authored Jul 4, 2024
1 parent 908e2b7 commit 26f1714
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"mode": "debug",
"program": "${workspaceFolder}/cli",
"cwd": "${workspaceFolder}/cli",
"args": ["uninstall", "--yes"]
"args": ["uninstall", "--yes"],
"buildFlags": "-tags=embed_manifests"
}
]
}
18 changes: 15 additions & 3 deletions cli/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,29 @@ func getWorkloadRolloutJsonPatch(obj client.Object, pts *v1.PodTemplateSpec) ([]
}

for envName, originalEnvValue := range manifestEnvOriginal.GetContainerStoredEnvs(c.Name) {
if origManifestEnv == nil {
// find the index of the env var in the env array:
iEnv := -1
for i, env := range c.Env {
if env.Name == envName {
iEnv = i
break
}
}
if iEnv == -1 {
return nil, fmt.Errorf("env var %s not found in container %s", envName, c.Name)
}

if originalEnvValue == nil {
// originally the value was absent, so we remove it
patchOperations = append(patchOperations, map[string]interface{}{
"op": "remove",
"path": fmt.Sprintf("/spec/template/spec/containers/%d/env/%d", iContainer, envName),
"path": fmt.Sprintf("/spec/template/spec/containers/%d/env/%d", iContainer, iEnv),
})
} else {
// revert the env var to its original value
patchOperations = append(patchOperations, map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/spec/template/spec/containers/%d/env/%d/value", iContainer, envName),
"path": fmt.Sprintf("/spec/template/spec/containers/%d/env/%d/value", iContainer, iEnv),
"value": *originalEnvValue,
})
}
Expand Down

0 comments on commit 26f1714

Please sign in to comment.