############################################################
# For the replicator example we need a larger source cluster
# to simulate a real world workload to sync to CCloud
GKE_BASE_NUM_NODES := 7
GKE_BASE_ZOOKEEPER_REPLICAS := 3
GKE_BASE_KAFKA_REPLICAS := 3
############################################################

REPLICATOR_GKE_CC_MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
REPLICATOR_GKE_CC_MKFILE_DIR := $(dir $(REPLICATOR_GKE_CC_MKFILE_PATH))

###############################################################################################
## The next two functions are requried to fill a minor gap in the Replicator helm chart where
## 	credentials cannot be specified in values but must be filled in last minute in a helm --set 
## 	argument.  These are used below in the replicator helm commands
CCLOUD_API_KEY = $(shell cat $(REPLICATOR_GKE_CC_MKFILE_DIR)cfg/my-values.yaml | grep username: | cut -d ':' -f2 | tr -d ' ')
CCLOUD_API_SECRET = $(shell cat $(REPLICATOR_GKE_CC_MKFILE_DIR)cfg/my-values.yaml | grep password: | cut -d ':' -f2 | tr -d ' ')
###############################################################################################

ADDITIONAL_HELM_FLAGS ?=
GKE_BASE_ADDITIONAL_HELM_FLAGS := -f $(REPLICATOR_GKE_CC_MKFILE_DIR)cfg/values.yaml -f $(REPLICATOR_GKE_CC_MKFILE_DIR)cfg/my-values.yaml $(ADDITIONAL_HELM_FLAGS)

include $(THIS_MKFILE_DIR)../gke-base/Makefile-impl

###### REPLICATOR MANAGEMENT ######
replicator-gke-cc-deploy-replicator: #_ Deploys Replicator into the configured k8s cluster
	@$(call echo_stdout_header,Deploy Replicator)
	@echo "helm upgrade --install --namespace operator --set replicator.enabled=true $(GKE_BASE_HELM_COMMON_FLAGS) --set global.sasl.plain.username=[redacted] --set global.sasl.plain.password=[redacted] replicator $(OPERATOR_PATH)helm/confluent-operator"
	@helm upgrade --install --namespace operator --set replicator.enabled=true $(GKE_BASE_HELM_COMMON_FLAGS) --set global.sasl.plain.username=$(CCLOUD_API_KEY) --set global.sasl.plain.password=$(CCLOUD_API_SECRET) replicator $(OPERATOR_PATH)helm/confluent-operator
	@$(call echo_stdout_footer_pass,Replicator deployed)

replicator-gke-cc-wait-for-replicator: #_ Waits until the Replicator rollout is complete
	@$(call echo_stdout_header,Wait for Replicator)
	source $(COMMON_MKFILE_DIR)bin/retry.sh; retry 15 kubectl --context $(GKE_BASE_KUBECTL_CONTEXT) -n operator get sts replicator 
	kubectl --context $(GKE_BASE_KUBECTL_CONTEXT) -n $(GKE_BASE_KUBECTL_NAMESPACE) rollout status statefulset/replicator
	@$(call echo_stdout_footer_pass,Replicator ready)

replicator-gke-cc-destroy-replicator: #_ Purge the Replicator package 
	@$(call echo_stdout_header,Purge the Replicator package)
	-helm delete --purge replicator
	@$(call echo_stdout_footer_pass,Replicator purged)

replicator-gke-cc-wait-for-replicator-destruction: #_ Waits until the Replicator cluster is destroyed
	@$(call echo_stdout_header,Ensure Replicator destroyed)
	-kubectl --context $(GKE_BASE_KUBECTL_CONTEXT) -n $(GKE_BASE_KUBECTL_NAMESPACE) wait --timeout=60s --for=delete statefulset/replicator
	@$(call echo_stdout_footer_pass,Replicator gone)
#################################

##### DEMO HELM CHART #####
replicator-gke-cc-deploy-demo-helm: #_ Deploys the helm chart for some custom items for this demo
	@$(call echo_stdout_header,Deploy Replicator Deploy Job)
	@echo "helm upgrade --install --namespace operator $(GKE_BASE_HELM_COMMON_FLAGS) --set global.sasl.plain.username=[redacted] --set global.sasl.plain.password=[redacted] replicator-gke-cc $(REPLICATOR_GKE_CC_MKFILE_DIR)helm/replicator-gke-cc"
	@helm upgrade --install --namespace operator $(GKE_BASE_HELM_COMMON_FLAGS) --set global.sasl.plain.username=$(CCLOUD_API_KEY) --set global.sasl.plain.password=$(CCLOUD_API_SECRET) replicator-gke-cc $(REPLICATOR_GKE_CC_MKFILE_DIR)helm/replicator-gke-cc
	@$(call echo_stdout_footer_pass,Replicator Deploy Job deployed)

replicator-gke-cc-destroy-demo-helm: #_ Destroys the deploy job and configuration for Replicator
	@$(call echo_stdout_header,destory Deploy stock trades datagen connector)
	-helm delete --purge replicator-gke-cc
	-kubectl --context $(GKE_BASE_KUBECTL_CONTEXT) -n $(GKE_BASE_KUBECTL_NAMESPACE) delete configmap/stock-trades-datagen-connector
	@$(call echo_stdout_footer_pass,stock trades datagen connector destroyed)
#################################

##### Update client console with destination cluster secrets #####
replicator-gke-cc-deploy-client-console: #_ Deploys the client console for CLI access inside k8s
	@$(call echo_stdout_header,Update client-console)
	-kubectl --context $(GKE_BASE_KUBECTL_CONTEXT) -n $(GKE_BASE_KUBECTL_NAMESPACE) delete pods/client-console
	kubectl --context $(GKE_BASE_KUBECTL_CONTEXT) -n $(GKE_BASE_KUBECTL_NAMESPACE) apply -f $(REPLICATOR_GKE_CC_MKFILE_DIR)cfg/client-console-pod.yaml
	@$(call echo_stdout_footer_pass,client-console deployed)
##################################################################

replicator-gke-cc-demo:
	@make --no-print-directory gke-base-demo GKE_BASE_SKIP_CLICKS=true GKE_BASE_SKIP_CLIENT_CONSOLE=true
	@make --no-print-directory gke-base-deploy-stock-trades-connector
	@make --no-print-directory replicator-gke-cc-deploy-replicator
	@make --no-print-directory replicator-gke-cc-wait-for-replicator
	@make --no-print-directory replicator-gke-cc-deploy-demo-helm
	@make --no-print-directory replicator-gke-cc-deploy-client-console
	@echo
	@$(call echo_pass,Replicator GKE->CC Demo running)

replicator-gke-cc-destroy-demo:
	@make --no-print-directory replicator-gke-cc-destroy-replicator
	@make --no-print-directory replicator-gke-cc-wait-for-replicator-destruction
	@make --no-print-directory replicator-gke-cc-destroy-demo-helm
	@make --no-print-directory gke-base-destroy-stock-trades-connector
	@make --no-print-directory gke-base-destroy-demo

