我正在寻找一种解决方案来控制输入值(在values.yaml中定义)。我想检查一下输入值是否经过授权。
示例:
values.yaml
provider: aws
services:
- nginx
- varnish
- php在另一个文件中(也许是_helpers.tpl?)
authorized_providers:
- aws
- azure
authorized_services:
- nginx
- php并抛出一个错误(如果可能的话是自定义消息),以指示输入值不受支持/授权。
我的目标是避免使用不支持的值生成Kubernetes configmap (helm安装可以工作,但此配置将生成容器错误)。
编辑:
我最终找到了一个使用"required“和一些技巧的解决方案。使用我的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.yaml文件中设置".Values.foo“。我使用它来使“必需的”检查失败,并引发错误。我尝试将"input-values-validation.yaml“的内容放入_helpers.tpl文件中,但这会生成一个错误"ERROR templates/: rendering template failed: runtime error: invalid memory address or nil pointer dereference”。"required“函数似乎只能在yaml文件中使用。
因此,使用此解决方案,我可以在_helpers.tpl文件中定义授权值并生成“自定义”错误消息。如果将来我支持更多的提供者/服务(我的例子),我只需要修改"authorized.provider“和"authorized.services”中的值。
发布于 2019-01-25 22:40:24
我还没有见过用helm2做到这一点,至少在official charts的扫描中没有,在尝试定义common functions in an incubator chart的过程中。
最棘手的是能够给出一个很好的错误-我见过的最接近的是sprig fail function
但是,无论是使用schemas还是lua,helm3都应该提供这种验证
或者你可以这样做:
aws:
nginx: false
varnish: false
php: true以便图表用户通过true/false选择他们想要的服务
https://stackoverflow.com/questions/54366031
复制相似问题