传递值给`range`内的`include`函数,使用合并时的默认值

huangapple go评论79阅读模式
英文:

Passing values to include function inside range, using defaults with merge

问题

根据您提供的YAML文件,问题在于您的Helm模板逻辑和值的处理方式。特别是在extraContainers的循环中,当您定义了某些值后,这些值会在后续迭代中保留,除非您明确覆盖它们。这是因为Helm模板默认采用逐层覆盖的方式,而不是合并。

要实现您所期望的行为,您可以采取以下步骤:

  1. 修改您的values.yaml文件,将extraContainers的资源值设置为nil,以便在后续迭代中不继承之前的值。如下所示:
  1. extraContainers:
  2. extra-container2:
  3. image:
  4. name: xxx.dkr.ecr.eu-west-1.amazonaws.com/foobar-two
  5. tag: test
  6. # 将资源设置为nil
  7. resources: nil
  8. command:
  9. - sleep
  10. - 10
  11. envVars:
  12. - name: FOO
  13. value: BAR
  14. probes:
  15. readinessProbe:
  16. path: /ContainerTwoReadinessPath
  17. livenessProbe:
  18. path: /ContainerTwoLivenessPath
  19. extra-container3:
  20. image:
  21. name: xx.dkr.ecr.eu-west-1.amazonaws.com/foobar-three
  22. tag: latest
  23. # 将资源设置为nil
  24. resources: nil
  25. command:
  26. - sleep
  27. - 10
  28. envVars:
  29. - name: FOO
  30. value: BAZ
  31. probes:
  32. readinessProbe:
  33. enabled: false
  34. livenessProbe:
  35. enabled: false
  1. 在您的deployment.yaml模板中,确保在循环中检查资源是否为nil,如果为nil,则使用默认值。如下所示:
  1. {{- range $k, $v := .Values.extraContainers }}
  2. {{- $nameDict := dict "name" $k -}}
  3. {{- include "app.container" (mustMergeOverwrite $.Values.app $nameDict $v) | nindent 8 }}
  4. {{- include "ports" (merge $nameDict $v ) | nindent 8 }}
  5. {{- with $v.resources }}
  6. resources:
  7. limits:
  8. cpu: {{ .limits.cpu | default .cpu | quote }}
  9. memory: {{ .limits.memory | default .memory | quote }}
  10. requests:
  11. cpu: {{ .requests.cpu | default .cpu | quote }}
  12. memory: {{ .requests.memory | default .memory | quote }}
  13. {{- end }}
  14. {{- end }}

通过将资源设置为nil,您可以确保extra-container3不会继承来自extra-container2的资源值,并且会使用默认的.values.app中的资源值。

英文:

Given this deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: deployment
  5. spec:
  6. revisionHistoryLimit: 5
  7. template:
  8. spec:
  9. containers:
  10. {{- include "app.container" (merge .Values.app $) | nindent 8 }}
  11. {{- include "ports" (merge .Values.app ) | nindent 8 }}
  12. {{- range $k, $v := .Values.extraContainers }}
  13. {{- $nameDict := dict "name" $k -}}
  14. {{- include "app.container" (mustMergeOverwrite $.Values.app $nameDict $v) | nindent 8 }}
  15. {{- include "ports" (merge $nameDict $v ) | nindent 8 }}
  16. {{- end }}

