From 36757f07b0fb2aca8293948702f283ef9335188d Mon Sep 17 00:00:00 2001 From: tamirdavid1 Date: Sun, 22 Dec 2024 16:29:35 +0200 Subject: [PATCH] fix: overwrite in case odigos update values --- .../migrate_runtime_details.go | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/cli/cmd/migrations/runtime_details_migration/migrate_runtime_details.go b/cli/cmd/migrations/runtime_details_migration/migrate_runtime_details.go index f57d2a227..65ad16cf2 100644 --- a/cli/cmd/migrations/runtime_details_migration/migrate_runtime_details.go +++ b/cli/cmd/migrations/runtime_details_migration/migrate_runtime_details.go @@ -231,8 +231,10 @@ func handleContainerRuntimeDetailsUpdate( annotationEnvVarsForContainer := originalWorkloadEnvVar.GetContainerStoredEnvs(containerObject.Name) for envKey, envValue := range annotationEnvVarsForContainer { isEnvVarAlreadyExists := isEnvVarPresent(containerRuntimeDetails.EnvVarsFromDockerFile, envKey) + // The runtimeDetails might already contain the environment variable if Odiglet started before the migration was executed and modified the env vars. + // In this case, we want to overwrite the value set by Odiglet with a new one. if isEnvVarAlreadyExists { - continue + containerRuntimeDetails.EnvVarsFromDockerFile = removeEnvVar(containerRuntimeDetails.EnvVarsFromDockerFile, envKey) } // Handle runtime-originated environment variables @@ -240,10 +242,14 @@ func handleContainerRuntimeDetailsUpdate( containerEnvFromManifestValue := k8scontainer.GetContainerEnvVarValue(&containerObject, envKey) if containerEnvFromManifestValue != nil { workloadEnvVarWithoutOdigosAdditions := cleanUpManifestValueFromOdigosAdditions(envKey, *containerEnvFromManifestValue) - envVarWithoutOdigosAddition := v1alpha1.EnvVar{Name: envKey, Value: workloadEnvVarWithoutOdigosAdditions} - containerRuntimeDetails.EnvVarsFromDockerFile = append(containerRuntimeDetails.EnvVarsFromDockerFile, envVarWithoutOdigosAddition) - state := v1alpha1.ProcessingStateSucceeded - containerRuntimeDetails.RuntimeUpdateState = &state + + if workloadEnvVarWithoutOdigosAdditions != "" { + envVarWithoutOdigosAddition := v1alpha1.EnvVar{Name: envKey, Value: workloadEnvVarWithoutOdigosAdditions} + containerRuntimeDetails.EnvVarsFromDockerFile = append(containerRuntimeDetails.EnvVarsFromDockerFile, envVarWithoutOdigosAddition) + state := v1alpha1.ProcessingStateSucceeded + containerRuntimeDetails.RuntimeUpdateState = &state + } + } } } @@ -294,3 +300,13 @@ func isEnvVarPresent(envVars []v1alpha1.EnvVar, envVarName string) bool { } return false } + +func removeEnvVar(envVars []v1alpha1.EnvVar, key string) []v1alpha1.EnvVar { + filteredEnvVars := []v1alpha1.EnvVar{} + for _, envVar := range envVars { + if envVar.Name != key { + filteredEnvVars = append(filteredEnvVars, envVar) + } + } + return filteredEnvVars +}