## Kafka Operator & Cluster Management
## HA Kafka кластер через Strimzi Operator

# Include common variables and functions
include common/Makefile

# Kafka configuration
# Using common namespace variables from common/Makefile
KAFKA_HELMFILE_OPERATOR := kafka/helmfile-operator.yaml
KAFKA_HELMFILE_CLUSTER := kafka/helmfile-cluster.yaml

.PHONY: kafka-help
kafka-help:
	@echo ""
	@echo "$(COLOR_BOLD)Kafka Commands:$(COLOR_RESET)"
	@echo "  make kafka-install-operator   - Install Strimzi Kafka Operator"
	@echo "  make kafka-update-operator    - Update Strimzi Kafka Operator config"
	@echo "  make kafka-uninstall-operator - Uninstall operator (keeps clusters)"
	@echo "  make kafka-create-cluster     - Create HA Kafka cluster (kafka-cluster)"
	@echo "  make kafka-update-cluster     - Update Kafka cluster configuration"
	@echo "  make kafka-delete-cluster     - Delete kafka-cluster (keeps operator)"
	@echo "  make kafka-install-ui        - Install Kafka UI"
	@echo "  make kafka-status             - Check Kafka operator and cluster status"
	@echo "  make kafka-check-secret       - Check KafkaUser secret status"
	@echo "  make kafka-uninstall          - Uninstall Kafka operator and clusters"
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Before installing, prepare nodes:"
	@echo "  cd ../.. && make k8s-prepare-nodes"

.PHONY: kafka-check-longhorn
kafka-check-longhorn:
	$(call check_longhorn)

.PHONY: kafka-install-operator
kafka-install-operator: install-namespaces kafka-check-longhorn
	$(call print_info,Creating Kafka namespaces...)
	@$(KUBECTL) apply -f kafka/manifests/tech-kafka-operator.yaml
	@$(KUBECTL) apply -f kafka/manifests/tech-kafka-clusters.yaml
	$(call print_info,Installing Strimzi Kafka Operator via helmfile...)
	$(call run_helmfile,$(KAFKA_HELMFILE_OPERATOR),sync)
	$(call print_info,Waiting for operator to be ready...)
	@$(KUBECTL) -n $(KAFKA_OPERATOR_NAMESPACE) rollout status deployment/strimzi-cluster-operator --timeout=5m || true
	$(call print_success,Kafka Operator installed!)

.PHONY: kafka-update-operator
kafka-update-operator:
	$(call print_info,Updating Strimzi Kafka Operator configuration via helmfile...)
	$(call run_helmfile,$(KAFKA_HELMFILE_OPERATOR),sync)
	$(call print_info,Waiting for operator to restart...)
	@$(KUBECTL) -n $(KAFKA_OPERATOR_NAMESPACE) rollout status deployment/strimzi-cluster-operator --timeout=3m || true
	$(call print_success,Kafka Operator updated!)

.PHONY: kafka-uninstall-operator
kafka-uninstall-operator:
	$(call print_warning,This will uninstall Kafka Operator!)
	@echo "Note: Kafka clusters will NOT be deleted."
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Uninstalling operator...)
	$(call run_helmfile,$(KAFKA_HELMFILE_OPERATOR),destroy) || true
	$(call print_success,Operator uninstalled. Clusters remain.)

.PHONY: kafka-create-cluster
kafka-create-cluster: kafka-check-longhorn
	$(call print_info,Applying Kafka resource policies...)
	@$(KUBECTL) apply -f kafka/manifests/limitrange.yaml
	@$(KUBECTL) apply -f kafka/manifests/resourcequota.yaml
	$(call print_info,Creating StorageClass for Kafka...)
	@$(KUBECTL) apply -f kafka/manifests/storageclass.yaml
	$(call print_info,Creating Kafka cluster kafka-cluster...)
	@$(KUBECTL) apply -f kafka/manifests/cluster/kafka-operator/kafka-cluster.yaml
	@echo ""
	@echo "$(COLOR_YELLOW)Waiting for cluster to be ready (this may take 5-10 minutes)...$(COLOR_RESET)"
	@echo "Monitor with: make kafka-status"
	@echo ""
	@echo "$(COLOR_BOLD)Checking initial status...$(COLOR_RESET)"
ifeq ($(DETECTED_OS),Windows)
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get kafka kafka-cluster -o jsonpath='{.status.conditions}' 2>nul || echo "Cluster is being created..."
else
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get kafka kafka-cluster -o jsonpath='{.status.conditions}' 2>/dev/null || echo "Cluster is being created..."
endif
	$(call sleep_seconds,30)
	@echo ""
	@echo "$(COLOR_BOLD)Current pods status:$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get pods -l strimzi.io/cluster=kafka-cluster || echo "Pods are being created..."
	@echo ""
	@echo "$(COLOR_YELLOW)Waiting for cluster to be ready...$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) wait --for=condition=ready kafka kafka-cluster --timeout=15m || \
		($(call print_warning,Cluster creation timeout. Check status with: make kafka-status) && exit 1)
	$(call print_success,Kafka cluster created!)

