5

We have an image deployed in an AKS cluster for which we need to update a config entry during deployment using configmaps.

The configuration file has the following key and we are trying to replace the value of the "ChildKey" without replacing the entire file -

{
  "ParentKey": {
    "ChildKey": "123"
  }
}

The configmap looks like -

apiVersion: v1
data:
  ParentKey: |
    ChildKey: 456
kind: ConfigMap
name: cf

And in the deployment, the configmap is used like this -

apiVersion: extensions/v1beta1
kind: Deployment
spec:
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: abc
    spec:
      containers:
      - env:
        - name: ParentKey
          valueFrom:
            configMapKeyRef:
              key: ParentKey
              name: cf

The replacement is not working with the setup above. Is there a different way to declare the key names for nested structures?

VAS
  • 8,538
  • 1
  • 28
  • 39
RahulB
  • 197
  • 1
  • 8

2 Answers2

0

We have addressed this in the following manner -

The configmap carries a simpler structure - only the child element -

apiVersion: v1
data:
  ChildKey: 456
kind: ConfigMap
name: cf

In the deployment, the environment variable key refers to the child key like this -

apiVersion: extensions/v1beta1
kind: Deployment
spec:
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: abc
    spec:
      containers:
      - env:
        - name: ParentKey__ChildKey
          valueFrom:
            configMapKeyRef:
              key: ChildKey
              name: cf

Posting this for reference.

RahulB
  • 197
  • 1
  • 8
0

use the double underscore for nested environment variables and arrays as explained here

To avoid explicit environment variables and typing names twice, you can use envFrom

configMap.yaml

apiVersion: v1
data:
  ParentKey__ChildKey: 456
kind: ConfigMap
name: cf

deployment.yml

containers:
       - name: $(name)
         image: $(image)
         envFrom:
         - configMapRef:
            name: common-config 
         - configMapRef:
            name: specific-config
shoop
  • 285
  • 3
  • 11