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