Ingress.yaml values.yaml 中的多个 Ingress

Ingress.yaml for multiple Ingresses in values.yaml

我有以下values.yaml

ingresses:
  - name: public
    class: "nginx"
    annotations:
      nginx.ingress.kubernetes.io/proxy-body-size: 122m
      nginx.ingress.kubernetes.io/proxy-connect-timeout: "7"
      nginx.ingress.kubernetes.io/proxy-read-timeout: "60"
      nginx.ingress.kubernetes.io/proxy-send-timeout: "30"
    labels: {}

    rules:
      - host: example.com
        http:
        paths:
        - path: /asd/as
          pathType: ImplementationSpecific
          backend:
            service:
              name: one
              port: 
                number: 8080
        - backend:
            service:
              name: log
              port:
                number: 8081
          path: /path/log
          pathType: ImplementationSpecific
        - backend:
            service:
              name: got
              port:
                number: 8082
          path: /api/got
          pathType: ImplementationSpecific
    tls:
    - hosts:
      - example.com
      secretName: cert
  
 - name: public
   annotations:
   labels: {}
   rules:
    - host: example1.com
      http:
      paths:
        - backend:
            service:
              name: web
              port:
                number: 8090
          pathType: ImplementationSpecific
      
    tls:
    - hosts:
      - example1.com
      secretName: qwe

我有以下入口文件:

{{- $top := . -}}
{{- range $ingress := .Values.ingresses }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ $ingress.name }}
  namespace: {{ $ingress.namespace }}
  {{- with $ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 8 }}
  {{- end }}
spec:
  {{- if and $ingress.class (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
  ingressClassName: {{ $ingress.class }}
  {{- end }}
  {{- if $ingress.tls }}
  tls:
    {{- range $ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
  {{- end }}
  rules:
    {{- range $ingress.rules }}
    - host: {{ .host | quote }}
      http:
        paths:
          {{- range .paths }}
          {{- if and .path (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
          - path: {{ .path }}
          {{ end }}
            {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
            pathType: {{ .pathType }}
            {{- end }}
            backend:
              service:
                name: {{ .backend.service.name }}
                port:
                  number: {{ .backend.service.port.number}}
          {{- end }}
    {{- end }}
    {{- end }}

这只会生成一个入口(以值文件中的最后一个为准)。我尝试使用 range $ingress := .Values.ingress 但每当我尝试 $ingress.name 时它总是给我一个错误。我对 ingress.yaml 做了哪些更改才能部署这两个入口。

编辑:根据 David 的回答进行了编辑。

您需要以某种方式在 Helm 值中分解两个单独的入口配置。现在它们位于 ingress: 下的单个地图对象中,因此 .Values.ingress.name 例如只有一个值,而不是您可以迭代的值。

这里的 YAML 列表很有意义:

# values.yaml
ingresses:
  - name: example-com
    class: nginx
    rules: [...]
  - name: example1-com
    class: nginx
    rules: [...]

然后您可以使用 range 循环遍历此列表。关于 range 循环需要了解的重要一点是它会重新绑定 . 特殊变量,这是 .Values 等结构的基础;这意味着您需要在循环外保存 . 的原始值($ 特殊变量也可以工作)。您可以在单个 Helm 模板文件中生成多个 Kubernetes 对象,只要每个对象都以 YAML --- start-of-document 标记开头(并且完全不生成输出是有效的)。

{{-/* save the original value of . */-}}
{{- $top := . -}}

{{-/* iterate over the ingress configurations */-}}
{{- range $ingress := .Values.ingresses }}
---
{{-/* your existing conditionals can go here, simplifying */}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  {{-/* this comes from the per-ingress config */}}
  {{- with $ingress.annotations }}
  annotations: {{- toYaml . | nindent 4 }}
  {{- end }}

  {{-/* if you need to use "standard" helper functions, make sure
        to pass the saved $top value as their parameter */}}
  name: {{ include "mychart.fullname $top }}-{{ $ingress.name }}
spec: { ... }
{{- end }}

您可能还需要重新考虑将其中多少适合包含在 arbitrarily-configurable 值中。与其在 Helm 值中写出整个 Ingress 对象,您可能会发现在模板文件本身中写出诸如路径映射之类的东西更容易,并且有一些 high-level 控件(“已启用”、“主机名” ", "TLS 秘密名称") 暴露。后端服务名称和端口之类的东西将对应于图表中的其他内容,您可能需要计算服务名称;只是安装您的图表的人不需要配置它。