Skip to content

Commit 43cb920

Browse files
dhaiducekopenshift-merge-bot[bot]
authored andcommitted
Sync common Makefile
Signed-off-by: Dale Haiducek <[email protected]>
1 parent fd7a535 commit 43cb920

File tree

9 files changed

+164
-30
lines changed

9 files changed

+164
-30
lines changed

Makefile

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ API_PLUGIN_PATH ?= $(KUSTOMIZE_PLUGIN_HOME)/policy.open-cluster-management.io/v1
2222
# Kustomize arguments
2323
SOURCE_DIR ?= examples/
2424

25-
# go-get-tool will 'go install' any package $1 and install it to LOCAL_BIN.
26-
define go-get-tool
27-
@set -e ;\
28-
echo "Checking installation of $(1)" ;\
29-
GOBIN=$(LOCAL_BIN) go install $(1)
30-
endef
31-
3225
include build/common/Makefile.common.mk
3326

3427
############################################################
@@ -96,23 +89,17 @@ layout:
9689

9790
.PHONY: fmt
9891
fmt:
99-
go fmt ./...
10092

10193
############################################################
10294
# lint section
10395
############################################################
10496

105-
.PHONY: lint-dependencies
106-
lint-dependencies:
107-
$(call go-get-tool,github.com/golangci/golangci-lint/cmd/[email protected])
108-
10997
.PHONY: lint
110-
lint: lint-dependencies lint-all
98+
lint:
11199

112100
############################################################
113101
# test section
114102
############################################################
115-
GOSEC = $(LOCAL_BIN)/gosec
116103

117104
.PHONY: test
118105
test:
@@ -122,10 +109,5 @@ test:
122109
test-coverage: TESTARGS = -json -cover -covermode=atomic -coverprofile=coverage.out
123110
test-coverage: test
124111

125-
.PHONY: gosec
126-
gosec:
127-
$(call go-get-tool,github.com/securego/gosec/v2/cmd/[email protected])
128-
129112
.PHONY: gosec-scan
130-
gosec-scan: gosec
131-
$(GOSEC) -fmt sonarqube -out gosec.json -no-fail -exclude-dir=.go ./...
113+
gosec-scan:

build/common/Makefile.common.mk

100644100755
Lines changed: 153 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,164 @@
1-
# Copyright (c) 2021 Red Hat, Inc.
1+
# Copyright (c) 2022 Red Hat, Inc.
22
# Copyright Contributors to the Open Cluster Management project
33

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+
############################################################
447
FINDFILES=find . \( -path ./.git -o -path ./.github -o -path ./.go \) -prune -o -type f
548
XARGS = xargs -0 ${XARGS_FLAGS}
649
CLEANXARGS = xargs ${XARGS_FLAGS}
750

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
1062

63+
.PHONY: lint-go
1164
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)" ]
13140

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)
15153

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))
18157

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])

cmd/PolicyGenerator/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/spf13/pflag"
11+
1112
"open-cluster-management.io/policy-generator-plugin/internal"
1213
)
1314

internal/expanders/gatekeeper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
89
"open-cluster-management.io/policy-generator-plugin/internal/types"
910
)
1011

internal/expanders/kyverno.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
89
"open-cluster-management.io/policy-generator-plugin/internal/types"
910
)
1011

internal/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1818
"k8s.io/apimachinery/pkg/runtime"
1919
"k8s.io/apimachinery/pkg/util/validation"
20+
2021
"open-cluster-management.io/policy-generator-plugin/internal/types"
2122
)
2223

internal/plugin_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"gopkg.in/yaml.v3"
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
1617
"open-cluster-management.io/policy-generator-plugin/internal/types"
1718
)
1819

internal/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313

1414
yaml "gopkg.in/yaml.v3"
1515
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
16-
"open-cluster-management.io/policy-generator-plugin/internal/expanders"
17-
"open-cluster-management.io/policy-generator-plugin/internal/types"
1816
"sigs.k8s.io/kustomize/api/krusty"
1917
"sigs.k8s.io/kustomize/kyaml/filesys"
18+
19+
"open-cluster-management.io/policy-generator-plugin/internal/expanders"
20+
"open-cluster-management.io/policy-generator-plugin/internal/types"
2021
)
2122

2223
// getManifests will get all of the manifest files associated with the input policy configuration

internal/utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/google/go-cmp/cmp/cmpopts"
1717
"gopkg.in/yaml.v3"
1818
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
19+
1920
"open-cluster-management.io/policy-generator-plugin/internal/types"
2021
)
2122

0 commit comments

Comments
 (0)