Skip to content

Commit

Permalink
Support ConfigMap items mounted as EnvValue
Browse files Browse the repository at this point in the history
  • Loading branch information
katyanna committed Oct 19, 2023
1 parent cd46a25 commit 6281a4e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
25 changes: 19 additions & 6 deletions controller/stack_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions controller/stack_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6281a4e

Please sign in to comment.