diff --git a/.vscode/launch.json b/.vscode/launch.json index 33af85654..eb87a8e79 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -54,7 +54,8 @@ "mode": "debug", "program": "${workspaceFolder}/cli", "cwd": "${workspaceFolder}/cli", - "args": ["uninstall", "--yes"] + "args": ["uninstall", "--yes"], + "buildFlags": "-tags=embed_manifests" } ] } \ No newline at end of file diff --git a/cli/cmd/uninstall.go b/cli/cmd/uninstall.go index 111e33ca1..2de63e443 100644 --- a/cli/cmd/uninstall.go +++ b/cli/cmd/uninstall.go @@ -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, }) }