Neelam Sharma
Neelam Sharma

Reputation: 179

How to use if condition in helm chart

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.

Upvotes: 14

Views: 65814

Answers (2)

Sadhvik Chirunomula
Sadhvik Chirunomula

Reputation: 1629

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

Upvotes: 2

Grigoriy Mikhalkin
Grigoriy Mikhalkin

Reputation: 5573

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.

Upvotes: 32

Related Questions