Skip to content

Commit ef1019c

Browse files
committed
migrate kubebuilder to v4
1 parent 4a2aa03 commit ef1019c

Some content is hidden

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

51 files changed

+466
-313
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bin
2222
*~
2323

2424
/manager
25+
./k6-operator
2526

2627
kubebuilder-tools-*
2728

Dockerfile.controller

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ COPY go.sum go.sum
1111
RUN go mod download
1212

1313
# Copy the go source
14-
COPY main.go main.go
14+
COPY cmd/main.go cmd/main.go
1515
COPY api/ api/
16-
COPY controllers/ controllers/
16+
COPY internal/controller/ internal/controller/
1717
COPY pkg/ pkg/
1818

1919

2020
# Build
21-
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o manager main.go
21+
RUN CGO_ENABLED=0 GOOS=linux go build -a -o manager cmd/main.go
2222

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

Makefile

Lines changed: 111 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SHELL = /usr/bin/env bash -o pipefail
66
# Current Operator version
77
VERSION ?= 0.0.21
88
# Image to use for building Go
9-
GO_BUILDER_IMG ?= "golang:1.22"
9+
GO_BUILDER_IMG ?= "golang:1.23"
1010
# Image URL to use all building/pushing image targets
1111
IMG_NAME ?= ghcr.io/grafana/k6-operator
1212
IMG_TAG ?= latest
@@ -24,53 +24,24 @@ BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
2424
endif
2525
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
2626

27-
## Location to install dependencies to
28-
LOCALBIN ?= $(shell pwd)/bin
29-
$(LOCALBIN):
30-
mkdir -p $(LOCALBIN)
31-
32-
## Tool Binaries
33-
KUSTOMIZE ?= $(LOCALBIN)/kustomize
34-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
35-
ENVTEST ?= $(LOCALBIN)/setup-envtest
36-
37-
## Tool Versions
38-
KUSTOMIZE_VERSION ?= v4.5.5 #v3.8.7
39-
CONTROLLER_TOOLS_VERSION ?= v0.16.1
40-
4127
CRD_OPTIONS ?= "crd:maxDescLen=0"
4228

43-
all: manager
29+
.PHONY: all
30+
all: build
4431

45-
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
46-
.PHONY: kustomize
47-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
48-
$(KUSTOMIZE): $(LOCALBIN)
49-
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
50-
51-
.PHONY: controller-gen
52-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
53-
$(CONTROLLER_GEN): $(LOCALBIN)
54-
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
55-
56-
.PHONY: envtest
57-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
58-
$(ENVTEST): $(LOCALBIN)
59-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
32+
.PHONY: help
33+
help: ## Display this help.
34+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
6035

6136
# Run tests
62-
ENVTEST_VERSION ?= latest # ref. https://github.com/kubernetes-sigs/controller-runtime/tree/main/tools/setup-envtest
63-
ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
64-
ENVTEST_K8S_VERSION ?= 1.30.0
6537
GOOS=$(shell go env GOOS)
6638
GOARCH=$(shell go env GOARCH)
6739
KUBEBUILDER_ASSETS_ROOT=/tmp
6840
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS_ROOT)/kubebuilder/bin
6941

70-
test: manifests generate fmt vet ## Run tests.
71-
mkdir -p ${ENVTEST_ASSETS_DIR}
72-
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
73-
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
42+
.PHONY: test
43+
test: manifests generate fmt vet envtest ## Run tests.
44+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
7445

7546
e2e: deploy
7647
kubectl create configmap crocodile-stress-test --from-file e2e/test.js
@@ -80,30 +51,13 @@ e2e-cleanup:
8051
kubectl delete configmap crocodile-stress-test
8152
kubectl delete -f e2e/test.yaml
8253

83-
# Build manager binary
84-
manager: generate fmt vet
85-
go build -o bin/manager main.go
54+
.PHONY: build
55+
build: manifests generate fmt vet ## Build manager binary.
56+
go build -o bin/manager cmd/main.go
8657

87-
# Run against the configured Kubernetes cluster in ~/.kube/config
88-
run: generate fmt vet manifests
89-
go run ./main.go
90-
91-
# Install CRDs into a cluster
92-
install: manifests kustomize
93-
$(KUSTOMIZE) build config/crd | kubectl apply -f -
94-
95-
# Uninstall CRDs from a cluster
96-
uninstall: manifests kustomize
97-
$(KUSTOMIZE) build config/crd | kubectl delete -f -
98-
99-
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
100-
deploy: manifests kustomize
101-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG_NAME}:${IMG_TAG}
102-
$(KUSTOMIZE) build config/default | kubectl apply -f -
103-
104-
# Delete operator from a cluster
105-
delete: manifests kustomize
106-
$(KUSTOMIZE) build config/default | kubectl delete -f -
58+
.PHONY: run
59+
run: manifests generate fmt vet ## Run a controller from your host.
60+
go run ./cmd/main.go
10761

10862
# Generate manifests e.g. CRD, RBAC etc.
10963
manifests: controller-gen
@@ -117,10 +71,13 @@ fmt:
11771
vet:
11872
go vet ./...
11973

