kubernetes: set environment variable as integer

5/12/2021

I want to set Kubernetes Deployment env with integer value, but I have to quote the value for Kubernetes Deployment to accept it. This makes the env value a string and is causing a TypeError in the app.

Is there any workaround to set integer or float value to env?

-- roy
environment-variables
kubernetes

1 Answer

5/13/2021

Generally it was answered in comment, however I'll add references from official kubernetes documentation.

env field uses EnvVar array. Based on EnvVar v1 core API group, name and value should be only strings. Please see EnvVar v1 core

And here is an official example to see how variables are set:

apiVersion: v1
kind: Pod
metadata:
  name: dependent-envars-demo
spec:
  containers:
    - name: dependent-envars-demo
      args:
        - while true; do echo -en '\n'; printf UNCHANGED_REFERENCE=$UNCHANGED_REFERENCE'\n'; printf SERVICE_ADDRESS=$SERVICE_ADDRESS'\n';printf ESCAPED_REFERENCE=$ESCAPED_REFERENCE'\n'; sleep 30; done;
      command:
        - sh
        - -c
      image: busybox
      env:
        - name: SERVICE_PORT
          value: "80"
        - name: SERVICE_IP
          value: "172.17.0.1"
        - name: UNCHANGED_REFERENCE
          value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"
        - name: PROTOCOL
          value: "https"
        - name: SERVICE_ADDRESS
          value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"
        - name: ESCAPED_REFERENCE
          value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"

Link to this example is here

-- moonkotte
Source: StackOverflow