Skip to content

Commit 310df59

Browse files
authored
upgrade kubebuilder to plugin/v4 (#418)
* upgrade kubebuilder to plugin/v4 * add test utils * fix controller test * fix gha test * chmod tortoisectl test * edit tortoisectl * fix lint * fix lint * add lint-fix to ci * go mod tidy * add make dependencies * remove lint-fix * upgrade tools * lint-fix * add tool chain version * change toolchain to 1.22 * add timeout * remove lint-fix * edit licenses * remove chmod
1 parent c878db0 commit 310df59

File tree

218 files changed

+1200
-828
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+1200
-828
lines changed

Diff for: .github/workflows/test.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515
name: Test
1616
runs-on: ubuntu-latest
1717
steps:
18-
- name: Set up Go 1.21
18+
- name: Set up Go 1.22
1919
uses: actions/[email protected]
2020
with:
21-
go-version: "1.21"
21+
go-version: "1.22"
2222
id: go
2323
- name: Check out code into the Go module directory
2424
uses: actions/checkout@v3
@@ -31,7 +31,7 @@ jobs:
3131
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
3232
restore-keys: |
3333
${{ runner.os }}-go-
34-
- name: Get dependencies
34+
- name: Dependencies
3535
run: make dependencies
3636
- name: Build
3737
run: make build

Diff for: Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.21 as builder
2+
FROM golang:1.22 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests
@@ -10,13 +10,13 @@ COPY go.sum go.sum
1010
RUN go mod download
1111

1212
# Copy the go source
13-
COPY main.go main.go
13+
COPY cmd/main.go cmd/main.go
1414
COPY api/ api/
15-
COPY controllers/ controllers/
15+
COPY internal/controller/ internal/controller/
1616
COPY pkg/ pkg/
1717

1818
# Build
19-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
19+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
2020

2121
# Use distroless as minimal base image to package the manager binary
2222
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Diff for: Makefile

+111-55
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
21
# Image URL to use all building/pushing image targets
3-
IMG ?= ghcr.io/mercari/tortoise:v0.9.0
2+
IMG ?= ghcr.io/mercari/tortoise:v0.11.0
43
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5-
ENVTEST_K8S_VERSION = 1.27.1
4+
ENVTEST_K8S_VERSION = 1.31.0
65

76
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
87
ifeq (,$(shell go env GOBIN))
@@ -11,6 +10,12 @@ else
1110
GOBIN=$(shell go env GOBIN)
1211
endif
1312

13+
# CONTAINER_TOOL defines the container tool to be used for building images.
14+
# Be aware that the target commands are only tested with Docker which is
15+
# scaffolded by default. However, you might want to replace it to use other
16+
# tools. (i.e. podman)
17+
CONTAINER_TOOL ?= docker
18+
1419
# Setting SHELL to bash allows bash commands to be executed by recipes.
1520
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
1621
SHELL = /usr/bin/env bash -o pipefail
@@ -23,7 +28,7 @@ all: build
2328

2429
# The help target prints out all targets with their descriptions organized
2530
# beneath their categories. The categories are represented by '##@' and the
26-
# target descriptions by '##'. The awk commands is responsible for reading the
31+
# target descriptions by '##'. The awk command is responsible for reading the
2732
# entire set of makefiles included in this invocation, looking for lines of the
2833
# file as xyz: ## something, and then pretty-format the target and help. Then,
2934
# if there's a line with ##@ something, that gets pretty-printed as a category.
@@ -39,11 +44,8 @@ help: ## Display this help.
3944
##@ Development
4045

4146
.PHONY: manifests
42-
manifests: kustomize controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
47+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
4348
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
44-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
45-
$(KUSTOMIZE) build config/default -o manifests/default
46-
$(KUSTOMIZE) build config/crd -o manifests/crd
4749

4850
.PHONY: generate
4951
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
@@ -57,39 +59,79 @@ fmt: ## Run go fmt against code.
5759
vet: ## Run go vet against code.
5860
go vet ./...
5961

60-
test: manifests generate fmt vet envtest ginkgo ## Run tests.
61-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) -r --cover --coverprofile cover.out
62-
63-
.PHONY: test-debug
64-
test-debug: envtest ginkgo
65-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" DEBUG=true $(GINKGO) -r --fail-fast -v --progress
66-
67-
.PHONY: test-update
68-
test-update: envtest ginkgo
69-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" UPDATE_TESTCASES=true $(GINKGO) -r --fail-fast
62+
.PHONY: test
63+
test: manifests generate fmt vet envtest ## Run tests.
64+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
65+
66+
# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
67+
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
68+
# Prometheus and CertManager are installed by default; skip with:
69+
# - PROMETHEUS_INSTALL_SKIP=true
70+
# - CERT_MANAGER_INSTALL_SKIP=true
71+
.PHONY: test-e2e
72+
test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
73+
@command -v kind >/dev/null 2>&1 || { \
74+
echo "Kind is not installed. Please install Kind manually."; \
75+
exit 1; \
76+
}
77+
@kind get clusters | grep -q 'kind' || { \
78+
echo "No Kind cluster is running. Please start a Kind cluster before running the e2e tests."; \
79+
exit 1; \
80+
}
81+
go test ./test/e2e/ -v -ginkgo.v
7082