.PHONY: kafka-update-cluster
kafka-update-cluster:
	$(call print_info,Updating Kafka resource policies...)
	@$(KUBECTL) apply -f kafka/manifests/limitrange.yaml
	@$(KUBECTL) apply -f kafka/manifests/resourcequota.yaml
	@$(KUBECTL) apply -f kafka/manifests/storageclass.yaml
	$(call print_info,Updating Kafka cluster kafka-cluster...)
	@$(KUBECTL) apply -f kafka/manifests/cluster/kafka-operator/kafka-cluster.yaml
	@echo ""
	@echo "$(COLOR_YELLOW)Waiting for cluster to be ready (this may take 5-10 minutes)...$(COLOR_RESET)"
	@echo "Monitor with: make kafka-status"
	$(call sleep_seconds,30)
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) wait --for=condition=ready kafka kafka-cluster --timeout=15m || \
		($(call print_warning,Cluster update timeout. Check status with: make kafka-status) && exit 1)
	$(call print_success,Kafka cluster updated!)

.PHONY: kafka-install-ui
kafka-install-ui:
	@echo ""
	@echo "$(COLOR_YELLOW)Checking if KafkaUser secret exists...$(COLOR_RESET)"
ifeq ($(DETECTED_OS),Windows)
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get secret kafka-cluster-user >nul 2>&1 && \
		($(call print_success,Secret found) && echo "") || \
		($(call print_warning,KafkaUser secret not found. Kafka UI will start but may not connect until secret is created.) && \
		 $(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get kafkauser kafka-cluster-user 2>nul || echo "KafkaUser not found" && \
		 echo "" && \
		 echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Kafka UI will wait for the secret to be created." && \
		 echo "$(COLOR_YELLOW)Check status: make kafka-check-secret$(COLOR_RESET)" && \
		 echo "")
else
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get secret kafka-cluster-user >/dev/null 2>&1 && \
		($(call print_success,Secret found) && echo "") || \
		($(call print_warning,KafkaUser secret not found. Kafka UI will start but may not connect until secret is created.) && \
		 $(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get kafkauser kafka-cluster-user 2>/dev/null || echo "KafkaUser not found" && \
		 echo "" && \
		 echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Kafka UI will wait for the secret to be created." && \
		 echo "$(COLOR_YELLOW)Check status: make kafka-check-secret$(COLOR_RESET)" && \
		 echo "")
endif
	$(call print_info,Installing Kafka UI...)
	@$(KUBECTL) apply -f kafka/manifests/cluster/kafka-operator/kafka-ui.yaml
	$(call print_info,Waiting for Kafka UI to be ready...)
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) rollout status deployment/kafka-ui --timeout=3m || true
ifeq ($(DETECTED_OS),Windows)
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get secret kafka-cluster-user >nul 2>&1 || \
		echo "$(COLOR_YELLOW)Warning:$(COLOR_RESET) Secret not found. Kafka UI may not be able to connect. Check with: make kafka-check-secret"
else
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get secret kafka-cluster-user >/dev/null 2>&1 || \
		echo "$(COLOR_YELLOW)Warning:$(COLOR_RESET) Secret not found. Kafka UI may not be able to connect. Check with: make kafka-check-secret"
endif
	$(call print_success,Kafka UI installed!)

.PHONY: kafka-delete-cluster
kafka-delete-cluster:
	$(call print_warning,This will delete the kafka-cluster and its data!)
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Deleting KafkaUser...)
	-@$(KUBECTL) delete kafkauser kafka-cluster-user -n $(KAFKA_CLUSTERS_NAMESPACE) || true
	$(call print_info,Deleting Kafka UI...)
	-@$(KUBECTL) delete deployment kafka-ui -n $(KAFKA_CLUSTERS_NAMESPACE) || true
	-@$(KUBECTL) delete svc kafka-ui -n $(KAFKA_CLUSTERS_NAMESPACE) || true
	$(call print_info,Deleting kafka-cluster CR and NodePools...)
	-@$(KUBECTL) delete kafka kafka-cluster -n $(KAFKA_CLUSTERS_NAMESPACE) || true
	-@$(KUBECTL) delete kafkanodepool controllers brokers -n $(KAFKA_CLUSTERS_NAMESPACE) || true
	-@$(KUBECTL) delete kafkanodepool --all -n $(KAFKA_CLUSTERS_NAMESPACE) 2>/dev/null || true
	$(call print_info,Waiting for CRs to be deleted...)
	$(call sleep_seconds,15)
	$(call print_info,Deleting StrimziPodSet and pods if still exist...)
	-@$(KUBECTL) delete strimzipodset -n $(KAFKA_CLUSTERS_NAMESPACE) -l strimzi.io/cluster=kafka-cluster --force --grace-period=0 2>/dev/null || true
	-@$(KUBECTL) delete pods -n $(KAFKA_CLUSTERS_NAMESPACE) -l strimzi.io/cluster=kafka-cluster --force --grace-period=0 2>/dev/null || true
	$(call sleep_seconds,5)
	$(call print_info,Deleting kafka-cluster PVCs (if any)...)
	-@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) delete pvc -l strimzi.io/cluster=kafka-cluster || true
	$(call print_info,Deleting secrets...)
	-@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) delete secret kafka-cluster-user || true
	$(call print_info,Deleting cluster resource policies...)
	-@$(KUBECTL) delete -f kafka/manifests/limitrange.yaml || true
	-@$(KUBECTL) delete -f kafka/manifests/resourcequota.yaml || true
	-@$(KUBECTL) delete -f kafka/manifests/storageclass.yaml || true
	$(call print_success,kafka-cluster deleted.)

