# This Makefile is an aid for ../Makefile and does not supposed to be used independently
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"

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

.PHONY: generate
generate: controller-gen v1/zz_generated.deepcopy.go

v1/zz_generated.deepcopy.go: v1/*_types.go
	$(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."

GOLANGCI_LINT = $(shell pwd)/../bin/golangci-lint
lint:
	$(GOLANGCI_LINT) run ./...
lint-fix:
	$(GOLANGCI_LINT) run --fix ./...

KUBEBUILDER_ASSETS=$(shell pwd)/../testbin/bin
GINKGO=$(shell pwd)/../bin/ginkgo
test: ## Run tests.
	KUBEBUILDER_ASSETS=${KUBEBUILDER_ASSETS} go test ./...

unit:
	go test -short ./...

doc: crd-to-markdown
	$(CRD_TO_MARKDOWN) -f v1/provider_types.go -n Provider --header template/provider-header.md > ../docs/reference/provider.md
	$(CRD_TO_MARKDOWN) -f v1/network_types.go -n Network --header template/network-header.md > ../docs/reference/network.md
	$(CRD_TO_MARKDOWN) -f v1/cluster_types.go -n Cluster --header template/cluster-header.md > ../docs/reference/cluster.md
	$(CRD_TO_MARKDOWN) -f v1/backup_types.go -n Backup --header template/ns-header.md > ../docs/reference/backup.md
	$(CRD_TO_MARKDOWN) -f v1/restore_types.go -n Restore --header template/ns-header.md > ../docs/reference/restore.md
	$(CRD_TO_MARKDOWN) -f v1/import_types.go -n Import --header template/ns-header.md > ../docs/reference/import.md
	$(CRD_TO_MARKDOWN) -f v1/clusterconfig_types.go -n ClusterConfig --header template/ns-header.md > ../docs/reference/clusterconfig.md

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
	$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)

CRD_TO_MARKDOWN = $(shell pwd)/bin/crd-to-markdown
crd-to-markdown:
	$(call go-get-tool,$(CRD_TO_MARKDOWN),github.com/clamoriniere/crd-to-markdown@v0.0.3)

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
