How to use if condition in helm chart
Asked Answered
R

2

12

I have below values in values.yaml

  pg_hba:
    - hostssl all all 0.0.0.0/0 md5
    - host    all all 0.0.0.0/0 md5

The reqiurement is to check the hostssl line exists , if yes it should go into the if loop and do something.

i tried to use {{ if has "hostssl" .Values.pg_hba }} but it seraches only for the exact string "hostssll" and not the entire line.

Please help on how i can check for the entrire line in if condition.

Rubberize answered 13/5, 2020 at 7:16 Comment(2)
What you mean by "hostssl line exists"? Do you want to check if line contains hostssl?Sparling
Hi Grigoriy, ii want to check if the "hostssl all all 0.0.0.0/0 md5" contains /exits in if condition Please note the values after hostall may chnage as per the requirement.Rubberize
S
30

I have not fully understood your question, so here are 3 options.

To check if two string are equal, Go has built in template function eq, here is use example:

{{ if eq "line" "line" }}
> true

If you want to check if line contains hostssl string. Helm has sprig as it's dependency -- it's module that provides additional template functions. One of these functions is contains, that checks if string is contained inside another:

{{ if contains "cat" "catch" }}
> true

If you want to check, if string has hostssl precisely at it's start you can use another function, provided by sprig -- hasPrefix:

{{ if hasPrefix "cat" "catch" }}
> true

Here is a list of all string functions that sprig offers. If none of above options does satisfy your requirement, you can use regex function for matching.

Sparling answered 13/5, 2020 at 7:41 Comment(6)
Below is the content in values.yaml , spec: patroni: pg_hba: - hostssl all all 0.0.0.0/0 md5 - host all all 0.0.0.0/0 md5 i tried as below , {{ if hasPrefix "hostssl" .Values.spec.patroni.pg_hba } But i see below error: template: postgres-operator-cluster/templates/pkicertificate.yaml:1:33: executing "postgres-operator-cluster/templates/pkicertificate.yaml" at <.Values.spec.patroni.pg_hba>: wrong type for value; expected string; got []interface {}Rubberize
@NeelamSharma .Values.spec.patroni.pg_hba is array. You need to iterate over it and apply hasPrefix to each entry. Also, you need to convert interface to string(or quote values in values.yaml). There's plenty of answers on how you can do that on SO. Use searchSparling
I have defined below function to iterate over the range and use if condition: {{- define "sslmode" }} {{- range $k, $v := $.Values.spec.patroni }} {{ index $k | trim | -}}: {{ if hasPrefix "hostssl" $k }} {{- $v | toYaml | trimSuffix "\n"| nindent 2 -}} {{- end }} {{- end }} {{- end }} But the O/P returned is as : pg_hba: Please let me know what I'm missingRubberize
You iterate over Values.spec.patroni, but need to iterate over Values.spec.patroni.pg_hbaSparling
i tried tht option too but it gives the same error as: executing "postgres-operator-cluster/templates/deployment.yaml" at <include "sslmode" .>: error calling include: template: postgres-operator-cluster/templates/_helpers.tpl:61:14: executing "sslmode" at <trim>: wrong type for value; expected string; got intRubberize
@NeelamSharma {{ index $k | trim | -}} -- you're trimming index, not value. Instead try {{ index $v | trim | -}}. But still, it probably won't work, because you have interface values, so, first you need to convert them to string.Sparling
P
2

I had a use case where I need to write my yaml's in such a way that, the same yaml should be used for multuple types of Kubernetes Cluster like gke, aks, eks, minikube etc.,

To achieve this, I declared a variable in values.yaml and used it as a operand to compare so that, if cluster_type is gke, my yaml will have one annotation, and if cluster_type is minikube, it takes another.... below is the sample code

my sample values.yaml file

global:
# Supported cluster types : gke, minikube
  cluster_type: gke
  name: core-services
  namespace: yeedu
  environment: test

in my service yaml file

{{- if eq "minikube" $.Values.global.cluster_type }}
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
{{- end }}
Pamphylia answered 26/8, 2022 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.