Skip to content

Commit 1f2eacb

Browse files
mprpicclaude
andcommitted
fix(RHOAIENG-52390): add kubectl context safety check
Adds a check-local-context prerequisite that verifies the current kubectl context points to a local cluster (kind- or minikube) before running targets that modify cluster state. Prevents accidentally applying manifests or restarting deployments on a remote cluster. Guarded targets: kind-rebuild, kind-port-forward, local-down, local-rebuild, local-reload-backend, local-reload-frontend, local-reload-operator, dev-bootstrap. Bypassable with SKIP_CONTEXT_CHECK=true for edge cases. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Martin Prpič <mprpic@redhat.com>
1 parent 9180a41 commit 1f2eacb

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

Makefile

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.PHONY: local-dev-token
44
.PHONY: local-logs local-logs-backend local-logs-frontend local-logs-operator local-shell local-shell-frontend
55
.PHONY: local-test local-test-dev local-test-quick test-all local-url local-troubleshoot local-port-forward local-stop-port-forward
6-
.PHONY: push-all registry-login setup-hooks remove-hooks lint check-minikube check-kind check-kubectl dev-bootstrap kind-rebuild
6+
.PHONY: push-all registry-login setup-hooks remove-hooks lint check-minikube check-kind check-kubectl check-local-context dev-bootstrap kind-rebuild
77
.PHONY: e2e-test e2e-setup e2e-clean deploy-langfuse-openshift
88
.PHONY: unleash-port-forward unleash-status
99
.PHONY: setup-minio minio-console minio-logs minio-status
@@ -320,7 +320,7 @@ local-up: check-minikube check-kubectl ## Start local development environment (m
320320
@echo " • Run: $(COLOR_BOLD)make local-status$(COLOR_RESET) to check deployment"
321321
@echo " • Run: $(COLOR_BOLD)make local-logs$(COLOR_RESET) to view logs"
322322

323-
local-down: check-kubectl ## Stop Ambient Code Platform (keep minikube running)
323+
local-down: check-kubectl check-local-context ## Stop Ambient Code Platform (keep minikube running)
324324
@echo "$(COLOR_BLUE)$(COLOR_RESET) Stopping Ambient Code Platform..."
325325
@$(MAKE) --no-print-directory local-stop-port-forward
326326
@kubectl delete namespace $(NAMESPACE) --ignore-not-found=true --timeout=60s
@@ -370,13 +370,13 @@ local-sync-version: ## Sync version from git to local deployment manifests
370370
rm -f components/manifests/minikube/frontend-deployment.yaml.bak && \
371371
echo " $(COLOR_GREEN)$(COLOR_RESET) Version synced to $$VERSION"
372372

373-
local-rebuild: ## Rebuild and reload all components
373+
local-rebuild: check-local-context ## Rebuild and reload all components
374374
@echo "$(COLOR_BOLD)🔄 Rebuilding all components...$(COLOR_RESET)"
375375
@$(MAKE) --no-print-directory _build-and-load
376376
@$(MAKE) --no-print-directory _restart-all
377377
@echo "$(COLOR_GREEN)$(COLOR_RESET) All components rebuilt and reloaded"
378378

379-
local-reload-backend: ## Rebuild and reload backend only
379+
local-reload-backend: check-local-context ## Rebuild and reload backend only
380380
@echo "$(COLOR_BLUE)$(COLOR_RESET) Rebuilding backend..."
381381
@cd components/backend && $(CONTAINER_ENGINE) build -t $(BACKEND_IMAGE) . >/dev/null 2>&1
382382
@$(CONTAINER_ENGINE) tag $(BACKEND_IMAGE) localhost/$(BACKEND_IMAGE) 2>/dev/null || true
@@ -399,7 +399,7 @@ local-reload-backend: ## Rebuild and reload backend only
399399
echo "$(COLOR_GREEN)$(COLOR_RESET) Backend port forward restarted"; \
400400
fi
401401

402-
local-reload-frontend: ## Rebuild and reload frontend only
402+
local-reload-frontend: check-local-context ## Rebuild and reload frontend only
403403
@echo "$(COLOR_BLUE)$(COLOR_RESET) Rebuilding frontend..."
404404
@cd components/frontend && $(CONTAINER_ENGINE) build -t $(FRONTEND_IMAGE) . >/dev/null 2>&1
405405
@$(CONTAINER_ENGINE) tag $(FRONTEND_IMAGE) localhost/$(FRONTEND_IMAGE) 2>/dev/null || true
@@ -423,7 +423,7 @@ local-reload-frontend: ## Rebuild and reload frontend only
423423
fi
424424

