{{- if .Values.registry.deploy }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "trigger-v4.fullname" . }}-registry
  labels:
    {{- $component := "registry" }}
    {{- include "trigger-v4.componentLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
spec:
  replicas: 1
  serviceName: {{ include "trigger-v4.fullname" . }}-registry-headless
  selector:
    matchLabels:
      {{- include "trigger-v4.componentSelectorLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 6 }}
  template:
    metadata:
      {{- with .Values.registry.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "trigger-v4.componentSelectorLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 8 }}
    spec:
      {{- with .Values.registry.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: registry
          {{- with .Values.registry.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.registry.image.registry }}/{{ .Values.registry.image.repository }}:{{ .Values.registry.image.tag }}"
          imagePullPolicy: {{ .Values.registry.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.registry.service.targetPort }}
              protocol: TCP
          {{- if or .Values.registry.auth.enabled .Values.registry.extraEnvVars }}
          env:
            {{- if .Values.registry.auth.enabled }}
            - name: REGISTRY_AUTH
              value: "htpasswd"
            - name: REGISTRY_AUTH_HTPASSWD_REALM
              value: "Registry Realm"
            - name: REGISTRY_AUTH_HTPASSWD_PATH
              value: "/auth/htpasswd"
            {{- end }}
            {{- with .Values.registry.extraEnvVars }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- end }}
          {{- if .Values.registry.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: {{ .Values.registry.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.registry.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.registry.livenessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.registry.livenessProbe.failureThreshold }}
            successThreshold: {{ .Values.registry.livenessProbe.successThreshold }}
          {{- end }}
          {{- if .Values.registry.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: {{ .Values.registry.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.registry.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.registry.readinessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.registry.readinessProbe.failureThreshold }}
            successThreshold: {{ .Values.registry.readinessProbe.successThreshold }}
          {{- end }}
          {{- if .Values.registry.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: {{ .Values.registry.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.registry.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.registry.startupProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.registry.startupProbe.failureThreshold }}
            successThreshold: {{ .Values.registry.startupProbe.successThreshold }}
          {{- end }}
          resources:
            {{- toYaml .Values.registry.resources | nindent 12 }}
          volumeMounts:
            - name: {{ .Values.registry.persistence.volumeName }}
              mountPath: {{ .Values.registry.persistence.mountPath }}
              {{- if .Values.registry.persistence.subPath }}
              subPath: {{ .Values.registry.persistence.subPath }}
              {{- end }}
            {{- if .Values.registry.auth.enabled }}
            - name: registry-auth
              mountPath: /auth
              readOnly: true
            {{- end }}
      volumes:
        {{- if .Values.registry.auth.enabled }}
        - name: registry-auth
          secret:
            secretName: {{ include "trigger-v4.fullname" . }}-registry-auth
        {{- end }}
        {{- if not .Values.registry.persistence.enabled }}
        - name: {{ .Values.registry.persistence.volumeName }}
          emptyDir: {}
        {{- else if .Values.registry.persistence.existingClaim }}
        - name: {{ .Values.registry.persistence.volumeName }}
          persistentVolumeClaim:
            claimName: {{ .Values.registry.persistence.existingClaim }}
        {{- end }}
  {{- if .Values.registry.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: {{ .Values.registry.persistence.volumeName }}
        {{- with .Values.registry.persistence.annotations }}
        annotations:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        labels:
          {{- $component := "registry" }}
          {{- include "trigger-v4.componentLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 10 }}
          {{- with .Values.registry.persistence.labels }}
          {{- toYaml . | nindent 10 }}
          {{- end }}
      spec:
        accessModes:
          {{- toYaml .Values.registry.persistence.accessModes | nindent 10 }}
        resources:
          requests:
            storage: {{ .Values.registry.persistence.size }}
        {{- $storageClass := .Values.registry.persistence.storageClass | default .Values.global.storageClass }}
        {{- if $storageClass }}
        storageClassName: {{ $storageClass | quote }}
        {{- end }}
        {{- with .Values.registry.persistence.selector }}
        selector:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        {{- with .Values.registry.persistence.dataSource }}
        dataSource:
          {{- toYaml . | nindent 10 }}
        {{- end }}
  {{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "trigger-v4.fullname" . }}-registry-headless
  labels:
    {{- $component := "registry" }}
    {{- include "trigger-v4.componentLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - name: http
      port: {{ .Values.registry.service.port }}
      targetPort: http
      protocol: TCP
  selector:
    {{- include "trigger-v4.componentSelectorLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "trigger-v4.fullname" . }}-registry
  labels:
    {{- $component := "registry" }}
    {{- include "trigger-v4.componentLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
spec:
  type: {{ .Values.registry.service.type }}
  ports:
    - name: http
      port: {{ .Values.registry.service.port }}
      targetPort: http
      protocol: TCP
  selector:
    {{- include "trigger-v4.componentSelectorLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
{{- end }}