open-cluster-management.io/governance-policy-propagator@v0.13.0/build/common/Makefile.common.mk (about) 1 # Copyright (c) 2022 Red Hat, Inc. 2 # Copyright Contributors to the Open Cluster Management project 3 4 ## CLI versions (with links to the latest releases) 5 # https://github.com/kubernetes-sigs/controller-tools/releases/latest 6 CONTROLLER_GEN_VERSION := v0.14.0 7 # https://github.com/kubernetes-sigs/kustomize/releases/latest 8 KUSTOMIZE_VERSION := v5.3.0 9 # https://github.com/golangci/golangci-lint/releases/latest 10 GOLANGCI_VERSION := v1.52.2 11 # https://github.com/mvdan/gofumpt/releases/latest 12 GOFUMPT_VERSION := v0.6.0 13 # https://github.com/daixiang0/gci/releases/latest 14 GCI_VERSION := v0.12.3 15 # https://github.com/securego/gosec/releases/latest 16 GOSEC_VERSION := v2.19.0 17 # https://github.com/kubernetes-sigs/kubebuilder/releases/latest 18 KBVERSION := 3.14.0 19 # https://github.com/kubernetes/kubernetes/releases/latest 20 ENVTEST_K8S_VERSION := 1.29.x 21 22 LOCAL_BIN ?= $(error LOCAL_BIN is not set.) 23 ifneq ($(findstring $(LOCAL_BIN), $(PATH)), $(LOCAL_BIN)) 24 $(error LOCAL_BIN is not in PATH.) 25 endif 26 27 # go-get-tool will 'go install' any package $1 and install it to LOCAL_BIN. 28 define go-get-tool 29 @set -e ;\ 30 echo "Checking installation of $(1)" ;\ 31 GOBIN=$(LOCAL_BIN) go install $(1) 32 endef 33 34 # Handle base64 OS differences 35 OS = $(shell uname -s | tr '[:upper:]' '[:lower:]') 36 BASE64 = base64 -w 0 37 ifeq ($(OS), darwin) 38 BASE64 = base64 39 endif 40 41 ############################################################ 42 # Work 43 ############################################################ 44 45 $(LOCAL_BIN): 46 @mkdir -p $(LOCAL_BIN) 47 48 ############################################################ 49 # Generate manifests 50 ############################################################ 51 CONTROLLER_GEN = $(LOCAL_BIN)/controller-gen 52 KUSTOMIZE = $(LOCAL_BIN)/kustomize 53 54 .PHONY: controller-gen 55 controller-gen: ## Download controller-gen locally if necessary. 56 $(call go-get-tool,sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION)) 57 58 .PHONY: kustomize 59 kustomize: ## Download kustomize locally if necessary. 60 $(call go-get-tool,sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)) 61 62 ############################################################ 63 # Lint 64 ############################################################ 65 FINDFILES=find . \( -path ./.git -o -path ./.github -o -path ./.go \) -prune -o -type f 66 XARGS = xargs -0 ${XARGS_FLAGS} 67 CLEANXARGS = xargs ${XARGS_FLAGS} 68 69 .PHONY: lint 70 lint: lint-dependencies lint-yaml lint-go 71 72 .PHONY: lint-dependencies 73 lint-dependencies: 74 $(call go-get-tool,github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION)) 75 76 .PHONY: lint-yaml 77 lint-yaml: 78 # Linting YAML 79 @$(FINDFILES) \( -name '*.yml' -o -name '*.yaml' \) -print0 | $(XARGS) grep -L -e "{{" | $(CLEANXARGS) yamllint -c ./build/common/config/.yamllint.yml 80 81 .PHONY: lint-go 82 lint-go: 83 # Linting Golang 84 @$(FINDFILES) -name '*.go' \( ! \( -name '*.gen.go' -o -name '*.pb.go' \) \) -print0 | $(XARGS) build/common/scripts/lint_go.sh 85 86 .PHONY: fmt-dependencies 87 fmt-dependencies: 88 $(call go-get-tool,github.com/daixiang0/gci@$(GCI_VERSION)) 89 $(call go-get-tool,mvdan.cc/gofumpt@$(GOFUMPT_VERSION)) 90 91 .PHONY: fmt 92 fmt: fmt-dependencies 93 find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gofmt -s -w 94 find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gofumpt -l -w 95 find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gci write --skip-generated -s standard -s default -s "prefix($(shell cat go.mod | head -1 | cut -d " " -f 2))" 96 go mod tidy 97 98 ############################################################ 99 # Unit Test 100 ############################################################ 101 GOSEC = $(LOCAL_BIN)/gosec 102 KUBEBUILDER = $(LOCAL_BIN)/kubebuilder 103 ENVTEST = $(LOCAL_BIN)/setup-envtest 104 105 .PHONY: kubebuilder 106 kubebuilder: 107 @if [ "$$($(KUBEBUILDER) version 2>/dev/null | grep -o KubeBuilderVersion:\"[0-9]*\.[0-9]\.[0-9]*\")" != "KubeBuilderVersion:\"$(KBVERSION)\"" ]; then \ 108 echo "Installing Kubebuilder"; \ 109 curl -L https://github.com/kubernetes-sigs/kubebuilder/releases/download/v$(KBVERSION)/kubebuilder_$(GOOS)_$(GOARCH) -o $(KUBEBUILDER); \ 110 chmod +x $(KUBEBUILDER); \ 111 fi 112 113 .PHONY: envtest 114 envtest: 115 $(call go-get-tool,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest) 116 117 .PHONY: gosec 118 gosec: 119 $(call go-get-tool,github.com/securego/gosec/v2/cmd/gosec@$(GOSEC_VERSION)) 120 121 .PHONY: gosec-scan 122 gosec-scan: gosec 123 $(GOSEC) -fmt sonarqube -out gosec.json -stdout -exclude-dir=.go -exclude-dir=test $(GOSEC_ARGS) ./... 124 125 ############################################################ 126 # E2E Test 127 ############################################################ 128 GINKGO = $(LOCAL_BIN)/ginkgo 129 CLUSTER_NAME ?= $(error CLUSTER_NAME is not set.) 130 CONTROLLER_NAME ?= $(error CONTROLLER_NAME is not set.) 131 KIND_NAME ?= test-$(CLUSTER_NAME) 132 KIND_CLUSTER_NAME = kind-$(KIND_NAME) 133 CONTROLLER_NAMESPACE ?= open-cluster-management-agent-addon 134 KIND_VERSION ?= latest 135 # Set the Kind version tag 136 ifdef KIND_VERSION 137 ifeq ($(KIND_VERSION), minimum) 138 KIND_ARGS = --image kindest/node:v1.25.16 139 else ifneq ($(KIND_VERSION), latest) 140 KIND_ARGS = --image kindest/node:$(KIND_VERSION) 141 endif 142 endif 143 144 .PHONY: kind-create-cluster 145 kind-create-cluster: 146 # Ensuring cluster $(KIND_NAME) 147 -kind create cluster --name $(KIND_NAME) $(KIND_ARGS) 148 kubectl config use-context $(KIND_CLUSTER_NAME) 149 kind get kubeconfig --name $(KIND_NAME) > kubeconfig_$(CLUSTER_NAME)_e2e 150 151 .PHONY: kind-ensure-sa 152 kind-ensure-sa: 153 @KUBECONFIG_TOKEN="$$(kubectl config view --raw -o jsonpath='{.users[].user.token}')"; \ 154 KUBECONFIG_USER="$$(echo "$${KUBECONFIG_TOKEN}" | jq -rR 'split(".") | .[1] | select(. != null) | @base64d | fromjson | .sub')"; \ 155 echo "Kubeconfig user detected from token: $${KUBECONFIG_USER}"; \ 156 [ "$${KUBECONFIG_USER}" = "system:serviceaccount:$(CONTROLLER_NAMESPACE):$(CONTROLLER_NAME)" ] 157 158 .PHONY: kind-controller-kubeconfig 159 kind-controller-kubeconfig: install-resources 160 kubectl -n $(CONTROLLER_NAMESPACE) apply -f test/resources/e2e_controller_secret.yaml --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME)_e2e 161 -rm kubeconfig_$(CLUSTER_NAME) 162 @kubectl config view --minify -o jsonpath='{.clusters[].cluster.certificate-authority-data}' --kubeconfig=kubeconfig_$(CLUSTER_NAME)_e2e --raw | base64 -d > temp-ca.crt 163 @kubectl config set-cluster $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) \ 164 --server=$(shell kubectl config view --minify -o jsonpath='{.clusters[].cluster.server}' --kubeconfig=kubeconfig_$(CLUSTER_NAME)_e2e) \ 165 --certificate-authority=temp-ca.crt --embed-certs=true 166 @rm -f temp-ca.crt 167 @kubectl config set-credentials $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) \ 168 --token=$$(kubectl get secret -n $(CONTROLLER_NAMESPACE) $(CONTROLLER_NAME) -o jsonpath='{.data.token}' --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME)_e2e | $(BASE64) --decode) 169 @kubectl config set-context $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) \ 170 --user=$(KIND_CLUSTER_NAME) --cluster=$(KIND_CLUSTER_NAME) 171 @kubectl config use-context $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) 172 173 .PHONY: e2e-dependencies 174 e2e-dependencies: 175 $(call go-get-tool,github.com/onsi/ginkgo/v2/ginkgo@$(shell awk '/github.com\/onsi\/ginkgo\/v2/ {print $$2}' go.mod)) 176 177 ############################################################ 178 # Test coverage 179 ############################################################ 180 GOCOVMERGE = $(LOCAL_BIN)/gocovmerge 181 .PHONY: coverage-dependencies 182 coverage-dependencies: 183 $(call go-get-tool,github.com/wadey/gocovmerge@v0.0.0-20160331181800-b5bfa59ec0ad)