Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(branch-profile): refuse stop and delete of a context minikube has no profile for

Alfredo Lopez1e5cfac72811
diff --git a/scripts/minikube-profiles/branch-profile.sh b/scripts/minikube-profiles/branch-profile.sh
index 130f6656b..c6620736f 100755
--- a/scripts/minikube-profiles/branch-profile.sh
+++ b/scripts/minikube-profiles/branch-profile.sh
@@ -656,7 +656,8 @@ load_context_identity() {
# shellcheck source=/dev/null
. "${CONTEXT_IDENTITY_SCRIPT}"
local function_name
- for function_name in kube_endpoint_host kube_endpoint_is_local minikube_nodes_identify_profile; do
+ for function_name in kube_endpoint_host kube_endpoint_is_local minikube_nodes_identify_profile \
+ minikube_profile_list_names; do
declare -F "${function_name}" >/dev/null ||
die "CONTEXT_IDENTITY_API_INVALID: missing ${function_name} in ${CONTEXT_IDENTITY_SCRIPT}"
done
@@ -692,6 +693,30 @@ require_local_context_endpoint() {
die "BRANCH_PROFILE_REMOTE_CONTEXT: kube context ${PROFILE} points at a non-local API server ($(kube_endpoint_host "${server}")); refusing to run minikube or kubectl against it. That context may belong to a cluster in use: stop and ask before renaming or removing it"
}
+# stop and delete address a profile that may not answer, so the node identity
+# check below cannot run for them. A local endpoint alone admits another local
+# cluster, or one on a private address, whose context carries the profile's
+# name, and `minikube -p <p> delete` for a profile minikube does not know
+# removes that context. So when a context named after the profile exists,
+# minikube must list the profile (valid or invalid) before either runs. An
+# unreadable profile list is a refusal, never an empty one. A missing context
+# is allowed for the reason given above require_local_context_endpoint.
+require_context_profile_known_to_minikube() {
+ load_context_identity
+ local server profiles status=0
+ server="$(kube_context_server "${PROFILE}")" || exit 1
+ [[ -n "${server}" ]] || return 0
+ profiles="$(run_bounded minikube-profile-list "${MINIKUBE_STATUS_TIMEOUT_SECONDS}" \
+ minikube profile list -o json)" ||
+ die "BRANCH_PROFILE_MINIKUBE_PROFILES_UNREADABLE: minikube profile list failed; refusing to run minikube -p ${PROFILE} while kube context ${PROFILE} exists"
+ minikube_profile_list_names "${profiles}" "${PROFILE}" || status=$?
+ case "${status}" in
+ 0) ;;
+ 1) die "BRANCH_PROFILE_UNKNOWN_MINIKUBE_PROFILE: minikube lists no profile ${PROFILE}, but kube context ${PROFILE} exists and points at $(kube_endpoint_host "${server}"); refusing to run minikube -p ${PROFILE}, which would act on that context. It may belong to another local cluster: stop and ask before renaming or removing it" ;;
+ *) die "BRANCH_PROFILE_MINIKUBE_PROFILES_UNREADABLE: minikube profile list printed no readable profile lists; refusing to run minikube -p ${PROFILE} while kube context ${PROFILE} exists" ;;
+ esac
+}
+
# A local endpoint is necessary but not sufficient: the context may name
# another local Minikube profile, or a LAN cluster on a private address. The
# cluster must have a node labelled minikube.k8s.io/name=<profile> at the IP
@@ -1294,6 +1319,7 @@ report_kept_pf_records() {
cmd_stop() {
require_existing_profile
require_local_context_endpoint
+ require_context_profile_known_to_minikube
# Clear the verified port-forward records first: a stopped cluster leaves
# records naming dead kubectl processes, and the next T2 preflight refuses
# them with PORT_FORWARD_CONFLICT. A record that cannot be verified is kept
@@ -1340,6 +1366,7 @@ cmd_delete() {
exit 1
fi
require_local_context_endpoint
+ require_context_profile_known_to_minikube
# The registry (pids/*.pid) outlives the cluster; clear the port-forward
# records so the deleted profile does not leave PORT_FORWARD_CONFLICT behind.
# As in cmd_stop, a kept record is reported and fails the command, but does
diff --git a/scripts/minikube/context-identity.sh b/scripts/minikube/context-identity.sh
index 61e4c2ab7..e9ef7827a 100644
--- a/scripts/minikube/context-identity.sh
+++ b/scripts/minikube/context-identity.sh
@@ -5,7 +5,8 @@
# (scripts/minikube-profiles/branch-profile.sh) so both apply the same rule.
# Every input is an argument, nothing is printed on a negative answer, and no
# function here contacts a cluster: callers fetch the kubeconfig endpoint, the
-# node list and the Minikube IP themselves, with their own timeouts.
+# node list, the Minikube IP and the Minikube profile list themselves, with
+# their own timeouts.
# kube_endpoint_host <server-url>
# Prints the lowercased host of an API server URL (brackets stripped from an
@@ -75,3 +76,46 @@ for node in payload.get("items", []):
raise SystemExit(1)
PY
}
+
+# minikube_profile_list_names <profile-list-json> <profile>
+# Answers whether `minikube profile list -o json` output lists <profile>, as a
+# valid or an invalid profile. Returns 0 when it does, 1 when it does not, and
+# 2 when the output is not {"invalid": [...], "valid": [...]} with an object
+# carrying a non-empty string "Name" in every entry: an error body, a missing
+# list or an unnamed entry cannot say which profiles exist, so it is never read
+# as "not listed".
+minikube_profile_list_names() {
+ local profiles_json="${1:-}" profile="${2:-}"
+ [[ -n "${profiles_json}" && -n "${profile}" ]] || return 2
+ python3 - "${profiles_json}" "${profile}" <<'PY'
+import json
+import sys
+
+
+def listed_names(payload):
+ if not isinstance(payload, dict):
+ return None
+ names = []
+ for key in ("valid", "invalid"):
+ entries = payload.get(key)
+ if not isinstance(entries, list):
+ return None
+ for entry in entries:
+ if not isinstance(entry, dict):
+ return None
+ name = entry.get("Name")
+ if not isinstance(name, str) or not name:
+ return None
+ names.append(name)
+ return names
+
+
+try:
+ names = listed_names(json.loads(sys.argv[1]))
+except (ValueError, RecursionError):
+ names = None
+if names is None:
+ raise SystemExit(2)
+raise SystemExit(0 if sys.argv[2] in names else 1)
+PY
+}