8

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Neelam Sharma
  • 99
  • 1
  • 2
  • 5
  • What you mean by "hostssl line exists"? Do you want to check if line contains `hostssl`? – Grigoriy Mikhalkin May 13 '20 at 07:27
  • 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. – Neelam Sharma May 13 '20 at 07:36

2 Answers2

23

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Grigoriy Mikhalkin
  • 5,035
  • 1
  • 18
  • 36
  • 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 {} – Neelam Sharma May 13 '20 at 07:55
  • @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 search – Grigoriy Mikhalkin May 13 '20 at 07:57
  • 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 missing – Neelam Sharma May 13 '20 at 08:18
  • You iterate over `Values.spec.patroni`, but need to iterate over `Values.spec.patroni.pg_hba` – Grigoriy Mikhalkin May 13 '20 at 09:05
  • i tried tht option too but it gives the same error as: executing "postgres-operator-cluster/templates/deployment.yaml" at : error calling include: template: postgres-operator-cluster/templates/_helpers.tpl:61:14: executing "sslmode" at : wrong type for value; expected string; got int – Neelam Sharma May 13 '20 at 09:27
  • @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. – Grigoriy Mikhalkin May 13 '20 at 09:40
1

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 }}