-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dale Haiducek <[email protected]> (cherry picked from commit 43cb920)
- Loading branch information
1 parent
5cf7d72
commit 946bfae
Showing
9 changed files
with
164 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,6 @@ API_PLUGIN_PATH ?= $(KUSTOMIZE_PLUGIN_HOME)/policy.open-cluster-management.io/v1 | |
# Kustomize arguments | ||
SOURCE_DIR ?= examples/ | ||
|
||
# go-get-tool will 'go install' any package $1 and install it to LOCAL_BIN. | ||
define go-get-tool | ||
@set -e ;\ | ||
echo "Checking installation of $(1)" ;\ | ||
GOBIN=$(LOCAL_BIN) go install $(1) | ||
endef | ||
|
||
include build/common/Makefile.common.mk | ||
|
||
############################################################ | ||
|
@@ -96,23 +89,17 @@ layout: | |
|
||
.PHONY: fmt | ||
fmt: | ||
go fmt ./... | ||
|
||
############################################################ | ||
# lint section | ||
############################################################ | ||
|
||
.PHONY: lint-dependencies | ||
lint-dependencies: | ||
$(call go-get-tool,github.com/golangci/golangci-lint/cmd/[email protected]) | ||
|
||
.PHONY: lint | ||
lint: lint-dependencies lint-all | ||
lint: | ||
|
||
############################################################ | ||
# test section | ||
############################################################ | ||
GOSEC = $(LOCAL_BIN)/gosec | ||
|
||
.PHONY: test | ||
test: | ||
|
@@ -122,10 +109,5 @@ test: | |
test-coverage: TESTARGS = -json -cover -covermode=atomic -coverprofile=coverage.out | ||
test-coverage: test | ||
|
||
.PHONY: gosec | ||
gosec: | ||
$(call go-get-tool,github.com/securego/gosec/v2/cmd/[email protected]) | ||
|
||
.PHONY: gosec-scan | ||
gosec-scan: gosec | ||
$(GOSEC) -fmt sonarqube -out gosec.json -no-fail -exclude-dir=.go ./... | ||
gosec-scan: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,164 @@ | ||
# Copyright (c) 2021 Red Hat, Inc. | ||
# Copyright (c) 2022 Red Hat, Inc. | ||
# Copyright Contributors to the Open Cluster Management project | ||
|
||
LOCAL_BIN ?= $(error LOCAL_BIN is not set.) | ||
ifneq ($(findstring $(LOCAL_BIN), $(PATH)), $(LOCAL_BIN)) | ||
$(error LOCAL_BIN is not in PATH.) | ||
endif | ||
|
||
# go-get-tool will 'go install' any package $1 and install it to LOCAL_BIN. | ||
define go-get-tool | ||
@set -e ;\ | ||
echo "Checking installation of $(1)" ;\ | ||
GOBIN=$(LOCAL_BIN) go install $(1) | ||
endef | ||
|
||
# Handle base64 OS differences | ||
OS = $(shell uname -s | tr '[:upper:]' '[:lower:]') | ||
BASE64 = base64 -w 0 | ||
ifeq ($(OS), darwin) | ||
BASE64 = base64 | ||
endif | ||
|
||
############################################################ | ||
# Work | ||
############################################################ | ||
|
||
$(LOCAL_BIN): | ||
@mkdir -p $(LOCAL_BIN) | ||
|
||
############################################################ | ||
# Generate manifests | ||
############################################################ | ||
CONTROLLER_GEN = $(LOCAL_BIN)/controller-gen | ||
KUSTOMIZE = $(LOCAL_BIN)/kustomize | ||
|
||
.PHONY: controller-gen | ||
controller-gen: ## Download controller-gen locally if necessary. | ||
$(call go-get-tool,sigs.k8s.io/controller-tools/cmd/[email protected]) | ||
|
||
.PHONY: kustomize | ||
kustomize: ## Download kustomize locally if necessary. | ||
$(call go-get-tool,sigs.k8s.io/kustomize/kustomize/[email protected]) | ||
|
||
############################################################ | ||
# Lint | ||
############################################################ | ||
FINDFILES=find . \( -path ./.git -o -path ./.github -o -path ./.go \) -prune -o -type f | ||
XARGS = xargs -0 ${XARGS_FLAGS} | ||
CLEANXARGS = xargs ${XARGS_FLAGS} | ||
|
||
# lint-yaml: | ||
# @${FINDFILES} \( -name '*.yml' -o -name '*.yaml' \) -print0 | ${XARGS} grep -L -e "{{" | ${CLEANXARGS} yamllint -c ./build/common/config/.yamllint.yml | ||
.PHONY: lint | ||
lint: lint-dependencies lint-yaml lint-go | ||
|
||
.PHONY: lint-dependencies | ||
lint-dependencies: | ||
$(call go-get-tool,github.com/golangci/golangci-lint/cmd/[email protected]) | ||
|
||
.PHONY: lint-yaml | ||
lint-yaml: | ||
# Linting YAML | ||
@$(FINDFILES) \( -name '*.yml' -o -name '*.yaml' \) -print0 | $(XARGS) grep -L -e "{{" | $(CLEANXARGS) yamllint -c ./build/common/config/.yamllint.yml | ||
|
||
.PHONY: lint-go | ||
lint-go: | ||
@${FINDFILES} -name '*.go' \( ! \( -name '*.gen.go' -o -name '*.pb.go' \) \) -print0 | ${XARGS} build/common/scripts/lint_go.sh | ||
# Linting Golang | ||
@$(FINDFILES) -name '*.go' \( ! \( -name '*.gen.go' -o -name '*.pb.go' \) \) -print0 | $(XARGS) build/common/scripts/lint_go.sh | ||
|
||
.PHONY: fmt-dependencies | ||
fmt-dependencies: | ||
$(call go-get-tool,github.com/daixiang0/[email protected]) | ||
$(call go-get-tool,mvdan.cc/[email protected]) | ||
|
||
.PHONY: fmt | ||
fmt: fmt-dependencies | ||
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gofmt -s -w | ||
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gofumpt -l -w | ||
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))" | ||
|
||
############################################################ | ||
# Unit Test | ||
############################################################ | ||
GOSEC = $(LOCAL_BIN)/gosec | ||
KUBEBUILDER = $(LOCAL_BIN)/kubebuilder | ||
ENVTEST = $(LOCAL_BIN)/setup-envtest | ||
KBVERSION = 3.12.0 | ||
ENVTEST_K8S_VERSION = 1.26.x | ||
|
||
.PHONY: kubebuilder | ||
kubebuilder: | ||
@if [ "$$($(KUBEBUILDER) version 2>/dev/null | grep -o KubeBuilderVersion:\"[0-9]*\.[0-9]\.[0-9]*\")" != "KubeBuilderVersion:\"$(KBVERSION)\"" ]; then \ | ||
echo "Installing Kubebuilder"; \ | ||
curl -L https://github.com/kubernetes-sigs/kubebuilder/releases/download/v$(KBVERSION)/kubebuilder_$(GOOS)_$(GOARCH) -o $(KUBEBUILDER); \ | ||
chmod +x $(KUBEBUILDER); \ | ||
fi | ||
|
||
.PHONY: envtest | ||
envtest: | ||
$(call go-get-tool,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest) | ||
|
||
.PHONY: gosec | ||
gosec: | ||
$(call go-get-tool,github.com/securego/gosec/v2/cmd/[email protected]) | ||
|
||
.PHONY: gosec-scan | ||
gosec-scan: gosec | ||
$(GOSEC) -fmt sonarqube -out gosec.json -stdout -exclude-dir=.go -exclude-dir=test ./... | ||
|
||
############################################################ | ||
# E2E Test | ||
############################################################ | ||
GINKGO = $(LOCAL_BIN)/ginkgo | ||
CLUSTER_NAME ?= $(error CLUSTER_NAME is not set.) | ||
CONTROLLER_NAME ?= $(error CONTROLLER_NAME is not set.) | ||
KIND_NAME ?= test-$(CLUSTER_NAME) | ||
KIND_CLUSTER_NAME = kind-$(KIND_NAME) | ||
CONTROLLER_NAMESPACE ?= open-cluster-management-agent-addon | ||
KIND_VERSION ?= latest | ||
# Set the Kind version tag | ||
ifdef KIND_VERSION | ||
ifeq ($(KIND_VERSION), minimum) | ||
KIND_ARGS = --image kindest/node:v1.19.16 | ||
else ifneq ($(KIND_VERSION), latest) | ||
KIND_ARGS = --image kindest/node:$(KIND_VERSION) | ||
endif | ||
endif | ||
|
||
.PHONY: kind-create-cluster | ||
kind-create-cluster: | ||
# Ensuring cluster $(KIND_NAME) | ||
-kind create cluster --name $(KIND_NAME) $(KIND_ARGS) | ||
kubectl config use-context $(KIND_CLUSTER_NAME) | ||
kind get kubeconfig --name $(KIND_NAME) > kubeconfig_$(CLUSTER_NAME)_e2e | ||
|
||
.PHONY: kind-ensure-sa | ||
kind-ensure-sa: | ||
@KUBECONFIG_TOKEN="$$(kubectl config view --raw -o jsonpath='{.users[].user.token}')"; \ | ||
KUBECONFIG_USER="$$(echo "$${KUBECONFIG_TOKEN}" | jq -rR 'split(".") | .[1] | select(. != null) | @base64d | fromjson | .sub')"; \ | ||
echo "Kubeconfig user detected from token: $${KUBECONFIG_USER}"; \ | ||
[ "$${KUBECONFIG_USER}" = "system:serviceaccount:$(CONTROLLER_NAMESPACE):$(CONTROLLER_NAME)" ] | ||
|
||
lint-all: lint-go lint-yaml | ||
.PHONY: kind-controller-kubeconfig | ||
kind-controller-kubeconfig: install-resources | ||
kubectl -n $(CONTROLLER_NAMESPACE) apply -f test/resources/e2e_controller_secret.yaml | ||
-rm kubeconfig_$(CLUSTER_NAME) | ||
@kubectl config set-cluster $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) \ | ||
--server=$(shell kubectl config view --minify -o jsonpath='{.clusters[].cluster.server}' --kubeconfig=kubeconfig_$(CLUSTER_NAME)_e2e) \ | ||
--insecure-skip-tls-verify=true | ||
@kubectl config set-credentials $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) \ | ||
--token=$$(kubectl get secret -n $(CONTROLLER_NAMESPACE) $(CONTROLLER_NAME) -o jsonpath='{.data.token}' --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME)_e2e | $(BASE64) --decode) | ||
@kubectl config set-context $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) \ | ||
--user=$(KIND_CLUSTER_NAME) --cluster=$(KIND_CLUSTER_NAME) | ||
@kubectl config use-context $(KIND_CLUSTER_NAME) --kubeconfig=$(PWD)/kubeconfig_$(CLUSTER_NAME) | ||
|
||
format-go: | ||
@${FINDFILES} -name '*.go' \( ! \( -name '*.gen.go' -o -name '*.pb.go' \) \) -print0 | ${XARGS} goimports -w -local "github.com/stolostron" | ||
.PHONY: e2e-dependencies | ||
e2e-dependencies: | ||
$(call go-get-tool,github.com/onsi/ginkgo/v2/ginkgo@$(shell awk '/github.com\/onsi\/ginkgo\/v2/ {print $$2}' go.mod)) | ||
|
||
.PHONY: lint-go lint-yaml | ||
############################################################ | ||
# Test coverage | ||
############################################################ | ||
GOCOVMERGE = $(LOCAL_BIN)/gocovmerge | ||
.PHONY: coverage-dependencies | ||
coverage-dependencies: | ||
$(call go-get-tool,github.com/wadey/[email protected]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters