Go Template 與 Values

Vibe Prompt

「幫我寫 deployment.yaml 的 Helm 模板:支援 replicas、image tag、resources limits、環境變數、nodeSelector。」

templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "my-chart.fullname" . }}
  labels:
    {{- include "my-chart.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "my-chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "my-chart.selectorLabels" . | nindent 8 }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: 3000
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          env:
            {{- toYaml .Values.env | nindent 12 }}

常用函式

| 函式 | 用途 | |------|------| | {{ .Values.key }} | 讀取 values.yaml | | {{ include "name" . }} | 引入 helpers.tpl | | {{ nindent n "text" }} | 縮排 n 格 | | {{ toYaml .Values.x }} | 轉為 YAML 字串 | | {{ default "dev" .Values.env }} | 預設值 | | {{- range .Values.items }} | 迴圈 | | {{- if .Values.ingress.enabled }} | 條件 |

條件式 Ingress

{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "my-chart.fullname" . }}
spec:
  ingressClassName: nginx
  rules:
    - host: {{ .Values.ingress.host }}
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: {{ include "my-chart.fullname" . }}
                port:
                  number: {{ .Values.service.port }}
{{- end }}

解鎖完整教學內容

本章為付費內容。加入專案即可解鎖超過 5000 字的深度解析,包含 10 個以上神級 Prompt 與真實 Source Code 範例!