Skip to content

Commit 6b54eb2

Browse files
authored
Add scripts to verify generated codes and Go Modules (#1999)
Signed-off-by: tenzen-y <[email protected]> Signed-off-by: tenzen-y <[email protected]>
1 parent bd91301 commit 6b54eb2

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

.github/workflows/test-go.yaml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,8 @@ jobs:
2929
with:
3030
go-version-file: ${{ env.GOPATH }}/src/github.com/kubeflow/katib/go.mod
3131

32-
# Verify that go.mod and go.sum is synchronized
33-
- name: Check Go modules
34-
run: |
35-
go mod tidy &&
36-
git add go.* &&
37-
git diff --cached --exit-code || (echo 'Please run "go mod tidy" to sync Go modules' && exit 1)
38-
39-
- name: Run Generate And Go Format Test
40-
run: |
41-
go mod download &&
42-
make check &&
43-
git add pkg/apis hack/gen-python-sdk &&
44-
git diff --cached --exit-code || (echo 'Please run "make check" to generate codes and to format Go codes' && exit 1)
32+
- name: Check Go Modules, Generated Go/Python codes, and Format
33+
run: make check
4534

4635
unittests:
4736
name: Unit Test

Makefile

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ HAS_LINT := $(shell command -v golangci-lint;)
22
HAS_YAMLLINT := $(shell command -v yamllint;)
33
HAS_SHELLCHECK := $(shell command -v shellcheck;)
44
HAS_SETUP_ENVTEST := $(shell command -v setup-envtest;)
5+
HAS_MOCKGEN := $(shell command -v mockgen;)
56

67
COMMIT := v1beta1-$(shell git rev-parse --short=7 HEAD)
78
KATIB_REGISTRY := docker.io/kubeflowkatib
89
CPU_ARCH ?= amd64
910
ENVTEST_K8S_VERSION ?= 1.25
11+
MOCKGEN_VERSION ?= $(shell grep 'github.com/golang/mock' go.mod | cut -d ' ' -f 2)
12+
GO_VERSION=$(shell grep '^go' go.mod | cut -d ' ' -f 2)
1013

1114
# for pytest
1215
PYTHONPATH := $(PYTHONPATH):$(CURDIR)/pkg/apis/manager/v1beta1/python:$(CURDIR)/pkg/apis/manager/health/python
@@ -21,26 +24,26 @@ test: envtest
2124
envtest:
2225
ifndef HAS_SETUP_ENVTEST
2326
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@2c3a6fa2996c026b284c7fe2b055274cd9a556bc #v0.13.0
24-
@echo "setup-envtest has been installed"
27+
$(info "setup-envtest has been installed")
2528
endif
26-
@echo "setup-envtest has already installed"
29+
$(info "setup-envtest has already installed")
2730

28-
check: generate fmt vet lint
31+
check: generated-codes go-mod fmt vet lint
2932

3033
fmt:
3134
hack/verify-gofmt.sh
3235

3336
lint:
3437
ifndef HAS_LINT
3538
go install github.com/golangci/golangci-lint/cmd/[email protected]
36-
@echo "golangci-lint has been installed"
39+
$(info "golangci-lint has been installed")
3740
endif
3841
hack/verify-golangci-lint.sh
3942

4043
yamllint:
4144
ifndef HAS_YAMLLINT
4245
pip install yamllint
43-
@echo "yamllint has been installed"
46+
$(info "yamllint has been installed")
4447
endif
4548
hack/verify-yamllint.sh
4649

@@ -50,7 +53,7 @@ vet:
5053
shellcheck:
5154
ifndef HAS_SHELLCHECK
5255
bash hack/install-shellcheck.sh
53-
@echo "shellcheck has been installed"
56+
$(info "shellcheck has been installed")
5457
endif
5558
hack/verify-shellcheck.sh
5659

@@ -65,19 +68,38 @@ deploy:
6568
undeploy:
6669
bash scripts/v1beta1/undeploy.sh
6770

71+
generated-codes: generate
72+
ifneq ($(shell bash hack/verify-generated-codes.sh '.'; echo $$?),0)
73+
$(error 'Please run "make generate" to generate codes')
74+
endif
75+
76+
go-mod: sync-go-mod
77+
ifneq ($(shell bash hack/verify-generated-codes.sh 'go.*'; echo $$?),0)
78+
$(error 'Please run "go mod tidy -go $(GO_VERSION)" to sync Go modules')
79+
endif
80+
81+
sync-go-mod:
82+
go mod tidy -go $(GO_VERSION)
83+
6884
# Run this if you update any existing controller APIs.
69-
# 1. Genereate deepcopy, clientset, listers, informers for the APIs (hack/update-codegen.sh)
85+
# 1. Generate deepcopy, clientset, listers, informers for the APIs (hack/update-codegen.sh)
7086
# 2. Generate open-api for the APIs (hack/update-openapigen)
7187
# 3. Generate Python SDK for Katib (hack/gen-python-sdk/gen-sdk.sh)
7288
# 4. Generate gRPC manager APIs (pkg/apis/manager/v1beta1/build.sh and pkg/apis/manager/health/build.sh)
89+
# 5. Generate Go mock codes
7390
generate:
7491
ifndef GOPATH
7592
$(error GOPATH not defined, please define GOPATH. Run "go help gopath" to learn more about GOPATH)
93+
endif
94+
ifndef HAS_MOCKGEN
95+
go install github.com/golang/mock/mockgen@$(MOCKGEN_VERSION)
96+
$(info "mockgen has been installed")
7697
endif
7798
go generate ./pkg/... ./cmd/...
7899
hack/gen-python-sdk/gen-sdk.sh
79100
pkg/apis/manager/v1beta1/build.sh
80101
pkg/apis/manager/health/build.sh
102+
hack/update-mockgen.sh
81103

82104
# Build images for the Katib v1beta1 components.
83105
build: generate

hack/verify-generated-codes.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2022 The Kubeflow Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
PATHS=( "${@}" )
22+
cd "$(dirname "$0")/.."
23+
24+
git diff --exit-code -- "${PATHS[@]}" > /dev/null

0 commit comments

Comments
 (0)