简体   繁体   English

如何验证 values.yaml 中不存在的 Helm 值

[英]How to validate Helm values that are not present in values.yaml

Input输入

I'm writing a helm chart containing following values.yaml:我正在编写一个包含以下 values.yaml 的舵图:

backend:
  container:
    resources: {}
  hpa:
    enabled: false
    targetCPUUtilizationPercentage: 50

Then in a template file a deployment definition looks like:然后在模板文件中,部署定义如下所示:

template:
  spec:
    containers:
      - name: "some-name"
        image: "some-repo/some-image:latest"
        resources:
          {{- toYaml .Values.backend.container.resources | nindent 12 }}

Problem问题

On default I disable the Horizontal Pod Autoscaler ( backend.hpa.enabled=false ), but if user enables it, I want to be sure that property backend.container.resources.requests.cpu is specified.默认情况下,我禁用 Horizo​​ntal Pod Autoscaler ( backend.hpa.enabled=false ),但如果用户启用它,我想确保属性backend.container.resources.requests.cpu已指定。 Otherwise HPA wouldn't work.否则 HPA 将无法工作。 So I would like to throw exception in that case.所以我想在这种情况下抛出异常。 How to implement such a validation check?如何实现这样的验证检查?

Attempt 1尝试 1

I tried to write HPA template in following wrapper:我尝试在以下包装器中编写 HPA 模板:

{{- if .Values.backend.hpa.enabled -}}
{{- if required "Specify requests!" .Values.backend.container.resources.requests.cpu -}}
...
{{- end -}}
{{- end -}}

But it throws nil exception on parsing template when backend.container.resources.requests.cpu is not specified (even if backend.hpa.enabled=false ).但是当backend.container.resources.requests.cpu未指定时(即使backend.hpa.enabled=false ),它会在解析模板上抛出 nil 异常。

Attempt 2尝试 2

If I replace values.yaml with the following fragment, then kubernetes will not allow to set such cpu value (illegal format, null is invalid too).如果我用下面的片段替换 values.yaml,那么 kubernetes 将不允许设置这样的 cpu 值(非法格式,null 也是无效的)。 Is that something that I can assign to a cpu field, that will be considered as default value in kubernetes?这是我可以分配给 cpu 字段的东西,在 kubernetes 中将被视为默认值吗? I think it could be not very elegant, but feasible solution.我认为这可能不是很优雅,但可行的解决方案。

backend:
  container:
    resources:
      requests:
        cpu: ""
  hpa:
    enabled: false
    targetCPUUtilizationPercentage: 50

In Helm 3 you can use JSON Schema Validation, you can read about it here .在 Helm 3 中,您可以使用 JSON Schema Validation,您可以在此处阅读相关内容。 You can use the IF condition there .您可以在那里使用IF 条件

I wouldn't try to make the validation in the chart itself, because it will make your helm chart more complex.我不会尝试在图表本身中进行验证,因为它会使您的舵图更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM