Skip to content

Commit f22c575

Browse files
committed
refactor: upgrade kubebuilder to 3.11.1
Also upgrade: - kubebuilder plugin go to v4 - k8s libs to 1.27.13 - controller-runtime to 0.15.0 - ginko to 2.15.0 - and more...
1 parent 3879969 commit f22c575

Some content is hidden

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

60 files changed

+663
-469
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore build and test binaries.
3+
bin/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ zz_generated.*
2121

2222
# editor and IDE paraphernalia
2323
.idea
24+
.vscode
2425
*.swp
2526
*.swo
2627
*~

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ COPY go.sum go.sum
1313
RUN go mod download
1414

1515
# Copy the go source
16-
COPY main.go main.go
1716
COPY api/ api/
18-
COPY controllers/ controllers/
1917
COPY cmd/ cmd/
2018
COPY internal/ internal/
2119

Makefile

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Image URL to use all building/pushing image targets
33
IMG ?= controller:latest
44
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5-
ENVTEST_K8S_VERSION ?= 1.26
5+
ENVTEST_K8S_VERSION ?= 1.27
66

77
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
88
ifeq (,$(shell go env GOBIN))
@@ -11,6 +11,12 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# CONTAINER_TOOL defines the container tool to be used for building images.
15+
# Be aware that the target commands are only tested with Docker which is
16+
# scaffolded by default. However, you might want to replace it to use other
17+
# tools. (i.e. podman)
18+
CONTAINER_TOOL ?= docker
19+
1420
# Setting SHELL to bash allows bash commands to be executed by recipes.
1521
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
1622
SHELL = /usr/bin/env bash -o pipefail
@@ -59,26 +65,27 @@ vet: ## Run go vet against code.
5965
.PHONY: test
6066
test: manifests generate fmt vet envtest ## Run tests.
6167
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -v ./... -covermode=count -coverprofile cover.out
68+
6269
##@ Build
6370

6471
.PHONY: build
6572
build: manifests generate fmt vet ## Build manager binary.
66-
go build -o bin/manager main.go
73+
go build -o bin/manager cmd/cache/main.go
6774

6875
.PHONY: run
6976
run: manifests generate fmt vet ## Run a controller from your host.
70-
go run ./main.go
77+
go run ./cmd/cache/main.go
7178

7279
# If you wish built the manager image targeting other platforms you can use the --platform flag.
7380
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
7481
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
7582
.PHONY: docker-build
7683
docker-build: test ## Build docker image with the manager.
77-
docker build -t ${IMG} .
84+
$(CONTAINER_TOOL) build -t ${IMG} .
7885

7986
.PHONY: docker-push
8087
docker-push: ## Push docker image with the manager.
81-
docker push ${IMG}
88+
$(CONTAINER_TOOL) push ${IMG}
8289

8390
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
8491
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
@@ -91,10 +98,10 @@ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
9198
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
9299
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
93100
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
94-
- docker buildx create --name project-v3-builder
95-
docker buildx use project-v3-builder
96-
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
97-
- docker buildx rm project-v3-builder
101+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
102+
$(CONTAINER_TOOL) buildx use project-v3-builder
103+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
104+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
98105
rm Dockerfile.cross
99106

100107
##@ Deployment
@@ -105,20 +112,20 @@ endif
105112

106113
.PHONY: install
107114
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
108-
$(KUSTOMIZE) build config/crd | kubectl apply -f -
115+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
109116

110117
.PHONY: uninstall
111118
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.
112-
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
119+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
113120

114121
.PHONY: deploy
115122
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
116123
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
117-
$(KUSTOMIZE) build config/default | kubectl apply -f -
124+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
118125

119126
.PHONY: undeploy
120127
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.
121-
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
128+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
122129

123130
##@ Build Dependencies
124131

@@ -128,23 +135,23 @@ $(LOCALBIN):
128135
mkdir -p $(LOCALBIN)
129136

130137
## Tool Binaries
138+
KUBECTL ?= kubectl
131139
KUSTOMIZE ?= $(LOCALBIN)/kustomize
132140
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
133141
ENVTEST ?= $(LOCALBIN)/setup-envtest
134142

135143
## Tool Versions
136-
KUSTOMIZE_VERSION ?= v3.8.7
144+
KUSTOMIZE_VERSION ?= v5.0.1
137145
CONTROLLER_TOOLS_VERSION ?= v0.15.0
138146

139-
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
140147
.PHONY: kustomize
141148
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
142149
$(KUSTOMIZE): $(LOCALBIN)
143150
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
144151
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
145152
rm -rf $(LOCALBIN)/kustomize; \
146153
fi
147-
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
154+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
148155

149156
.PHONY: controller-gen
150157
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
@@ -155,7 +162,7 @@ $(CONTROLLER_GEN): $(LOCALBIN)
155162
.PHONY: envtest
156163
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
157164
$(ENVTEST): $(LOCALBIN)
158-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.17
165+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
159166

160167
# This is not used for kubebuilder, but to generate the Helm chart template README.
161168
.PHONY: helm-docs

PROJECT

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# More info: https://book.kubebuilder.io/reference/project-config.html
55
domain: enix.io
66
layout:
7-
- go.kubebuilder.io/v3
7+
- go.kubebuilder.io/v4
8+
multigroup: true
89
projectName: kube-image-keeper
910
repo: github.com/enix/kube-image-keeper
1011
resources:
@@ -15,7 +16,7 @@ resources:
1516
domain: enix.io
1617
group: kuik
1718
kind: CachedImage
18-
path: github.com/enix/kube-image-keeper/api/v1alpha1
19+
path: github.com/enix/kube-image-keeper/api/kuik/v1alpha1
1920
version: v1alpha1
2021
webhooks:
2122
defaulting: true
@@ -35,6 +36,6 @@ resources:
3536
domain: enix.io
3637
group: kuik
3738
kind: Repository
38-
path: github.com/enix/kube-image-keeper/api/v1alpha1
39+
path: github.com/enix/kube-image-keeper/api/kuik/v1alpha1
3940
version: v1alpha1
4041
version: "3"

api/v1/pod_webhook.go renamed to api/core/v1/pod_webhook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
_ "crypto/sha256"
1313

14-
"github.com/enix/kube-image-keeper/controllers"
14+
"github.com/enix/kube-image-keeper/internal/controller/core"
1515
"github.com/enix/kube-image-keeper/internal/registry"
1616
"github.com/google/go-containerregistry/pkg/name"
1717
admissionv1 "k8s.io/api/admission/v1"
@@ -82,10 +82,10 @@ func (a *ImageRewriter) RewriteImages(pod *corev1.Pod, isNewPod bool) []Rewritte
8282
pod.Labels = map[string]string{}
8383
}
8484

85-
rewriteImages := pod.Annotations[controllers.AnnotationRewriteImagesName] == "true" || isNewPod
85+
rewriteImages := pod.Annotations[core.AnnotationRewriteImagesName] == "true" || isNewPod
8686

87-
pod.Labels[controllers.LabelManagedName] = "true"
88-
pod.Annotations[controllers.AnnotationRewriteImagesName] = fmt.Sprintf("%t", rewriteImages)
87+
pod.Labels[core.LabelManagedName] = "true"
88+
pod.Annotations[core.AnnotationRewriteImagesName] = fmt.Sprintf("%t", rewriteImages)
8989

9090
rewrittenImages := []RewrittenImage{}
9191

api/v1/pod_webhook_test.go renamed to api/core/v1/pod_webhook_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"regexp"
77
"testing"
88

9-
"github.com/enix/kube-image-keeper/controllers"
9+
"github.com/enix/kube-image-keeper/internal/controller/core"
1010
"github.com/enix/kube-image-keeper/internal/registry"
1111
. "github.com/onsi/gomega"
1212
corev1 "k8s.io/api/core/v1"
@@ -43,7 +43,7 @@ func TestRewriteImages(t *testing.T) {
4343
}
4444

4545
ir.RewriteImages(&podStub, false)
46-
g.Expect(podStub.Annotations[controllers.AnnotationRewriteImagesName]).To(Equal("false"))
46+
g.Expect(podStub.Annotations[core.AnnotationRewriteImagesName]).To(Equal("false"))
4747

4848
ir.RewriteImages(&podStub, true)
4949