71-
.PHONY: test-tortoisectl
72-
test-tortoisectl: envtest
73-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -timeout 30s -v -run Test_TortoiseCtlStop ./cmd/tortoisectl/test/...
74-
75-
GINKGO ?= $(LOCALBIN)/ginkgo
76-
GINKGO_VERSION ?= v2.1.4
83+
.PHONY: lint
84+
lint:
85+
golangci-lint run --timeout=10m ./...
7786

78-
.PHONY: ginkgo
79-
ginkgo: $(GINKGO) ## Download controller-gen locally if necessary.
80-
$(GINKGO): $(LOCALBIN)
81-
test -s $(LOCALBIN)/ginkgo || GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo@$(GINKGO_VERSION)
87+
.PHONY: lint-fix
88+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
89+
find . -name "*.go" | xargs gci write --section Standard --section Default --section "Prefix(github.com/mercari/tortoise)" --section blank --section dot
90+
@make lint args='--fix -v --timeout=10m' cons_args='-v'
8291

8392
##@ Build
8493

8594
.PHONY: build
86-
build: generate fmt vet ## Build manager binary.
87-
go build -o bin/manager main.go
88-
go build -o bin/tortoisectl cmd/tortoisectl/main.go
95+
build: manifests generate fmt vet ## Build manager binary.
96+
go build -o bin/manager cmd/main.go
8997

9098
.PHONY: run
9199
run: manifests generate fmt vet ## Run a controller from your host.
92-
go run ./main.go
100+
go run ./cmd/main.go
101+
102+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
103+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
104+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
105+
.PHONY: docker-build
106+
docker-build: ## Build docker image with the manager.
107+
$(CONTAINER_TOOL) build -t ${IMG} .
108+
109+
.PHONY: docker-push
110+
docker-push: ## Push docker image with the manager.
111+
$(CONTAINER_TOOL) push ${IMG}
112+
113+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
114+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
115+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
116+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
117+
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
118+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
119+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
120+
.PHONY: docker-buildx
121+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
122+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
123+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
124+
- $(CONTAINER_TOOL) buildx create --name project-v4-builder
125+
$(CONTAINER_TOOL) buildx use project-v4-builder
126+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
127+
- $(CONTAINER_TOOL) buildx rm project-v4-builder
128+
rm Dockerfile.cross
129+
130+
.PHONY: build-installer
131+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
132+
mkdir -p dist
133+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
134+
$(KUSTOMIZE) build config/default > dist/install.yaml
93135

