set fallback := true

export K8S_CONTEXT := env("K8S_CONTEXT", "")
export SERVER_IP := env("K3S_SERVER_IP","192.168.178.45")
export USER := env("K3S_USER","basti")


[private]
default:
    @just --list --unsorted --list-submodules


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

    METALLB_VERSION="v0.15.3"

    username=$(gum input --prompt="SSH username: " --value="${USER}" --width=100)
    context=""
    if gum confirm "Update KUBECONFIG?"; then
        context=$(
            gum input --prompt="Context name: " --value="${K8S_CONTEXT}" --width=100
        )
    fi

    if [ -n "${context}" ]; then
        kubectl config use-context "${context}"
    fi

    kubectl apply -f "https://raw.githubusercontent.com/metallb/metallb/${METALLB_VERSION}/config/manifests/metallb-native.yaml"
    gum spin --spinner dot --title "Waiting for MetalLB to be ready..." -- kubectl wait --namespace metallb-system --for=condition=available deployment --all --timeout=120s
    echo "MetalLB ${METALLB_VERSION} installed successfully."

    gomplate -f address-pool.gomplate.yaml | kubectl apply -f -
    echo "Address pool configured."

    kubectl apply -f advertisement.yaml
    echo "Advertisement created."

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

    kubectl get namespace metallb-system &>/dev/null && kubectl delete ns metallb-system

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

    kubectl apply -f test-deployment.yaml

    echo "Test deployment created. You can check the service with 'kubectl get svc nginx -o wide -n test'."

    echo "To clean up, run 'just test-deployment-cleanup'."

test-deployment-cleanup:
    #!/bin/bash
    set -euo pipefail
    just env::check

    kubectl delete -f test-deployment.yaml
    echo "Test deployment and service deleted."
