# Copyright © 2017 Amdocs, Bell Canada
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ include "common.fullname" . }}
  namespace: {{ include "common.namespace" . }}
  labels:
    app: {{ include "common.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ include "common.name" . }}
        release: {{ .Release.Name }}
    spec:
      initContainers:
      - command:
        - /root/job_complete.py
        args:
        - --job-name
        - {{ include "common.fullname" . }}-galera-config
        env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        image: "{{ .Values.global.readinessRepository }}/{{ .Values.global.readinessImage }}"
        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
        name: {{ include "common.name" . }}-readiness
      containers:
        - name: {{ include "common.name" . }}
          image: "{{ include "common.repository" . }}/{{ .Values.image }}"
          imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
          ports:
          - containerPort: {{ .Values.service.internalPort }}
          # disable liveness probe when breakpoints set in debugger
          # so K8s doesn't restart unresponsive container
          {{- if eq .Values.liveness.enabled true }}
          livenessProbe:
            tcpSocket:
              port: {{ .Values.service.internalPort }}
            initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
            periodSeconds: {{ .Values.liveness.periodSeconds }}
          {{ end -}}
          readinessProbe:
            tcpSocket:
              port: {{ .Values.service.internalPort }}
            initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds }}
            periodSeconds: {{ .Values.readiness.periodSeconds }}
          env:
            - name: ASDC_CLIENT_REST_HOST
              value: sdc-be.{{ include "common.namespace" . }}
            - name: ASDC_CLIENT_REST_AUTH
              value: "{{ .Values.config.asdcclientrestauth }}"
            - name: ASDC_CLIENT_REST_PORT
              value: "{{ .Values.config.asdcclientrestport }}"
            - name: ASDC_CLIENT_REST_PROTOCOL
              value: "https"
            - name: VID_AAI_URL
              value: https://aai.{{ include "common.namespace" . }}:{{ .Values.config.vidaaiport }}
            - name: VID_ECOMP_SHARED_CONTEXT_REST_URL
              value: http://portal-app.{{ include "common.namespace" . }}:{{ .Values.config.onapport }}/ONAPPORTAL/context
            - name: VID_MSO_SERVER_URL
              value: http://so.{{ include "common.namespace" . }}:{{ .Values.config.msoport }}/onap/so/infra
            - name: VID_MSO_PASS
              value: "{{ .Values.config.vidmsopass }}"
            - name: MSO_DME2_SERVER_URL
              value: "{{ .Values.config.msodme2serverurl }}"
            - name: MSO_DME2_ENABLED
              value: {{ .Values.global.debugEnabled | default .Values.debugEnabled | quote }}
            - name: VID_ECOMP_REDIRECT_URL
              value: https://{{ .Values.config.portalhost }}:{{ .Values.config.onapport }}/ONAPPORTAL/login.htm
            - name: VID_ECOMP_REST_URL
              value: https://portal-app:{{ .Values.config.onapportrest }}/ONAPPORTAL/auxapi
            - name: VID_ROLE_ACCESS_CENTRALIZED
              value: "{{ .Values.config.roleaccesscentralized }}"
            - name: VID_CONTACT_US_LINK
              value: "{{ .Values.config.vidcontactuslink }}"
            - name: VID_KEYSTORE_PASSWORD
              value: {{ .Values.config.vidkeystorepassword | quote }}
            - name: CATALINA_OPTS
              value: "-Dvid.keystore.password=$(VID_KEYSTORE_PASSWORD) -Dvid.keyalias=vid@vid.onap.org -Dvid.keystore.filename=/opt/app/vid/etc/org.onap.vid.jks -Dcom.att.eelf.logging.file=logback.xml -Dcom.att.eelf.logging.path=/tmp"
            - name: VID_UEB_URL_LIST
              value: message-router.{{ include "common.namespace" . }}
            - name: VID_MYSQL_HOST
              value: "{{ .Values.config.vidmysqlhost }}"
            - name: VID_MYSQL_PORT
              value: "{{ .Values.config.vidmysqlport }}"
            - name: VID_MYSQL_DBNAME
              value: "{{ .Values.config.vidmysqldbname }}"
            - name: VID_MYSQL_USER
              value: "{{ .Values.config.vidmysqluser }}"
            - name: VID_MYSQL_PASS
              value: "{{ .Values.config.vidmysqlpassword }}"
              #valueFrom:
              #  secretKeyRef: {name: {{ include "common.fullname" . }}, key: vid-password}
            - name: VID_MYSQL_MAXCONNECTIONS
              value: "{{ .Values.config.vidmysqlmaxconnections }}"
          volumeMounts:
          - mountPath: /opt/app/vid/etc
            name: vid-certs
          - mountPath: /etc/localtime
            name: localtime
            readOnly: true
          - mountPath: /var/log/onap
            name: vid-logs
          - mountPath: /tmp/logback.xml
            name: vid-logback
            subPath: logback.xml
          resources:
{{ include "common.resources" . | indent 12 }}
        {{- if .Values.nodeSelector }}
        nodeSelector:
{{ toYaml .Values.nodeSelector | indent 10 }}
        {{- end -}}
        {{- if .Values.affinity }}
        affinity:
{{ toYaml .Values.affinity | indent 10 }}
        {{- end }}
      # side car containers
        - name: filebeat-onap
          image: "{{ .Values.global.loggingRepository }}/{{ .Values.global.loggingImage }}"
          imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
          volumeMounts:
          - mountPath: /usr/share/filebeat/filebeat.yml
            name: filebeat-conf
            subPath: filebeat.yml
          - mountPath: /var/log/onap
            name: vid-logs
          - mountPath: /usr/share/filebeat/data
            name: vid-data-filebeat
      volumes:
        - name: vid-certs
          secret:
            secretName: {{ include "common.fullname" . }}-certs
        - name: localtime
          hostPath:
            path: /etc/localtime
        - name: filebeat-conf
          configMap:
            name: {{ include "common.fullname" . }}-filebeat-configmap
        - name: vid-logs
          emptyDir: {}
        - name: vid-data-filebeat
          emptyDir: {}
        - name: vid-logback
          configMap:
            name: {{ include "common.fullname" . }}-log-configmap
      imagePullSecrets:
      - name: "{{ include "common.namespace" . }}-docker-registry-key"