name: e2e-kind
on: [push, pull_request]
jobs:
  e2e-kind:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - docker-file: images/Dockerfile.thick
            cni-version: "0.3.1"
            multus-manifest: multus-daemonset-thick.yml
          - docker-file: images/Dockerfile
            cni-version: "0.3.1"
            multus-manifest: multus-daemonset.yml
          - docker-file: images/Dockerfile.thick
            cni-version: "0.4.0"
            multus-manifest: multus-daemonset-thick.yml
          - docker-file: images/Dockerfile
            cni-version: "0.4.0"
            multus-manifest: multus-daemonset.yml
          # need to wait kind to support CNI 1.0.0 (now kind 0.11 supports up to 0.4.0)
#          - docker-file: images/Dockerfile.thick
#            cni-version: "1.0.0"
#            multus-manifest: multus-thick-daemonset.yml
#          - docker-file: images/Dockerfile
#            cni-version: "1.0.0"
#            multus-manifest: multus-daemonset.yml
    env:
      JOB_NAME: "${{ matrix.cni-version }}-${{ matrix.multus-manifest }}"

    if: >
      (( github.event.pull_request.head.repo.owner.login != github.event.pull_request.base.repo.owner.login ) &&
        github.event_name == 'pull_request' ) || (github.event_name == 'push' && github.event.commits != '[]' )
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Setup python
        uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - name: Setup j2cli
        run: |
          sudo apt-get install -y j2cli
          echo $(j2 --version)

      - name: Build latest-amd64
        uses: docker/build-push-action@v5
        with:
          context: .
          load: true
          tags: localhost:5000/multus:e2e
          file: ${{ matrix.docker-file }}
          platforms: linux/amd64

      - name: Get kind/kubectl/koko
        working-directory: ./e2e
        run: ./get_tools.sh

      - name: generate yaml files
        working-directory: ./e2e
        run: env CNI_VERSION=${{ matrix.cni-version }} ./generate_yamls.sh

      - name: Setup cluster
        working-directory: ./e2e
        run: MULTUS_MANIFEST=${{ matrix.multus-manifest }} MULTUS_DOCKERFILE=none ./setup_cluster.sh

      - name: Test simple pod
        working-directory: ./e2e
        run: ./test-simple-pod.sh

      - name: Test macvlan1
        working-directory: ./e2e
        run: ./test-simple-macvlan1.sh

      - name: Test static pod
        working-directory: ./e2e
        run: ./test-static-pod.sh

      - name: Test default route1
        working-directory: ./e2e
        run: ./test-default-route1.sh

#      - name: Test DRA integration
#        working-directory: ./e2e
#        run: ./test-dra-integration.sh

      - name: Test subdirectory CNI chaining
        if: ${{ matrix.multus-manifest == 'multus-daemonset-thick.yml' }}
        working-directory: ./e2e
        run: ./test-subdirectory-chaining.sh

      - name: Test subdirectory CNI chaining with passthru CNI / auxiliaryCNIChainName
        if: ${{ matrix.multus-manifest == 'multus-daemonset-thick.yml' }}
        working-directory: ./e2e
        run: ./test-subdirectory-chaining-passthru.sh

      - name: Export kind logs
        if: always()
        run: |
          mkdir -p /tmp/kind/logs
          kind export logs /tmp/kind/logs -v 2147483647

      - name: Upload kind logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: kind-logs-${{ env.JOB_NAME }}-${{ github.run_id }}
          path: /tmp/kind/logs

      - name: cleanup cluster and registry
        run: |
          kind delete cluster
