Skip to content

Commit

Permalink
Sync common Makefile
Browse files Browse the repository at this point in the history
Signed-off-by: Dale Haiducek <[email protected]>
(cherry picked from commit 43cb920)
  • Loading branch information
dhaiducek authored and magic-mirror-bot[bot] committed Nov 21, 2023
1 parent 5cf7d72 commit 946bfae
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 30 deletions.
22 changes: 2 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

############################################################
Expand Down Expand Up @@ -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:
Expand All @@ -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:
161 changes: 153 additions & 8 deletions build/common/Makefile.common.mk
100644 → 100755
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])
1 change: 1 addition & 0 deletions cmd/PolicyGenerator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/spf13/pflag"

"open-cluster-management.io/policy-generator-plugin/internal"
)

Expand Down
1 change: 1 addition & 0 deletions internal/expanders/gatekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"open-cluster-management.io/policy-generator-plugin/internal/types"
)

Expand Down
1 change: 1 addition & 0 deletions internal/expanders/kyverno.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"open-cluster-management.io/policy-generator-plugin/internal/types"
)

Expand Down
1 change: 1 addition & 0 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"

"open-cluster-management.io/policy-generator-plugin/internal/types"
)

Expand Down
1 change: 1 addition & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"open-cluster-management.io/policy-generator-plugin/internal/types"
)

Expand Down
5 changes: 3 additions & 2 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (

yaml "gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"open-cluster-management.io/policy-generator-plugin/internal/expanders"
"open-cluster-management.io/policy-generator-plugin/internal/types"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/kyaml/filesys"

"open-cluster-management.io/policy-generator-plugin/internal/expanders"
"open-cluster-management.io/policy-generator-plugin/internal/types"
)

// getManifests will get all of the manifest files associated with the input policy configuration
Expand Down
1 change: 1 addition & 0 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"open-cluster-management.io/policy-generator-plugin/internal/types"
)

Expand Down

0 comments on commit 946bfae

Please sign in to comment.