helm chart 读取values.yaml 一个或多个ConfigMap和Secret,并注入容器环境变量

内容纲要
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "base.fullname" . }}
  labels:
    {{- include "base.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "base.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "base.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "base.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          envFrom:
          {{- if .Values.application.configmaps }}  # 检查values文件中是否存在configMapName
          {{- range .Values.application.configmaps }}
          - configMapRef:
              name: {{ . }} 
          {{- end }}
          {{- end }} 
          {{- if .Values.application.secrets }}  # 检查values文件中是否存在secretName
          {{- range .Values.application.secrets }}
          - secretRef:
              name: {{ . }}
          {{- end }}
          {{- end }} 
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

values.yaml

image:
  repository: nginx
  pullPolicy: IfNotPresent
  tag: "1.25.3"

application:
  configmaps:
    - test-config
  secrets:
    - test-secret

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部