travisghansen / argo-cd-helmfile

Integration between argo-cd and helmfile
MIT License
213 stars 55 forks source link

Cant read .Capabilities.APIVersions.Has #23

Closed kfirfer closed 2 years ago

kfirfer commented 2 years ago

Hello

Theres some charts for example ingress-nginx that cannot read the go template .Capabilities.APIVersions.Has

For example im trying to install ingress-nginx chart with prometheus rule enabled and he doesnt get know if it capability

Example go template:

{{- if and ( .Values.controller.metrics.enabled ) ( .Values.controller.metrics.prometheusRule.enabled ) ( .Capabilities.APIVersions.Has "monitoring.coreos.com/v1" ) -}}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ include "ingress-nginx.controller.fullname" . }}
{{- if .Values.controller.metrics.prometheusRule.namespace }}
  namespace: {{ .Values.controller.metrics.prometheusRule.namespace | quote }}
{{- end }}
  labels:
    {{- include "ingress-nginx.labels" . | nindent 4 }}
    app.kubernetes.io/component: controller
  {{- if .Values.controller.metrics.prometheusRule.additionalLabels }}
    {{- toYaml .Values.controller.metrics.prometheusRule.additionalLabels | nindent 4 }}
  {{- end }}
spec:
{{- if .Values.controller.metrics.prometheusRule.rules }}
  groups:
  - name: {{ template "ingress-nginx.name" . }}
    rules: {{- toYaml .Values.controller.metrics.prometheusRule.rules | nindent 4 }}
{{- end }}
{{- end }}

What should I do ?

travisghansen commented 2 years ago

Put something like this in your helmfiles:

apiVersions:
{{- if (env "KUBE_API_VERSIONS") }}
{{- range $apiV := ( requiredEnv "KUBE_API_VERSIONS" | split "," ) }}
- {{ $apiV }}
{{- end }}
{{- end }}

I personally put that snippet in as base file and have something like this at the top of all my release yaml:

---
bases:
- ../../bases/repositories.yaml
- ../../bases/environments.yaml.gotmpl
- ../../bases/api-versions.yaml

# https://github.com/roboll/helmfile/issues/1230
---
releases:
...
kfirfer commented 2 years ago

Thanks ill try it

I also trying to put this env var of repo server:

    - name: HELM_TEMPLATE_OPTIONS
      value: "--kube-version 1.22 --validate"

Will update if its works

kfirfer commented 2 years ago

The HELM_TEMPLATE_OPTIONS envar doesnt worked although I tried to run helm templat and its outputting the prometheusrule:

helm template ingress-nginx ingress-nginx/ingress-nginx --version 4.2.1 --namespace ingress-nginx --values nucs/dev/us-east/infra/namespaces/ingress-nginx/helmfile/ingress-nginx/values.yaml --values nucs/dev/us-east/infra/namespaces/ingress-nginx/helmfile/ingress-nginx/values-env-specific.yaml --kube-version 1.22 --validate > output.yaml

travisghansen commented 2 years ago

You shouldn't need to handle those values manually, argocd passes both the kube version and api versions off to the plugin as standard procedure (which is good because if you have argocd handling multiple clusters the values should be dynamic anyway).

kfirfer commented 2 years ago

I think what makes the difference is the --validate argument to the helm template, not the kubeapi version

I mean both makes the differences I guess

travisghansen commented 2 years ago

Is it working with the snippet I sent then?

kfirfer commented 2 years ago

yes it is, thanks !