.PHONY: kafka-status
kafka-status:
	@echo ""
	@echo "$(COLOR_BOLD)=== Kafka Operator Status ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_OPERATOR_NAMESPACE) get pods,svc
	@echo ""
	@echo "$(COLOR_BOLD)=== Kafka Clusters (CRs) ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get kafka || echo "No clusters found"
	@echo ""
	@echo "$(COLOR_BOLD)=== kafka-cluster Pods ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get pods -l strimzi.io/cluster=kafka-cluster -o wide || echo "Cluster kafka-cluster not created yet"
	@echo ""
	@echo "$(COLOR_BOLD)=== kafka-cluster Services ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get svc -l strimzi.io/cluster=kafka-cluster || true
	@echo ""
	@echo "$(COLOR_BOLD)=== Kafka UI ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get pods,svc -l app=kafka-ui || echo "Kafka UI not installed"
	@echo ""

.PHONY: kafka-check-secret
kafka-check-secret:
	@echo ""
	@echo "$(COLOR_BOLD)=== KafkaUser Status ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get kafkauser kafka-cluster-user || echo "KafkaUser not found"
	@echo ""
	@echo "$(COLOR_BOLD)=== KafkaUser Secret ===$(COLOR_RESET)"
ifeq ($(DETECTED_OS),Windows)
	@powershell -NoProfile -Command "$$curDir = [System.IO.Directory]::GetCurrentDirectory(); $$kubeconfig = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($$curDir, '$(KUBECONFIG_REL)')); $$env:KUBECONFIG = $$kubeconfig; $$result = kubectl -n $(KAFKA_CLUSTERS_NAMESPACE) get secret kafka-cluster-user 2>&1; if ($$LASTEXITCODE -eq 0) { Write-Host '$(COLOR_GREEN)$(COLOR_BOLD)✓ Secret kafka-cluster-user exists$(COLOR_RESET)'; Write-Host '$(COLOR_YELLOW)Secret keys:$(COLOR_RESET)'; $$json = kubectl -n $(KAFKA_CLUSTERS_NAMESPACE) get secret kafka-cluster-user -o json | ConvertFrom-Json; $$json.data.PSObject.Properties.Name | ForEach-Object { Write-Host $$_ } } else { Write-Host '$(COLOR_YELLOW)$(COLOR_BOLD)⚠ Secret kafka-cluster-user not found$(COLOR_RESET)'; Write-Host '$(COLOR_YELLOW)KafkaUser may still be processed by operator.$(COLOR_RESET)'; Write-Host '$(COLOR_YELLOW)Check KafkaUser status: kubectl -n $(KAFKA_CLUSTERS_NAMESPACE) describe kafkauser kafka-cluster-user$(COLOR_RESET)' }"
else
	@$(KUBECTL) -n $(KAFKA_CLUSTERS_NAMESPACE) get secret kafka-cluster-user >/dev/null 2>&1 && \
		($(call print_success,Secret kafka-cluster-user exists) && \
		 echo "$(COLOR_YELLOW)Secret keys:$(COLOR_RESET)" && \
		 $(call get_secret_keys,$(KAFKA_CLUSTERS_NAMESPACE),kafka-cluster-user)) || \
		($(call print_warning,Secret kafka-cluster-user not found) && \
		 echo "$(COLOR_YELLOW)KafkaUser may still be processed by operator.$(COLOR_RESET)" && \
		 echo "$(COLOR_YELLOW)Check KafkaUser status: kubectl -n $(KAFKA_CLUSTERS_NAMESPACE) describe kafkauser kafka-cluster-user$(COLOR_RESET)")
endif
	@echo ""

.PHONY: kafka-uninstall
kafka-uninstall:
	$(call print_warning,This will delete ALL Kafka clusters and data!)
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Deleting Kafka clusters...)
	-@$(KUBECTL) delete kafka --all -n $(KAFKA_CLUSTERS_NAMESPACE) || true
	-@$(KUBECTL) delete kafkausers --all -n $(KAFKA_CLUSTERS_NAMESPACE) || true
	$(call print_info,Uninstalling Kafka UI...)
	-@$(KUBECTL) delete -f kafka/manifests/cluster/kafka-operator/kafka-ui.yaml || true
	$(call print_info,Uninstalling operator...)
	$(call run_helmfile,$(KAFKA_HELMFILE_OPERATOR),destroy) || true
	$(call print_success,Kafka uninstalled.)
