roboll / helmfile

Deploy Kubernetes Helm Charts
MIT License
4.05k stars 565 forks source link

Add resource lookup function like in helm3 #1097

Open paulczar opened 4 years ago

paulczar commented 4 years ago

Helm 3 now has a lookup template function that lets you check for resources. This would allow us to run hooks based on pre-existing resources... for example helm3 does not currently create namespaces ... we could put a condition around a release that looks something like:

{{ if not (lookup "v1" "Namespace" .Release.namespace "my-app") }}
hooks:
  - events: ["prepare", "cleanup"]
    showlogs: true
    command: "kubectl"
    args: ["create", "ns", "my-app"]
{{- end }}
mumoshu commented 4 years ago

@paulczar Thanks! I'm not familiar with the lookup function but is it basically kubectl get -n NAMESPACE KIND NAME?

Unfortunately, this doesn't seem to comply with how Helmfile works. If we add lookup, all the lookups would be executed BEFORE all the releases are parsed. Is that really an expected behavior?

mumoshu commented 4 years ago

Probably a better way would be to enhance hooks so that you can easily write things like this.

How about adding if to make a hook conditional, and optionally add run as an alternative to the pair of command and args, so that run and if seems visually consistent?

hooks:
- events: ["prepare", "cleanup"]
  sohwlogs: true
  if: ["kubectl", "get", "ns", "{{.Release.namespace}}"]
  run: ["kubectl", "create", "ns", "{{.Release.namespace}}"]
chrisferry commented 3 years ago

I also need this functionality I am trying to dynamically generate values based on configmap data. Example: domain: "example.{{ (lookup "v1" "ConfigMap" .Environment.Values.namespace "cluster-config").data.cluster_domain }}" Currently when trying to template this we receive: ➜ helmfile -e staging template in ./helmfile.yaml: error during helmfile.yaml.part.0 parsing: template: stringTemplate:21: function "lookup" not defined This should be relatively easy to implement no?

jiangfoxi commented 3 years ago

really hope to add this function

lucioveloso commented 3 years ago

This is a big challenge with the helmfile usage in general. The most updated Bitnami helm charts for example are relying on it, as they have a common library used by all helm charts with lookup function.

https://github.com/bitnami/charts/blob/70d602fea38010145c20e1ca59be06e4cf32bf80/bitnami/common/templates/_secrets.tpl#L84