name: AKS

on:
  pull_request: {}
  push:
    branches:
      - master
  schedule:
    - cron:  '0 */6 * * *'

env:
  clusterName: cilium-cli-ci-${{ github.run_number }}

jobs:
  installation-and-connectivitiy:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Build cilium CLI binary
        run: make

      - name: Install cilium CLI binary
        run: sudo make install

      - name: Azure Login
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Azure CLI info
        uses: azure/CLI@v1
        with:
          azcliversion: 2.0.72
          inlineScript: |
            az account show

      - name: Create AKS cluster
        run: |
          az aks create \
            --resource-group cilium-ci \
            --name ${{ env.clusterName }} \
            --location westeurope \
            --node-count 2 \
            --network-plugin azure \
            --generate-ssh-keys

      - name: Get Credentials
        run: |
          az aks get-credentials --name ${{ env.clusterName }} --resource-group cilium-ci

      - name: Install cilium
        run: |
          cilium install --cluster-name ${{ env.clusterName }} --azure-resource-group cilium-ci --azure-tenant-id ${{ secrets.AZURE_TENANT_ID}} --azure-client-id ${{ secrets.AZURE_CLIENT_ID }} --azure-client-secret ${{ secrets.AZURE_CLIENT_SECRET }} --config monitor-aggregation=none

      - name: Enable relay
        run: |
          cilium hubble enable

      - name: Status
        run: |
          cilium status --wait

      - name: Relay Port Forward
        run: |
          kubectl port-forward -n kube-system deployment/hubble-relay 4245:4245&
          sleep 5s

      - name: Connectivity Test
        run: |
          cilium connectivity test

      - name: Cleanup
        if: ${{ always() }}
        run: |
          cilium status
          kubectl get pods --all-namespaces -o wide
          curl -sLO https://github.com/cilium/cilium-sysdump/releases/latest/download/cilium-sysdump.zip
          python cilium-sysdump.zip --output cilium-sysdump-out
          az aks delete --yes --name ${{ env.clusterName }} --resource-group cilium-ci

      - name: Upload Artifacts
        if: ${{ always() }}
        uses: actions/upload-artifact@v2
        with:
          name: cilium-sysdump-out.zip
          path: cilium-sysdump-out.zip
          retention-days: 5
