name: VelaUX APIServer Test

on:
  push:
    branches:
      - master
      - release-*
      - apiserver
    tags:
      - v*
  workflow_dispatch: { }
  pull_request:
    branches:
      - master
      - release-*
      - apiserver

env:
  # Common versions
  GO_VERSION: '1.19'

permissions:
  contents: read

jobs:

  detect-noop:
    runs-on: ubuntu-20.04
    outputs:
      noop: ${{ steps.noop.outputs.should_skip }}
    steps:
      - name: Detect No-op Changes
        id: noop
        uses: fkirc/skip-duplicate-actions@12aca0a884f6137d619d6a8a09fcc3406ced5281
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          paths_ignore: '["**.md", "**.mdx", "**.png", "**.jpg"]'
          do_not_skip: '["workflow_dispatch", "schedule", "push"]'
        continue-on-error: true

  apiserver-unit-tests:
    runs-on: ubuntu-20.04
    needs: detect-noop
    if: needs.detect-noop.outputs.noop != 'true'

    steps:
      - name: Set up Go
        uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568
        with:
          go-version: ${{ env.GO_VERSION }}
        id: go

      - name: Check out code into the Go module directory
        uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b
        with:
          submodules: true

      - name: Cache Go Dependencies
        uses: actions/cache@9b0c1fce7a93df8e3bb8926b0d6e9d89e92f20a7
        with:
          path: .work/pkg
          key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-pkg-

      - name: Install ginkgo
        run: |
          sudo sed -i 's/azure\.//' /etc/apt/sources.list
          sudo apt-get update
          sudo apt-get install -y golang-ginkgo-dev

      - name: Start MongoDB
        uses: supercharge/mongodb-github-action@538a4d2a1041920c47630172445cb688592d6e51 # 1.8.0
        with:
          mongodb-version: '5.0'

        # TODO need update action version to resolve node 12 deprecated.
      - name: install Kubebuilder
        uses: RyanSiu1995/kubebuilder-action@ff52bff1bae252239223476e5ab0d71d6ba02343
        with:
          version: 3.1.0
          kubebuilderOnly: false
          kubernetesVersion: v1.21.2

      - name: Run api server unit test
        run: make unit-test-apiserver

      - name: Upload coverage report
        uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: ./coverage.txt
          flags: apiserver-unittests
          name: codecov-umbrella

  apiserver-e2e-tests:
    runs-on: aliyun
    needs: [ detect-noop ]
    if: needs.detect-noop.outputs.noop != 'true'
    strategy:
      matrix:
        k8s-version: ["v1.20","v1.24"]
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.k8s-version }}
      cancel-in-progress: true

    steps:
      - name: Set up Go
        uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568
        with:
          go-version: ${{ env.GO_VERSION }}
        id: go

      - name: Check out code into the Go module directory
        uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b
        with:
          submodules: true

      - name: Get dependencies
        run: |
          go get -v -t -d ./...

      - name: Tear down K3d if exist
        run: |
          k3d cluster delete || true
          k3d cluster delete worker || true

      - name: Calculate K3d args
        run: |
          EGRESS_ARG=""
          if [[ "${{ matrix.k8s-version }}" == v1.24 ]]; then
            EGRESS_ARG="--k3s-arg --egress-selector-mode=disabled@server:0"
          fi
          echo "EGRESS_ARG=${EGRESS_ARG}" >> $GITHUB_ENV 

      - name: Setup K3d (Hub)
        uses: nolar/setup-k3d-k3s@293b8e5822a20bc0d5bcdd4826f1a665e72aba96
        with:
          version: ${{ matrix.k8s-version }}
          github-token: ${{ secrets.GITHUB_TOKEN }}
          k3d-args: ${{ env.EGRESS_ARG }}


      - name: Setup K3d (Worker)
        uses: nolar/setup-k3d-k3s@293b8e5822a20bc0d5bcdd4826f1a665e72aba96
        with:
          version: ${{ matrix.k8s-version }}
          github-token: ${{ secrets.GITHUB_TOKEN }}
          k3d-name: worker
          k3d-args: --kubeconfig-update-default=false --network=k3d-k3s-default ${{ env.EGRESS_ARG }}


      - name: Kind Cluster (Worker)
        run: |
          internal_ip=$(docker network inspect k3d-k3s-default|jq ".[0].Containers"| jq -r '.[]| select(.Name=="k3d-worker-server-0")|.IPv4Address' | cut -d/ -f1)
          k3d kubeconfig get worker > /tmp/worker.client.kubeconfig
          cp /tmp/worker.client.kubeconfig /tmp/worker.kubeconfig
          sed -i "s/0.0.0.0:[0-9]\+/$internal_ip:6443/"  /tmp/worker.kubeconfig

      - name: Load image to k3d cluster
        run: make image-load

      - name: Cleanup for e2e tests
        run: |
          make vela-cli
          make e2e-cleanup
          make e2e-setup-core
          bin/vela addon enable fluxcd
          bin/vela addon enable vela-workflow --override-definitions
          timeout 600s bash -c -- 'while true; do kubectl get ns flux-system; if [ $? -eq 0 ] ; then break; else sleep 5; fi;done'
          kubectl wait --for=condition=Ready pod -l app.kubernetes.io/name=vela-core,app.kubernetes.io/instance=kubevela -n vela-system --timeout=600s
          kubectl wait --for=condition=Ready pod -l app=source-controller -n flux-system --timeout=600s
          kubectl wait --for=condition=Ready pod -l app=helm-controller -n flux-system --timeout=600s
          kubectl wait --for=condition=Ready pod -l app.kubernetes.io/name=vela-workflow -n vela-system --timeout=600s

      - name: Run api server e2e test
        run: |
          export ALIYUN_ACCESS_KEY_ID=${{ secrets.ALIYUN_ACCESS_KEY_ID }}
          export ALIYUN_ACCESS_KEY_SECRET=${{ secrets.ALIYUN_ACCESS_KEY_SECRET }}
          export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
          make e2e-apiserver-test

      - name: Stop kubevela, get profile
        run: make end-e2e-core

      - name: Upload coverage report
        uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: /tmp/e2e-profile.out, /tmp/e2e_apiserver_test.out
          flags: apiserver-e2etests
          name: codecov-umbrella

      - name: Clean e2e profile
        run: rm /tmp/e2e-profile.out

      - name: Cleanup image
        if: ${{ always() }}
        run: make image-cleanup