94136
##@ Deployment
95137

@@ -99,63 +141,77 @@ endif
99141

100142
.PHONY: install
101143
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
102-
$(KUSTOMIZE) build config/crd | kubectl apply -f -
144+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
103145

104146
.PHONY: uninstall
105147
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
106-
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
148+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
107149

108150
.PHONY: deploy
109151
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
110-
$(KUSTOMIZE) build config/default | kubectl apply -f -
152+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
153+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
111154

112155
.PHONY: undeploy
113-
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
114-
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
156+
undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
157+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
115158

116-
##@ Build Dependencies
159+
##@ Dependencies
117160

118161
## Location to install dependencies to
119162
LOCALBIN ?= $(shell pwd)/bin
120163
$(LOCALBIN):
121164
mkdir -p $(LOCALBIN)
122165

123166
## Tool Binaries
167+
KUBECTL ?= kubectl
124168
KUSTOMIZE ?= $(LOCALBIN)/kustomize
125169
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
126170
ENVTEST ?= $(LOCALBIN)/setup-envtest
171+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
127172

128173
## Tool Versions
129-
KUSTOMIZE_VERSION ?= v3.8.7
130-
CONTROLLER_TOOLS_VERSION ?= v0.13.0
174+
KUSTOMIZE_VERSION ?= v5.4.3
175+
CONTROLLER_TOOLS_VERSION ?= v0.16.1
176+
ENVTEST_VERSION ?= release-0.19
177+
GOLANGCI_LINT_VERSION ?= v1.59.1
131178

132-
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
133179
.PHONY: kustomize
134180
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
135181
$(KUSTOMIZE): $(LOCALBIN)
136-
test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
182+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
137183

138184
.PHONY: controller-gen
139185
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
140186
$(CONTROLLER_GEN): $(LOCALBIN)
141-
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
187+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
142188

143189
.PHONY: envtest
144-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
190+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
145191
$(ENVTEST): $(LOCALBIN)
146-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
192+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
193+
194+
.PHONY: golangci-lint
195+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
196+
$(GOLANGCI_LINT): $(LOCALBIN)
197+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
147198

148-
## Install tools
149199
.PHONY: dependencies
150200
dependencies:
151201
@./scripts/dependencies.sh
152202

153-
.PHONY: lint
154-
lint:
155-
golangci-lint run $(args) ./...
156-
157-
.PHONY: lint-fix
158-
lint-fix:
159-
# Note: gci's autofix on golangci-lint was disabled. We can remove this if that is enabled again.
160-
find . -name "*.go" | xargs gci write --section Standard --section Default --section "Prefix(github.com/mercari/tortoise)" --section blank --section dot
161-
@make lint args='--fix -v' cons_args='-v'
203+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
204+
# $1 - target path with name of binary
205+
# $2 - package url which can be installed
206+
# $3 - specific version of package
207+
define go-install-tool
208+
@[ -f "$(1)-$(3)" ] || { \
209+
set -e; \
210+
package=$(2)@$(3) ;\
211+
echo "Downloading $${package}" ;\
212+
rm -f $(1) || true ;\
213+
GOBIN=$(LOCALBIN) go install $${package} ;\
214+
mv $(1) $(1)-$(3) ;\
215+
} ;\
216+
ln -sf $(1)-$(3) $(1)
217+
endef

Diff for: PROJECT

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: mercari.com
66
layout:
7-
- go.kubebuilder.io/v3
7+
- go.kubebuilder.io/v4
88
projectName: tortoise
99
repo: github.com/mercari/tortoise
1010
resources:

