【问题标题】:Helm control input valuesHelm 控制输入值
【发布时间】:2019-01-25 13:12:42
【问题描述】:

我正在寻找一种解决方案来控制输入值(在 values.yaml 中定义)。我想检查输入的值是否被授权。

例子:

values.yaml

provider: aws

services:
  - nginx
  - varnish
  - php

在另一个文件中(可能是 _helpers.tpl?)

authorized_providers:
  - aws
  - azure
authorized_services:
  - nginx
  - php

并引发错误(如果可能,请自定义消息)以指示输入值不受支持/授权。

我的目标是避免使用不受支持的值生成 Kubernetes configmap(helm install 有效,但此配置会产生容器错误)。

编辑:

我终于找到了一个使用“必需”和一些技巧的解决方案。 按照我的 values.yaml 配置文件的示例。

我在 _helpers.tpl 中定义:

{{/*
Define the authorized Value for the parameter: .Values.provider 
*/}}
{{- define "authorized.provider" }}
{{- printf "aws,azure" }}
{{- end }}

{{/*
Define the error message if the .Values.provider doesn't respect the authorized.provider condition.
*/}}
{{- define "error.provider" }}
{{- $provider := include "authorized.provider" . }}
{{- printf "Invalid value set for .Values.provider - Must be one of %s" $provider }}
{{- end }}



{{/*
Define the authorized Value for the parameter: .Values.services 
*/}}
{{- define "authorized.services" }}
{{- printf "nginx,php" }}
{{- end }}

{{/*
Define the error message if the .Values.services doesn't respect the authorized.services condition.
*/}}
{{- define "error.services" }}
{{- $services := include "authorized.services" . }}
{{- printf "Invalid value set for .Values.services - Authorized values are %s" $services }}
{{- end }}

接下来,我创建了另一个文件:input-values-validation.yaml

{{- $provider := include "authorized.provider" . }}
{{- $errorProvider := include "error.provider" . }}
{{- if not (has .Values.provider (splitList "," $provider)) }}
{{ required  $errorProvider .Values.foo  }}
{{- end }}

{{- $services := include "authorized.services" . }}
{{- $errorServices := include "error.Services" . }}
{{- $root := . -}}
{{- range .Values.services }}
{{- if not (has . (splitList "," $services)) }}
{{ required  $errorServices $root.Values.foo  }}
{{- end }}
{{- end }}

如果输入值错误则输出:

==> Linting 
[ERROR] templates/: render error in "templates/input-values-validation.yaml": template: templates/input-values-validation.yaml:12:3: executing "templates/input-values-validation.yaml" at<required $errorServ...>: error calling required: Invalid value set for .Values.services - Authorized values are nginx,php

信息:

".Values.foo" 绝不能在 values.yaml 文件中设置。我用它使“必需”检查失败并引发错误。 我尝试将“input-values-validation.yaml”的内容放在 _helpers.tpl 文件中,但这会产生错误“[ERROR] templates/: rendering template failed: runtime error: invalid memory address or nil pointer dereference ”。看来“必填”功能只能在yaml文件中使用。

因此,使用此解决方案,我可以在 _helpers.tpl 文件中定义授权值并生成“自定义”错误消息。如果将来我支持更多的提供者/服务(我的例子),我只需要修改“authorized.provider”和“authorized.services”中的值。

【问题讨论】:

    标签: kubernetes-helm


    【解决方案1】:

    我还没有看到用 helm2 完成它,至少在扫描 official charts 时没有看到,尝试定义 common functions in an incubator chart

    最棘手的一点是能够给出一个好的错误 - 我见过的最接近的是sprig fail function

    但是 helm3 应该提供这种验证,可以使用 schemaslua

    否则你可以这样做:

    aws:
      nginx: false
      varnish: false
      php: true
    

    以便图表用户通过真/假选择他们想要的服务

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2021-05-10
    • 2016-09-06
    相关资源
    最近更新 更多