apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "scdf.fullname" . }}-server
  labels:
    app: {{ template "scdf.name" . }}
    component: server
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  selector:
    matchLabels:
      app: {{ template "scdf.name" . }}
      component: server
      release: "{{ .Release.Name }}"
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ template "scdf.name" . }}
        component: server
        release: "{{ .Release.Name }}"
    spec:
      containers:
      - name: {{ template "scdf.fullname" . }}-server
        image: {{ .Values.server.image }}:{{ .Values.server.version }}
        imagePullPolicy: {{ .Values.server.imagePullPolicy }}
        volumeMounts:
          - name: database
            mountPath: /etc/secrets/database
            readOnly: true
        ports:
        - containerPort: 8080
          name: http
        resources:
{{ toYaml .Values.server.resources | indent 10 }}
        livenessProbe:
          httpGet:
            path: /management/health
            port: http
          initialDelaySeconds: 45
        readinessProbe:
          httpGet:
            path: /management/info
            port: http
          initialDelaySeconds: 45
        env:
        - name: KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: "metadata.namespace"
        - name: SERVER_PORT
          value: '8080'
        - name: SPRING_CLOUD_CONFIG_ENABLED
          value: 'false'
        - name: SPRING_CLOUD_KUBERNETES_SECRETS_ENABLE_API
          value: 'true'
        - name: SPRING_CLOUD_KUBERNETES_SECRETS_PATHS
          value: /etc/secrets
        - name: SPRING_CLOUD_KUBERNETES_CONFIG_NAME
          {{- if .Values.server.configMap }}
          value: {{ .Values.server.configMap | quote }}
          {{- else }}
          value: {{ template "scdf.fullname" . }}-server
          {{- end }}
          {{- if .Values.features.streaming.enabled }}
        - name: SPRING_CLOUD_SKIPPER_CLIENT_SERVER_URI
          value: 'http://${{ printf "{" }}{{ template "scdf.envname" . }}_SKIPPER_SERVICE_HOST}/api'
          {{- end }}
        - name: SPRING_CLOUD_DATAFLOW_SERVER_URI
          value: 'http://${{ printf "{" }}{{ template "scdf.envname" . }}_SERVER_SERVICE_HOST}:${{ printf "{" }}{{ template "scdf.envname" . }}_SERVER_SERVICE_PORT}'
        - name: SPRING_APPLICATION_JSON
          value: "{ \"maven\": { \"local-repository\": null, \"remote-repositories\": { \"repo1\": { \"url\": \"https://repo.spring.io/libs-snapshot\"} } } }"
        - name: KUBERNETES_TRUST_CERTIFICATES
          value: {{ .Values.server.trustCerts | quote }}
        - name: SPRING_CLOUD_DATAFLOW_FEATURES_STREAMS_ENABLED
          value: {{ .Values.features.streaming.enabled | quote }}
        - name: SPRING_CLOUD_DATAFLOW_FEATURES_TASKS_ENABLED
          value: {{ .Values.features.batch.enabled | quote }}
        - name: SPRING_CLOUD_DATAFLOW_FEATURES_SCHEDULES_ENABLED
          value: {{ .Values.features.batch.enabled | quote }}
        - name: SPRING_CLOUD_DATAFLOW_TASK_COMPOSED_TASK_RUNNER_URI
          value: 'docker://springcloud/spring-cloud-dataflow-composed-task-runner:2.6.0'
        {{- range $key, $value := .Values.server.extraEnv }}
        - name: {{ $key }}
          value: "{{ $value }}"
        {{- end }}
      volumes:
        - name: database
          secret:
            {{- if .Values.mysql.enabled }}
            secretName: {{ printf "%s-%s" .Release.Name "mysql" | trunc 63 | trimSuffix "-" }}
            {{- else }}
            secretName: {{ printf "%s-%s" .Release.Name "database" | trunc 63 | trimSuffix "-" }}
            {{- end }}
      initContainers:
      - name: init-db-wait
        image: {{ .Values.initContainers.dbWait.image }}:{{ .Values.initContainers.dbWait.tag }}
        imagePullPolicy: {{ .Values.initContainers.dbWait.imagePullPolicy }}
        command: ['sh', '-c', 'until nc -w3 -z {{ template "scdf.database.host" . }} {{ template "scdf.database.port" . }}; do echo waiting for database; sleep 3; done;']
      serviceAccountName: {{ template "scdf.serviceAccountName" . }}
