Multiline values in kustomize environment source file

7/22/2019

I'm trying to set multiline values (contents of a ca certificate file) to kustomize environment file for a particular key as displayed in the code below. Is there a way to achieve this?

Note: Adding quotes to the value in some_params.env isn't working.

kustomize.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  role: authentication
bases:
- ./somebase

configMapGenerator:
- name: some-parameters
  env: some_params.env
vars:
- name: ca_contents
  objref:
    kind: ConfigMap
    name: some-parameters
    apiVersion: v1
  fieldref:
    fieldpath: data.ca_contents
configurations:
 - some_params.yaml

some_params.yaml

varReference:
- path: data/ca.pem
  kind: ConfigMap

some_params.env

ca_contents= |
-----BEGIN CERTIFICATE-----
YOUR CA CERTIFICATE CONTENTS
-----END CERTIFICATE-----

Running the following command: kustomize build base

Returns: Error: NewResMapFromConfigMapArgs: NewResMapFromConfigMapArgs: env source file: some_params.env: "-----BEGIN CERTIFICATE-----" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name', or 'MY_ENV.NAME', or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')

-- Krishna D
environment-variables
kubernetes
kustomize

1 Answer

3/28/2020

So it looks like you're creating ConfigMaps using Generators in Kustomize. And in doing so you're attempting to pass the contents of the desired ConfigMap to it directly in your kustomization.yml file.

Better practice would be to instead save the contents of these configuration files to disk and maintain them in the same location as your manifests. Then import them from file into your generators in Kustomize. For example:

configMapGenerator:
    - name: ca_contents
      files:
        - ca.crt

This will produce a ConfigMap with the name ca.crt with the contents of the file.

However, if you wanted, you can do the same thing with the literals operator and use multiline configuration directly in your kustomization.yml. For example:

configMapGenerator:
    - name: ca_contents
      literals:
        - ca.crt=|
            CERT_CONTENTS
            EACH_NEWLINE_IS_INDENTED_ANOTHER_TIME
            You_Can_Add_Additional_literals
        - ca.key=>
            AND_EVEN
            CONCATENATE_THEM
            _ONTO_ONE_LINE_WITH_OTHER_OPERATORS_LIKE_>
        - cert.crt="You can even mix literals with files in generators."
      files:
        - cert.key

Let me know if this makes sense. I advise you keep your configuration in files and import them into generators but either solution should work. Feel free to reach out if you need any other help. Kustomize is a really cool project!

-- TJ Zimmerman
Source: StackOverflow