name: Multicluster

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

env:
  clusterName1: cilium-cli-ci-multicluster-1-${{ github.run_number }}
  clusterName2: cilium-cli-ci-multicluster-2-${{ github.run_number }}
  zone: us-west2-a

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

      - name: Build cilium CLI binary
        run: make

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

      - name: Set up gcloud
        uses: google-github-actions/setup-gcloud@master
        with:
          project_id: ${{ secrets.GCP_PROJECT_ID }}
          service_account_key: ${{ secrets.GCP_SA_KEY }}
          export_default_credentials: true

      - name: gcloud info
        run: |
          gcloud info

      - name: Create GKE cluster1
        run: |
          gcloud container clusters create ${{ env.clusterName1 }} --image-type COS --num-nodes 2 --machine-type n1-standard-4 --zone ${{ env.zone }}
          gcloud container clusters get-credentials ${{ env.clusterName1 }} --zone ${{ env.zone }}

      - name: Install cilium in cluster1
        run: |
          cilium install --cluster-name ${{ env.clusterName1 }} --cluster-id 1 --kube-proxy-replacement disabled --config monitor-aggregation=none
          cilium hubble enable

      - name: Enable ClusterMesh cluster1
        run: |
          cilium clustermesh enable

      - name: Create GKE cluster2
        run: |
          gcloud container clusters create ${{ env.clusterName2 }} --image-type COS --num-nodes 2 --machine-type n1-standard-4 --zone ${{ env.zone }}
          gcloud container clusters get-credentials ${{ env.clusterName2 }} --zone ${{ env.zone }}

      - name: Install cilium in cluster2
        run: |
          cilium install --cluster-name ${{ env.clusterName2 }} --cluster-id 2 --kube-proxy-replacement disabled --config monitor-aggregation=none
          cilium hubble enable

      - name: Enable ClusterMesh cluster2
        run: |
          cilium clustermesh enable

      - name: Wait for ClusterMesh to be enabled cluster1
        run: |
          CONTEXT1=$(kubectl config view | grep ${{ env.clusterName1 }} | head -1 | awk '{print $2}')
          cilium --context $CONTEXT1 clustermesh status --wait

      - name: Wait for ClusterMesh to be enabled cluster2
        run: |
          CONTEXT2=$(kubectl config view | grep ${{ env.clusterName2 }} | head -1 | awk '{print $2}')
          cilium --context $CONTEXT2 clustermesh status --wait

      - name: Connect clusters
        run: |
          CONTEXT1=$(kubectl config view | grep ${{ env.clusterName1 }} | head -1 | awk '{print $2}')
          CONTEXT2=$(kubectl config view | grep ${{ env.clusterName2 }} | head -1 | awk '{print $2}')
          cilium --context $CONTEXT1 clustermesh connect --destination-context $CONTEXT2

      - name: Wait for ClusterMesh to be connected cluster1
        run: |
          CONTEXT1=$(kubectl config view | grep ${{ env.clusterName1 }} | head -1 | awk '{print $2}')
          cilium --context $CONTEXT1 clustermesh status --wait --wait-duration 5m

      - name: Wait for ClusterMesh to be connected cluster2
        run: |
          CONTEXT2=$(kubectl config view | grep ${{ env.clusterName2 }} | head -1 | awk '{print $2}')
          cilium --context $CONTEXT2 clustermesh status --wait --wait-duration 5m

      - name: Relay Port Forward
        run: |
          CONTEXT1=$(kubectl config view | grep ${{ env.clusterName1 }} | head -1 | awk '{print $2}')
          kubectl --context $CONTEXT1 port-forward -n kube-system deployment/hubble-relay 4245:4245&
          sleep 5s

      - name: Connectivity test
        run: |
          CONTEXT1=$(kubectl config view | grep ${{ env.clusterName1 }} | head -1 | awk '{print $2}')
          CONTEXT2=$(kubectl config view | grep ${{ env.clusterName2 }} | head -1 | awk '{print $2}')
          cilium --context $CONTEXT1 connectivity test --multi-cluster $CONTEXT2 --test '!pod-to-nodeport' --test '!pod-to-local-nodeport'

      - name: Cleanup
        if: ${{ always() }}
        run: |
          CONTEXT1=$(kubectl config view | grep ${{ env.clusterName1 }} | head -1 | awk '{print $2}')
          CONTEXT2=$(kubectl config view | grep ${{ env.clusterName2 }} | head -1 | awk '{print $2}')
          cilium --context $CONTEXT1 status
          cilium --context $CONTEXT1 clustermesh status
          cilium --context $CONTEXT2 status
          cilium --context $CONTEXT2 clustermesh 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
          gcloud container clusters delete --quiet ${{ env.clusterName1 }} --zone ${{ env.zone }}
          gcloud container clusters delete --quiet ${{ env.clusterName2 }} --zone ${{ env.zone }}

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