Helm 3 complete tutorial (7): Helm function explanation (1) logic and flow control functions

Summarizes the common knowledge points and concepts of Helm 3. Part of the content in the document comes from major blogs and official documents, and some content has been reorganized based on my own understanding and practice. All concept explanations will be equipped with practical codes. The article is updated from time to time, welcome to follow and subscribe (a paid column is opened, otherwise it will be crawled away by other private websites. Please be considerate of the big brothers and sisters, it is just a lunch, and please support it).
Link to the original text of this column: https://blog.csdn.net/xzk9381/category_10895812.html, please indicate the source for reprinting

The use method and function structure of some commonly used functions are introduced above. In this part of the article, all functions will be summarized and briefly explained. The detailed usage will be demonstrated in a later article combined with specific cases.

Helm functions can be roughly divided into the following categories:

  • Encrypted function
  • Date type function
  • Dictionary function
  • Encoding function
  • File path function
  • Kubernetes functions
  • Logic and flow control functions
  • List function
  • Mathematical calculation function
  • Network function
  • Mapping function
  • Regular expression functions
  • Semantic version function
  • String function
  • Type conversion function
  • URL processing function
  • UUID generation function

Logic and flow control functions

Helm provides several functions for processing logic and flow control:

  • and
  • coalesce
  • default
  • empty
  • eq
  • fail
  • ge
  • gt
  • le
  • lt
  • ne
  • not
  • or

1. and function

The and function is used to return the logical AND result (Boolean value) of two parameters, which means that if both parameters return true, the result is true. If one of them is not true, then the result is false.

For example, enter the following in the value.yaml file:

flag:
  open: true
  start: false

Create a template file named configmap.yaml in the template directory with the following content:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type: {
    
    {
    
     and .Values.flag.open .Values.flag.start | quote }}

Render the template file and view the result:

[@k8s-master1 /opt/helm/work]# helm install and-test ./mychart/ --debug --dry-run
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /opt/helm/work/mychart

NAME: and-test
LAST DEPLOYED: Fri Mar 26 11:33:56 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{
    
    }

COMPUTED VALUES:
flag:
  open: true
  start: false

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: and-test-configmap
data:
  type: "false"

2. or function

The or function is used to determine the logical OR relationship of two parameters. It returns the first parameter that is not empty or returns the last parameter. For example, the content of the template file configmap.yaml is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     or 1 "" 3 | quote }}
  type2: {
    
    {
    
     or 1 2 "" | quote }}
  type3: {
    
    {
    
     or "" 2 3  | quote }}
  type4: {
    
    {
    
     or "" "" 3 | quote }}

The test results of the rendering template are as follows:

[@k8s-master1 /opt/helm/work]# helm install or-test ./mychart/ --debug --dry-run
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /opt/helm/work/mychart

NAME: or-test
LAST DEPLOYED: Fri Mar 26 14:27:19 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
......
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: or-test-configmap
data:
  type1: "1"
  type2: "1"
  type3: "2"
  type4: "3"

3. not function

The not function is used to reverse the boolean value of the parameter. For example, the content of the template file configmap.yaml is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     not 1 | quote }}
  type2: {
    
    {
    
     not "" | quote }}

The test results of the rendering template are as follows:

[@k8s-master1 /opt/helm/work]# helm install not-test ./mychart/ --debug --dry-run
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /opt/helm/work/mychart

NAME: not-test
LAST DEPLOYED: Fri Mar 26 14:51:17 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
......

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: not-test-configmap
data:
  type1: "false"
  type2: "true"

Link to the original text of this column: https://blog.csdn.net/xzk9381/category_10895812.html, please indicate the source for reprinting

4. ne function

The ne function is used to determine whether two parameters are not equal, if not equal to true, and equal to false. Use the configmap.yaml file to test, the content is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     ne 1 2 }}
  type2: {
    
    {
    
     ne 1 1 }}

The test results of the rendering template are as follows:

[@k8s-master1 /opt/helm/work]# helm install ne-test ./mychart/ --debug --dry-run
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /opt/helm/work/mychart

NAME: ne-test
LAST DEPLOYED: Fri Mar 26 14:57:10 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
......

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: ne-test-configmap
data:
  type1: true
  type2: false

5. lt and gt functions

The lt function is used to determine whether the first parameter is less than the second parameter, if it is less than it is true, if it is greater than it is false.

