From 60977b150c9f38e2fc4eb4ffa20acb77be2084d3 Mon Sep 17 00:00:00 2001 From: yuswift Date: Fri, 27 Aug 2021 22:59:29 +0800 Subject: [PATCH 1/3] upgrade kubebuilder envtest way Signed-off-by: yuswift --- .gitignore | 1 + Makefile | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fffd01e33..70dce1ef8 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ kustomize/network/etcd apiserver.local.config tmp/ kubesphere.yaml +testbin/ diff --git a/Makefile b/Makefile index 5385d3f53..0cb34889a 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,13 @@ # Use of this source code is governed by a Apache license # that can be found in the LICENSE file. +# Setting SHELL to bash allows bash commands to be executed by recipes. +# This is a requirement for 'setup-envtest.sh' in the test target. +# Options are set to exit when a recipe line exits non-zero or a piped command fails. +SHELL = /usr/bin/env bash -o pipefail +.SHELLFLAGS = -ec + + # Produce CRDs that work back to Kubernetes 1.11 (no version conversion) CRD_OPTIONS ?= "crd:trivialVersions=true" @@ -137,8 +144,11 @@ helm-uninstall: ; $(info $(M)...Begin to helm-uninstall.) @ ## Helm-uninstall. kubectl delete -f https://raw.githubusercontent.com/kubesphere/ks-installer/master/roles/ks-core/prepare/files/ks-init/role-templates.yaml # Run tests +ENVTEST_ASSETS_DIR=$(shell pwd)/testbin test: vet ;$(info $(M)...Begin to run tests.) @ ## Run tests. - export KUBEBUILDER_CONTROLPLANE_START_TIMEOUT=2m; go test ./pkg/... ./cmd/... -covermode=atomic -coverprofile=coverage.txt + mkdir -p ${ENVTEST_ASSETS_DIR} + test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh + source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/... ./cmd/... -covermode=atomic -coverprofile=coverage.txt cd staging/src/kubesphere.io/api ; GOFLAGS="" go test ./... cd staging/src/kubesphere.io/client-go ; GOFLAGS="" go test ./... From 6064853692d49cfd9f04510a9227c144de9f080b Mon Sep 17 00:00:00 2001 From: yuswift Date: Mon, 30 Aug 2021 18:04:18 +0800 Subject: [PATCH 2/3] add new entry for env setup Signed-off-by: yuswift --- Makefile | 16 ++++++---------- hack/setup-kubebuilder-env.sh | 10 ++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100755 hack/setup-kubebuilder-env.sh diff --git a/Makefile b/Makefile index 0cb34889a..6b15c376d 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,6 @@ # Use of this source code is governed by a Apache license # that can be found in the LICENSE file. -# Setting SHELL to bash allows bash commands to be executed by recipes. -# This is a requirement for 'setup-envtest.sh' in the test target. -# Options are set to exit when a recipe line exits non-zero or a piped command fails. -SHELL = /usr/bin/env bash -o pipefail -.SHELLFLAGS = -ec - # Produce CRDs that work back to Kubernetes 1.11 (no version conversion) CRD_OPTIONS ?= "crd:trivialVersions=true" @@ -145,13 +139,15 @@ helm-uninstall: ; $(info $(M)...Begin to helm-uninstall.) @ ## Helm-uninstall. # Run tests ENVTEST_ASSETS_DIR=$(shell pwd)/testbin -test: vet ;$(info $(M)...Begin to run tests.) @ ## Run tests. - mkdir -p ${ENVTEST_ASSETS_DIR} - test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh - source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/... ./cmd/... -covermode=atomic -coverprofile=coverage.txt +test: vet test-env ;$(info $(M)...Begin to run tests.) @ ## Run tests. + export KUBEBUILDER_ASSETS=$(shell pwd)/testbin/bin; go test ./pkg/... ./cmd/... -covermode=atomic -coverprofile=coverage.txt cd staging/src/kubesphere.io/api ; GOFLAGS="" go test ./... cd staging/src/kubesphere.io/client-go ; GOFLAGS="" go test ./... +.PHONY: test-env +test-env: ;$(info $(M)...Begin to setup test env) @ ## Download unit test libraries e.g. kube-apiserver etcd. + @hack/setup-kubebuilder-env.sh + .PHONY: clean clean: ;$(info $(M)...Begin to clean.) @ ## Clean. -make -C ./pkg/version clean diff --git a/hack/setup-kubebuilder-env.sh b/hack/setup-kubebuilder-env.sh new file mode 100755 index 000000000..04e578164 --- /dev/null +++ b/hack/setup-kubebuilder-env.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# Setting SHELL to bash allows bash commands to be executed by recipes. +# This is a requirement for 'setup-envtest.sh' in the test target. +# Options are set to exit when a recipe line exits non-zero or a piped command fails. +SHELL="/usr/bin/env bash -o pipefail" + +ENVTEST_ASSETS_DIR=$(pwd)/testbin +mkdir -p ${ENVTEST_ASSETS_DIR} +test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh +source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools ${ENVTEST_ASSETS_DIR}; setup_envtest_env ${ENVTEST_ASSETS_DIR} From a242eaa4ebfa67a1dbd5295026c7f5e715b814e7 Mon Sep 17 00:00:00 2001 From: yuswift Date: Mon, 30 Aug 2021 19:40:39 +0800 Subject: [PATCH 3/3] fix shellcheck typo Signed-off-by: yuswift --- hack/setup-kubebuilder-env.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hack/setup-kubebuilder-env.sh b/hack/setup-kubebuilder-env.sh index 04e578164..e089e71eb 100755 --- a/hack/setup-kubebuilder-env.sh +++ b/hack/setup-kubebuilder-env.sh @@ -5,6 +5,6 @@ SHELL="/usr/bin/env bash -o pipefail" ENVTEST_ASSETS_DIR=$(pwd)/testbin -mkdir -p ${ENVTEST_ASSETS_DIR} -test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh -source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools ${ENVTEST_ASSETS_DIR}; setup_envtest_env ${ENVTEST_ASSETS_DIR} +mkdir -p "${ENVTEST_ASSETS_DIR}" +test -f "${ENVTEST_ASSETS_DIR}"/setup-envtest.sh || curl -sSLo "${ENVTEST_ASSETS_DIR}"/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh +source "${ENVTEST_ASSETS_DIR}"/setup-envtest.sh; fetch_envtest_tools "${ENVTEST_ASSETS_DIR}"; setup_envtest_env "${ENVTEST_ASSETS_DIR}"