## Kubernetes Dashboard Management
## Kubernetes Dashboard через официальный Helm chart

# Include common variables and functions
include common/Makefile

# Kubernetes Dashboard configuration
DASHBOARD_NAMESPACE := tech-dashboard

.PHONY: dashboard-help
dashboard-help:
	@echo ""
	@echo "$(COLOR_BOLD)Kubernetes Dashboard Commands:$(COLOR_RESET)"
	@echo "  make dashboard-install         - Install Kubernetes Dashboard"
	@echo "  make dashboard-update         - Update Kubernetes Dashboard"
	@echo "  make dashboard-uninstall       - Uninstall Kubernetes Dashboard"
	@echo "  make dashboard-status          - Check Kubernetes Dashboard status"
	@echo "  make dashboard-create-token    - Create service account and get access token"
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Before installing, prepare nodes:"
	@echo "  cd ../.. && make k8s-prepare-nodes"

.PHONY: dashboard-install
dashboard-install: install-namespaces
	$(call print_info,Creating namespace $(DASHBOARD_NAMESPACE)...)
	@$(KUBECTL) apply -f k8s-dashboard/manifests/namespace.yaml
	$(call print_info,Installing Kubernetes Dashboard via helmfile...)
	$(call run_helmfile,k8s-dashboard/helmfile.yaml,sync)
	$(call print_info,Waiting for Kubernetes Dashboard to be ready...)
	$(call sleep_seconds,30)
	@$(KUBECTL) -n $(DASHBOARD_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=kubernetes-dashboard-web --timeout=10m || \
		$(KUBECTL) -n $(DASHBOARD_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=kubernetes-dashboard --timeout=10m || \
		$(KUBECTL) -n $(DASHBOARD_NAMESPACE) wait --for=condition=ready pod -l k8s-app=kubernetes-dashboard --timeout=10m || true
	$(call print_info,Configuring RBAC permissions for Dashboard...)
	@$(KUBECTL) apply -f k8s-dashboard/manifests/dashboard/dashboard-rbac.yaml
	$(call print_success,Kubernetes Dashboard installed!)
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Create access token: make dashboard-create-token"

.PHONY: dashboard-update
dashboard-update:
	$(call print_info,Updating Kubernetes Dashboard via helmfile...)
	$(call run_helmfile,k8s-dashboard/helmfile.yaml,sync)
	$(call print_info,Waiting for Kubernetes Dashboard to be ready...)
	$(call sleep_seconds,30)
	@$(KUBECTL) -n $(DASHBOARD_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=kubernetes-dashboard-web --timeout=10m || \
		$(KUBECTL) -n $(DASHBOARD_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=kubernetes-dashboard --timeout=10m || \
		$(KUBECTL) -n $(DASHBOARD_NAMESPACE) wait --for=condition=ready pod -l k8s-app=kubernetes-dashboard --timeout=10m || true
	$(call print_info,Ensuring RBAC permissions for Dashboard...)
	@$(KUBECTL) apply -f k8s-dashboard/manifests/dashboard/dashboard-rbac.yaml
	$(call print_success,Kubernetes Dashboard updated!)

.PHONY: dashboard-uninstall
dashboard-uninstall:
	$(call print_warning,This will uninstall Kubernetes Dashboard!)
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Uninstalling Kubernetes Dashboard...)
	@$(HELM) uninstall kubernetes-dashboard -n $(DASHBOARD_NAMESPACE) || \
		($(call print_warning,Helm release not found or already uninstalled.) && exit 0)
	$(call print_success,Kubernetes Dashboard uninstalled.)

.PHONY: dashboard-status
dashboard-status:
	@echo ""
	@echo "$(COLOR_BOLD)=== Kubernetes Dashboard Helm Release ===$(COLOR_RESET)"
	@$(HELM) list -n $(DASHBOARD_NAMESPACE) || echo "No Helm release found. Install with: make dashboard-install"
	@echo ""
	@echo "$(COLOR_BOLD)=== Kubernetes Dashboard Pods ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(DASHBOARD_NAMESPACE) get pods -o wide || echo "No pods found. Install with: make dashboard-install"
	@echo ""
	@echo "$(COLOR_BOLD)=== Kubernetes Dashboard Services ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(DASHBOARD_NAMESPACE) get svc || echo "No services found. Install with: make dashboard-install"
	@echo ""
	@echo "$(COLOR_BOLD)=== Kubernetes Dashboard Service Accounts ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(DASHBOARD_NAMESPACE) get sa || echo "No service accounts found"
	@echo ""
	@echo "$(COLOR_BOLD)=== Kubernetes Dashboard Pod Logs (last 20 lines) ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(DASHBOARD_NAMESPACE) logs -l app.kubernetes.io/name=kubernetes-dashboard-web --tail=20 2>&1 || \
		$(KUBECTL) -n $(DASHBOARD_NAMESPACE) logs -l app.kubernetes.io/name=kubernetes-dashboard --tail=20 2>&1 || \
		$(KUBECTL) -n $(DASHBOARD_NAMESPACE) logs -l k8s-app=kubernetes-dashboard --tail=20 2>&1 || \
		echo "No logs available"

.PHONY: dashboard-create-token
dashboard-create-token:
	$(call print_info,Creating service account and cluster role binding for Dashboard access...)
	@$(KUBECTL) apply -f k8s-dashboard/manifests/dashboard/dashboard-admin.yaml
	$(call sleep_seconds,3)
	$(call print_info,Getting access token...)
	@bash k8s-dashboard/scripts/create-token.sh $(DASHBOARD_NAMESPACE) $(KUBECONFIG) || \
		($(call print_warning,Error creating token. Check service account and secret.) && exit 1)
	$(call print_success,Token created! Use it to login to Dashboard.)
