---
apiVersion: apps/v1
kind: {{ .Values.stateful | ternary "StatefulSet" "Deployment" | quote }}
metadata:
  name: {{ .Release.Name }}
  labels:
    app.kubernetes.io/name: {{ .Values.appName }}
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  {{- if .Values.stateful }}
  serviceName: {{ .Values.appName }}
  {{ end }}
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ .Values.appName }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      {{- with .Values.securityContext }}
      securityContext:
        {{- . | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.registryCred }}
      imagePullSecrets:
        - name: {{ .Release.Name }}-regcred
      {{- end }}
      {{- if .Values.volume }}
      volumes:
        - name: {{ .Values.volume.name }}
          persistentVolumeClaim:
            claimName: {{ .Values.volume.name }}
      {{- end}}
      containers:
        - image: {{ .Values.image.repo }}:{{ .Values.image.tag }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          name: {{ .Values.appName }}
          {{- with .Values.command }}
          command:
            {{- . | toYaml | nindent 12 }}
          {{- end }}
          {{- with .Values.args }}
          args:
            {{- . | toYaml | nindent 12 }}
          {{- end }}
          {{- with .Values.env }}
          env:
            {{- range $name,$value := . }}
            - name: {{ $name }}
              value: {{ $value | quote }}
            {{- end }}
          {{- end }}
          {{- if .Values.secretEnv }}
          envFrom:
            - secretRef:
                name: {{ .Release.Name }}-env
          {{- end }}
          {{- if .Values.volume }}
          volumeMounts:
            - name: {{ .Values.volume.name }}
              mountPath: {{ .Values.volume.mountPath }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 20
            httpGet:
              path: {{ .Values.readinessProbe.path }}
              {{- if .Values.readinessProbe.port }}
              port: {{ .Values.readinessProbe.port }}
              {{- else }}
              port: {{ .Values.port }}
              {{- end }}
          {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            initialDelaySeconds: 5
            periodSeconds: 10
            httpGet:
              path: {{ .Values.livenessProbe.path }}
              {{- if .Values.livenessProbe.port }}
              port: {{ .Values.livenessProbe.port }}
              {{- else }}
              port: {{ .Values.port }}
              {{- end }}
          {{- end }}