This helpers file...

  1. {{/* vim: set filetype=helm: */}}
  2. {{/*
  3. app container base
  4. */}}
  5. {{- define "app.containerBase" -}}
  6. - name: {{ .name | default "app" }}
  7. image: {{ .image.name }}:{{ .image.tag }}
  8. {{- if .command }}
  9. command:
  10. {{- range .command }}
  11. - {{ . | quote }}
  12. {{- end }}
  13. {{- end }}
  14. {{- if .args }}
  15. args:
  16. {{- range .args }}
  17. - {{ . | quote }}
  18. {{- end }}
  19. {{- end }}
  20. {{- range .envVars }}
  21. - name: {{ .name }}
  22. {{- with .value }}
  23. value: {{ . | quote }}
  24. {{- end }}
  25. {{- with .valueFrom }}
  26. valueFrom:
  27. {{- toYaml . | nindent 8 }}
  28. {{- end }}
  29. {{- end }}
  30. {{- if or .Values.configMaps .Values.secrets .Values.configMapRef }}
  31. envFrom:
  32. {{- if .Values.configMaps }}
  33. - configMapRef:
  34. name: "{{- include "app.fullname" $ }}"
  35. {{- end }}
  36. {{- range .Values.configMapRef }}
  37. - configMapRef:
  38. name: {{ . }}
  39. {{- end }}
  40. {{- range $name, $idk := $.Values.secrets }}
  41. - secretRef:
  42. name: "{{- include "app.fullname" $ }}-{{ $name }}"
  43. {{- end }}
  44. {{- end }}
  45. {{- end -}}
  46. {{/*
  47. app container
  48. */}}
  49. {{- define "app.container" -}}
  50. {{- include "app.containerBase" . }}
  51. resources:
  52. limits:
  53. cpu: {{ .resources.limits.cpu | default "100m" | quote }}
  54. memory: {{ .resources.limits.memory | default "128Mi" | quote }}
  55. {{- if .resources.limits.ephemeralStorage }}
  56. ephemeral-storage: {{ .resources.limits.ephemeralStorage }}
  57. {{- end }}
  58. requests:
  59. cpu: {{ .resources.requests.cpu | default "100m" | quote }}
  60. memory: {{ .resources.requests.memory | default "128Mi" | quote }}
  61. {{- if .resources.requests.ephemeralStorage }}
  62. ephemeral-storage: {{ .resources.requests.ephemeralStorage }}
  63. {{- end }}
  64. securityContext:
  65. runAsNonRoot: true
  66. runAsUser: {{ .securityContext.runAsUser }}
  67. runAsGroup: {{ .securityContext.runAsGroup }}
  68. allowPrivilegeEscalation: false
  69. {{- end -}}
  70. {{/*
  71. ports
  72. */}}
  73. {{- define "ports" -}}
  74. {{- if .port }}
  75. ports:
  76. - containerPort: {{ .port }}
  77. protocol: {{ .protocol | default "tcp" | upper }}
  78. {{- range .extraPorts}}
  79. - containerPort: {{ required ".port is required." .port }}
  80. {{- if .name }}
  81. name: {{ .name }}
  82. {{- end }}
  83. {{- if .protocol }}
  84. protocol: {{ .protocol | upper }}
  85. {{- end }}
  86. {{- end }}
  87. {{- end }}
  88. {{- end -}}

This values.yaml

  1. extraContainers:
  2. extra-container2:
  3. image:
  4. name: xxx.dkr.ecr.eu-west-1.amazonaws.com/foobar-two
  5. tag: test
  6. command:
  7. - sleep
  8. - 10
  9. envVars:
  10. - name: FOO
  11. value: BAR
  12. probes:
  13. readinessProbe:
  14. path: /ContainerTwoReadinessPath
  15. livenessProbe:
  16. path: /ContainerTwoLivenessPath
  17. resources:
  18. limits:
  19. cpu: 200Mi
  20. memory: 2Gi
  21. requests:
  22. cpu: 200Mi
  23. memory: 2Gi
  24. extra-container3:
  25. image:
  26. name: xx.dkr.ecr.eu-west-1.amazonaws.com/foobar-three
  27. tag: latest
  28. command:
  29. - sleep
  30. - 10
  31. envVars:
  32. - name: FOO
  33. value: BAZ
  34. probes:
  35. readinessProbe:
  36. enabled: false
  37. livenessProbe:
  38. enabled: false
  39. app:
  40. image:
  41. name: xx.dkr.ecr.eu-west-1.amazonaws.com/foobar
  42. tag: latest
  43. probes:
  44. readinessProbe:
  45. path: /_readiness
  46. enabled: true
  47. livenessProbe:
  48. path: /_liveness
  49. enabled: true
  50. port: 100
  51. resources:
  52. limits:
  53. cpu: 100Mi
  54. memory: 1Gi
  55. requests:
  56. cpu: 100Mi
  57. memory: 1Gi

