name: ovn-ci

on:
  push:
  pull_request:
    branches: [ master ]

env:
  GO_VERSION: 1.13.4
  K8S_VERSION: v1.17.2
  KIND_CLUSTER_NAME: ovn
  KIND_INSTALL_INGRESS: true

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - name: Free up disk space
      run: sudo eatmydata apt-get remove --auto-remove -y aspnetcore-* dotnet-* libmono-* mono-* msbuild php-* php7* ghc-* zulu-*

    - 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
        go get golang.org/x/tools/cmd/cover
        pushd go-controller
           make
           make windows
           COVERALLS=1 make check
        popd
        pushd dist/images
           if [ -n "$(git diff --stat origin/master.. | grep dist/images/Dockerfile)" ]; then make all; fi
        popd
        
        # Combine separate code coverage profiles into one
        go get github.com/modocache/gover
        gover go-controller/ gover.coverprofile
        
        # Convert coverage profile to LCOV format for coveralls github action
        go get github.com/jandelgado/gcov2lcov
        mkdir -p src/github.com/ovn-org
        ln -sf $(pwd) src/github.com/ovn-org/ovn-kubernetes
        GOPATH=$(pwd) gcov2lcov -infile gover.coverprofile -outfile coverage.lcov
        
    - name: Submit code coverage to Coveralls
      uses: coverallsapp/github-action@master
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        path-to-lcov: coverage.lcov

  k8s:
    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
      if: github.event_name == 'push' || github.event_name == 'pull_request'
      uses: actions/cache@v1
      with:
        path: ${{ env.GOPATH }}/src/k8s.io/kubernetes/
        key: k8s-go-2-${{ env.K8S_VERSION }}

    - name: Set up Go
      uses: actions/setup-go@v1
      with:
        go-version: ${{ env.GO_VERSION }}
      id: go
      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

  e2e:
    name: e2e
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target:
          - shard-n
          - shard-np
          - shard-s
          - shard-other
          - control-plane
        ha:
         - enabled: "true"
           name: "HA"
         - enabled: "false"
           name: "noHA"
    needs: k8s
    env:
      JOB_NAME: "${{ matrix.target }}-${{ matrix.ha.name }}"
      KIND_HA: "${{ matrix.ha.enabled }}"
    steps:
    
    - name: Free up disk space
      run: sudo eatmydata apt-get remove --auto-remove -y aspnetcore-* dotnet-* libmono-* mono-* msbuild php-* php7* ghc-* zulu-*
    
    - 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 Kubernetes from cache
      id: cache-k8s
      uses: actions/cache@v1
      with:
        path: "${{ env.GOPATH }}/src/k8s.io/kubernetes/"
        key: k8s-go-2-${{ 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
        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
    
    - name: kind setup
      run: |
        make -C test install-kind

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

    - 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@v1
      with:
        name: kind-logs-${{ env.JOB_NAME }}-${{ github.run_id }}
        path: /tmp/kind/logs
