name: ovn-ci-periodic

on:
  schedule:
    - cron:  '0 6 * * *'
  workflow_dispatch:

env:
  GO_VERSION: 1.15.1
  K8S_VERSION: v1.19.0
  KIND_CLUSTER_NAME: ovn
  KIND_INSTALL_INGRESS: true
  KIND_ALLOW_SYSTEM_WRITES: true
  # This skips tests tagged as Serial
  # Current Serial tests are not relevant for OVN
  PARALLEL: true

jobs:
  build:
    if: github.repository == 'ovn-org/ovn-kubernetes' || github.event_name == 'workflow_dispatch'
    name: Build
    runs-on: ubuntu-latest
    steps:
    - name: Set up Go
      uses: actions/setup-go@v1
      with:
        go-version: ${{ env.GO_VERSION }}
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Set up environment
      run: |
        export GOPATH=$(go env GOPATH)
        echo "::set-env name=GOPATH::$GOPATH"
        export PATH=$GOPATH/bin:$PATH
        echo "::add-path::$GOPATH/bin"

    - name: Verify
      run: |
        pushd go-controller
          make gofmt
          make install.tools
          make lint
        popd

    - name: Build
      run: |
        set -x
        pushd go-controller
           make
        popd

    - name: Build docker image
      run: |
        pushd dist/images
          sudo cp -f ../../go-controller/_output/go/bin/ovn* .
          echo "ref: $(git rev-parse  --symbolic-full-name HEAD)  commit: $(git rev-parse  HEAD)" > git_info
          docker build -t ovn-daemonset-f:dev -f Dockerfile.fedora .
          mkdir _output
          docker save ovn-daemonset-f:dev > _output/image.tar
        popd

    - uses: actions/upload-artifact@v2
      with:
        name: test-image
        path: dist/images/_output/image.tar

  k8s:
    if: github.repository == 'ovn-org/ovn-kubernetes' || github.event_name == 'workflow_dispatch'
    name: Build k8s
    runs-on: ubuntu-latest
    steps:

    - name: Set up environment
      run: |
        export GOPATH=$(go env GOPATH)
        echo "::set-env name=GOPATH::$GOPATH"
        export PATH=$GOPATH/bin:$PATH
        echo "::add-path::$GOPATH/bin"

    - name: Cache Kubernetes
      id: cache-k8s
      uses: actions/cache@v2
      with:
        path: ${{ env.GOPATH }}/src/k8s.io/kubernetes/
        key: kubernetes-${{ env.K8S_VERSION }}

    - name: Set up Go
      uses: actions/setup-go@v1
      with:
        go-version: ${{ env.GO_VERSION }}
      if: steps.cache-k8s.outputs.cache-hit != 'true'

    - name: Build and install Kubernetes
      if: steps.cache-k8s.outputs.cache-hit != 'true'
      run: |
        set -x
        rm -rf $GOPATH/src/k8s.io/kubernetes
        git clone --single-branch --branch $K8S_VERSION https://github.com/kubernetes/kubernetes.git $GOPATH/src/k8s.io/kubernetes/
        pushd $GOPATH/src/k8s.io/kubernetes/
          make WHAT="test/e2e/e2e.test vendor/github.com/onsi/ginkgo/ginkgo cmd/kubectl"
          rm -rf .git
        popd

  e2e-dual:
    if: github.repository == 'ovn-org/ovn-kubernetes' || github.event_name == 'workflow_dispatch'
    name: e2e-dual
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        target:
          - shard: shard-conformance
          - shard: control-plane
        ha:
         - enabled: "true"
           name: "HA"
         - enabled: "false"
           name: "noHA"
    needs: [build, k8s]
    env:
      JOB_NAME: "${{ matrix.target.shard }}-${{ matrix.ha.name }}"
      OVN_HA: "${{ matrix.ha.enabled }}"
      # Deploy dual stack cluster
      KIND_IPV4_SUPPORT: true
      KIND_IPV6_SUPPORT: true
    steps:

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

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Set up environment
      run: |
        export GOPATH=$(go env GOPATH)
        echo "::set-env name=GOPATH::$GOPATH"
        export PATH=$GOPATH/bin:$PATH
        echo "::add-path::$GOPATH/bin"

    - name: Restore cache Kubernetes
      id: cache-k8s
      uses: actions/cache@v2
      with:
        path: ${{ env.GOPATH }}/src/k8s.io/kubernetes/
        key: kubernetes-${{ env.K8S_VERSION }}

    # Re-build if kube wasn't in the cache due to
    # https://github.com/actions/cache/issues/107#issuecomment-598188250
    # https://github.com/actions/cache/issues/208#issuecomment-596664572
    - name: Build and install Kubernetes
      if: steps.cache-k8s.outputs.cache-hit != 'true'
      run: |
        set -x
        rm -rf $GOPATH/src/k8s.io/kubernetes
        git clone --single-branch --branch $K8S_VERSION https://github.com/kubernetes/kubernetes.git $GOPATH/src/k8s.io/kubernetes/
        pushd $GOPATH/src/k8s.io/kubernetes/
          make WHAT="test/e2e/e2e.test vendor/github.com/onsi/ginkgo/ginkgo cmd/kubectl"
          rm -rf .git
        popd

    - uses: actions/download-artifact@v2
      with:
        name: test-image
    - name: Load docker image
      run: |
        docker load --input image.tar

    - name: kind setup
      run: |
        export OVN_IMAGE="ovn-daemonset-f:dev"
        # Copy the kubernetes binaries
        pushd $GOPATH/src/k8s.io/kubernetes/
          if [[ ! -f /usr/local/bin/kubectl ]]; then
            sudo ln ./_output/local/go/bin/kubectl /usr/local/bin/kubectl
          fi
          if [[ ! -f /usr/local/bin/e2e.test ]]; then
            sudo ln ./_output/local/go/bin/e2e.test /usr/local/bin/e2e.test
          fi
        popd
        # Install KIND
        sudo curl -Lo /usr/local/bin/kind https://github.com/aojea/kind/releases/download/dualstack/kind
        sudo chmod +x /usr/local/bin/kind
        # Run KIND
        pushd ./contrib
          ./kind.sh
        popd

    - name: Run Tests
      run: |
        make -C test ${{ matrix.target.shard }}

    - name: Upload Junit Reports
      if: always()
      uses: actions/upload-artifact@v2
      with:
        name: kind-junit-${{ env.JOB_NAME }}-${{ github.run_id }}
        path: './test/_artifacts/*.xml'

    - name: Generate Test Report
      id: xunit-viewer
      if: always()
      uses: AutoModality/action-xunit-viewer@v1
      with:
        results: ./test/_artifacts/

    - name: Upload Test Report
      if: always()
      uses: actions/upload-artifact@v2
      with:
        name: test-report-${{ env.JOB_NAME }}-${{ github.run_id }}
        path: './test/_artifacts/index.html'

    - name: Export logs
      if: always()
      run: |
        mkdir -p /tmp/kind/logs
        kind export logs --name ${KIND_CLUSTER_NAME} --loglevel=debug /tmp/kind/logs

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