helmfile icon indicating copy to clipboard operation
helmfile copied to clipboard

Variables in kustomization.yaml

Open olfway opened this issue 5 years ago • 6 comments

Is it possible to use go template variables in kustomization.yaml file?

For example I want to have something like this:

$ cat kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- https://github.com/kubernetes-sigs/metrics-server/releases/download/{{ .Values.appVersion }}/components.yaml

patches:
- target:
    group: apps
    version: v1
    kind: Deployment
    name: metrics-server
  path: patches/deployment.yaml

olfway avatar Jul 08 '20 18:07 olfway

@olfway Hey! Interesting idea. It's impossible today, but worth investigating.

What .Values.appVersion should refer to, btw? The values of the release, or helmfile state(environment) values?

mumoshu avatar Aug 01 '20 03:08 mumoshu

I.e. what if helmfile allowed this?

releases:
- name: yourapp
  kustomization: some/dir/kustomization.yaml.gotmpl
  values:
  - appVersion: ver

mumoshu avatar Aug 01 '20 03:08 mumoshu

I thought .Values. should refer to the values of the release, like in your example

olfway avatar Aug 09 '20 19:08 olfway

I absolutely need this feature. My use case is this use a namespace for the installation but also set the subjects[0].namespace in ClusterRoleBinding.rbac.authorization.k8s.io. So the question is, how do I inject env var of ROOK_NFS_OPERATOR_NAMESPACE into the kustomize with helmfile?

releases:
  {{- if eq (env "NFS_STRATEGY") "rook" }}
  - name: rook-nfs-operator
    namespace: {{ env "ROOK_NFS_OPERATOR_NAMESPACE" | default "rook-nfs-system" }}
    chart: ./rook-nfs-operator
    hooks:
      - events:
          - prepare
          - cleanup
        command: ./helmify.sh
        args:
          - "{{`{{if eq .Event.Name \"prepare\"}}build{{else}}clean{{end}}`}}"
          - "{{`{{.Release.Chart}}`}}"
          - default
  {{- end }}

In my rook-nfs-operator-kustomize/base/operator.yaml, I will have this:

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: rook-nfs-operator
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: rook-nfs-operator
subjects:
- kind: ServiceAccount
  name: rook-nfs-operator
  namespace: $(ROOK_NFS_OPERATOR_NAMESPACE)

darkn3rd avatar Oct 25 '20 06:10 darkn3rd

@darkn3rd Hey! How do you define ROOK_NFS_OPERATOR_NAMESPACE in your kustomization? Is it just an entry under kustomize vars?

AFAIK, it's kustomize's own restriction that you can't inject arbitrary built-time args. I'm not sure how we workaround that.

mumoshu avatar Nov 09 '20 00:11 mumoshu

This feature would be pretty amazing. As far as I've been testing, we can't pass values to the Kustomize builds, which is very important in case you are building the manifests for different teams and/or environments. For example, if you are using Helmfile to deploy overlays of Kubeflow that depend on the environment, you must write your overlays and patches in the helmfile.yaml file because the patches that would be inside the different Kustomize overlays wouldn't get the values available in the helmfile.yaml file.

davidggz avatar Feb 10 '23 10:02 davidggz