425425

426-
local-reload-operator: ## Rebuild and reload operator only
426+
local-reload-operator: check-local-context ## Rebuild and reload operator only
427427
@echo "$(COLOR_BLUE)$(COLOR_RESET) Rebuilding operator..."
428428
@cd components/operator && $(CONTAINER_ENGINE) build -t $(OPERATOR_IMAGE) . >/dev/null 2>&1
429429
@$(CONTAINER_ENGINE) tag $(OPERATOR_IMAGE) localhost/$(OPERATOR_IMAGE) 2>/dev/null || true
@@ -687,7 +687,7 @@ kind-down: ## Stop and delete kind cluster
687687
@cd e2e && CONTAINER_ENGINE=$(CONTAINER_ENGINE) ./scripts/cleanup.sh
688688
@echo "$(COLOR_GREEN)$(COLOR_RESET) Kind cluster deleted"
689689

690-
kind-port-forward: check-kubectl ## Port-forward kind services (for remote Podman)
690+
kind-port-forward: check-kubectl check-local-context ## Port-forward kind services (for remote Podman)
691691
@echo "$(COLOR_BOLD)🔌 Port forwarding kind services$(COLOR_RESET)"
692692
@echo ""
693693
@echo " Frontend: http://localhost:8080"
@@ -700,7 +700,7 @@ kind-port-forward: check-kubectl ## Port-forward kind services (for remote Podma
700700
(kubectl port-forward -n ambient-code svc/backend-service 8081:8080 >/dev/null 2>&1 &); \
701701
wait
702702

703-
dev-bootstrap: check-kubectl ## Bootstrap developer workspace with API key and integrations
703+
dev-bootstrap: check-kubectl check-local-context ## Bootstrap developer workspace with API key and integrations
704704
@./scripts/bootstrap-workspace.sh
705705

706706
##@ E2E Testing (Portable)
@@ -731,7 +731,7 @@ test-e2e-setup: ## Install e2e test dependencies
731731

732732
e2e-setup: test-e2e-setup ## Alias for test-e2e-setup (backward compatibility)
733733

734-
kind-rebuild: check-kind check-kubectl build-all ## Rebuild, reload, and restart all components in kind
734+
kind-rebuild: check-kind check-kubectl check-local-context build-all ## Rebuild, reload, and restart all components in kind
735735
@kind get clusters 2>/dev/null | grep -q '^ambient-local$$' || \
736736
(echo "$(COLOR_RED)$(COLOR_RESET) Kind cluster 'ambient-local' not found. Run 'make kind-up LOCAL_IMAGES=true' first." && exit 1)
737737
@echo "$(COLOR_BLUE)$(COLOR_RESET) Loading images into kind..."
@@ -791,6 +791,21 @@ check-kubectl: ## Check if kubectl is installed
791791
@command -v kubectl >/dev/null 2>&1 || \
792792
(echo "$(COLOR_RED)$(COLOR_RESET) kubectl not found. Install: https://kubernetes.io/docs/tasks/tools/" && exit 1)
793793

794+
check-local-context: ## Verify kubectl context points to a local cluster (kind or minikube)
795+
ifneq ($(SKIP_CONTEXT_CHECK),true)
796+
@ctx=$$(kubectl config current-context 2>/dev/null || echo ""); \
797+
if echo "$$ctx" | grep -qE '^(kind-|minikube$$)'; then \
798+
: ; \
799+
else \
800+
echo "$(COLOR_RED)✗$(COLOR_RESET) Current kubectl context '$$ctx' does not look like a local cluster."; \
801+
echo " Expected a context starting with 'kind-' or named 'minikube'."; \
802+
echo " Switch context first, e.g.: kubectl config use-context kind-ambient-local"; \
803+
echo ""; \
804+
echo " To bypass this check: make <target> SKIP_CONTEXT_CHECK=true"; \
805+
exit 1; \
806+
fi
807+
endif
808+
794809
check-architecture: ## Validate build architecture matches host
795810
@echo "$(COLOR_BOLD)Architecture Check$(COLOR_RESET)"
796811
@echo " Host: $(HOST_OS) / $(HOST_ARCH)"

0 commit comments

Comments
 (0)