The gt function is used to determine whether the first parameter is greater than the second parameter, if it is greater than it is true, if it is less than it is false.

Use the configmap.yaml file to test, the content is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     lt 1 2 }}
  type2: {
    
    {
    
     lt 2 1 }}
  type3: {
    
    {
    
     gt 2 1 }}
  type4: {
    
    {
    
     gt 1 2 }}

The test results of the rendering template are as follows:

[@k8s-master1 /opt/helm/work]# helm install fun-test ./mychart/ --debug --dry-run
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /opt/helm/work/mychart

NAME: fun-test
LAST DEPLOYED: Fri Mar 26 15:02:21 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
......

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fun-test-configmap
data:
  type1: true
  type2: false
  type3: true
  type4: false

6. ge function

The ge function is used to determine whether the first parameter is greater than or equal to the second parameter. If it is true, it will be true, and if it is not true, it will be false. Use the configmap.yaml file to test, the content is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     ge 2 2 }}
  type2: {
    
    {
    
     ge 2 1 }}
  type3: {
    
    {
    
     ge 1 2 }}

The test results of the rendering template are as follows:

[@k8s-master1 /opt/helm/work]# helm install fun-test ./mychart/ --debug --dry-run
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /opt/helm/work/mychart

NAME: fun-test
LAST DEPLOYED: Fri Mar 26 15:05:12 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fun-test-configmap
data:
  type1: true
  type2: true
  type3: false

7. default function

The default function has been introduced in the previous article. It is mainly used to set a default value. If the value of the parameter is empty, the default value will be used, otherwise the value of the parameter will be returned. Here need to explain the scope of the empty type.

The empty types are mainly the following types:

  • Integer:0
  • String: ""
  • List: []
  • dictionary: {}
  • Boolean: false
  • And all nil(or null)

For the structure, there is no empty definition, so the structure never returns to the default value.

Use the configmap.yaml file to test, the content is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     0 | default "zero" | quote }}
  type2: {
    
    {
    
     "" | default "null" | quote }}
  type3: {
    
    {
    
     default "FALSE" false | quote }}

The results of the rendering template test are as follows:

[@k8s-master1 /opt/helm/work]# helm install fun-test ./mychart/ --debug --dry-run
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /opt/helm/work/mychart

NAME: fun-test
LAST DEPLOYED: Fri Mar 26 15:23:28 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
......

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fun-test-configmap
data:
  type1: "zero"
  type2: "null"
  type3: "FALSE"

8. empty function

The empty function is used to determine whether the given value is empty, and returns true if it is empty. Use the configmap.yaml file to test, the content is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     0 | empty }}
  type2: {
    
    {
    
     1 | empty }}

The results of the rendering template test are as follows:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fun-test-configmap
data:
  type1: true
  type2: false

Note that in Go template conditions, null values ​​are calculated for you. So you rarely need if empty .Footo use only if .Foocan be.

9. The fail function

The fail function is used to return an empty string and a specified error message if the template rendering fails.

How to use this function is not clear to me for the time being, I will add it if I find relevant information later.

10. coalesce function

The coalesce function is used to scan a given list and return the first non-empty value. Use the configmap.yaml file to test, the content is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     coalesce 0 1 2 }}
  type2: {
    
    {
    
     coalesce "" false "Matt" }}

The results of the template rendering test are as follows:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fun-test-configmap
data:
  type1: 1
  type2: Matt

11. ternary function

The ternary function accepts two parameters and a test value. If the Boolean value of test is true, it returns the value of the first parameter; if the test value is empty or the Boolean value is false, it returns the value of the second parameter.

Use the configmap.yaml file to test, the content is as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {
    
    {
    
     .Release.Name }}-configmap
data:
  type1: {
    
    {
    
     ternary "First" "Second" true | quote }}
  type2: {
    
    {
    
     ternary "First" "Second" false | quote }}
  type3: {
    
    {
    
     empty "" | ternary "First" "Second" | quote }}
  type4: {
    
    {
    
     empty hello | ternary "First" "Second" | quote }}

The results of the rendering template test are as follows:

MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fun-test-configmap
data:
  type1: "First"
  type2: "Second"
  type3: "First"
  type4: "Second"

Link to the original text of this column: https://blog.csdn.net/xzk9381/category_10895812.html, please indicate the source for reprinting

Guess you like

Origin blog.csdn.net/xzk9381/article/details/115374494