name: e2e

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  kind:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Restore Go cache
        uses: actions/cache@v1
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.15.x
      - name: Setup Kubernetes
        uses: engineerd/setup-kind@v0.5.0
        with:
          image: kindest/node:v1.16.9
      - name: Run test
        run: make test
      - name: Check if working tree is dirty
        run: |
          if [[ $(git diff --stat) != '' ]]; then
            git diff
            echo 'run make test and commit changes'
            exit 1
          fi
      - name: Build
        run: sudo go build -o ./bin/flux ./cmd/flux
      - name: flux check --pre
        run: |
          ./bin/flux check --pre
      - name: flux install --manifests
        run: |
          ./bin/flux install --manifests ./manifests/install/
      - name: flux create source git
        run: |
          ./bin/flux create source git podinfo \
            --url https://github.com/stefanprodan/podinfo  \
            --tag-semver=">=3.2.3"
      - name: flux create source git export apply
        run: |
          ./bin/flux create source git podinfo-export \
            --url https://github.com/stefanprodan/podinfo  \
            --tag-semver=">=3.2.3" \
            --export | kubectl apply -f -
          ./bin/flux delete source git podinfo-export --silent
      - name: flux get sources git
        run: |
          ./bin/flux get sources git
      - name: flux get sources git --all-namespaces
        run: |
          ./bin/flux get sources git --all-namespaces
      - name: flux create kustomization
        run: |
          ./bin/flux create kustomization podinfo \
            --source=podinfo \
            --path="./deploy/overlays/dev" \
            --prune=true \
            --interval=5m \
            --validation=client \
            --health-check="Deployment/frontend.dev" \
            --health-check="Deployment/backend.dev" \
            --health-check-timeout=3m
      - name: flux reconcile kustomization --with-source
        run: |
          ./bin/flux reconcile kustomization podinfo --with-source
      - name: flux get kustomizations
        run: |
          ./bin/flux get kustomizations
      - name: flux get kustomizations --all-namespaces
        run: |
          ./bin/flux get kustomizations --all-namespaces
      - name: flux suspend kustomization
        run: |
          ./bin/flux suspend kustomization podinfo
      - name: flux resume kustomization
        run: |
          ./bin/flux resume kustomization podinfo
      - name: flux export
        run: |
          ./bin/flux export source git --all
          ./bin/flux export kustomization --all
      - name: flux delete kustomization
        run: |
          ./bin/flux delete kustomization podinfo --silent
      - name: flux create source helm
        run: |
          ./bin/flux create source helm podinfo \
            --url https://stefanprodan.github.io/podinfo
      - name: flux create helmrelease --source=HelmRepository/podinfo
        run: |
          ./bin/flux create hr podinfo-helm \
            --target-namespace=default \
            --source=HelmRepository/podinfo \
            --chart=podinfo \
            --chart-version=">4.0.0 <5.0.0"
      - name: flux create helmrelease --source=GitRepository/podinfo
        run: |
          ./bin/flux create hr podinfo-git \
            --target-namespace=default \
            --source=GitRepository/podinfo \
            --chart=./charts/podinfo
      - name: flux reconcile helmrelease --with-source
        run: |
          ./bin/flux reconcile helmrelease podinfo-git --with-source
      - name: flux get helmreleases
        run: |
          ./bin/flux get helmreleases
      - name: flux get helmreleases --all-namespaces
        run: |
          ./bin/flux get helmreleases --all-namespaces
      - name: flux export helmrelease
        run: |
          ./bin/flux export hr --all
      - name: flux delete helmrelease podinfo-helm
        run: |
          ./bin/flux delete hr podinfo-helm --silent
      - name: flux delete helmrelease podinfo-git
        run: |
          ./bin/flux delete hr podinfo-git --silent
      - name: flux delete source helm
        run: |
          ./bin/flux delete source helm podinfo --silent
      - name: flux delete source git
        run: |
          ./bin/flux delete source git podinfo --silent
      - name: flux create tenant
        run: |
          ./bin/flux create tenant dev-team --with-namespace=apps
          ./bin/flux -n apps create source helm podinfo \
            --url https://stefanprodan.github.io/podinfo
          ./bin/flux -n apps create hr podinfo-helm \
            --source=HelmRepository/podinfo \
            --chart=podinfo \
            --chart-version="5.0.x" \
            --service-account=dev-team
      - name: flux2-kustomize-helm-example
        run: |
          ./bin/flux create source git flux-system \
          --url=https://github.com/fluxcd/flux2-kustomize-helm-example \
          --branch=main
          ./bin/flux create kustomization flux-system \
          --source=flux-system \
          --path=./clusters/staging
          kubectl -n flux-system wait kustomization/apps --for=condition=ready --timeout=2m
      - name: flux check
        run: |
          ./bin/flux check
      - name: flux uninstall
        run: |
          ./bin/flux uninstall --crds --silent --timeout=10m
      - name: Debug failure
        if: failure()
        run: |
          kubectl version --client --short
          kubectl -n flux-system get all
          kubectl -n flux-system get kustomizations -oyaml
          kubectl -n flux-system logs deploy/source-controller
          kubectl -n flux-system logs deploy/kustomize-controller
