{{- if .Values.static.enabled -}}
{{- $fullName := include "gitdata.fullname" . -}}
{{- $ns := include "gitdata.namespace" . -}}
{{- $svc := .Values.static -}}
{{- $port := print "0.0.0.0:" ($svc.service.port | toString) -}}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $fullName }}-static
  namespace: {{ $ns }}
  labels:
    app.kubernetes.io/name: {{ $fullName }}-static
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
spec:
  replicas: {{ $svc.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ $fullName }}-static
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ $fullName }}-static
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      {{- if $.Values.image.pullSecrets }}
      imagePullSecrets:
        {{- range $.Values.image.pullSecrets }}
        - name: {{ . }}
        {{- end }}
      {{- end }}
      containers:
        - name: static
          image: "{{ $.Values.image.registry }}/{{ $svc.image.repository }}:{{ $svc.image.tag }}"
          imagePullPolicy: {{ $svc.image.pullPolicy | default $.Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ $svc.service.port }}
              protocol: TCP
          envFrom:
            - configMapRef:
                name: {{ $fullName }}-config
          env:
            - name: STATIC_ROOT
              value: /data
            - name: STATIC_BIND
              value: {{ $port | quote }}
            - name: STATIC_CORS
              value: "true"
            {{- if $svc.logLevel }}
            - name: STATIC_LOG_LEVEL
              value: {{ $svc.logLevel | quote }}
            {{- end }}
            {{- range $svc.env }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}
          {{- if $svc.livenessProbe }}
          livenessProbe:
            httpGet:
              path: /health
              port: {{ $svc.service.port }}
            initialDelaySeconds: {{ $svc.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ $svc.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ $svc.livenessProbe.timeoutSeconds }}
            failureThreshold: {{ $svc.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if $svc.readinessProbe }}
          readinessProbe:
            httpGet:
              path: /health
              port: {{ $svc.service.port }}
            initialDelaySeconds: {{ $svc.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ $svc.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ $svc.readinessProbe.timeoutSeconds }}
            failureThreshold: {{ $svc.readinessProbe.failureThreshold }}
          {{- end }}
          volumeMounts:
            - name: shared-data
              mountPath: /data
      volumes:
        - name: shared-data
          persistentVolumeClaim:
            claimName: {{ $fullName }}-shared-data
      {{- with $svc.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $svc.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $svc.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

---
apiVersion: v1
kind: Service
metadata:
  name: {{ $fullName }}-static
  namespace: {{ $ns }}
  labels:
    app.kubernetes.io/name: {{ $fullName }}-static
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  type: {{ $svc.service.type }}
  ports:
    - name: http
      port: {{ $svc.service.port }}
      targetPort: http
      protocol: TCP
  selector:
    app.kubernetes.io/name: {{ $fullName }}-static
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