@@ -62,7 +62,7 @@ func TestRewriteImages(t *testing.T) {
6262
g.Expect(podStub.Spec.InitContainers).To(Equal(rewrittenInitContainers))
6363
g.Expect(podStub.Spec.Containers).To(Equal(rewrittenContainers))
6464

65-
g.Expect(podStub.Labels[controllers.LabelManagedName]).To(Equal("true"))
65+
g.Expect(podStub.Labels[core.LabelManagedName]).To(Equal("true"))
6666

6767
g.Expect(podStub.Annotations[registry.ContainerAnnotationKey("a", true)]).To(Equal("original-init"))
6868
g.Expect(podStub.Annotations[registry.ContainerAnnotationKey("b", false)]).To(Equal("original"))
@@ -72,7 +72,7 @@ func TestRewriteImages(t *testing.T) {
7272
g.Expect(podStub.Annotations[registry.ContainerAnnotationKey("f", false)]).To(Equal(""))
7373

7474
ir.RewriteImages(&podStub, false)
75-
g.Expect(podStub.Annotations[controllers.AnnotationRewriteImagesName]).To(Equal("true"))
75+
g.Expect(podStub.Annotations[core.AnnotationRewriteImagesName]).To(Equal("true"))
7676
})
7777
}
7878

@@ -105,7 +105,7 @@ func TestRewriteImagesWithIgnore(t *testing.T) {
105105
g.Expect(podStub.Spec.InitContainers).To(Equal(rewrittenInitContainers))
106106
g.Expect(podStub.Spec.Containers).To(Equal(rewrittenContainers))
107107

108-
g.Expect(podStub.Labels[controllers.LabelManagedName]).To(Equal("true"))
108+
g.Expect(podStub.Labels[core.LabelManagedName]).To(Equal("true"))
109109

110110
g.Expect(podStub.Annotations[registry.ContainerAnnotationKey("a", true)]).To(Equal(""))
111111
g.Expect(podStub.Annotations[registry.ContainerAnnotationKey("b", false)]).To(Equal(""))

cmd/cache/main.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1717
"sigs.k8s.io/controller-runtime/pkg/webhook"
1818

19-
kuikenixiov1 "github.com/enix/kube-image-keeper/api/v1"
20-
kuikv1alpha1 "github.com/enix/kube-image-keeper/api/v1alpha1"
21-
"github.com/enix/kube-image-keeper/controllers"
19+
kuikenixiov1 "github.com/enix/kube-image-keeper/api/core/v1"
20+
kuikv1alpha1 "github.com/enix/kube-image-keeper/api/kuik/v1alpha1"
2221
"github.com/enix/kube-image-keeper/internal"
22+
kuikController "github.com/enix/kube-image-keeper/internal/controller"
23+
"github.com/enix/kube-image-keeper/internal/controller/core"
24+
"github.com/enix/kube-image-keeper/internal/controller/kuik"
2325
"github.com/enix/kube-image-keeper/internal/registry"
2426
"github.com/enix/kube-image-keeper/internal/scheme"
2527
//+kubebuilder:scaffold:imports
@@ -85,7 +87,7 @@ func main() {
8587
os.Exit(1)
8688
}
8789

88-
if err = (&controllers.CachedImageReconciler{
90+
if err = (&kuik.CachedImageReconciler{
8991
Client: mgr.GetClient(),
9092
Scheme: mgr.GetScheme(),
9193
Recorder: mgr.GetEventRecorderFor("cachedimage-controller"),
@@ -98,7 +100,7 @@ func main() {
98100
setupLog.Error(err, "unable to create controller", "controller", "CachedImage")
99101
os.Exit(1)
100102
}
101-
if err = (&controllers.PodReconciler{
103+
if err = (&core.PodReconciler{
102104
Client: mgr.GetClient(),
103105
Scheme: mgr.GetScheme(),
104106
}).SetupWithManager(mgr); err != nil {
@@ -116,7 +118,7 @@ func main() {
116118
setupLog.Error(err, "unable to create webhook", "webhook", "CachedImage")
117119
os.Exit(1)
118120
}
119-
if err = (&controllers.RepositoryReconciler{
121+
if err = (&kuik.RepositoryReconciler{
120122
Client: mgr.GetClient(),
121123
Scheme: mgr.GetScheme(),
122124
Recorder: mgr.GetEventRecorderFor("epository-controller"),
@@ -132,23 +134,23 @@ func main() {
132134
os.Exit(1)
133135
}
134136

135-
if err := mgr.AddHealthzCheck("healthz", controllers.MakeChecker(controllers.Healthz)); err != nil {
137+
if err := mgr.AddHealthzCheck("healthz", kuikController.MakeChecker(kuikController.Healthz)); err != nil {
136138
setupLog.Error(err, "unable to set up health check")
137139
os.Exit(1)
138140
}
139-
if err := mgr.AddReadyzCheck("readyz", controllers.MakeChecker(controllers.Readyz)); err != nil {
141+
if err := mgr.AddReadyzCheck("readyz", kuikController.MakeChecker(kuikController.Readyz)); err != nil {
140142
setupLog.Error(err, "unable to set up ready check")
141143
os.Exit(1)
142144
}
143145

144-
controllers.SetLeader(false)
146+
kuikController.SetLeader(false)
145147
go func() {
146148
<-mgr.Elected()
147-
controllers.SetLeader(true)
149+
kuikController.SetLeader(true)
148150
}()
149151

150-
controllers.ProbeAddr = probeAddr
151-
controllers.RegisterMetrics(mgr.GetClient())
152+
kuikController.ProbeAddr = probeAddr
153+
kuikController.RegisterMetrics(mgr.GetClient())
152154

153155
setupLog.Info("starting manager")
154156
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {

cmd/proxy/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ func main() {
5959
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
6060
}
6161

62+
if err != nil {
63+
panic(err)
64+
}
65+
6266
klog.Info("starting")
6367

68+
httpClient, err := rest.HTTPClientFor(config)
6469
if err != nil {
6570
panic(err)
6671
}
67-
68-
restMapper, err := apiutil.NewDynamicRESTMapper(config, apiutil.WithLazyDiscovery)
72+
restMapper, err := apiutil.NewDynamicRESTMapper(config, httpClient)
6973
if err != nil {
7074
panic(err)
7175
}

config/certmanager/certificate.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ apiVersion: cert-manager.io/v1
55
kind: Issuer
66
metadata:
77
labels:
8-
app.kubernetes.io/name: issuer
9-
app.kubernetes.io/instance: selfsigned-issuer
8+
app.kubernetes.io/name: certificate
9+
app.kubernetes.io/instance: serving-cert
1010
app.kubernetes.io/component: certificate
11-
app.kubernetes.io/created-by: kuik
12-
app.kubernetes.io/part-of: kuik
11+
app.kubernetes.io/created-by: kube-image-keeper
12+
app.kubernetes.io/part-of: kube-image-keeper
1313
app.kubernetes.io/managed-by: kustomize
1414
name: selfsigned-issuer
1515
namespace: system
@@ -23,16 +23,16 @@ metadata:
2323
app.kubernetes.io/name: certificate
2424
app.kubernetes.io/instance: serving-cert
2525
app.kubernetes.io/component: certificate
26-
app.kubernetes.io/created-by: kuik
27-
app.kubernetes.io/part-of: kuik
26+
app.kubernetes.io/created-by: kube-image-keeper
27+
app.kubernetes.io/part-of: kube-image-keeper
2828
app.kubernetes.io/managed-by: kustomize
2929
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
3030
namespace: system
3131
spec:
32-
# $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize
32+
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
3333
dnsNames:
34-
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
35-
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
34+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
35+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
3636
issuerRef:
3737
kind: Issuer
3838
name: selfsigned-issuer
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
# This configuration is for teaching kustomize how to update name ref and var substitution
1+
# This configuration is for teaching kustomize how to update name ref substitution
22
nameReference:
33
- kind: Issuer
44
group: cert-manager.io
55
fieldSpecs:
66
- kind: Certificate
77
group: cert-manager.io
88
path: spec/issuerRef/name
9-
10-
varReference:
11-
- kind: Certificate
12-
group: cert-manager.io
13-
path: spec/commonName
14-
- kind: Certificate
15-
group: cert-manager.io
16-
path: spec/dnsNames

0 commit comments

Comments
 (0)