#!/bin/bash

CLUSTERS=""

echo "Cluster;Docker;K8S;Rancher;Metallb;Longhorn;Monitoring" > /tmp/k8s-matrix-global.csv
echo "Cluster;Prometheus;Vault;Authelia;Gitlab_Runners;Kube-Fledged;Cert-Manager" > /tmp/k8s-matrix-detail_versions.csv
echo "Cluster;Prometheus;Vault;Authelia;Gitlab_Runners;Kube-Fledged;Cert-Manager" > /tmp/k8s-matrix-detail_helms.csv

for cluster in $CLUSTERS ; do
    shopt -s expand_aliases
    export KUBECONFIG="/slashroot/home/jarchina/.kube/config_${cluster}"

    KUBECTL_VERSION=$(grep go_$cluster ~/.bashrc | cut -d "'" -f 2)
    HELM_VERSION=$(grep go_$cluster ~/.bashrc | cut -d "'" -f 6)

    alias helm="/usr/local/bin/$HELM_VERSION"
    alias kubectl="/usr/local/bin/$KUBECTL_VERSION"

    docker_version=$(clush -N -w @$cluster "rpm -qa | grep ^docker-ce-[0-9] | cut -d "-" -f 3" | sort | uniq)

    rancher_version=$(helm ls -n cattle-system -f '^rancher$' -o json | jq -r .[].app_version)

    metallb_version=$(kubectl get pods -n metallb-system -l component=controller -o json | jq -r .items[].spec.containers[].image | cut -d ":" -f 2)

    longhorn_version=$(helm ls -n longhorn-system -f '^longhorn$' -o json | jq -r .[].app_version)

    k8s_version=$(kubectl version -o json | jq -r .serverVersion.gitVersion)

    cert_manager_version=$(helm ls -n cert-manager -f '^cert-manager$' -o json  | jq -r .[].app_version)
    cert_manager_helm_version=$(helm ls -n cert-manager -f '^cert-manager$' -o json  | jq -r .[].chart | cut -d "-" -f 3)

    vault_version=$(helm ls -n infra-vault -f '^infra-vault-injector$' -o json  | jq -r .[].app_version)
    vault_helm_version=$(helm ls -n infra-vault -f '^infra-vault-injector$' -o json  | jq -r .[].chart | cut -d "-" -f 2)

    monitoring_version=$(helm ls -n cattle-monitoring-system -f '^rancher-monitoring$' -o json  | jq -r .[].chart | cut -d "-" -f 3)

    prometheus_version=$(helm ls -n infra-monitoring -f '^infra-prometheus$' -o json  | jq -r .[].app_version)
    prometheus_helm_version=$(helm ls -n infra-monitoring -f '^infra-prometheus$' -o json  | jq -r .[].chart | cut -d "-" -f 2)

    authelia_version=$(helm ls -n infra-authelia -f '^infra-authelia$' -o json  | jq -r .[].app_version)
    authelia_helm_version=$(helm ls -n infra-authelia -f '^infra-authelia$' -o json  | jq -r .[].chart | cut -d "-" -f 2)

    gitlab_runners_helm_versions=$(helm ls -n infra-gitlab-runners | grep gitlab-runner | awk '{print $9}' | uniq | cut -d "-" -f 3)
    gitlab_runners_versions=$(helm ls -n infra-gitlab-runners | grep gitlab-runner | awk '{print $10}' | uniq)


    kubefledged_version=$(helm ls -n infra-kube-fledged -f '^infra-kube-fledged$' -o json  | jq -r .[].app_version)
    kubefledged_helm_version=$(helm ls -n infra-kube-fledged -f '^infra-kube-fledged$' -o json  | jq -r .[].chart | cut -d "-" -f 3)

    echo "$cluster;"$docker_version";$k8s_version;$rancher_version;$metallb_version;$longhorn_version;$monitoring_version" >> /tmp/k8s-matrix-global.csv
    echo "$cluster;$prometheus_version;$vault_version;$authelia_version;"$gitlab_runners_versions";$kubefledged_version;$cert_manager_version" >> /tmp/k8s-matrix-detail_versions.csv
    echo "$cluster;$prometheus_helm_version;$vault_helm_version;$authelia_helm_version;"$gitlab_runners_helm_versions";$kubefledged_helm_version;$cert_manager_helm_version" >> /tmp/k8s-matrix-detail_helms.csv

done
echo ""
echo "----- Global -----"
echo ""
python3 -c "import sys,prettytable; print(prettytable.from_csv(sys.stdin))" < /tmp/k8s-matrix-global.csv
echo ""
echo "----- Details -----"
echo ""
echo "Versions des logiciels"
echo ""
python3 -c "import sys,prettytable; print(prettytable.from_csv(sys.stdin))" < /tmp/k8s-matrix-detail_versions.csv
echo ""
echo "Versions des helms"
echo ""
python3 -c "import sys,prettytable; print(prettytable.from_csv(sys.stdin))" < /tmp/k8s-matrix-detail_helms.csv
