Commit 423a7b37 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

CI end-to-end tests:

- Foxed microk8s preparation
parent 14af1bb3
Loading
Loading
Loading
Loading
+47 −13
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ MICROK8S_CMD="${MICROK8S_CMD:-microk8s}"
KUBECTL_CMD="${KUBECTL_CMD:-kubectl}"
HELM_CMD="${HELM_CMD:-helm3}"
CI_RESET_MICROK8S_STORAGE_REGISTRY="${CI_RESET_MICROK8S_STORAGE_REGISTRY:-yes}"
CI_RESET_MICROK8S_STORAGE_ADDON="${CI_RESET_MICROK8S_STORAGE_ADDON:-auto}"
CI_K8S_NAMESPACES_PATTERN="${CI_K8S_NAMESPACES_PATTERN:-^(tfs|crdb|qdb|kafka|nats)}"
CI_MICROK8S_WAIT_ATTEMPTS="${CI_MICROK8S_WAIT_ATTEMPTS:-30}"
CI_MICROK8S_WAIT_SLEEP="${CI_MICROK8S_WAIT_SLEEP:-2}"
@@ -48,10 +49,11 @@ delete_pods_by_phase() {
wait_for_addon_pods() {
    local namespace=$1
    local label=$2
    if ${KUBECTL_CMD} get namespace "${namespace}" >/dev/null 2>&1; then
    if ! ${KUBECTL_CMD} get namespace "${namespace}" >/dev/null 2>&1; then
        return 1
    fi
    ${KUBECTL_CMD} wait --namespace "${namespace}" --for=condition=Ready pod \
        --selector "${label}" --timeout="${CI_MICROK8S_ADDON_WAIT_TIMEOUT}"
    fi
}

wait_for_storage_ready() {
@@ -72,6 +74,21 @@ wait_for_registry_ready() {
    return 1
}

can_reset_storage_addon() {
    case "${CI_RESET_MICROK8S_STORAGE_ADDON}" in
        yes) return 0 ;;
        no) return 1 ;;
        auto)
            sudo -n true >/dev/null 2>&1
            return $?
            ;;
        *)
            echo "Invalid CI_RESET_MICROK8S_STORAGE_ADDON=${CI_RESET_MICROK8S_STORAGE_ADDON}; use auto, yes, or no." >&2
            return 1
            ;;
    esac
}

echo "Checking MicroK8s readiness..."
${MICROK8S_CMD} status --wait-ready
wait_for_kubectl
@@ -114,24 +131,41 @@ if [[ "${CI_RESET_MICROK8S_STORAGE_REGISTRY}" != "yes" ]]; then
    exit 0
fi

echo "Disabling MicroK8s registry and storage addons..."
echo "Disabling MicroK8s registry addon..."
${MICROK8S_CMD} disable registry || true

RESET_STORAGE_ADDON="no"
if can_reset_storage_addon; then
    RESET_STORAGE_ADDON="yes"
    echo "Disabling MicroK8s storage addon and destroying storage..."
    ${MICROK8S_CMD} disable hostpath-storage:destroy-storage || \
        ${MICROK8S_CMD} disable storage:destroy-storage || \
        ${MICROK8S_CMD} disable hostpath-storage --destroy-storage || \
        ${MICROK8S_CMD} disable storage --destroy-storage || \
        ${MICROK8S_CMD} disable hostpath-storage || \
        ${MICROK8S_CMD} disable storage || true
else
    echo "Skipping MicroK8s storage addon reset; non-interactive sudo is not available."
    echo "Set CI_RESET_MICROK8S_STORAGE_ADDON=yes only on runners where MicroK8s can recreate hostpath storage non-interactively."
fi

echo "Deleting leftover hostpath provisioner pods and jobs..."
if [[ "${RESET_STORAGE_ADDON}" == "yes" ]]; then
    ${KUBECTL_CMD} delete pods --namespace kube-system --selector name=hostpath-provisioner --ignore-not-found || true
    ${KUBECTL_CMD} delete pods --namespace kube-system --selector app=hostpath-provisioner --ignore-not-found || true
    ${KUBECTL_CMD} delete pods --namespace kube-system --selector k8s-app=hostpath-provisioner --ignore-not-found || true
fi
${KUBECTL_CMD} get jobs --all-namespaces --no-headers \
    -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name |
    grep -E 'hostpath|provision' |
    xargs --no-run-if-empty --max-args=2 ${KUBECTL_CMD} delete job --namespace || true

echo "Re-enabling MicroK8s storage and registry addons..."
if [[ "${RESET_STORAGE_ADDON}" == "yes" ]]; then
    echo "Re-enabling MicroK8s storage addon..."
    ${MICROK8S_CMD} enable hostpath-storage || ${MICROK8S_CMD} enable storage
fi

echo "Re-enabling MicroK8s registry addon..."
${MICROK8S_CMD} enable registry

echo "Waiting for storage and registry pods..."