Escaping '.' in helms chart yaml file

3/27/2020

I need to define an env var with name contains '.' characters, and Kubenetes does not seem to like it.

    spec:
      containers:
        env:
        - name: "com.my.app.dir"
          value: "/myapp/subdir/"

I tried single quotes, double quotes, backslashes, double backslashes, and many other ways. Still cannot make it work. I wonder if anyone knows a way to escape the '.' characters. Thanks in advance.

-- Edwin
kubernetes

1 Answer

3/28/2020

Kubernetes doesn't have a problem setting an environment variable with a .

Here's a simple spec that logs the environment by directly running the node executable

apiVersion: v1
kind: Pod
metadata:
  name: env-node
spec:
  containers:
  - image: 'node:12-slim'
    name: env-node
    command:
    - node
    - '-pe'
    - process.env
    env:
    - name: OTHER
      value: here
    - name: 'ONE_two-Three.four'
      value: 'diditwork'

And the environment output (with some kubernetes default vars removed for brevity)

{
  PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  HOSTNAME: 'env-node',
  NODE_VERSION: '12.16.1',
  OTHER: 'here',
  'ONE_two-Three.four': 'diditwork',
  HOME: '/root'
}

Most shells (sh, bash, zsh) won't accept environment variables with a . in them. POSIX defines [a-zA-Z_][a-zA-Z0-9_]* as the allowed characters in the name of an environment variable.

So running the same node process via a shell:

spec:
  containers:
  - image: 'node:12-slim'
    name: nodeenvtest-simple-shell
    command:
    - sh
    - '-c'
    - 'node -e "console.log(process.env)"'
    env:
    - name: 'ONE_two-Three.four'
      value: 'diditwork'
    - name: 'OTHER'
      value: 'here'

Results in a missing environment variable:

{
  NODE_VERSION: '12.16.1',
  HOSTNAME: 'env-shell',
  HOME: '/root',
  OTHER: 'here',
  PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  PWD: '/'
}

If there is no shell between the container and the app running, a . after the first character in the environment variable should be fine.

-- Matt
Source: StackOverflow