120-
# Run golangci-lint
121-
lint:
122-
go install github.com/golangci/golangci-lint/cmd/[email protected]
123-
golangci-lint --timeout 5m run ./...
74+
.PHONY: lint
75+
lint: golangci-lint ## Run golangci-lint linter
76+
$(GOLANGCI_LINT) --timeout 5m run ./...
77+
78+
.PHONY: lint-fix
79+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
80+
$(GOLANGCI_LINT) run --fix
12481

12582
# Generate code
12683
generate: controller-gen
@@ -134,6 +91,34 @@ docker-build: test
13491
docker-push:
13592
docker push ${IMG_NAME}:${IMG_TAG}
13693

94+
# TODO: check and re-use in bundle.yaml creation
95+
# .PHONY: build-installer
96+
# build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
97+
# mkdir -p dist
98+
# cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
99+
# $(KUSTOMIZE) build config/default > dist/install.yaml
100+
101+
ifndef ignore-not-found
102+
ignore-not-found = false
103+
endif
104+
105+
.PHONY: install
106+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
107+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
108+
109+
.PHONY: uninstall
110+
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.
111+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
112+
113+
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
114+
deploy: manifests kustomize
115+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG_NAME}:${IMG_TAG}
116+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
117+
118+
# Delete operator from a cluster
119+
undeploy: manifests kustomize
120+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
121+
137122
# Generate bundle manifests and metadata, then validate generated files.
138123
.PHONY: bundle
139124
bundle: manifests
@@ -189,3 +174,64 @@ HELM=$(shell which helm)
189174
else
190175
HELM=$(shell which helm)
191176
endif
177+
178+
# ===============================================================
179+
# Dependencies
180+
# ===============================================================
181+
182+
## Location to install dependencies to
183+
LOCALBIN ?= $(shell pwd)/bin
184+
$(LOCALBIN):
185+
mkdir -p $(LOCALBIN)
186+
187+
## Tool Binaries
188+
KUBECTL ?= kubectl
189+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
190+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
191+
ENVTEST ?= $(LOCALBIN)/setup-envtest
192+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
193+
194+
## Tool Versions
195+
KUSTOMIZE_VERSION ?= v5.5.0
196+
CONTROLLER_TOOLS_VERSION ?= v0.16.1
197+
# ENVTEST_VERSION ?= latest # ref. https://github.com/kubernetes-sigs/controller-runtime/tree/main/tools/setup-envtest
198+
ENVTEST_VERSION ?= release-0.19
199+
GOLANGCI_LINT_VERSION ?= v1.61.0
200+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
201+
ENVTEST_K8S_VERSION = 1.31.0
202+
203+
.PHONY: kustomize
204+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
205+
$(KUSTOMIZE): $(LOCALBIN)
206+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
207+
208+
.PHONY: controller-gen
209+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
210+
$(CONTROLLER_GEN): $(LOCALBIN)
211+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
212+
213+
.PHONY: envtest
214+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
215+
$(ENVTEST): $(LOCALBIN)
216+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
217+
218+
.PHONY: golangci-lint
219+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
220+
$(GOLANGCI_LINT): $(LOCALBIN)
221+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
222+
223+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
224+
# $1 - target path with name of binary
225+
# $2 - package url which can be installed
226+
# $3 - specific version of package
227+
define go-install-tool
228+
@[ -f "$(1)-$(3)" ] || { \
229+
set -e; \
230+
package=$(2)@$(3) ;\
231+
echo "Downloading $${package}" ;\
232+
rm -f $(1) || true ;\
233+
GOBIN=$(LOCALBIN) go install $${package} ;\
234+
mv $(1) $(1)-$(3) ;\
235+
} ;\
236+
ln -sf $(1)-$(3) $(1)
237+
endef

PROJECT

Lines changed: 1 addition & 1 deletion
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: io
66
layout:
7-
- go.kubebuilder.io/v3
7+
- go.kubebuilder.io/v4
88
plugins:
99
manifests.sdk.operatorframework.io/v2: {}
1010
scorecard.sdk.operatorframework.io/v2: {}

main.go renamed to cmd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"os"
2222
"strings"
2323

24-
"github.com/grafana/k6-operator/controllers"
24+
controllers "github.com/grafana/k6-operator/internal/controller"
2525
"github.com/grafana/k6-operator/pkg/plz"
2626

2727
"k8s.io/apimachinery/pkg/runtime"
@@ -80,6 +80,7 @@ func main() {
8080
LeaderElection: enableLeaderElection,
8181
LeaderElectionID: "fcdfce80.io",
8282
LeaderElectionResourceLock: "leases",
83+
HealthProbeBindAddress: healthAddr,
8384
}
8485

8586
if watchNamespaces, multiNamespaced := getWatchNamespaces(); multiNamespaced {
@@ -94,7 +95,7 @@ func main() {
9495
} else if watchNamespace, namespaced := getWatchNamespace(); namespaced {
9596
mgrOpts.Cache = cache.Options{
9697
DefaultNamespaces: map[string]cache.Config{
97-
watchNamespace: cache.Config{},
98+
watchNamespace: {},
9899
},
99100
}
100101
setupLog.Info("WATCH_NAMESPACE is configured", "ns", watchNamespace)

config/certmanager/certificate.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.

config/certmanager/kustomization.yaml

Lines changed: 0 additions & 6 deletions
This file was deleted.

config/certmanager/kustomizeconfig.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)