# cluster-map Deckhouse module.
#
#   make test       unit tests of the app and the hooks
#   make lint       golangci-lint + dmt
#   make render     helm template with test values (catches template errors fast)
#   make publish VERSION=v0.7.0 [CHANNEL=alpha]   build, push and register the module
#   make release VERSION=v0.7.0 CHANNEL=stable     move a release channel
#
# The registry layout follows deckhouse/modules-actions: <SOURCE>:<module>
# registers the module, <SOURCE>/<module>:<version> is the bundle,
# <SOURCE>/<module>/release:<version|channel> is the release image.

empty :=
space := $(empty) $(empty)

MODULE             ?= cluster-map
SOURCE             ?= git.project-chimera.ru/fanchji/cluster-map
CHANNEL            ?= alpha
BASE_IMAGES_VERSION ?= v2.1.7
CRANE              ?= crane
DMT                ?= dmt
API_VERSIONS       ?= --api-versions autoscaling.k8s.io/v1/VerticalPodAutoscaler --api-versions monitoring.coreos.com/v1/PodMonitor

.PHONY: test lint render base-images build publish release

test:
	cd images/cluster-map/src && go test -race -count=1 ./...
	cd hooks/batch && go test -count=1 ./...

lint:
	cd images/cluster-map/src && golangci-lint run ./...
	cd hooks/batch && go vet ./...
	$(DMT) lint .

render:
	helm template $(MODULE) . --namespace d8-cluster-map $(API_VERSIONS) -f tests/values/default.yaml > /dev/null
	helm template $(MODULE) . --namespace d8-cluster-map $(API_VERSIONS) -f tests/values/default.yaml -f tests/values/no-hubble.yaml > /dev/null
	@echo render OK

# Deckhouse publishes its base image digests; the same file official modules
# build from. .werf/stages/base-images.yaml turns every key into a werf image,
# and the full file has ~1200 of them, so only the keys this module uses are kept.
BASE_IMAGE_KEYS := REGISTRY_PATH builder/golang-alpine builder/alpine builder/scratch base/distroless jq

base_images.yml:
	curl -sfL "https://fox.flant.com/api/v4/projects/deckhouse%2Fcontainer-base%2Fbase-images/packages/generic/base_images/$(BASE_IMAGES_VERSION)/base_images.yml" \
	  | grep -E '^($(subst $(space),|,$(BASE_IMAGE_KEYS))):' > $@
	@test "$$(wc -l < $@)" -eq $(words $(BASE_IMAGE_KEYS)) || (echo "base_images.yml: expected $(words $(BASE_IMAGE_KEYS)) keys"; cat $@; rm -f $@; exit 1)

base-images: base_images.yml

build: base_images.yml
	@test -n "$(VERSION)" || (echo "VERSION is required, e.g. VERSION=v0.7.0" && exit 1)
	MODULES_MODULE_TAG=$(VERSION) werf build --repo=$(SOURCE)/$(MODULE) --save-build-report --build-report-path images_tags_werf.json

publish: build
	$(CRANE) copy "$$(jq -r '.Images.bundle.DockerImageName' images_tags_werf.json)" "$(SOURCE)/$(MODULE):$(VERSION)"
	$(CRANE) copy "$$(jq -r '.Images."release-channel-version".DockerImageName' images_tags_werf.json)" "$(SOURCE)/$(MODULE)/release:$(VERSION)"
	$(CRANE) append --oci-empty-base --new_layer "" --new_tag "$(SOURCE):$(MODULE)"
	$(MAKE) release VERSION=$(VERSION) CHANNEL=$(CHANNEL)

release:
	@test -n "$(VERSION)" || (echo "VERSION is required" && exit 1)
	$(CRANE) copy "$(SOURCE)/$(MODULE)/release:$(VERSION)" "$(SOURCE)/$(MODULE)/release:$(CHANNEL)"
