From 6281a4eb80538fd9a18b1ca33fdf86582cd992e6 Mon Sep 17 00:00:00 2001 From: Katyanna Moura Date: Thu, 19 Oct 2023 17:33:56 +0200 Subject: [PATCH] Support ConfigMap items mounted as EnvValue --- controller/stack_resources.go | 25 +++++++++++++---- controller/stack_resources_test.go | 45 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/controller/stack_resources.go b/controller/stack_resources.go index 74160d54..1704aa6e 100644 --- a/controller/stack_resources.go +++ b/controller/stack_resources.go @@ -340,30 +340,43 @@ func (c *StackSetController) deleteConfigMapTemplate(ctx context.Context, stack return nil } +// Update referencings of the ConfigMap on the PodTemplate +// The ConfigMap name is updated to the expected versioned name in the Stack.PodTemplate +// to ensure Pods rely on the Stack owned resource. func (c *StackSetController) updateStackConfigMap( ctx context.Context, stack *zv1.Stack, configMapNames map[string]string, ) error { for templateName, versionedName := range configMapNames { + // Change ConfigMap reference on Stack's Volumes + for _, volume := range stack.Spec.PodTemplate.Spec.Volumes { + if volume.ConfigMap != nil && volume.ConfigMap.Name == templateName { + volume.ConfigMap.Name = versionedName + } + } + // Change ConfigMap reference on Stack's EnvFrom for _, container := range stack.Spec.PodTemplate.Spec.Containers { for _, envFrom := range container.EnvFrom { - if envFrom.ConfigMapRef.Name == templateName { + if envFrom.ConfigMapRef != nil && envFrom.ConfigMapRef.Name == templateName { envFrom.ConfigMapRef.Name = versionedName } } } - // Change ConfigMap reference on Stack's Volumes - for _, volume := range stack.Spec.PodTemplate.Spec.Volumes { - if volume.ConfigMap.Name == templateName { - volume.ConfigMap.Name = versionedName + // Change ConfigMap reference on Stack's EnvValue + for _, container := range stack.Spec.PodTemplate.Spec.Containers { + for _, env := range container.Env { + if env.ValueFrom != nil && env.ValueFrom.ConfigMapKeyRef != nil { + if env.ValueFrom.ConfigMapKeyRef.Name == templateName { + env.ValueFrom.ConfigMapKeyRef.Name = versionedName + } + } } } } - // Update Stack _, err := c.client.ZalandoV1().Stacks(stack.Namespace).Update(ctx, stack, metav1.UpdateOptions{}) if err != nil { return err diff --git a/controller/stack_resources_test.go b/controller/stack_resources_test.go index 487efc10..cc7a0625 100644 --- a/controller/stack_resources_test.go +++ b/controller/stack_resources_test.go @@ -1006,6 +1006,25 @@ func TestReconcileStackConfigMap(t *testing.T) { }, } + singleEnvValueConfigMapStack := singleConfigMapStack + singleEnvValueConfigMapStack.Spec.PodTemplate.Spec = v1.PodSpec{ + Containers: []v1.Container{ + { + Env: []v1.EnvVar{ + { + ValueFrom: &v1.EnvVarSource{ + ConfigMapKeyRef: &v1.ConfigMapKeySelector{ + LocalObjectReference: v1.LocalObjectReference{ + Name: "single-configmap", + }, + }, + }, + }, + }, + }, + }, + } + multipleConfigMapsStack := baseTestStack multipleConfigMapsStack.Spec = zv1.StackSpecInternal{ StackSpec: zv1.StackSpec{ @@ -1130,6 +1149,32 @@ func TestReconcileStackConfigMap(t *testing.T) { }, }, }, + { + name: "configmap version is created, set as envValue", + stack: singleEnvValueConfigMapStack, + existing: nil, + template: []*v1.ConfigMap{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "single-configmap", + Namespace: singleConfigMapStack.Namespace, + }, + Data: map[string]string{ + "testK": "testV", + }, + Immutable: nil, + }, + }, + expected: []*v1.ConfigMap{ + { + ObjectMeta: singleConfigMapMetaObj, + Data: map[string]string{ + "testK": "testV", + }, + Immutable: &immutable, + }, + }, + }, { name: "stack already has a configmap version", stack: singleConfigMapStack,