apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "kubeinvaders.fullname" . }}
  labels:
    app.kubernetes.io/name: kubeinvaders
    helm.sh/chart: {{ include "kubeinvaders.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    {{- with .Values.additionalLabels }}
    {{- . | toYaml | nindent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.deployment.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: kubeinvaders
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: kubeinvaders
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        {{- with .Values.additionalLabels }}
        {{- . | toYaml | nindent 8 }}
        {{- end }}
      {{-  with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      serviceAccountName: kubeinvaders
      {{- with .Values.deployment.securityContext }}
      securityContext: 
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - env:
          {{- if .Values.ingress.enabled }}
          - name: ENDPOINT
            value: {{ required ".Values.ingress.hostName is required if Ingress is enabled" .Values.ingress.hostName }}
          {{- else }}
          - name: ENDPOINT
            value: {{ required ".Values.route_host is required if Ingress is disabled" .Values.route_host }}
          {{- end }}
          - name: NAMESPACE
            value: {{ .Values.config.target_namespace }}
          - name: ALIENPROXIMITY
            value: "{{ .Values.config.alienProximity }}"
          - name: HITSLIMIT
            value: "{{ .Values.config.hitsLimit }}"
          - name: UPDATETIME
            value: "{{ .Values.config.updateTime }}"
          {{- with .Values.deployment.extraEnv }}
          {{ toYaml . | nindent 10 }}
          {{- end }}
          name: {{ .Chart.Name }}
          image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}"
          imagePullPolicy: {{ .Values.deployment.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          {{- with .Values.deployment.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.deployment.extraVolumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.deployment.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.deployment.extraVolumes }} 
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.deployment.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.deployment.tolerations }}
      tolerations: 
        {{- toYaml . | nindent 8 }}
      {{- end }}
