Home > OS >  Can I consume a ConfigMap value in arbitrary places in YAML?
Can I consume a ConfigMap value in arbitrary places in YAML?

Time:04-24

In Azure Kubernetes Service, my goal is to configure both staging and production k8 clusters with a common YAML file, with critical values & environment variables parameterized from a ConfigMap.

I can set container environment variables easily using valueFrom but I would like to use the ConfigMap values in other areas of the YAML file, for example:

staging-config-map.yaml:

kind: ConfigMap 
apiVersion: v1 
metadata:
  name: base-config 
data:
  ENVIRONMENT_NAME: staging
  ...

prod-config-map.yaml:

kind: ConfigMap 
apiVersion: v1 
metadata:
  name: base-config 
data:
  ENVIRONMENT_NAME: prod
  ...

common-cluster-config.yaml:

apiVersion: v1
kind: Service
metadata:
  name: my-amazing-microservice
  annotations:
    service.beta.kubernetes.io/azure-dns-label-name: "my-amazing-microservice-$ENVIRONMENT_NAME"
spec:
  type: LoadBalancer
  ports:
  - targetPort: 5000
    name: port5000
    port: 5000
    protocol: TCP
  selector:
    app: my-amazing-microservice
---
...

Note the reference to $ENVIRONMENT_NAME which is where I want to insert something from the ConfigMap.

Can I do this, so I don't have to maintain duplicated manifests for staging and prod?

CodePudding user response:

No you cant with vanilla k8's manifests. ConfigMaps are just resources that get mounted into the container when it starts. Either as env variables or as a file. You cant access a config map at deployment time.

I suggest looking into helm that can do this with templating.

  • Related