Why does helm template result in the below:

  1. ---
  2. # Source: corp-service/templates/deployment.yaml
  3. apiVersion: apps/v1
  4. kind: Deployment
  5. metadata:
  6. name: deployment
  7. spec:
  8. revisionHistoryLimit: 5
  9. template:
  10. spec:
  11. containers:
  12. - name: app
  13. image: xx.dkr.ecr.eu-west-1.amazonaws.com/foobar:latest
  14. resources:
  15. limits:
  16. cpu: "100Mi"
  17. memory: "1Gi"
  18. requests:
  19. cpu: "100Mi"
  20. memory: "1Gi"
  21. securityContext:
  22. runAsNonRoot: true
  23. runAsUser: 2000
  24. runAsGroup: 2000
  25. allowPrivilegeEscalation: false
  26. ports:
  27. - containerPort: 100
  28. protocol: TCP
  29. - name: extra-container2
  30. image: xx.dkr.ecr.eu-west-1.amazonaws.com/foobar-two:test
  31. command:
  32. - "sleep"
  33. - "10"
  34. - name: FOO
  35. value: "BAR"
  36. resources:
  37. limits:
  38. cpu: "200Mi"
  39. memory: "2Gi"
  40. requests:
  41. cpu: "200Mi"
  42. memory: "2Gi"
  43. securityContext:
  44. runAsNonRoot: true
  45. runAsUser: 2000
  46. runAsGroup: 2000
  47. allowPrivilegeEscalation: false
  48. - name: extra-container3
  49. image: xx.dkr.ecr.eu-west-1.amazonaws.com/foobar-three:latest
  50. command:
  51. - "sleep"
  52. - "10"
  53. - name: FOO
  54. value: "BAZ"
  55. resources:
  56. limits:
  57. cpu: "200Mi"
  58. memory: "2Gi"
  59. requests:
  60. cpu: "200Mi"
  61. memory: "2Gi"
  62. securityContext:
  63. runAsNonRoot: true
  64. runAsUser: 2000
  65. runAsGroup: 2000
  66. allowPrivilegeEscalation: false

i.e why does extra-container3 have resources: set with values from extra-container2 instead of taking them from the default set of resources found under .values.app.

essentially, i want extracontainers to use default values from .values.app unless i explicitly set them inside the extracontainers section. however it seems that if a previous iteration of the loop over extracontainers defines some values, they are used in the next iteration if they are not overriden.

the resources for extra-container3 i am expecting are:

  1. resources:
  2. limits:
  3. cpu: 100Mi
  4. memory: 1Gi
  5. requests:
  6. cpu: 100Mi
  7. memory: 1Gi

what am i doing wrong?

答案1