Diff for: api/autoscaling/v2/webhook_suite_test.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import (
3737
"time"
3838

3939
//+kubebuilder:scaffold:imports
40-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
41-
admissionv1 "k8s.io/api/admissionregistration/v1"
40+
admissionv1 "k8s.io/api/admission/v1"
41+
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
4242
autoscalingv1 "k8s.io/api/autoscaling/v1"
4343
autoscalingv2 "k8s.io/api/autoscaling/v2"
4444
"k8s.io/apimachinery/pkg/runtime"
@@ -50,6 +50,8 @@ import (
5050
"sigs.k8s.io/controller-runtime/pkg/envtest"
5151
logf "sigs.k8s.io/controller-runtime/pkg/log"
5252
"sigs.k8s.io/controller-runtime/pkg/log/zap"
53+
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
54+
"sigs.k8s.io/controller-runtime/pkg/webhook"
5355

5456
"github.com/mercari/tortoise/api/v1beta3"
5557
"github.com/mercari/tortoise/pkg/config"
@@ -84,8 +86,8 @@ var _ = BeforeSuite(func() {
8486

8587
y, err := os.ReadFile(filepath.Join("..", "..", "..", "config", "webhook", "manifests.yaml"))
8688
Expect(err).NotTo(HaveOccurred())
87-
mutatingWebhookConfig := &admissionv1.MutatingWebhookConfiguration{}
88-
validatingWebhookConfig := &admissionv1.ValidatingWebhookConfiguration{}
89+
mutatingWebhookConfig := &admissionregistrationv1.MutatingWebhookConfiguration{}
90+
validatingWebhookConfig := &admissionregistrationv1.ValidatingWebhookConfiguration{}
8991
d := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(y), 4096)
9092
err = d.Decode(mutatingWebhookConfig)
9193
Expect(err).NotTo(HaveOccurred())
@@ -119,8 +121,8 @@ var _ = BeforeSuite(func() {
119121
ErrorIfCRDPathMissing: false,
120122
WebhookInstallOptions: envtest.WebhookInstallOptions{
121123
Paths: []string{filepath.Join("..", "..", "..", "config", "webhook", "service.yaml")},
122-
MutatingWebhooks: []*admissionv1.MutatingWebhookConfiguration{mutatingWebhookConfig},
123-
ValidatingWebhooks: []*admissionv1.ValidatingWebhookConfiguration{validatingWebhookConfig},
124+
MutatingWebhooks: []*admissionregistrationv1.MutatingWebhookConfiguration{mutatingWebhookConfig},
125+
ValidatingWebhooks: []*admissionregistrationv1.ValidatingWebhookConfiguration{validatingWebhookConfig},
124126
},
125127
}
126128

@@ -139,7 +141,7 @@ var _ = BeforeSuite(func() {
139141
err = autoscalingv2.AddToScheme(scheme)
140142
Expect(err).NotTo(HaveOccurred())
141143

142-
err = admissionv1beta1.AddToScheme(scheme)
144+
err = admissionv1.AddToScheme(scheme)
143145
Expect(err).NotTo(HaveOccurred())
144146

145147
//+kubebuilder:scaffold:scheme
@@ -151,12 +153,14 @@ var _ = BeforeSuite(func() {
151153
// start webhook server using Manager
152154
webhookInstallOptions := &testEnv.WebhookInstallOptions
153155
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
154-
Scheme: scheme,
155-
Host: webhookInstallOptions.LocalServingHost,
156-
Port: webhookInstallOptions.LocalServingPort,
157-
CertDir: webhookInstallOptions.LocalServingCertDir,
158-
LeaderElection: false,
159-
MetricsBindAddress: "0",
156+
Scheme: scheme,
157+
LeaderElection: false,
158+
WebhookServer: webhook.NewServer(webhook.Options{
159+
Host: webhookInstallOptions.LocalServingHost,
160+
Port: webhookInstallOptions.LocalServingPort,
161+
CertDir: webhookInstallOptions.LocalServingCertDir,
162+
}),
163+
Metrics: metricsserver.Options{BindAddress: "0"},
160164
})
161165
Expect(err).NotTo(HaveOccurred())
162166
config, err := config.ParseConfig("")

0 commit comments

Comments
 (0)