.PHONY: vendor

API_PACKAGE ?= "github.com/tidbcloud/config-service/api/..."
GOOS := $(if $(GOOS),$(GOOS),linux)
GOARCH := $(if $(GOARCH),$(GOARCH),amd64)
GOENV  := CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH)
GO     := $(GOENV) go
GO_BUILD := $(GO) build -trimpath -mod=vendor

COMMIT := $(shell git rev-parse --short HEAD)
VERSION := $(shell git describe --abbrev=14 --tags --match 'v*' ${COMMIT} 2>/dev/null || git describe --always --abbrev=14 --tags ${COMMIT})
REPO ?= gcr.io/pingcap-public
ifneq ($(shell git status --porcelain),)
    VERSION := $(VERSION)-dirty
endif
# Image URL to use all building/pushing image targets
IMG ?= $(REPO)/config-service:$(VERSION)
IMG_OVERLAY ?= $(REPO)/overlay:$(VERSION)
IMG_OVERLAY_LATEST = $(REPO)/overlay:latest

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

.PHONY: help
help: ## Display this help.
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

all: \
	manager \
	overlay

check: test lint tidy
	@echo "checking that branch is clean"
	@test -z "$$(git status --porcelain)" || (echo -e "unclean working tree, did you forget to run 'make reviewable'?\ngit status:\n$$(git status --porcelain)" && exit 1)
	@echo "branch is clean"

test: generate fmt vet manifests ## Run tests
	go test ./server/... -coverprofile cover.out
	go test ./controllers/... -coverprofile cover.out
	go test ./pkg/config/... -coverprofile cover.out
	go test ./app/overlay/... -coverprofile cover.out

test-overlay:
	go test ./app/overlay/... -coverprofile cover.out

manager: generate fmt vet ## Build manager binary
	go build -o bin/manager app/config-service/main.go

overlay: fmt vet lint
	go build -o bin/overlay ./app/overlay/

run: generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
	go run ./main.go

install: manifests ## Install CRDs into a cluster
	kustomize build config/crd | kubectl apply -f -

uninstall: manifests ## Uninstall CRDs from a cluster
	kustomize build config/crd | kubectl delete -f -

deploy: manifests ## Deploy controller in the configured Kubernetes cluster in ~/.kube/config
	cd config/manager && kustomize edit set image controller=${IMG}
	kustomize build config/default | kubectl apply -f -

tidy:
	go mod tidy

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
	$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths=${API_PACKAGE} output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
	$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths=${API_PACKAGE}

.PHONY: fmt
fmt: ## Run go fmt against code.
	go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
	go vet ./...

lint: golangci-lint
	golangci-lint run ./app/overlay/...

golangci-lint:
	which golangci-lint || go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2

vendor:
	go mod vendor

docker-build: vendor ## Build the docker image
	docker build --build-arg GOOS=$(GOOS) --build-arg GOARCH=$(GOARCH) . -t ${IMG}

docker-push: ## Push the docker image
	echo "image-tag of config-service: ${IMG}"
	echo "image-tag of overlay: ${IMG_OVERLAY}"

	docker push ${IMG}
	docker push ${IMG_OVERLAY}
	docker push ${IMG_OVERLAY_LATEST}

docker-build-overlay: vendor
	docker build --build-arg GOOS=$(GOOS) --build-arg GOARCH=$(GOARCH) --file overlay.Dockerfile -t ${IMG_OVERLAY} -t ${IMG_OVERLAY_LATEST} .

controller-gen: ## Download controller-gen locally if necessary.
ifeq (, $(shell which controller-gen))
	@{ \
	set -e ;\
	go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0 ;\
	}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