得分: 3

  1. 所以这里涉及到了一些事情,但大多数情况下答案是 `(mergeMustOverwrite)` **改变** `$dest` 映射,这会导致你的范围“记住”了它所看到的最后一个值,根据你的问题,这并不是你想要的行为。最简单的答案是将 `(deepCopy $.Values.app)` 用作 `$dest`,但由于我们稍后将要讨论的另一个错误,这里有一个星号。
  2. 你看,`(deepCopy $.Values.app)` 会因为你莫名其妙地使用了 `(merge .Values.app $)` 而在 `helm` 中导致堆栈溢出,它会引入 `Chart``Files``Capabilities` 和整个世界。我猜想你的 `_helpers.tpl` 必须是从别处复制粘贴过来的,这解释了在 `define` 中错误的相对 `.Values` 引用。修复的一种方式是移除 `.Values.configMaps`,这样它将跟踪 `.app` 上下文,就像我猜测你想要的那样,或者你可以将第一个 `(merge)` 更改为人为地创建一个 `"Values": {}` 项,只是为了在尝试引用 `.app.Values.configMaps` 时防止模板崩溃。正确的方法将取决于你的意图,但 `(merge .Values.app $)` 几乎肯定不是它。
  3. 所以,要么:
  4. ```diff
  5. --- a/templates/_helpers.tpl
  6. +++ b/templates/_helpers.tpl
  7. @@ -28,13 +28,13 @@ app container base
  8. {{- toYaml . | nindent 8 }}
  9. {{- end }}
  10. {{- end }}
  11. - {{- if or .Values.configMaps .Values.secrets .Values.configMapRef }}
  12. + {{- if or .configMaps .secrets .configMapRef }}
  13. envFrom:
  14. - {{- if .Values.configMaps }}
  15. + {{- if .configMaps }}
  16. - configMapRef:
  17. name: "{{- include "app.fullname" $ }}"
  18. {{- end }}
  19. - {{- range .Values.configMapRef }}
  20. + {{- range .configMapRef }}
  21. - configMapRef:
  22. name: {{ . }}
  23. {{- end }}

或者:

  1. containers:
  2. - {{- include "app.container" (merge .Values.app $) | nindent 8 }}
  3. + {{- include "app.container" (merge .Values.app (dict "Values" (dict))) | nindent 8 }}
  4. {{- include "ports" (merge .Values.app ) | nindent 8 }}
  1. <details>
  2. <summary>英文:</summary>
  3. So there are a couple of things going on here, but mostly the answer is that `(mergeMustOverwrite)` **mutates** the `$dest` map, which causes your range to &quot;remember&quot; the last value it saw, which according to your question isn&#39;t the behavior you want. The simplest answer is to use `(deepCopy $.Values.app)` as the `$dest`, but there&#39;s an asterisk to that due to another bug we&#39;ll cover in a second
  4. ```diff
  5. {{- $nameDict := dict &quot;name&quot; $k -}}
  6. - {{- include &quot;app.container&quot; (mustMergeOverwrite $.Values.app $nameDict $v) | nindent 8 }}
  7. + {{- include &quot;app.container&quot; (mustMergeOverwrite (deepCopy $.Values.app) $nameDict $v) | nindent 8 }}
  8. {{- include &quot;ports&quot; (merge $nameDict $v ) | nindent 8 }}
  9. {{- end }}

You see, (deepCopy $.Values.app) stack-overflows helm because of your inexplicable use of (merge .Values.app $) drags in Chart, Files, Capabilities and the whole world. I'm guessing your _helpers.tpl must have been copy-pasted from somewhere else, which explains the erroneous relative .Values reference inside that define. One way to fix that is to remove the .Values.configMaps, so it will track the .app context like I expect you meant, or you can change the first (merge) to artificially create a &quot;Values&quot;: {} item just to keep the template from blowing up when in tries to reference .app.Values.configMaps. The correct one will depend on what you were intending, but (merge .Values.app $) is almost certainly not it

so, either:

  1. --- a/templates/_helpers.tpl
  2. +++ b/templates/_helpers.tpl
  3. @@ -28,13 +28,13 @@ app container base
  4. {{- toYaml . | nindent 8 }}
  5. {{- end }}
  6. {{- end }}
  7. - {{- if or .Values.configMaps .Values.secrets .Values.configMapRef }}
  8. + {{- if or .configMaps .secrets .configMapRef }}
  9. envFrom:
  10. - {{- if .Values.configMaps }}
  11. + {{- if .configMaps }}
  12. - configMapRef:
  13. name: &quot;{{- include &quot;app.fullname&quot; $ }}&quot;
  14. {{- end }}
  15. - {{- range .Values.configMapRef }}
  16. + {{- range .configMapRef }}
  17. - configMapRef:
  18. name: {{ . }}
  19. {{- end }}

or

  1. containers:
  2. - {{- include &quot;app.container&quot; (merge .Values.app $) | nindent 8 }}
  3. + {{- include &quot;app.container&quot; (merge .Values.app (dict &quot;Values&quot; (dict))) | nindent 8 }}
  4. {{- include &quot;ports&quot; (merge .Values.app ) | nindent 8 }}

huangapple
  • 本文由 发表于 2023年2月18日 03:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定