# 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
WERF               ?= werf
# Stages are built into a local registry and only the final artifacts are
# copied to SOURCE. werf keeps every stage in --repo, base images included
# (Deckhouse's builder/golang-alpine has a 762 MB layer), which a remote or
# small registry doesn't need to hold. crane copy keeps digests, so the
# images_digests.json baked into the bundle stays valid.
BUILD_REGISTRY     ?= localhost:5555
BUILD_REPO         ?= $(BUILD_REGISTRY)/$(MODULE)
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-registry 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-registry:
	@docker inspect cluster-map-build-registry >/dev/null 2>&1 || \
	  docker run -d --name cluster-map-build-registry -p 127.0.0.1:5555:5000 -v cluster-map-build-registry:/var/lib/registry registry:2 >/dev/null
	@until curl -sf http://$(BUILD_REGISTRY)/v2/ >/dev/null; do sleep 1; done

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

# Application images first (the bundle refers to them by digest), then the
# bundle and the release, then the module registration.
publish: build
	jq -r '.Images | to_entries[] | select((.key | startswith("$(MODULE)/")) and (.key | endswith("-build") | not)) | .value.DockerImageName' images_tags_werf.json \
	  | while read -r img; do $(CRANE) copy --insecure "$$img" "$(SOURCE)/$(MODULE):$${img##*:}"; done
	$(CRANE) copy --insecure "$$(jq -r '.Images.bundle.DockerImageName' images_tags_werf.json)" "$(SOURCE)/$(MODULE):$(VERSION)"
	$(CRANE) copy --insecure "$$(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)"
