# helm_charts_skipper-deployment.yaml
{{- if .Values.features.streaming.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "scdf.fullname" . }}-skipper
  labels:
    app: {{ template "scdf.name" . }}
    component: skipper
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  selector:
    matchLabels:
      app: {{ template "scdf.name" . }}
      component: skipper
      release: "{{ .Release.Name }}"
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ template "scdf.name" . }}
        component: skipper
        release: "{{ .Release.Name }}"
    spec:
      containers:
      - name: {{ template "scdf.fullname" . }}-skipper
        image: {{ .Values.skipper.image }}:{{ .Values.skipper.version }}
        imagePullPolicy: {{ .Values.skipper.imagePullPolicy }}
        volumeMounts:
          {{- if or .Values.rabbitmq.enabled (index .Values "rabbitmq-ha" "enabled") }}
          - name: rabbitmq
            mountPath: /etc/secrets/rabbitmq
            readOnly: true
          {{- end}}
          - name: database
            mountPath: /etc/secrets/database
            readOnly: true
        ports:
        - containerPort: 7577
          name: http
        resources:
{{ toYaml .Values.skipper.resources | indent 10 }}
        livenessProbe:
          httpGet:
            path: /actuator/health
            port: http
          initialDelaySeconds: 45
        readinessProbe:
          httpGet:
            path: /actuator/info
            port: http
          initialDelaySeconds: 45
        env:
        - name: KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: "metadata.namespace"
        - name: SERVER_PORT
          value: '7577'
        - 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.skipper.configMap }}
          value: {{ .Values.skipper.configMap | quote }}
          {{- else }}
          value: {{ template "scdf.fullname" . }}-skipper
          {{- end }}
        {{- if or .Values.rabbitmq.enabled (index .Values "rabbitmq-ha" "enabled") }}
        - name: RABBITMQ_PASSWORD
          valueFrom:
            secretKeyRef:
              {{- if index .Values "rabbitmq-ha" "enabled" }}
              name: {{ printf "%s-%s" .Release.Name "rabbitmq-ha" | trunc 63 | trimSuffix "-" }}
              {{- else }}
              name: {{ printf "%s-%s" .Release.Name "rabbitmq" | trunc 63 | trimSuffix "-" }}
              {{- end }}
              key: rabbitmq-password
        {{- end }}
        - name: KUBERNETES_TRUST_CERTIFICATES
          value: {{ .Values.skipper.trustCerts | quote }}
        {{- range $key, $value := .Values.skipper.extraEnv }}
        - name: {{ $key }}
          value: "{{ $value }}"
        {{- end }}
      volumes:
        {{- if or .Values.rabbitmq.enabled (index .Values "rabbitmq-ha" "enabled") }}
        - name: rabbitmq
          secret:
            {{- if index .Values "rabbitmq-ha" "enabled" }}
            secretName: {{ printf "%s-%s" .Release.Name "rabbitmq-ha" | trunc 63 | trimSuffix "-" }}
            {{- else }}
            secretName: {{ printf "%s-%s" .Release.Name "rabbitmq" | trunc 63 | trimSuffix "-" }}
            {{- end }}
        {{- end }}
        - 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;']
      {{- if .Values.mysql.enabled }}
      - name: init-mysql-database
        image: "{{ .Values.mysql.image }}:{{ .Values.mysql.imageTag }}"
        imagePullPolicy: {{ .Values.mysql.imagePullPolicy }}
        env:
        - name: MYSQL_PWD
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "mysql" | trunc 63 | trimSuffix "-" }}
              key: mysql-root-password
        command:
        - 'sh'
        - '-c'
        - 'mysql -h {{ template "scdf.database.host" . }} -P {{ template "scdf.database.port" . }} -u root -e "CREATE DATABASE IF NOT EXISTS {{ template "scdf.database.skipper" . }};"'
      {{- end }}
      serviceAccountName: {{ template "scdf.serviceAccountName" . }}
{{- end }}