【问题标题】:Validating helm chart content验证 helm 图表内容
【发布时间】:2018-07-17 19:49:34
【问题描述】:

我正在开发一个图表,但其中有一个错误——错误地放置了imagePullSecrets。当我尝试通过安装它时

helm install ./mychart

放错位置的元素被简单地忽略了,我想知道出了什么问题。

当我这样做时

helm template ./mychart | kubectl apply --dry-run -f -

它改为打印:

error: error validating "STDIN": error validating data: ValidationError(Deployment.spec.template.spec.containers[0]): unknown field "imagePullSecrets" in io.k8s.api.core.v1.Container

这清楚地表明出了什么问题。我不确定它是否与分蘖对扩展模板的实际操作相匹配。

但如果我只是做一个

helm install --dry-run --debug ./mychart

它只是显示展开后的模板,看起来还不错。

那么如何正确验证我的所有模板是否与 helm 匹配对应的模式?

【问题讨论】:

  • 这是来自 Kubernetes 的错误,而不是来自 Helm。也许this doc entry可以帮助你

标签: debugging kubernetes kubernetes-helm


【解决方案1】:

您可以通过 helm lint ./mychart 对图表进行 lint,如果发现问题,应该会打印以下内容:

$ helm lint ./mychart
==> Linting ./mychart
[ERROR] Chart.yaml: version is required
[INFO] Chart.yaml: icon is recommended

Error: 1 chart(s) linted, 1 chart(s) failed

helm lint

【讨论】:

  • helm lint 似乎做得不够。例如:helm lint chart/myProject 给出1 chart(s) linted, no failures,但helm template chart/myProject/ | kubectl apply --dry-run -f - 给出error: error validating "STDIN": error validating data: ValidationError(Deployment.spec.template.spec): unknown field "env" in io.k8s.api.core.v1.PodSpec; if you choose to ignore these errors, turn validation off with --validate=false
  • Kubeval 了解 k8s 清单规范,请参阅下面的答案:stackoverflow.com/a/64895342
【解决方案2】:

使用kubeconform

helm template ./mychart | kubeconform -strict

如果您有 CRD,您可能需要使用 kubeconform -ignore-missing-schemas。我建议提供架构版本:kubeconform -kubernetes-version 1.18

建议:专业化您的图表并验证它们。示例如下。

简单:

helm template --set some.key="val" | kubeconform -strict

复杂:

VALUES_FILE=$(cat << EOF
some:
  key: "val"

another:
  key:
    another: "val"
EOF
)
# It is important to quote "$VALUES_FILE" to ensure line breaks and indentation are preserved
echo "$VALUES_FILE" | helm template -f - | kubeconform -strict

【讨论】:

    【解决方案3】:

    我强烈建议使用以下两种解决方案的组合

    解决方案 1

    使用values.schema.json 对 values.yaml 文件施加结构。

    例子:

    {
      "$schema": "https://json-schema.org/draft-07/schema#",
      "properties": {
        "image": {
          "description": "Container Image",
          "properties": {
            "repo": {
              "type": "string"
            },
            "tag": {
              "type": "string"
            }
          },
          "type": "object"
        },
        "name": {
          "description": "Service name",
          "type": "string"
        },
        "port": {
          "description": "Port",
          "minimum": 0,
          "type": "integer"
        },
        "protocol": {
          "type": "string"
        }
      },
      "required": [
        "protocol",
        "port"
      ],
      "title": "Values",
      "type": "object"
    }
    

    (!) 此模式将应用于值以对其进行验证。当调用以下任何命令时会进行验证:

    helm install
    helm upgrade
    helm lint
    helm template
    

    为了给验证添加条件 - 读入here

    (*) 延伸阅读:Nice article.

    解决方案 2

    在某些情况下,values.schema.json 中的代码在使用条件时可能不太可读,或者我们希望在验证中使用更动态的逻辑。

    在这种情况下,我们可以创建一个validations.yaml 文件(一些供应商更喜欢使用.tpl 文件)并使用go templates 添加验证逻辑。

    例子:

    如果特定功能(启用时)需要 ip 或 dns:

      some_feature:
        enabled: true
        ip:
        dns:
    

    验证逻辑可以显式编写:

    {{- if .Values.some_feature.enabled -}}
        {{- if and (not .Values.some_feature.ip ) (not .Values.some_feature.dns  ) -}}
            When some_feature is enabled, ip or dns must be given.
        {{- end -}}
    {{- end -}}
    

    (*) 这个逻辑也可以使用json-schema if-else statement 来编写,但它可能不太可读。

    (**) 考虑将所有验证放在 /tests/validations 文件夹下,其中所有测试都分为单独的文件(如单元测试)。

    【讨论】:

    • 这个问题主要是为了验证图表本身是否正确实现,这无济于事,但它仍然是一个有用的补充。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2023-03-22
    • 2019-05-15
    相关资源
    最近更新 更多