Skip to content

Commit 25f2e05

Browse files
authored
Upgrade kubebuilder v4.1.1 to v4.5.2 (#405)
* Upgrade kubebuilder v4.1.1 to v4.5.2 We've not done this in a while so there's a lot of changes made. Basically I used kubebuilder alpha generate to create all new files and structure that are created using the newer version. Then I diffed all files with the ones we already had and merged them together. I have not noticed any problems, everything builds OK and I was able to start several testruns using the controller so I am quite certain this works. Note that most changes are automatic and I did not make any functional changes from our side (except for a few golangci-lint complaints). The newer version of kubebuilder also enabled a few github workflows for testing so I fixed a few of the tests for us so they actually work. I will be sending another PR where I increase the coverage of our tests as well. * Fix the e2e tests so that they can be run multiple times There were a few dangling resources causing us to have to recreate the kind cluster every time we wanted to re-run tests. These tests can now be run multiple times in a row. I also enabled Prometheus in the e2e tests. This is not something we need right now, but we will probably need it in the future and it does not cost that much to add it. * Add conversion webhooks This is not something we are using at the moment, but sadly with the old version of kubebuilder these conversion webhooks were created automatically when creating webhooks in general. That means that we have a conversion webhook in the CRD spec and we are not allowed to update the CRDs without recreating them (when running with apply --server-side). * Enable prometheus * Remove a label for selector since it is immutable * Reduce the length of a line to make make lint pass * Fix the testrun_webhook so that it actually defaults to cluster Added more logging as well and fixed a few spelling errors
1 parent 7a6a5e6 commit 25f2e05

File tree

89 files changed

+2648
-1358
lines changed

Some content is hidden

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

89 files changed

+2648
-1358
lines changed

.devcontainer/devcontainer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "docker.io/golang:1.23",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+

.devcontainer/post-install.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -x
3+
4+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
5+
chmod +x ./kind
6+
mv ./kind /usr/local/bin/kind
7+
8+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
9+
chmod +x kubebuilder
10+
mv kubebuilder /usr/local/bin/
11+
12+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
13+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
14+
chmod +x kubectl
15+
mv kubectl /usr/local/bin/kubectl
16+
17+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
18+
19+
kind version
20+
kubebuilder version
21+
docker --version
22+
go version
23+
kubectl version --client

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
22
# Ignore build and test binaries.
3-
bin
3+
bin/
44
dist/install.yaml
55
config

.github/workflows/lint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
lint:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Run linter
21+
uses: golangci/golangci-lint-action@v6
22+
with:
23+
version: v1.63.4

.github/workflows/test-e2e.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: E2E Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test-e2e:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Install the latest version of kind
21+
run: |
22+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
23+
chmod +x ./kind
24+
sudo mv ./kind /usr/local/bin/kind
25+
26+
- name: Verify kind installation
27+
run: kind version
28+
29+
- name: Create kind cluster
30+
run: kind create cluster
31+
32+
- name: Running Test e2e
33+
run: |
34+
go mod tidy
35+
make test-e2e

.github/workflows/test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Running Tests
21+
run: |
22+
go mod tidy
23+
make test

.gitignore

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
# Temporary and binary files
1+
# Binaries for programs and plugins
22
*~
33
*.py[cod]
4+
*.exe
5+
*.exe~
6+
*.dll
47
*.so
8+
*.dylib
9+
bin/*
10+
Dockerfile.cross
511
*.cfg
612
!.isort.cfg
713
!setup.cfg
@@ -14,7 +20,6 @@ __pycache__/*
1420
*/.ipynb_checkpoints/*
1521
.DS_Store
1622
.tags
17-
bin
1823

1924
# Project files
2025
.ropeproject
@@ -25,6 +30,25 @@ bin
2530
.vscode
2631
tags
2732

33+
# Test binary, built with `go test -c`
34+
*.test
35+
36+
# Output of the go coverage tool, specifically when used with LiteIDE
37+
*.out
38+
39+
# Go workspace file
40+
go.work
41+
42+
# Kubernetes Generated files - skip generated files, except for vendored files
43+
!vendor/**/zz_generated.*
44+
45+
# editor and IDE paraphernalia
46+
.idea
47+
.vscode
48+
*.swp
49+
*.swo
50+
*~
51+
2852
# Package files
2953
*.egg
3054
*.eggs/

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ linters:
2121
enable:
2222
- dupl
2323
- errcheck
24-
- exportloopref
24+
- copyloopvar
2525
- ginkgolinter
2626
- goconst
2727
- gocyclo

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.22 AS builder
2+
FROM docker.io/golang:1.23 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

Makefile

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Image URL to use all building/pushing image targets
22
IMG ?= ghcr.io/eiffel-community/etos-controller:latest
3-
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
4-
ENVTEST_K8S_VERSION = 1.30.0
53

64
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
75
ifeq (,$(shell go env GOBIN))
@@ -60,12 +58,23 @@ vet: ## Run go vet against code.
6058
go vet ./...
6159

6260
.PHONY: test
63-
test: manifests generate fmt vet envtest ## Run tests.
61+
test: manifests generate fmt vet setup-envtest ## Run tests.
6462
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
6563

66-
# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
67-
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
68-
test-e2e:
64+
# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
65+
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
66+
# CertManager is installed by default; skip with:
67+
# - CERT_MANAGER_INSTALL_SKIP=true
68+
.PHONY: test-e2e
69+
test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
70+
@command -v $(KIND) >/dev/null 2>&1 || { \
71+
echo "Kind is not installed. Please install Kind manually."; \
72+
exit 1; \
73+
}
74+
@$(KIND) get clusters | grep -q 'kind' || { \
75+
echo "No Kind cluster is running. Please start a Kind cluster before running the e2e tests."; \
76+
exit 1; \
77+
}
6978
go test ./test/e2e/ -v -ginkgo.v
7079

7180
.PHONY: lint
@@ -76,6 +85,10 @@ lint: golangci-lint ## Run golangci-lint linter
7685
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
7786
$(GOLANGCI_LINT) run --fix
7887

88+
.PHONY: lint-config
89+
lint-config: golangci-lint ## Verify golangci-lint linter configuration
90+
$(GOLANGCI_LINT) config verify
91+
7992
##@ Build
8093

8194
.PHONY: build
@@ -156,16 +169,20 @@ $(LOCALBIN):
156169

157170
## Tool Binaries
158171
KUBECTL ?= kubectl
159-
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
160-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
161-
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
162-
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
172+
KIND ?= kind
173+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
174+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
175+
ENVTEST ?= $(LOCALBIN)/setup-envtest
176+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
163177

164178
## Tool Versions
165-
KUSTOMIZE_VERSION ?= v5.4.1
166-
CONTROLLER_TOOLS_VERSION ?= v0.15.0
167-
ENVTEST_VERSION ?= release-0.18
168-
GOLANGCI_LINT_VERSION ?= v1.57.2
179+
KUSTOMIZE_VERSION ?= v5.6.0
180+
CONTROLLER_TOOLS_VERSION ?= v0.17.2
181+
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
182+
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
183+
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
184+
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
185+
GOLANGCI_LINT_VERSION ?= v1.63.4
169186

170187
.PHONY: kustomize
171188
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
@@ -177,6 +194,14 @@ controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessar
177194
$(CONTROLLER_GEN): $(LOCALBIN)
178195
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
179196

197+
.PHONY: setup-envtest
198+
setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory.
199+
@echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..."
200+
@$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path || { \
201+
echo "Error: Failed to set up envtest binaries for version $(ENVTEST_K8S_VERSION)."; \
202+
exit 1; \
203+
}
204+
180205
.PHONY: envtest
181206
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
182207
$(ENVTEST): $(LOCALBIN)
@@ -185,18 +210,20 @@ $(ENVTEST): $(LOCALBIN)
185210
.PHONY: golangci-lint
186211
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
187212
$(GOLANGCI_LINT): $(LOCALBIN)
188-
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
213+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
189214

190215
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
191-
# $1 - target path with name of binary (ideally with version)
216+
# $1 - target path with name of binary
192217
# $2 - package url which can be installed
193218
# $3 - specific version of package
194219
define go-install-tool
195-
@[ -f $(1) ] || { \
220+
@[ -f "$(1)-$(3)" ] || { \
196221
set -e; \
197222
package=$(2)@$(3) ;\
198223
echo "Downloading $${package}" ;\
224+
rm -f $(1) || true ;\
199225
GOBIN=$(LOCALBIN) go install $${package} ;\
200-
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
201-
}
226+
mv $(1) $(1)-$(3) ;\
227+
} ;\
228+
ln -sf $(1)-$(3) $(1)
202229
endef

PROJECT

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ resources:
1818
path: github.com/eiffel-community/etos/api/v1alpha1
1919
version: v1alpha1
2020
webhooks:
21+
conversion: true
2122
defaulting: true
23+
validation: true
2224
webhookVersion: v1
2325
- api:
2426
crdVersion: v1
@@ -30,7 +32,9 @@ resources:
3032
path: github.com/eiffel-community/etos/api/v1alpha1
3133
version: v1alpha1
3234
webhooks:
35+
conversion: true
3336
defaulting: true
37+
validation: true
3438
webhookVersion: v1
3539
- api:
3640
crdVersion: v1
@@ -60,6 +64,7 @@ resources:
6064
path: github.com/eiffel-community/etos/api/v1alpha1
6165
version: v1alpha1
6266
webhooks:
67+
conversion: true
6368
defaulting: true
6469
webhookVersion: v1
6570
version: "3"

api/v1alpha1/testrun_webhook_test.go renamed to api/v1alpha1/environmentrequest_conversion.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,7 @@
1616

1717
package v1alpha1
1818

19-
import (
20-
. "github.com/onsi/ginkgo/v2"
21-
)
19+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2220

23-
var _ = Describe("TestRun Webhook", func() {
24-
25-
Context("When creating TestRun under Defaulting Webhook", func() {
26-
It("Should fill in the default value if a required field is empty", func() {
27-
28-
// TODO(user): Add your logic here
29-
30-
})
31-
})
32-
33-
})
21+
// Hub marks this type as a conversion hub.
22+
func (*EnvironmentRequest) Hub() {}

api/v1alpha1/environmentrequest_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type EnvironmentRequestStatus struct {
9898
}
9999

100100
// +kubebuilder:object:root=true
101+
// +kubebuilder:storageversion
102+
// +kubebuilder:conversion:hub
101103
// +kubebuilder:subresource:status
102104

103105
// EnvironmentRequest is the Schema for the environmentrequests API

0 commit comments

Comments
 (0)