From e58f4cc23163a6495fcc47fe72b032bf37b34400 Mon Sep 17 00:00:00 2001 From: forsaken628 Date: Mon, 10 Jun 2024 17:50:40 +0800 Subject: [PATCH] mockgen Signed-off-by: forsaken628 --- Makefile | 18 ++++++++---------- hack/update-mockgen.sh | 23 +++++++++++------------ 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index c4e4ccee067..18f710264bf 100755 --- a/Makefile +++ b/Makefile @@ -1,12 +1,10 @@ HAS_YAMLLINT := $(shell command -v yamllint;) HAS_SHELLCHECK := $(shell command -v shellcheck;) -HAS_MOCKGEN := $(shell command -v mockgen;) COMMIT := v1beta1-$(shell git rev-parse --short=7 HEAD) KATIB_REGISTRY := docker.io/kubeflowkatib CPU_ARCH ?= linux/amd64,linux/arm64 ENVTEST_K8S_VERSION ?= 1.29 -MOCKGEN_VERSION ?= $(shell grep 'github.com/golang/mock' go.mod | cut -d ' ' -f 2) GO_VERSION=$(shell grep '^go' go.mod | cut -d ' ' -f 2) # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) @@ -87,14 +85,7 @@ controller-gen: # 3. Generate Python SDK for Katib (hack/gen-python-sdk/gen-sdk.sh) # 4. Generate gRPC manager APIs (pkg/apis/manager/v1beta1/build.sh and pkg/apis/manager/health/build.sh) # 5. Generate Go mock codes -generate: controller-gen -ifndef GOPATH - $(error GOPATH not defined, please define GOPATH. Run "go help gopath" to learn more about GOPATH) -endif -ifndef HAS_MOCKGEN - go install github.com/golang/mock/mockgen@$(MOCKGEN_VERSION) - $(info "mockgen has been installed") -endif +generate: controller-gen mockgen go generate ./pkg/... ./cmd/... hack/gen-python-sdk/gen-sdk.sh pkg/apis/manager/v1beta1/build.sh @@ -187,10 +178,12 @@ pytest-skopt: ##@ Dependencies ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION) GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION) +MOCKGEN = $(LOCALBIN)/mockgen-$(MOCKGEN_VERSION) ## Tool Versions ENVTEST_VERSION ?= bf15e44028f908c790721fc8fe67c7bf2d06a611 GOLANGCI_LINT_VERSION ?= v1.57.2 +MOCKGEN_VERSION ?= $(shell grep 'github.com/golang/mock' go.mod | cut -d ' ' -f 2) ## Location to install dependencies to LOCALBIN ?= $(shell pwd)/bin @@ -207,6 +200,11 @@ golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. $(GOLANGCI_LINT): $(LOCALBIN) $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION}) +.PHONY: mockgen +mockgen: $(MOCKGEN) ## Download mockgen locally if necessary. +$(MOCKGEN): $(LOCALBIN) + $(call go-install-tool,$(MOCKGEN),github.com/golang/mock/mockgen,${MOCKGEN_VERSION}) + # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary (ideally with version) # $2 - package url which can be installed diff --git a/hack/update-mockgen.sh b/hack/update-mockgen.sh index db738cdb067..48a405a2587 100755 --- a/hack/update-mockgen.sh +++ b/hack/update-mockgen.sh @@ -28,26 +28,25 @@ cd "${SCRIPT_ROOT}" # Grab mockgen version from go.mod MOCKGEN_VERSION=$(grep 'github.com/golang/mock' go.mod | awk '{print $2}') -if [[ ! $(mockgen -version) == "${MOCKGEN_VERSION}" ]]; then - echo "You must use ${MOCKGEN_VERSION} mockgen version to run this script" - echo "To install mockgen follow this doc: https://github.com/golang/mock/tree/main#installation" - echo "Run 'mockgen -version' to check the installed version" +MOCKGEN="./bin/mockgen-${MOCKGEN_VERSION}" +if [ ! -f "${MOCKGEN}" ]; then + echo "Please run 'make mockgen' first" exit 1 fi echo "Generating v1beta1 Suggestion RPC Client..." -mockgen -package mock -destination pkg/mock/v1beta1/api/suggestion.go github.com/kubeflow/katib/pkg/apis/manager/v1beta1 SuggestionClient +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/api/suggestion.go github.com/kubeflow/katib/pkg/apis/manager/v1beta1 SuggestionClient echo "Generating v1beta1 EarlyStopping RPC Client..." -mockgen -package mock -destination pkg/mock/v1beta1/api/earlystopping.go github.com/kubeflow/katib/pkg/apis/manager/v1beta1 EarlyStoppingClient +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/api/earlystopping.go github.com/kubeflow/katib/pkg/apis/manager/v1beta1 EarlyStoppingClient echo "Generating v1beta1 KatibDBInterface..." -mockgen -package mock -destination pkg/mock/v1beta1/db/db.go github.com/kubeflow/katib/pkg/db/v1beta1/common KatibDBInterface +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/db/db.go github.com/kubeflow/katib/pkg/db/v1beta1/common KatibDBInterface echo "Generating v1beta1 Generator..." -mockgen -package mock -destination pkg/mock/v1beta1/experiment/manifest/generator.go github.com/kubeflow/katib/pkg/controller.v1beta1/experiment/manifest Generator +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/experiment/manifest/generator.go github.com/kubeflow/katib/pkg/controller.v1beta1/experiment/manifest Generator echo "Generating v1beta1 KatibClient..." -mockgen -package mock -destination pkg/mock/v1beta1/util/katibclient/katibclient.go github.com/kubeflow/katib/pkg/util/v1beta1/katibclient Client +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/util/katibclient/katibclient.go github.com/kubeflow/katib/pkg/util/v1beta1/katibclient Client echo "Generating v1beta1 ManagerClient in Trial Controller..." -mockgen -package mock -destination pkg/mock/v1beta1/trial/managerclient/katibmanager.go github.com/kubeflow/katib/pkg/controller.v1beta1/trial/managerclient ManagerClient +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/trial/managerclient/katibmanager.go github.com/kubeflow/katib/pkg/controller.v1beta1/trial/managerclient ManagerClient echo "Generating v1beta1 Suggestion in Experiment Controller..." -mockgen -package mock -destination pkg/mock/v1beta1/experiment/suggestion/suggestion.go github.com/kubeflow/katib/pkg/controller.v1beta1/experiment/suggestion Suggestion +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/experiment/suggestion/suggestion.go github.com/kubeflow/katib/pkg/controller.v1beta1/experiment/suggestion Suggestion echo "Generating v1beta1 SuggestionClient in Suggestion Controller..." -mockgen -package mock -destination pkg/mock/v1beta1/suggestion/suggestionclient/suggestionclient.go github.com/kubeflow/katib/pkg/controller.v1beta1/suggestion/suggestionclient SuggestionClient +"${MOCKGEN}" -package mock -destination pkg/mock/v1beta1/suggestion/suggestionclient/suggestionclient.go github.com/kubeflow/katib/pkg/controller.v1beta1/suggestion/suggestionclient SuggestionClient