{{- if .Values.app.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "gitdata.fullname" . }}-app
  namespace: {{ include "gitdata.namespace" . }}
  labels:
    app.kubernetes.io/name: {{ include "gitdata.fullname" . }}-app
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
spec:
  replicas: {{ .Values.app.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "gitdata.fullname" . }}-app
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "gitdata.fullname" . }}-app
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      {{- if $.Values.image.pullSecrets }}
      imagePullSecrets:
        {{- range $.Values.image.pullSecrets }}
        - name: {{ . }}
        {{- end }}
      {{- end }}
      terminationGracePeriodSeconds: 30
      containers:
        - name: app
          image: "{{ .Values.image.registry }}/{{ .Values.app.image.repository }}:{{ .Values.app.image.tag }}"
          imagePullPolicy: {{ .Values.app.image.pullPolicy | default .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.app.service.port }}
              protocol: TCP
          args:
            - --bind
            - 0.0.0.0:{{ .Values.app.service.port }}
          envFrom:
            - configMapRef:
                name: {{ include "gitdata.fullname" . }}-config
            {{- range .Values.app.env }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.app.livenessProbe.path }}
              port: {{ .Values.app.livenessProbe.port }}
            initialDelaySeconds: {{ .Values.app.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.app.livenessProbe.periodSeconds }}
          readinessProbe:
            httpGet:
              path: {{ .Values.app.readinessProbe.path }}
              port: {{ .Values.app.readinessProbe.port }}
            initialDelaySeconds: {{ .Values.app.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.app.readinessProbe.periodSeconds }}
          {{- if .Values.app.startupProbe }}
          startupProbe:
            httpGet:
              path: {{ .Values.app.startupProbe.path }}
              port: {{ .Values.app.startupProbe.port }}
            initialDelaySeconds: {{ .Values.app.startupProbe.initialDelaySeconds | default 0 }}
            periodSeconds: {{ .Values.app.startupProbe.periodSeconds }}
            failureThreshold: {{ .Values.app.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.storage.enabled }}
          volumeMounts:
            - name: shared-data
              mountPath: /data
          {{- end }}
      {{- if .Values.storage.enabled }}
      volumes:
        - name: shared-data
          persistentVolumeClaim:
            claimName: {{ include "gitdata.fullname" . }}-shared-data
      {{- end }}
      {{- with .Values.app.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.app.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.app.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "gitdata.fullname" . }}-app
  namespace: {{ include "gitdata.namespace" . }}
  labels:
    app.kubernetes.io/name: {{ include "gitdata.fullname" . }}-app
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  type: {{ .Values.app.service.type }}
  ports:
    - port: {{ .Values.app.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: {{ include "gitdata.fullname" . }}-app
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
