set fallback:=true

export LONGHORN_NAMESPACE := env("LONGHORN_NAMESPACE","longhorn-system")
export LONGHORN_VERSION := env("LONGHORN_VERSION","1.10.1")

add-helm-repo:
    helm repo add longhorn https://charts.longhorn.io --force-update
    helm repo update

# Delete namespace
delete-namespace:
    #!/bin/bash
    set -euo pipefail
    if kubectl get namespace ${LONGHORN_NAMESPACE} &>/dev/null; then
        kubectl delete namespace ${LONGHORN_NAMESPACE} --ignore-not-found
    else
        echo "Namespace ${LONGHORN_NAMESPACE} does not exist."
    fi


install:
    #!/bin/bash
    set -euo pipefail
    just env::check

    just add-helm-repo

    helm upgrade longhorn longhorn/longhorn \
      --install \
      --cleanup-on-fail \
      --namespace ${LONGHORN_NAMESPACE} \
      --create-namespace \
      --version ${LONGHORN_VERSION} \
      --values longhorn-values.yaml

    # remove default storage class annotation from local-path storage class
    kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'

uninstall:
    #!/bin/bash
    set -euo pipefail

    for crd in $(kubectl get crd -o name | grep longhorn); do
        kubectl patch $crd -p '{"metadata":{"finalizers":[]}}' --type=merge
    done

    kubectl -n ${LONGHORN_NAMESPACE} patch -p '{"value": "true"}' --type=merge lhs deleting-confirmation-flag || true

    helm uninstall longhorn --namespace ${LONGHORN_NAMESPACE} || true
    just delete-namespace


install-dashboard-ingress:
    #!/bin/bash
    set -euo pipefail
    just env::check

    echo "Deploying Longhorn Dashboard Ingress with EXTERNAL_DOMAIN=${EXTERNAL_DOMAIN}"
    gomplate -f longhorn-certificate-gomplate.yaml | kubectl apply -f -
    gomplate -f longhorn-ingressroute-gomplate.yaml | kubectl apply -f -

uninstall-dashboard-ingress:
    #!/bin/bash
    set -euo pipefail

    kubectl delete -f longhorn-ingressroute-gomplate.yaml || true
    kubectl delete -f longhorn-certificate-gomplate.yaml || true