Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is there no "break" support for flow control methods in helm template

I have a range function but would like to break from the control if certain condition is met. Is there a way this can be achieved in helm template

UPDATE:

I have seen some related queries where conclusion is "break" is unsupported. But would like to get some official documentation OR reason in case this is not supported.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are correct that there is no direct support for break in Helm templates, as they use the Go text/template package under the hood, which does not provide a built-in break statement. The Go text/template package is intentionally kept simple and limited in its control structures, which is why there is no support for break within range loops.

To work around this limitation, you can use if and else statements within the loop to control the execution flow based on a condition. However, you cannot fully break out of the loop; you can only skip iterations. You can also use variables and the and function to achieve a similar effect.

Here’s an example of using if and else to control the flow within a Helm template.

{{- $found := false -}}
{{- range $index, $element := .Values.elements -}}
  {{- if not $found -}}
    {{- if eq $element "target" -}}
      {{- $found = true -}}
      {{- /* Process the target element here */ -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

In this example, we use the $found variable to track if the target element has been found. Once it’s found, we set $found to true, and further iterations will not process the target element.

Unfortunately, there is no official documentation that explicitly states that break is unsupported. The closest resource is the Go text/template package documentation, which can be found here: https://pkg.go.dev/text/template

This documentation lists the available actions and control structures but does not mention a break statement. The fact that it is not listed suggests that it is not supported by the Go text/template package, which in turn means it is not supported in Helm templates either.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading