## OLM (Operator Lifecycle Manager) Management
## Установка OLM через Helm chart (ArtifactHub: olm/olm) из OCI registry.

include common/Makefile

OLM_NAMESPACE := olm
OPERATORS_NAMESPACE := operators

# chart version (helmfile тоже зафиксирован)
OLM_CHART_VERSION := 0.38.0

.PHONY: olm-help
olm-help:
	@echo ""
	@echo "$(COLOR_BOLD)OLM Commands:$(COLOR_RESET)"
	@echo "  make olm-install        - Install OLM via helmfile (OCI chart)"
	@echo "  make olm-update         - Update OLM via helmfile"
	@echo "  make olm-uninstall      - Uninstall OLM (helm uninstall + best-effort cleanup)"
	@echo "  make olm-status         - Show OLM status (pods, deployments, CRDs)"
	@echo "  make olm-catalog-schedule-infra - Pin operatorhubio catalog pod to infra nodes"
	@echo ""
	@echo "$(COLOR_YELLOW)Pinned chart version:$(COLOR_RESET) $(OLM_CHART_VERSION)"

.PHONY: olm-install
olm-install:
	$(call print_info,Installing OLM via helmfile (OCI chart)...)
	$(call run_helmfile,olm/helmfile.yaml,sync)
	$(call print_info,Waiting for OLM deployments...)
	@$(KUBECTL) -n $(OLM_NAMESPACE) rollout status deploy/olm-operator --timeout=10m || true
	@$(KUBECTL) -n $(OLM_NAMESPACE) rollout status deploy/catalog-operator --timeout=10m || true
	@$(KUBECTL) apply -f olm/manifests/catalogsource-operatorhubio-infra-nodes.yaml || true
	$(call print_success,OLM installed!)
	@bash olm/scripts/status.sh $(KUBECONFIG) || true

.PHONY: olm-update
olm-update:
	$(call print_info,Updating OLM via helmfile...)
	$(call run_helmfile,olm/helmfile.yaml,sync)
	$(call print_info,Waiting for OLM deployments...)
	@$(KUBECTL) -n $(OLM_NAMESPACE) rollout status deploy/olm-operator --timeout=10m || true
	@$(KUBECTL) -n $(OLM_NAMESPACE) rollout status deploy/catalog-operator --timeout=10m || true
	@$(KUBECTL) apply -f olm/manifests/catalogsource-operatorhubio-infra-nodes.yaml || true
	$(call print_success,OLM updated!)
	@bash olm/scripts/status.sh $(KUBECONFIG) || true

.PHONY: olm-uninstall
olm-uninstall:
	$(call print_warning,This will uninstall OLM!)
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Uninstalling OLM helm release...)
	@$(HELM) uninstall olm -n $(OLM_NAMESPACE) || \
		($(call print_warning,Helm release not found or already uninstalled.) && true)
	$(call print_info,Best-effort cleanup of leftovers...)
	@bash olm/scripts/uninstall-leftovers.sh $(KUBECONFIG) || true
	$(call print_success,OLM uninstalled.)

.PHONY: olm-status
olm-status:
	@bash olm/scripts/status.sh $(KUBECONFIG)

# Apply CatalogSource patch so operatorhubio catalog pod schedules on workload-pool=infra (stable quay.io pulls).
.PHONY: olm-catalog-schedule-infra
olm-catalog-schedule-infra:
	$(call print_info,Patching operatorhubio-catalog nodeSelector...)
	@$(KUBECTL) apply -f olm/manifests/catalogsource-operatorhubio-infra-nodes.yaml
	$(call print_success,CatalogSource updated. Check: kubectl get pods -n operator-lifecycle-manager -l olm.catalogSource=operatorhubio-catalog -o wide)
