`&`和`*`在Helm模板中分别表示什么?

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

What does & and * denote in a helm template?

问题

& 在 helm 模板中表示引用一个标签或值。
* 在 helm 模板中表示将一个标签或值展开为多个键值对。
英文:

What does & and * denote in a helm template?

https://github.com/SeleniumHQ/docker-selenium/blob/trunk/charts/selenium-grid/templates/chrome-node-deployment.yaml#L7

https://github.com/SeleniumHQ/docker-selenium/blob/trunk/charts/selenium-grid/templates/chrome-node-deployment.yaml#L25

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "seleniumGrid.chromeNode.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: &chrome_node_labels //**& here**
    app: selenium-chrome-node
    app.kubernetes.io/name: selenium-chrome-node
    {{- include "seleniumGrid.commonLabels" . | nindent 4 }}
    {{- with .Values.chromeNode.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.customLabels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.chromeNode.replicas }}
  selector:
    matchLabels:
      app: selenium-chrome-node
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels: *chrome_node_labels // **star here **
      annotations:

答案1

得分: 1

这看起来像是使用了YAML锚点,但我记得如果直接在Helm模板中使用会导致错误。我猜他们可能在这里进行了第二次渲染,先解析了YAML。

YAML锚点

锚点和别名。这包括两个部分:

  • 锚点 & 用于定义一块配置

  • 别名 * 用于在其他地方引用该配置块

因此,在下面的示例中,我们使用 &build-test 来定义一个步骤实体,它包含了多行可重用的配置,然后使用别名 *build-test 进行引用。

definitions: 
  steps:
    - step: &build-test
        name: 构建和测试
        script:
          - mvn package
        artifacts:
          - target/**

pipelines:
  branches:
    develop:
      - step: *build-test
    main:
      - step: *build-test

anchors-and-aliases

英文:

This looks like the usage of YAML anchors, but I remember it would result in an error if used directly in the Helm templates. I guess they might be doing a second rendering here, where the YAML is parsed first.

YAML anchors

Anchors and aliases. There are 2 parts to this:

  • The anchor & which defines a chunk of configuration

  • The alias * used to refer to that chunk elsewhere

So in the example below we use &build-test to define a step entity, which has several lines for reuse, and the alias *build-test to reuse it.

definitions: 
  steps:
    - step: &build-test
        name: Build and test
        script:
          - mvn package
        artifacts:
          - target/**

pipelines:
  branches:
    develop:
      - step: *build-test
    main:
      - step: *build-test

anchors-and-aliases

huangapple
  • 本文由 发表于 2023年3月1日 16:06:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600964.html
匿名

发表评论

匿名网友

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

确定