kuberneteskubernetes-helmhelmfile

Helmfile - Files.Get a javascript file


I am using helmfile as oppose to helm, and tried to follow this example: Kubernetes - How to define ConfigMap built using a file in a yaml? but didn't seem to work (I guess its due to me using helmfile).

I have a values file with the following, with two data objects:

configmap:
  configMapName: default-users-data-configmap
  configMapData:
    default-users-config.json: |-
      {
        "foo": "bar",
      }
    someJavaScriptFile.js: |-
      {{ .Files.Get "js/someJavaScriptFile.js" }} 

My configmap looks like this:

{{- with .Values.configmap }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .configMapName }}
data:
  {{- .configMapData | toYaml | nindent 2 }}
{{- end }}

Now what is positive is I am getting the json contents to show in configmap when I run a helmfile sync

But the issue is for the javascript contents its showing:

Data
====
someJavaScriptFile.js:
----
{{ .Files.Get "js/someJavaScriptFile.js" }}

Its not reading the contents from the javascript folder and putting it into config and can't figure out if my syntax is wrong.

my structure of directory looks like the following:

helmfile -> charts -> contains charts
helmfile -> js -> someJavaScriptFile.js
helmfile -> helmfile.yaml

The charts directory contains my generic chart with my configmap.yaml, deployment.yaml, etc I'm running the sync in the helmfile directory so assumed it would pick up that javascript, it does dump the json, so assuming its struggling with the javascript.

Any ideas where the issue is? Thanks


Solution

  • I was able to make it work by making some changes to how you use the templates:

    Example

    my-helmchart/values.yaml

    configmap:
      configMapName: my-configmap
      defaultUsersConfig:
        user1:
          name: John Doe
          email: john.doe@example.com
        user2:
          name: Jane Doe
          email: jane.doe@example.com
      javascriptFilePath: js/someJavaScriptFile.js
    

    my-helmchart/js/someJavaScriptFile.js

    console.log("Hello, world!");
    

    my-helmchart/templates/configmap.yaml

    {{- with .Values.configmap }}
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .configMapName }}
    data:
      default-users-config.json: |-
    {{ .defaultUsersConfig | toPrettyJson | indent 4 }}
      someJavaScriptFile.js: |-
    {{ $.Files.Get .javascriptFilePath | indent 4 }}
    {{- end }}
    

    Please note that $.Files.Get .javascriptFilePath is used instead of .Files.Get .javascriptFilePath.

    The $ symbol is used to refer to the root context, allowing you to access .Files.Get from within the with block.

    Running helm template:

    ---
    # Source: my-helmchart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-configmap
    data:
      default-users-config.json: |-
        {
          "user1": {
            "email": "john.doe@example.com",
            "name": "John Doe"
          },
          "user2": {
            "email": "jane.doe@example.com",
            "name": "Jane Doe"
          }
        }
      someJavaScriptFile.js: |-
        console.log("Hello, world!");