#
# Copyright 2021 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: Validate Tutorials

on:
  workflow_dispatch:
  push:
    branches:
      - master
      - feature/new_quickstarts
      - release-*
    tags:
      - v*
  pull_request:
    branches:
      - master
      - feature/new_quickstarts
      - release-*
jobs:
  deploy:
    name: Validate tutorials on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    timeout-minutes: 40
    env:
      GOVER: 1.22
      KUBERNETES_VERSION: v1.29.4
      KIND_VERSION: v0.23.0
      KIND_IMAGE_SHA: sha256:3abb816a5b1061fb15c6e9e60856ec40d56b7b52bcea5f5f1350bc6e2320b6f8
    strategy:
      matrix:
        os: [ubuntu-latest]
      fail-fast: false
    steps:
      - name: Check out code
        uses: actions/checkout@v2
      - name: Load environment variables
        uses: artursouza/export-env-action@v2
        with:
          envFile: './.github/env/global.env'
          expand: 'true'
      - name: Pinning Python to ${{ env.MACOS_PYTHON_VERSION }} on MacOS
        if: matrix.os == 'macos-latest'
        run: brew install python@${{ env.MACOS_PYTHON_VERSION }} && brew unlink python@${{ env.MACOS_PYTHON_VERSION }} && brew link --overwrite python@${{ env.MACOS_PYTHON_VERSION }}
      - name: Verify Python version
        run: python3 --version
      - name: Upgrade pip and setuptools
        if: matrix.os == 'macos-latest'
        run: pip3 install --upgrade pip && python3 -m pip install --upgrade setuptools
      - name: Show .Net version
        run: dotnet --version
      - name: Install Docker - MacOS
        if: matrix.os == 'macos-latest'
        uses: docker-practice/actions-setup-docker@v1
        with:
          docker_buildx: false
          docker_version: 20.10
      - name: Install Helm - MacOS
        if: matrix.os == 'macos-latest'
        run: brew install helm
      - name: Configure KinD
        if: matrix.os == 'ubuntu-latest'
        # Generate a KinD configuration file that uses:
        # A couple of worker nodes: this is needed to run both
        #     ZooKeeper + Kakfa
        working-directory: ./
        run: |
          cat > kind.yaml <<EOF
          apiVersion: kind.x-k8s.io/v1alpha4
          kind: Cluster
          nodes:
          - role: control-plane
            image: kindest/node:${{ env.KUBERNETES_VERSION }}@${{ env.KIND_IMAGE_SHA }}
          - role: worker
            image: kindest/node:${{ env.KUBERNETES_VERSION }}@${{ env.KIND_IMAGE_SHA }}
          - role: worker
            image: kindest/node:${{ env.KUBERNETES_VERSION }}@${{ env.KIND_IMAGE_SHA }}
          - role: worker
            image: kindest/node:${{ env.KUBERNETES_VERSION }}@${{ env.KIND_IMAGE_SHA }}
          - role: worker
            image: kindest/node:${{ env.KUBERNETES_VERSION }}@${{ env.KIND_IMAGE_SHA }}
          EOF

          # Log the generated kind.yaml for easy reference.
          cat kind.yaml

      - name: Create KinD Cluster - Linux
        if: matrix.os == 'ubuntu-latest'
        uses: helm/kind-action@v1.2.0
        with:
          config: kind.yaml
          cluster_name: kind
          version: ${{ env.KIND_VERSION }}
      - name: Install minikube - MacOS
        if: matrix.os == 'macos-latest'
        run: |
          brew install kubernetes-cli || brew link --overwrite kubernetes-cli
          brew install minikube
          minikube start --driver=virtualbox --memory 8192 --host-only-cidr "192.168.59.1/24"
      - name: Get KinD info
        if: matrix.os == 'ubuntu-latest'
        run: |
          kubectl cluster-info --context kind-kind
          NODE_IP=$(kubectl get nodes \
                    -lkubernetes.io/hostname!=kind-control-plane \
                    -ojsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}')
          echo "SERVICE_IP=$NODE_IP" >> $GITHUB_ENV
      - name: Set up Go ${{ env.GOVER }}
        uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GOVER }}
      - name: Set up Dapr CLI - Mac/Linux
        if: matrix.os != 'windows-latest'
        run: wget -q ${{ env.DAPR_INSTALL_URL }}/install.sh -O - | /bin/bash -s ${{ env.DAPR_CLI_VERSION }}
      - name: Set up Dapr CLI - Windows
        if: matrix.os == 'windows-latest'
        run: powershell -Command "\$$script=iwr -useb ${{ env.DAPR_INSTALL_URL }}/install.ps1; \$$block=[ScriptBlock]::Create(\$$script); invoke-command -ScriptBlock \$$block -ArgumentList ${{ env.DAPR_CLI_VERSION }}"
      - name: Install Dapr
        run: |
          export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
          dapr init --runtime-version=${{ env.DAPR_RUNTIME_VERSION }}
          dapr --version
      - name: Install Dapr - Kubernetes
        run: |
          dapr init -k --dev --runtime-version=${{ env.DAPR_RUNTIME_VERSION }} --wait || kubectl get pods --all-namespaces
          kubectl get nodes -o wide
          kubectl describe pod dapr-dev-redis-master-0
          helm list
          for pod in `dapr status -k | awk '/dapr/ {print $1}'`; do kubectl describe pod -l app=$pod -n dapr-system ; kubectl logs -l app=$pod -n dapr-system; done
      - name: Install utilities dependencies
        run: |
          echo "PATH=$PATH:$HOME/.local/bin" >> $GITHUB_ENV
          pip3 install setuptools wheel
          pip3 install mechanical-markdown
      - name: Validate hello-world
        run: |
          pushd tutorials/hello-world
          make validate
          popd
      # - name: Validate hello-kubernetes multi app run
      #   if: matrix.os == 'ubuntu-latest'
      #   run: |
      #     pushd tutorials/hello-kubernetes
      #     make validate_multi_app_run
      #     popd
      - name: Validate hello-kubernetes normal run
        if: matrix.os == 'ubuntu-latest'
        run: |
          pushd tutorials/hello-kubernetes
          make validate_normal_run
          popd
      # - name: Validate distributed-calculator
      #   if: matrix.os == 'ubuntu-latest'
      #   run: |
      #     pushd tutorials/distributed-calculator
      #     make validate
      #     popd
      # - name: Validate pub-sub
      #   if: matrix.os == 'ubuntu-latest'
      #   run: |
      #     pushd tutorials/pub-sub
      #     make validate
      #     popd
      # - name: Validate bindings
      #   if: matrix.os == 'ubuntu-latest'
      #   run: |
      #     pushd tutorials/bindings
      #     make validate
      #     popd
      # - name: Validate secretstore
      #   if: matrix.os == 'ubuntu-latest'
      #   run: |
      #     pushd tutorials/secretstore
      #     make validate
      #     popd
      # - name: Validate observability
      #   if: matrix.os == 'ubuntu-latest'
      #   run: |
      #     pushd tutorials/observability
      #     make validate
      #     popd
      - name: Linkcheck README.md
        run: |
          make validate
