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@v4
        with:
          cache: false
          go-version: "1.20"
      - run: make validate-code
      - 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@v2
        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
        # If a previous run left Acorn installed, uninstall it
        run: |
          if acorn check; then
            acorn uninstall -af
          fi
          # Install Acorn with NetworkPolicies disabled because EKS does not enforce them, so tests will fail if we leave them enabled
          acorn install --image ghcr.io/acorn-io/runtime:main --network-policies=false
          
        env:
          KUBECONFIG: "/home/runner/.kube/config"

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

      - name: Build test summary
        id: test_summary
        uses: test-summary/action@v2
        if: "!cancelled() && steps.test.conclusion != 'skipped'"
        with:
          paths: test-summary.xml

      - 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 }},${{ secrets.SLACK_BOT_SUCCESS_CHANNEL }}'
          slack-message: "❌ Nightly EKS test had ${{ steps.test_summary.outputs.failed }} test case(s) fail: ${{ 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: Upload test results to Datadog
        uses: datadog/junit-upload-github-action@v1
        if: "!cancelled() && steps.test_summary.conclusion != 'skipped'"
        with:
          api-key: ${{ secrets.DD_API_KEY }}
          service: runtime-eks-tests
          env: ci
          files: test-summary.xml
          datadog-site: us3.datadoghq.com

      - name: create run artifacts
        if: always()
        # Add any artifacts that should be associated with this run to /tmp/artifacts/${{ github.run_id }}
        run: |
          mkdir -p /tmp/artifacts/${{ github.run_id }}/{logs,resources}
          kubectl logs -n acorn-system -l app=acorn-api --tail -1 > /tmp/artifacts/${{ github.run_id }}/logs/acorn-api.log || true
          kubectl logs -n acorn-system -l app=acorn-controller --tail -1 > /tmp/artifacts/${{ github.run_id }}/logs/acorn-controller.log || true
          kubectl get all -n acorn-image-system > /tmp/artifacts/${{ github.run_id }}/resources/acorn-image-system.txt || true
          kubectl get all -n acorn-system > /tmp/artifacts/${{ github.run_id }}/resources/acorn-system.txt || true
          kubectl get all -n acorn > /tmp/artifacts/${{ github.run_id }}/resources/acorn.txt || true
          acorn all -A > /tmp/artifacts/${{ github.run_id }}/resources/acorn-resources-state.txt || true

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

      # Only uninstall on success, keep around on failure for debugging
      - name: uninstall acorn
        if: success()
        run: |
          acorn uninstall -af
        env:
          KUBECONFIG: "/home/runner/.kube/config"
