name: Deploy on microk8s
on:
  workflow_dispatch:
  pull_request:
  schedule:
    - cron: "0 0 * * *" # This will run the workflow every day at midnight UTC
jobs:
  deploy:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-22.04, ubuntu-24.04]
        #microk8s: [1.29/stable, 1.30/stable, 1.31/stable, 1.32/stable]
        microk8s: [1.32/stable]
    steps:
      - uses: balchua/microk8s-actions@v0.4.3
        with:
          channel: ${{ matrix.microk8s }}
          addons: '["dns", "dashboard", "hostpath-storage", "nvidia"]'
      - name: Set permissions
        run: |
          sudo chown -f -R $USER $HOME/.kube $HOME/.config
      - name: Test microk8s
        id: list-pods
        run: |
          kubectl get no
          kubectl get pods -A -o wide
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Create storage class
        id: create-storage-class
        run: |
          kubectl apply -f ./base/ssd-hostpath-storageclass.yaml
          echo "Sleeping for 30 seconds, give time for the storage class to be created" && sleep 30
          kubectl get sc
      - name: Install Prometheus operator
        id: install-prometheus-operator
        run: |
          kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.48.1/bundle.yaml
          kubectl wait --for=condition=available deployment/prometheus-operator -n default --timeout=300s
          kubectl get crd
          kubectl get po -A -o wide
      - name: Install Minio
        id: install-minio
        run: |
          git clone --depth 1 --branch v6.0.1 https://github.com/minio/operator.git && kubectl apply -k operator/
          kubectl apply -f ./base/minio/minio-tenant-base.yaml
          echo "Waiting for resources to be created..."
          sleep 10
          kubectl wait --for=jsonpath='{.status.readyReplicas}'=1 statefulset/myminio-pool-0 -n minio-tenant --timeout=300s
          kubectl get po -A -o wide
          kubectl get po -A -o wide | grep myminio-pool-0-0 | awk '{print $3}' | grep -q '2/2' && echo "myminio-pool-0-0 pod is running with status 2/2" || (echo "myminio-pool-0-0 pod is not running with status 2/2" && exit 1)
      - name: Install MongoDB
        id: install-mongodb
        run: |
          helm repo add bitnami https://charts.bitnami.com/bitnami
          kubectl create namespace mongodb
          helm install mongodb -n mongodb bitnami/mongodb --values ./base/mongodb/mongodb-values.yaml --wait --timeout=10m
          kubectl get pods -A -o wide
      - name: Install RabbitMQ
        id: install-rabbitmq
        run: |
          helm repo add bitnami https://charts.bitnami.com/bitnami
          kubectl create namespace rabbitmq
          helm install rabbitmq -n rabbitmq bitnami/rabbitmq --values ./base/rabbitmq/rabbitmq-values.yaml --wait --timeout=10m
          kubectl get pods -A -o wide
          kubectl exec --namespace rabbitmq rabbitmq-0 -- rabbitmqctl status
      - name: Install Vernemq (MQTT Broker)
        id: install-vernemq
        run: |
          kubectl create namespace vernemq
          helm repo add vernemq https://vernemq.github.io/docker-vernemq
          helm install vernemq vernemq/vernemq --values ./base/vernemq/vernemq-values.yaml -n vernemq --create-namespace --wait --timeout=10m
          kubectl get pods -A -o wide
          kubectl exec --namespace vernemq vernemq-0 -- /vernemq/bin/vmq-admin cluster show
      - name: Install Vault
        id: install-vault
        run: |
          kubectl create namespace kerberos-vault
          kubectl apply -f ./base/vault/mongodb-configmap.yaml -n kerberos-vault
          kubectl apply -f ./base/vault/kerberos-vault-deployment.yaml -n kerberos-vault
          kubectl apply -f ./base/vault/kerberos-vault-service.yaml -n kerberos-vault
          sleep 10
          kubectl wait --for=condition=available deployment/vault -n kerberos-vault --timeout=300s
          kubectl get pods -A -o wide 
          kubectl get pods -A -o wide | grep vault | awk '{print $3}' | grep -q '1/1' && echo "vault pod is running with status 1/1" || (echo "vault pod is not running with status 1/1" && exit 1)
      - name: Install Agent
        id: install-agent
        run: |
          kubectl create namespace kerberos-agent
          kubectl apply -f ./base/agent/kerberos-agent-deployment.yaml -n kerberos-agent
          kubectl wait --for=condition=available deployment/agent1 -n kerberos-agent --timeout=300s
          kubectl get pods -A -o wide
          kubectl get pods -A -o wide | grep agent1 | awk '{print $3}' | grep -q '1/1' && echo "agent1 pod is running with status 1/1" || (echo "agent1 pod is not running with status 1/1" && exit 1)
      - name: Install Hub Helm chart
        id: install-hub
        run: |
          helm repo add kerberos https://charts.kerberos.io
          kubectl create namespace kerberos-hub
          helm install hub kerberos/hub --values ./base/hub/kerberos-hub-values.yaml -n kerberos-hub --create-namespace
          sleep 10
          kubectl wait --for=condition=available deployment/admin -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/hub-api -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/hub-cleanup -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/hub-frontend -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/hub-frontend-demo -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/hub-monitor-device -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/hub-reactivate-subscription -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-analysis -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-counting -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-dominantcolor -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-event -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-export -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-monitor -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-notify -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-notify-test -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-sequence -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-sprite -n kerberos-hub --timeout=300s
          kubectl wait --for=condition=available deployment/pipe-throttler -n kerberos-hub --timeout=300s
          kubectl get pods -A -o wide
      - name: Send Slack Notification on Success
        if: success()
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        run: |
          curl -X POST -H 'Content-type: application/json' --data "{
            \"text\": \"✅ MicroK8s Deployment Successful! :rocket:\n*OS*: ${{ matrix.os }}\n*MicroK8s Version*: ${{ matrix.microk8s }}\n*Workflow*: ${{ github.workflow }}\n*Triggered by*: ${{ github.actor }}\n*Repository*: ${{ github.repository }}\"
          }" $SLACK_WEBHOOK_URL
      - name: Send Slack Notification on Failure
        if: failure()
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        run: |
          curl -X POST -H 'Content-type: application/json' --data "{
            \"text\": \"❌ MicroK8s Deployment Failed! :x:\n*OS*: ${{ matrix.os }}\n*MicroK8s Version*: ${{ matrix.microk8s }}\n*Workflow*: ${{ github.workflow }}\n*Triggered by*: ${{ github.actor }}\n*Repository*: ${{ github.repository }}\n*Run URL*: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\"
          }" $SLACK_WEBHOOK_URL
          