【问题标题】:helm chart always not going into the if condition舵图总是不进入 if 条件
【发布时间】:2021-12-27 08:42:43
【问题描述】:

yaml 文件,其中定义了以下值,包括一个称为“环境”的特定值

image:
 repository: my_repo_url
 tag: my_tag
 pullPolicy: IfNotPresent

releaseName: cron_script
schedule: "0 10 * * *"
namespace: deploy_cron
rav_admin_password: asdf
environment: testing
testing_forwarder_ip: 10.2.71.21
prod_us_forwarder_ip: 10.2.71.15

现在在基于此环境值的我的掌舵图中,我需要为新变量分配一个值,为此我编写了如下代码,但它始终没有进入 if else 块本身

{{- $fwip := .Values.prod_us_forwarder_ip }}
  {{- if contains .Values.environment  "testing" }}
    {{- $fwip := .Values.testing_forwarder_ip }}
{{- end }}
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: "{{ .Values.releaseName }}"
  namespace: "{{ .Values.namespace }}"
  labels:
  ....................................
  ....................................
  ....................................
  spec:
      restartPolicy: Never
      containers:
      - name: "{{ .Values.releaseName }}"
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: IfNotPresent
        args:
          - python3
          - test.py
          - --data
          - 100
          - {{ $fwip }}

在上面的代码中,我总是得到 $fwip 值作为 10.2.71.21,无论环境值是测试还是生产,我都得到相同的值

如果我没有在 if else 语句之前声明变量 $fwip 那么它说 $fwip 变量未定义错误,所以我不确定为什么根本没有使用 if else 语句,如何调试进一步?

【问题讨论】:

    标签: kubernetes-helm helmfile


    【解决方案1】:

    这是变量和局部变量的语法问题。

    if 中的fwip 应使用= 而不是:=

    {{- $fwip := .Values.prod_us_forwarder_ip }}
    {{- if contains .Values.environment  "testing" }}
      {{- $fwip = .Values.testing_forwarder_ip }}
    {{- end }}
    

    为了方便大家理解,我把它翻译成了go代码。

    (在go语言中,:=表示定义和赋值,=表示赋值)

    // := 
    env := "testing"
    test := "10.2.71.21"
    prod := "10.2.71.15"
    
    fwip := prod
    if strings.Contains(env,"testing"){
        fwip := test
        fmt.Println(fwip) // 10.2.71.21
    }
    fmt.Println(fwip) // 10.2.71.15
    
    // =
    env := "testing"
    test := "10.2.71.21"
    prod := "10.2.71.15"
    
    fwip := prod
    if strings.Contains(env,"testing"){
        fwip = test
        fmt.Println(fwip) // 10.2.71.21
    }
    fmt.Println(fwip) // 10.2.71.21
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多