{{- if .Values.nginx.enabled -}}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "artifactory.nginx.fullname" . }}
  labels:
    app: {{ template "artifactory.name" . }}
    chart: {{ template "artifactory.chart" . }}
    component: {{ .Values.nginx.name }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  replicas: {{ .Values.nginx.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "artifactory.name" . }}
        component: {{ .Values.nginx.name }}
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ template "artifactory.serviceAccountName" . }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecrets }}
    {{- end }}
      initContainers:
    {{- if .Values.nginx.persistence.enabled }}
      - name: "remove-lost-found"
        image: "{{ .Values.initContainerImage }}"
        imagePullPolicy: {{ .Values.nginx.image.pullPolicy }}
        command:
        - '/bin/sh'
        - '-c'
        - 'rm -rfv {{ .Values.nginx.persistence.mountPath }}/lost+found'
        volumeMounts:
        - mountPath: {{ .Values.nginx.persistence.mountPath | quote }}
          name: nginx-volume
    {{- end }}
      - name: "wait-for-artifactory"
        image: "{{ .Values.initContainerImage }}"
        command:
        - 'sh'
        - '-c'
        - >
          until nc -z -w 2 {{ template "artifactory.fullname" . }} {{ .Values.artifactory.externalPort }} && echo artifactory ok; do
            sleep 2;
          done;
      containers:
      - name: {{ .Values.nginx.name }}
        image: "{{ .Values.nginx.image.repository }}:{{ default .Chart.AppVersion .Values.nginx.image.version }}"
        imagePullPolicy: {{ .Values.nginx.image.pullPolicy }}
        lifecycle:
          postStart:
            exec:
              command:
              - '/bin/sh'
              - '-c'
              - >
                {{- if .Values.nginx.customConfigMap }}
                cp -Lrf /tmp/nginx.conf /etc/nginx/nginx.conf;
                {{- end }}
                if [ -f /tmp/replicator-nginx.conf ]; then
                cp -Lfv /tmp/replicator-nginx.conf /etc/nginx/conf.d/replicator-nginx.conf;
                fi;
                if [ -f /tmp/ssl/*.crt ]; then
                rm -rf /var/opt/jfrog/nginx/ssl/example.*;
                cp -Lfv /tmp/ssl/* /var/opt/jfrog/nginx/ssl;
                fi;
                until [ -f /etc/nginx/conf.d/artifactory.conf ]; do sleep 1; done;
                if ! grep -q 'upstream' /etc/nginx/conf.d/artifactory.conf; then
                sed -i -e 's,proxy_pass .*,proxy_pass     http://{{ template "artifactory.fullname" . }}:{{ .Values.artifactory.externalPort }}/artifactory/;,g' \
                    -e 's,server_name .*,server_name ~(?<repo>.+)\\.{{ template "artifactory.fullname" . }} {{ template "artifactory.fullname" . }};,g' \
                    /etc/nginx/conf.d/artifactory.conf;
                fi;
                if ! grep -q 'proxy_http_version' /etc/nginx/conf.d/artifactory.conf; then
                sed -i 's,\(proxy_next_upstream .*\),proxy_http_version  1.1;\n    \1,g' /etc/nginx/conf.d/artifactory.conf;
                fi;
                sleep 5; nginx -s reload; touch /var/log/nginx/conf.done
        env:
        - name: ART_BASE_URL
          {{- if .Values.nginx.env.artUrl }}
          value: {{ .Values.nginx.env.artUrl }}
          {{- else }}
          value: "http://{{ template "artifactory.fullname" . }}:{{ .Values.artifactory.externalPort }}/artifactory"
          {{- end }}
        - name: SSL
          value: "{{ .Values.nginx.env.ssl }}"
        - name: SKIP_AUTO_UPDATE_CONFIG
          value: "{{ .Values.nginx.env.skipAutoConfigUpdate }}"
        ports:
        - containerPort: {{ .Values.nginx.internalPortHttp }}
        - containerPort: {{ .Values.nginx.internalPortHttps }}
        {{- if .Values.artifactory.replicator.enabled }}
        - containerPort: {{ .Values.nginx.internalPortReplicator }}
        {{- end }}
        volumeMounts:
        - name: nginx-volume
          mountPath: {{ .Values.nginx.persistence.mountPath | quote }}
        {{- if .Values.artifactory.replicator.enabled }}
        - name: replicator-nginx-config
          mountPath: "/tmp/replicator-nginx.conf"
          subPath: replicator-nginx.conf
        {{- end }}
        {{- if .Values.nginx.tlsSecretName }}
        - name: ssl-secret-volume
          mountPath: "/tmp/ssl"
        {{- end }}
        {{- if .Values.nginx.customConfigMap }}
        - name: nginx-config
          mountPath: "/tmp/"
        {{- end}}
        resources:
{{ toYaml .Values.nginx.resources | indent 10 }}
    {{- with .Values.nginx.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.nginx.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.nginx.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
        {{- if .Values.nginx.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: '/artifactory/webapp/#/login'
            port: 80
          initialDelaySeconds: {{ .Values.nginx.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.nginx.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.nginx.readinessProbe.timeoutSeconds }}
          failureThreshold: {{ .Values.nginx.readinessProbe.failureThreshold }}
          successThreshold: {{ .Values.nginx.readinessProbe.successThreshold }}
        {{- end }}
        {{- if .Values.nginx.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: '/artifactory/webapp/#/login'
            port: 80
          initialDelaySeconds: {{ .Values.nginx.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.nginx.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.nginx.livenessProbe.timeoutSeconds }}
          failureThreshold: {{ .Values.nginx.livenessProbe.failureThreshold }}
          successThreshold: {{ .Values.nginx.livenessProbe.successThreshold }}
        {{- end }}
      volumes:
      {{- if .Values.artifactory.replicator.enabled }}
      - name: replicator-nginx-config
        configMap:
          name: {{ template "artifactory.fullname" . }}-replicator-nginx-config
      {{- end}}
      {{- if .Values.nginx.customConfigMap }}
      - name: nginx-config
        configMap:
          name: {{ .Values.nginx.customConfigMap }}
      {{- end}}
      - name: nginx-volume
      {{- if .Values.nginx.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.nginx.persistence.existingClaim | default (include "artifactory.nginx.fullname" .) }}
      {{- else }}
        emptyDir: {}
      {{- end -}}
      {{- if .Values.nginx.tlsSecretName }}
      - name: ssl-secret-volume
        secret:
          secretName: {{ .Values.nginx.tlsSecretName }}
      {{- end -}}
{{- end }}
