Skip to content

Commit 752e299

Browse files
authored
separate pre-build steps from build and add test coverage reporting and go sec scan (#4)
Signed-off-by: Mike Ng <ming@redhat.com>
1 parent ec7e8c4 commit 752e299

File tree

4 files changed

+88
-15
lines changed

4 files changed

+88
-15
lines changed

.codecov.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
coverage:
2+
status:
3+
patch: off # disable patch status, https://docs.codecov.com/docs/commit-status#patch-status
4+
project:
5+
default:
6+
target: auto
7+
threshold: 1%
8+
9+
ignore:
10+
- "**/*generated*.go"

.github/workflows/go-postsubmit.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ defaults:
1616
working-directory: go/src/github.com/open-cluster-management/cluster-permission
1717

1818
jobs:
19+
test:
20+
name: test
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: checkout code
24+
uses: actions/checkout@v3
25+
with:
26+
fetch-depth: 1
27+
path: go/src/github.com/open-cluster-management/cluster-permission
28+
- name: install Go
29+
uses: actions/setup-go@v3
30+
with:
31+
go-version: ${{ env.GO_VERSION }}
32+
- name: test
33+
run: make test
34+
- name: report-coverage
35+
uses: codecov/codecov-action@v3
36+
with:
37+
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
38+
files: /home/runner/work/cluster-permission/cluster-permission/go/src/github.com/open-cluster-management/cluster-permission/coverage.out
39+
flags: unit
40+
name: unit
41+
verbose: true
42+
fail_ci_if_error: true
43+
1944
images:
2045
name: images
2146
runs-on: ubuntu-latest
@@ -31,6 +56,8 @@ jobs:
3156
go-version: ${{ env.GO_VERSION }}
3257
- name: install imagebuilder
3358
run: go install github.com/openshift/imagebuilder/cmd/imagebuilder@v1.2.3
59+
- name: pre-build
60+
run: make pre-build
3461
- name: images
3562
run: make docker-build
3663
- name: push

.github/workflows/go-presubmit.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ jobs:
2929
uses: actions/setup-go@v3
3030
with:
3131
go-version: ${{ env.GO_VERSION }}
32+
- name: pre-build
33+
run: make pre-build
3234
- name: build
3335
run: make build
3436

@@ -65,6 +67,28 @@ jobs:
6567
go-version: ${{ env.GO_VERSION }}
6668
- name: test
6769
run: make test
70+
- name: report-coverage
71+
uses: codecov/codecov-action@v3
72+
with:
73+
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
74+
files: /home/runner/work/cluster-permission/cluster-permission/go/src/github.com/open-cluster-management/cluster-permission/coverage.out
75+
flags: unit
76+
name: unit
77+
verbose: true
78+
fail_ci_if_error: true
79+
80+
go-sec-scan:
81+
name: go-sec-scan
82+
runs-on: ubuntu-latest
83+
env:
84+
GO111MODULE: on
85+
steps:
86+
- name: Checkout Source
87+
uses: actions/checkout@v3
88+
- name: Run Gosec Security Scanner
89+
uses: securego/gosec@master
90+
with:
91+
args: -exclude-generated ./...
6892

6993
e2e:
7094
name: e2e

Makefile

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ IMG ?= quay.io/open-cluster-management/cluster-permission:latest
44
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
55
ENVTEST_K8S_VERSION = 1.26.0
66

7-
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8-
ifeq (,$(shell go env GOBIN))
9-
GOBIN=$(shell go env GOPATH)/bin
10-
else
11-
GOBIN=$(shell go env GOBIN)
12-
endif
7+
TEST_TMP :=/tmp
8+
export KUBEBUILDER_ASSETS ?=$(TEST_TMP)/kubebuilder/bin
9+
K8S_VERSION ?=1.19.2
10+
GOHOSTOS ?=$(shell go env GOHOSTOS)
11+
GOHOSTARCH ?= $(shell go env GOHOSTARCH)
12+
KB_TOOLS_ARCHIVE_NAME :=kubebuilder-tools-$(K8S_VERSION)-$(GOHOSTOS)-$(GOHOSTARCH).tar.gz
13+
KB_TOOLS_ARCHIVE_PATH := $(TEST_TMP)/$(KB_TOOLS_ARCHIVE_NAME)
1314

1415
# Setting SHELL to bash allows bash commands to be executed by recipes.
1516
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
@@ -54,14 +55,13 @@ fmt: ## Run go fmt against code.
5455
vet: ## Run go vet against code.
5556
go vet ./...
5657

57-
.PHONY: test
58-
test: manifests generate fmt vet envtest ## Run tests.
59-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
60-
6158
##@ Build
6259

60+
.PHONY: pre-build
61+
pre-build: manifests generate fmt vet
62+
6363
.PHONY: build
64-
build: manifests generate fmt vet ## Build manager binary.
64+
build:
6565
go build -o bin/cluster-permission main.go
6666

6767
.PHONY: run
@@ -152,10 +152,22 @@ $(CONTROLLER_GEN): $(LOCALBIN)
152152
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
153153
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
154154

155-
.PHONY: envtest
156-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
157-
$(ENVTEST): $(LOCALBIN)
158-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
155+
.PHONY: test
156+
157+
# download the kubebuilder-tools to get kube-apiserver binaries from it
158+
ensure-kubebuilder-tools:
159+
ifeq "" "$(wildcard $(KUBEBUILDER_ASSETS))"
160+
$(info Downloading kube-apiserver into '$(KUBEBUILDER_ASSETS)')
161+
mkdir -p '$(KUBEBUILDER_ASSETS)'
162+
curl -s -f -L https://storage.googleapis.com/kubebuilder-tools/$(KB_TOOLS_ARCHIVE_NAME) -o '$(KB_TOOLS_ARCHIVE_PATH)'
163+
tar -C '$(KUBEBUILDER_ASSETS)' --strip-components=2 -zvxf '$(KB_TOOLS_ARCHIVE_PATH)'
164+
else
165+
$(info Using existing kube-apiserver from "$(KUBEBUILDER_ASSETS)")
166+
endif
167+
.PHONY: ensure-kubebuilder-tools
168+
169+
test: ensure-kubebuilder-tools
170+
go test -timeout 300s -v ./controllers/... -coverprofile=coverage.out
159171

160172
.PHONY: deploy-ocm
161173
deploy-ocm:

0 commit comments

Comments
 (0)