apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "insight-server.fullname" . }}
  labels:
    app: {{ template "mission-control.name" . }}
    chart: {{ template "mission-control.chart" . }}
    component: {{ .Values.insightServer.name }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  replicas: {{ .Values.insightServer.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "mission-control.name" . }}
      component: {{ .Values.insightServer.name }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "mission-control.name" . }}
        component: {{ .Values.insightServer.name }}
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ template "mission-control.serviceAccountName" . }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecrets }}
    {{- end }}
      initContainers:
      - name: init-data
        image: "{{ .Values.initContainerImage }}"
        command:
        - 'sh'
        - '-c'
        - >
          until nc -z -w 2 {{ .Release.Name }}-mongodb 27017 && echo mongodb ok && \
                nc -z -w 2 {{ .Release.Name }}-elasticsearch 9200 && echo elasticsearch ok;
            do sleep 2;
          done;
          sleep 10
      containers:
      - name: {{ .Values.insightServer.name }}
        image: {{ .Values.insightServer.image }}:{{ default .Chart.AppVersion .Values.insightServer.version }}
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        env:
        - name: CORE_URL
          value: 'http://{{ template "insight-server.fullname" . }}:{{ .Values.insightServer.internalHttpPort }}'
        - name: EXECUTOR_URL
          value: 'http://{{ template "insight-executor.fullname" . }}:{{ .Values.insightExecutor.internalPort }}/executorservice'
        - name: SCHEDULER_URL
          value: 'http://{{ template "insight-scheduler.fullname" . }}:{{ .Values.insightScheduler.internalPort }}/schedulerservice'
        - name: MONGO_URL
          value: '{{ .Release.Name }}-mongodb:27017'
        - name: MONGODB_USERNAME
          value: '{{ .Values.mongodb.db.insightUser }}'
        - name: MONGODB_PASSWORD
          value: '{{ .Values.mongodb.db.insightPassword }}'
        - name: MONGODB_ADMIN_USERNAME
          value: '{{ .Values.mongodb.db.adminUser }}'
        - name: MONGODB_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mission-control.fullname" . }}-mongodb-cred
              key: adminPassword
        - name: JFMC_URL
          value: 'http://{{ template "mission-control.fullname" . }}:{{ .Values.missionControl.internalPort }}'
        - name: ELASTIC_SEARCH_URL
          value: 'http://{{ .Release.Name }}-elasticsearch:9200'
        - name: ELASTIC_CLUSTER_NAME
          value: '{{ .Values.elasticsearch.env.clusterName }}'
        - name: ELASTIC_SEARCH_USERNAME
          value: '{{ .Values.elasticsearch.env.esUsername }}'
        - name: ELASTIC_SEARCH_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ .Release.Name }}-elasticsearch
              key: esPassword
        - name: ELASTIC_COMMUNICATION_NODE_URL
          value: '{{ .Release.Name }}-elasticsearch:9300'
        - name: JFI_HOME
          value: '/var/cloudbox'
        - name: JFI_HOME_CORE
          value: '/var/cloudbox/core'
        - name: JFMC_MISSION_CONTROL_CERT
          value: "/var/cloudbox/core/_MASTER_/data/contexts/security/jfmc.crt"
        - name: JFMC_INSIGHT_SERVER_CERT
          value: "/var/cloudbox/core/_MASTER_/data/contexts/security/insight.crt"
        - name: JFMC_INSIGHT_SERVER_KEY
          value: "/var/cloudbox/core/_MASTER_/data/contexts/security/insight.key"
        - name: JFMC_INSIGHT_SERVER_PORT
          value: "{{ .Values.insightServer.internalHttpPort }}"
        - name: JFMC_INSIGHT_SERVER_SSL_PORT
          value: "{{ .Values.insightServer.internalHttpsPort }}"
        ports:
        - containerPort: {{ .Values.insightServer.internalHttpPort }}
          protocol: TCP
        - containerPort: {{ .Values.insightServer.internalHttpsPort }}
          protocol: TCP
        volumeMounts:
        - name: mission-control-certs
          mountPath: /var/cloudbox/core/_MASTER_/data/contexts/security/insight.key
          subPath: insight.key
        - name: mission-control-certs
          mountPath: /var/cloudbox/core/_MASTER_/data/contexts/security/insight.crt
          subPath: insight.crt
        - name: mission-control-certs
          mountPath: /var/cloudbox/core/_MASTER_/data/contexts/security/jfmc.crt
          subPath: jfmc.crt
        livenessProbe:
          httpGet:
            path: /api/status
            port: 8082
          initialDelaySeconds: 300
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /api/status
            port: 8082
          initialDelaySeconds: 300
          periodSeconds: 10
        resources:
{{ toYaml .Values.insightServer.resources | indent 10 }}
    {{- with .Values.insightServer.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.insightServer.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.insightServer.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
      volumes:
      - name: mission-control-certs
        secret:
          {{- if .Values.existingCertsSecret }}
          secretName: {{ .Values.existingCertsSecret }}
          {{- else }}
          secretName: {{ template "mission-control.fullname" . }}-certs
          {{- end }}
