apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ include "chartmuseum.fullname" . }}
  annotations:
{{ toYaml .Values.deployment.annotations | indent 4 }}
  labels:
{{ include "chartmuseum.labels.standard" . | indent 4 }}
{{- if .Values.deployment.labels }}
{{ toYaml .Values.deployment.labels | indent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  strategy:
{{ toYaml .Values.strategy | indent 4 }}
  revisionHistoryLimit: 10
{{- if .Values.deployment.matchlabes }}
  selector:
    matchLabels:
{{ toYaml .Values.deployment.matchlabes | indent 6 }}
{{- end }}
  template:
    metadata:
      name: {{ include "chartmuseum.fullname" . }}
      annotations:
{{ toYaml .Values.replica.annotations | indent 8 }}
      labels:
        app: {{ template "chartmuseum.name" . }}
        release: {{ .Release.Name | quote }}
{{- if .Values.deployment.labels }}
{{ toYaml .Values.deployment.labels | indent 8 }}
{{- end }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
{{- range $name, $value := .Values.env.open }}
{{- if not (empty $value) }}
        - name: {{ $name | quote }}
          value: {{ $value | quote }}
{{- end }}
{{- end }}
{{- range $name, $value := .Values.env.field }}
{{- if not ( empty $value) }}
        - name: {{ $name | quote }}
          valueFrom:
            fieldRef:
              fieldPath: {{ $value | quote }}
{{- end }}
{{- end }}
{{- if .Values.gcp.secret.enabled }}
        - name: GOOGLE_APPLICATION_CREDENTIALS
          value: "/etc/secrets/google/credentials.json"
{{- end }}
{{- if .Values.env.existingSecret }}
{{- $secret_name := .Values.env.existingSecret }}
{{- range $name, $key := .Values.env.existingSecretMappings }}
{{- if not ( empty $key) }}
        - name: {{ $name | quote }}
          valueFrom:
            secretKeyRef:
              name: {{ $secret_name | quote }}
              key: {{ $key | quote }}
{{- end }}
{{- end }}
{{- else }}
{{- $secret_name := include "chartmuseum.fullname" . }}
{{- range $name, $value := .Values.env.secret }}
{{- if not ( empty $value) }}
        - name: {{ $name | quote }}
          valueFrom:
            secretKeyRef:
              name: {{ $secret_name }}
              key: {{ $name | quote }}
{{- end }}
{{- end }}
{{- end }}
        args:
        - --port=8080
{{- if eq .Values.env.open.STORAGE "local" }}
        - --storage-local-rootdir=/storage
{{- end }}
        ports:
        - name: http
          containerPort: 8080
        livenessProbe:
          httpGet:
            path: {{ .Values.env.open.CONTEXT_PATH }}/health
            port: http
{{ toYaml .Values.probes.liveness | indent 10 }}
        readinessProbe:
          httpGet:
            path: {{ .Values.env.open.CONTEXT_PATH }}/health
            port: http
{{ toYaml .Values.probes.readiness | indent 10 }}
{{- if eq .Values.env.open.STORAGE "local" }}
        volumeMounts:
        - mountPath: /storage
          name: storage-volume
{{- end }}
{{- if  .Values.gcp.secret.enabled }}
        volumeMounts:
        - mountPath: /etc/secrets/google
          name: {{ include "chartmuseum.fullname" . }}-gcp
{{- end }}
{{- if  .Values.oracle.secret.enabled }}
        volumeMounts:
        - mountPath: /home/chartmuseum/.oci
          name: {{ include "chartmuseum.fullname" . }}-oracle
{{- end }}
      {{- with .Values.resources }}
        resources:
{{ toYaml . | indent 10 }}
      {{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- if .Values.serviceAccount.create }}
      serviceAccountName: {{ include "chartmuseum.fullname" . }}
    {{- else if .Values.serviceAccount.name }}
      serviceAccountName: {{ .Values.serviceAccount.name }}
    {{- end }}
    {{- with .Values.securityContext }}
      securityContext:
{{ toYaml . | indent 8 }}
    {{- end }}
      volumes:
      - name: storage-volume
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim | default (include "chartmuseum.fullname" .) }}
      {{- else }}
        emptyDir: {}
      {{- end -}}
      {{ if .Values.gcp.secret.enabled }}
      - name: {{ include "chartmuseum.fullname" . }}-gcp
        secret:
      {{ if .Values.env.secret.GOOGLE_CREDENTIALS_JSON }}
          secretName: {{ include "chartmuseum.fullname" . }}
          items:
          - key: GOOGLE_CREDENTIALS_JSON
            path: credentials.json
      {{ else }}
          secretName: {{ .Values.gcp.secret.name }}
          items:
          - key: {{ .Values.gcp.secret.key }}
            path: credentials.json
      {{ end }}
      {{ end }}
      {{ if .Values.oracle.secret.enabled }}
      - name: {{ include "chartmuseum.fullname" . }}-oracle
        secret:
          secretName: {{ .Values.oracle.secret.name }}
          items:
          - key: {{ .Values.oracle.secret.config }}
            path: config
          - key: {{ .Values.oracle.secret.key_file }}
            path: oci.key
      {{ end }}
