apiVersion: apps/v1
kind: Deployment
metadata:
  name: airflow-workers-deploy
  namespace: airflow-tls
spec:
  replicas: 2
  selector:
    matchLabels:
      name: airflow-workers
      instance: airflow-workers-celery
      version: "1.0.0"
      component: worker
      part-of: airflow
  strategy:
    rollingUpdate:
      maxSurge: 100%
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        name: airflow-workers
        instance: airflow-workers-celery
        version: "1.0.0"
        component: worker
        part-of: airflow
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      serviceAccountName: airflow-rbac
      volumes:
        - name: az-dags
          persistentVolumeClaim:
            claimName: azure-claim-dags
        - name: az-scripts
          persistentVolumeClaim:
            claimName: azure-claim-scripts
        - name: az-logs
          persistentVolumeClaim:
            claimName: azure-claim-logs
        - name: azure-spn
          secret:
            secretName: azure-spn-secret
        - name: box-key-file
          secret:
            secretName: box-secret
      initContainers:
        - name: airflow-wait-web
          image: busybox:1.32.0
          command: ['sh', '-c', "until nslookup airflow-web-service.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for airflow-web-service; sleep 5; done"]
          resources:
            requests:
              cpu: "50m"
              memory: "50Mi"
            limits:
              cpu: "500m"
              memory: "500Mi"
      containers:
        - name: airflow
          image: rkoH1pVL.azurecr.io/airflow:1.10.14
          imagePullPolicy: Always  # IfNotPresent
          command:
              - "airflow"
          args:
              - worker
              - -q 
              - default,airq1,airq2
          volumeMounts:
            - name: az-dags
              mountPath: "/opt/airflow/dags"
            - name: az-scripts
              mountPath: "/opt/airflow/scripts"
            - name: az-logs
              mountPath: /opt/airflow/logs
              subPath: logs
            - name: azure-spn
              mountPath: "/opt/airflow/az-sec"
              readOnly: true
            - name: box-key-file
              mountPath: "/opt/airflow/box-sec"
              readOnly: true
          env:
            - name: AZURE_AUTH_LOCATION
              value: "/opt/airflow/az-sec/azure-auth"
            - name: BOX_AUTH_LOCATION
              value: "/opt/airflow/box-sec/box-auth"
          envFrom:
          - configMapRef:
              name: airflow-configmap
          resources:
            requests:
              cpu: "500m"
              memory: "1024Mi"
            limits:
              cpu: "4000m"
              memory: "4Gi"
          ports:
            - name: http
              containerPort: 8793
              protocol: TCP
          # TODO: Consider readiness or startup probe into database
          # readinessProbe:
          #   httpGet:
          #     path: /health
          #     port: 8080
          #   initialDelaySeconds: 5
          #   periodSeconds: 5
          #   failureThreshold: 3
          #   successThreshold: 1
          # livenessProbe:
          #   exec:
          #     command:
          #       - airflow
          #       - info
          #       - --anonymize
          #   initialDelaySeconds: 5
          #   periodSeconds: 5
# ---
# apiVersion: v1
# kind: Service
# metadata:
#   name: airflow-worker-service
#   namespace: airflow-tls
#   labels:
#     name: airflow-worker-service
#     instance: airflow-worker-celery
#     version: "1.0.0"
#     component: worker
#     part-of: airflow
# spec:
#   type: ClusterIP
#   selector:
#     name: airflow-workers
#     instance: airflow-workers-celery
#     version: "1.0.0"
#     component: worker
#     part-of: airflow
#   ports:
#     - name: http
#       protocol: TCP
#       port: 8793
# ---
# apiVersion: autoscaling/v1
# kind: HorizontalPodAutoscaler
# metadata:
#   name: airflow-workers-hpa
#   namespace: airflow-tls
# spec:
#   maxReplicas: 3 # define max replica count
#   minReplicas: 1 # define min replica count
#   scaleTargetRef:
#     apiVersion: apps/v1
#     kind: Deployment
#     name: airflow-workers-deploy
#   targetCPUUtilizationPercentage: 45 # target CPU utilization