# code: language=ansible
# ---
- name: 'Create java API Deployment'
  hosts: "localhost"
  environment:
    GOOGLE_CREDENTIALS: "{{ service_account_file }}"
  tasks:
    - name: 'Create k8s deployment for java api'
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: "swifty-java-receiver-api"
            namespace: "{{ gke_namespace }}"
            labels:
              app: "swifty-java-receiver-api"
              env: "{{ env_ }}"
          spec:
            replicas: 2
            selector:
              matchLabels:
                app: "swifty-java-receiver-api"
                env: "{{ env_ }}"
            template:
              metadata:
                labels:
                  app: "swifty-java-receiver-api"
                  env: "{{ env_ }}"
              spec:
                topologySpreadConstraints:
                  - maxSkew: 1
                    topologyKey: "kubernetes.io/hostname"
                    whenUnsatisfiable: ScheduleAnyway
                    labelSelector:
                      matchLabels:
                        app: "swifty-java-receiver-api"
                        env: "{{ env_ }}"
                affinity:
                  nodeAffinity:
                    requiredDuringSchedulingIgnoredDuringExecution:
                      nodeSelectorTerms:
                      - matchExpressions:
                        - key: "cloud.google.com/gke-nodepool"
                          operator: In
                          values:
                            - "main-pool"
                containers:
                  - name: "swifty-java-receiver-api"
                    image: "rxfoundry/swifty_receiver:{{ swifty_version }}"
                    imagePup2rxPolicy: Always
                    stdin: "{{ stdin }}"
                    tty: "{{ tty }}"
                    resources:
                      requests:
                        memory: "512Mi"
                        cpu: "100m"
                      limits:
                        memory: "1024Mi"
                    ports:
                    - containerPort: 8080
                      protocol: TCP
                    env:
                      - name: ENVIRONMENT_NAME
                        value: "{{ env_ }}"
                      - name: GOOGLE_PROJECT_NAME
                        value: "{{ project }}"
                      - name: APP_MESSAGING
                        value: "pubsub"
                      - name: PUBSUB_TOPIC
                        value: "swifty-message-receiver"
                      - name: LOGLEVEL
                        value: "DEBUG"
                    livenessProbe:
                      httpGet:
                        port: 8080
                        path: /api/receiver/1/version
                      initialDelaySeconds: 90
                      timeoutSeconds: 10
                    readinessProbe:
                      httpGet:
                        port: 8080
                        path: /api/receiver/1/version
                      initialDelaySeconds: 30
                      timeoutSeconds: 10
                    command:
                      - "java"
                      - "-jar"
                      - "/app/app.jar"

    - name: 'Create k8s service for java api'
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: v1
          kind: Service
          metadata:
            name: "swifty-java-receiver-api"
            namespace: "{{ gke_namespace }}"
            annotations:
              cloud.google.com/neg: '{"ingress": true}'
              cloud.google.com/healthcheck-path: "/api/receiver/1/version"
          spec:
            type: NodePort
            ports:
            - port: 80
              targetPort: 8080
              protocol: TCP
            selector:
              app: "swifty-java-receiver-api"