apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "chart.resourceNamePrefix" . }}hub
  labels:
    app.kubernetes.io/component: hub
    {{- include "chart.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.hub.deploy.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/component: hub
      {{- include "chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/path: "/metrics"
        prometheus.io/port: "8001"
      labels:
        app.kubernetes.io/component: hub
        {{- include "chart.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.hub.deploy.readinessGates }}
      readinessGates:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      {{- if .Release.IsInstall }}
      serviceAccountName: {{ include "chart.resourceNamePrefix" . }}hub
      {{- end }}
      initContainers:
      - name: check-db-ready
        image: {{ .Values.postgresql.image.repository }}:{{ .Values.postgresql.image.tag }}
        imagePullPolicy: {{ .Values.pullPolicy }}
        env:
          - name: PGHOST
            value: {{ default (printf "%s-postgresql.%s" .Release.Name .Release.Namespace) .Values.db.host }}
          - name: PGPORT
            value: "{{ .Values.db.port }}"
        command: ['sh', '-c', 'until pg_isready; do echo waiting for database; sleep 2; done;']
      {{- if .Release.IsInstall }}
      - name: check-db-migrator-run
        image: "bitnami/kubectl:{{ template "chart.KubernetesVersion" . }}"
        imagePullPolicy: IfNotPresent
        command: ['kubectl', 'wait', '--namespace={{ .Release.Namespace }}', '--for=condition=complete', 'job/{{ include "chart.resourceNamePrefix" . }}db-migrator-install', '--timeout=60s']
      {{- end }}
      containers:
        - name: hub
          image: {{ .Values.hub.deploy.image.repository }}:{{ .Values.imageTag | default (printf "v%s" .Chart.AppVersion) }}
          imagePullPolicy: {{ .Values.pullPolicy }}
          {{- if .Values.hub.server.cacheDir }}
          env:
            - name: XDG_CACHE_HOME
              value: {{ .Values.hub.server.cacheDir | quote }}
          {{- end }}
          volumeMounts:
          - name: hub-config
            mountPath: {{ .Values.hub.server.configDir | quote }}
            readOnly: true
          {{- if .Values.hub.server.cacheDir }}
          - name: cache-dir
            mountPath: {{ .Values.hub.server.cacheDir | quote }}
          {{- end }}
          ports:
            - name: http
              containerPort: 8000
              protocol: TCP
          resources:
            {{- toYaml .Values.hub.deploy.resources | nindent 12 }}
          {{- if .Values.hub.deploy.livenessProbe }}
          livenessProbe:
            {{- toYaml .Values.hub.deploy.livenessProbe | nindent 12}}
          {{- end }}
          {{- if .Values.hub.deploy.readinessProbe }}
          readinessProbe:
            {{- toYaml .Values.hub.deploy.readinessProbe | nindent 12}}
          {{- end }}
      volumes:
      - name: hub-config
        secret:
          secretName: {{ include "chart.resourceNamePrefix" . }}hub-config
      {{- if .Values.hub.server.cacheDir }}
      - name: cache-dir
        emptyDir: {}
      {{- end }}
