Kubernetes - ConfigMap for nested variables

10/4/2018

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?

-- RahulB
configmap
kubernetes

1 Answer

10/5/2018

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
Source: StackOverflow