name: test acorn on EKS
on:
  workflow_dispatch:
  schedule:
    - cron: '00 7 * * *'   # time in UTC
jobs:
  acorn-test-eks:
    runs-on: ["self-hosted", "gha-eks"]
    permissions:
      id-token: write
    steps:
      - name: install curl
        run: |
           sudo apt update
           sudo apt install -y curl build-essential make 
           curl -LO https://dl.k8s.io/release/v1.25.0/bin/linux/amd64/kubectl
           sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
           curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
           unzip awscliv2.zip
           sudo ./aws/install

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v3
        with:
          go-version: "1.19"
      - run: make validate-ci
      - run: make validate
      - run: make build
      - run: sudo install -o root -g root -m 0755 ./bin/acorn /usr/local/bin/acorn

      - name: configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.GHA_SVC_ACC_AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.GHA_SVC_ACC_AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.GHA_SVC_ACC_AWS_REGION }}

      - name: update kubeconfig 
        run: |
          aws eks update-kubeconfig --region ${{ secrets.GHA_SVC_ACC_AWS_REGION }} --name ${{ secrets.GHA_SVC_ACC_EKS_CLUSTER_NAME }}

      - name: install acorn
        run: |
          acorn install --image ghcr.io/acorn-io/acorn:main
        env:
          KUBECONFIG: "/home/runner/.kube/config"

      - name: run acorn integration tests
        run: |
          make TEST_ACORN_CONTROLLER=external TEST_FLAGS="-timeout=7m" test
        env:
          KUBECONFIG: "/home/runner/.kube/config"

      - name: report failure to slack
        if: failure()
        id: slack-failure
        uses: slackapi/slack-github-action@v1.23.0
        with:
          channel-id: '${{ secrets.SLACK_BOT_FAILURE_CHANNEL }}'
          slack-message: "❌ Nightly EKS test failed: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

      - name: report success to slack
        id: slack-success
        uses: slackapi/slack-github-action@v1.23.0
        with:
          channel-id: '${{ secrets.SLACK_BOT_SUCCESS_CHANNEL }}'
          slack-message: " ✅ Nightly EKS test passed: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
        env:
          SLACK_BOT_TOKEN: '${{ secrets.SLACK_BOT_TOKEN }}'

      - name: create run artifacts
        if: always()
        # Add any artifacts that should be assosciated with this run to /tmp/artifacts/${{ github.run_id }}
        run: |
          mkdir -p /tmp/artifacts/${{ github.run_id }} ||
          kubectl logs -n acorn-system -l app=acorn-api > /tmp/artifacts/${{ github.run_id }}/acorn-api.log ||
          kubectl logs -n acorn-system -l app=acorn-controller > /tmp/artifacts/${{ github.run_id }}/acorn-controller.log ||
          true

      - name: upload run artifacts
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: run-artifacts
          path: /tmp/artifacts/${{ github.run_id }}

      - name: uninstall acorn
        if: always()
        run: |
          acorn uninstall -af
        env:
          KUBECONFIG: "/home/runner/.kube/config"
