DESTDIR=./

REG_CRED=$(shell kubectl get secret -n tekton -l type=regcred -o jsonpath='{.items[0].metadata.name}')
SUBNET=$(shell docker network inspect kind --format '{{ (index .IPAM.Config 0).Subnet }}')
BASEIP=$(shell docker network inspect kind --format '{{ (index .IPAM.Config 0).Subnet }}' | cut -d "/" -f 1)

.PHONY: help
help:
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: deploy
create-cluster: ## Créer un cluster kubernetes avec Kind
	kind create cluster --name tekton --config kind/kind-tekton.yaml
	make setup-cluster
	@echo "SubNet Docker :" $(SUBNET)
	@echo -e " \x1b[32m✓\x1b[0m Cluster Kind déployé"

.PHONY: setup-cluster
setup-cluster:
	kubectl apply -k kind/cluster --server-side
	kubectl --namespace ingress-nginx rollout status --timeout 5m deployment/ingress-nginx-controller
	kubectl --namespace metallb-system rollout status --timeout 5m deployment controller

	sed -i "/mse.dev.local/ s/.*/            $(BASEIP)\tmse.dev.local/g" kind/cluster/coredns/custom-dns.yaml
	kubectl apply -f kind/cluster/coredns/custom-dns.yaml
	kubectl -n kube-system rollout restart deployment/coredns
	kubectl -n kube-system rollout status --timeout 5m deployment/coredns
	@yq -y -i ".spec.addresses = [\"$(SUBNET)\"]" kind/cluster/lb/resources/ipaddresspoool.yaml
	kubectl apply -k kind/cluster/lb --server-side

ssh-secret:
	cp kustomization/overlays/dev/resources/tekton/secret/secret.yaml.distkustomization/overlays/dev/resources/tekton/secret/secret.yaml

deploy-dev: ## Déploie le projet dans le cluster (nécessite la variable $HARBOR_USER_NAME)
	skaffold dev -p dev --cleanup=false --default-repo reg.cadoles.com/${HARBOR_USER_NAME}

delete-cluster: ## Supprime le cluster kubernetes Kind
	kind delete clusters tekton



.mktools:
	rm -rf .mktools
	curl -q https://forge.cadoles.com/Cadoles/mktools/raw/branch/master/install.sh | $(SHELL)

-include .mktools/*.mk