Skip to content

Commit 5a36c88

Browse files
authored
Update the kubernetes to v1.30 and controller runtime to v0.18 (#1112)
1 parent b6aea6c commit 5a36c88

Some content is hidden

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

42 files changed

+4652
-852
lines changed

.bazeliskrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
USE_BAZEL_VERSION=5.4.0
1+
USE_BAZEL_VERSION=6.5.0

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414
linters-settings:
1515
staticcheck:
16-
go: "1.17"
16+
go: "1.24"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ dev/update-codegen:
244244
.PHONY: dev/update-crds
245245
dev/update-crds:
246246
@bazel run //hack/bin:controller-gen \
247-
crd:trivialVersions=true \
247+
crd \
248248
rbac:roleName=role \
249249
webhook \
250250
paths=./... \

WORKSPACE

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
workspace(name = "com_github_coachroachdb_cockroach_operator")
22

3+
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
4+
35
#####################################
46
# Bazel macros for downloading deps #
57
#####################################
68
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
7-
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
89

910
http_archive(
1011
name = "com_google_protobuf",
@@ -45,10 +46,10 @@ bazel_skylib_workspace()
4546

4647
http_archive(
4748
name = "io_bazel_rules_go",
48-
sha256 = "33acc4ae0f70502db4b893c9fc1dd7a9bf998c23e7ff2c4517741d4049a976f8",
49+
sha256 = "f2d15bea3e241aa0e3a90fb17a82e6a8ab12214789f6aeddd53b8d04316d2b7c",
4950
urls = [
50-
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
51-
"https://github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
51+
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.54.0/rules_go-v0.54.0.zip",
52+
"https://github.com/bazel-contrib/rules_go/releases/download/v0.54.0/rules_go-v0.54.0.zip",
5253
],
5354
)
5455

@@ -73,11 +74,10 @@ go_rules_dependencies()
7374
# gazelle:repository_macro hack/build/repos.bzl%_go_dependencies
7475
go_dependencies()
7576

76-
go_register_toolchains(version = "1.22.3")
77+
go_register_toolchains(version = "1.24.2")
7778

7879
gazelle_dependencies()
7980

80-
8181
################################
8282
# begin rules_oci dependencies #
8383
################################

apis/v1alpha1/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ go_library(
2929
"@io_k8s_sigs_controller_runtime//pkg/log:go_default_library",
3030
"@io_k8s_sigs_controller_runtime//pkg/scheme:go_default_library",
3131
"@io_k8s_sigs_controller_runtime//pkg/webhook:go_default_library",
32+
"@io_k8s_sigs_controller_runtime//pkg/webhook/admission:go_default_library",
3233
],
3334
)
3435

apis/v1alpha1/webhook.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"reflect"
2223

@@ -26,6 +27,7 @@ import (
2627
ctrl "sigs.k8s.io/controller-runtime"
2728
logf "sigs.k8s.io/controller-runtime/pkg/log"
2829
"sigs.k8s.io/controller-runtime/pkg/webhook"
30+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2931
)
3032

3133
var (
@@ -44,19 +46,24 @@ var (
4446
webhookLog = logf.Log.WithName("webhooks")
4547

4648
// this just ensures that we've implemented the interface
47-
_ webhook.Defaulter = &CrdbCluster{}
48-
_ webhook.Validator = &CrdbCluster{}
49+
_ webhook.CustomDefaulter = &CrdbCluster{}
50+
_ webhook.CustomValidator = &CrdbCluster{}
4951
)
5052

5153
// SetupWebhookWithManager ensures webhooks are enabled for the CrdbCluster resource.
5254
func (r *CrdbCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
53-
return ctrl.NewWebhookManagedBy(mgr).For(r).Complete()
55+
return ctrl.NewWebhookManagedBy(mgr).
56+
For(r).
57+
WithDefaulter(r).
58+
WithValidator(r).
59+
Complete()
5460
}
5561

5662
// +kubebuilder:webhook:path=/mutate-crdb-cockroachlabs-com-v1alpha1-crdbcluster,mutating=true,failurePolicy=fail,groups=crdb.cockroachlabs.com,resources=crdbclusters,verbs=create;update,versions=v1alpha1,name=mcrdbcluster.kb.io,sideEffects=None,admissionReviewVersions=v1
5763

5864
// Default implements webhook.Defaulter so a webhook will be registered for the type.
59-
func (r *CrdbCluster) Default() {
65+
func (r *CrdbCluster) Default(ctx context.Context, obj runtime.Object) error {
66+
r = obj.(*CrdbCluster)
6067
webhookLog.Info("default", "name", r.Name)
6168

6269
if r.Spec.GRPCPort == nil {
@@ -79,12 +86,15 @@ func (r *CrdbCluster) Default() {
7986
policy := v1.PullIfNotPresent
8087
r.Spec.Image.PullPolicyName = &policy
8188
}
89+
90+
return nil
8291
}
8392

8493
// +kubebuilder:webhook:path=/validate-crdb-cockroachlabs-com-v1alpha1-crdbcluster,mutating=false,failurePolicy=fail,groups=crdb.cockroachlabs.com,resources=crdbclusters,verbs=create;update,versions=v1alpha1,name=vcrdbcluster.kb.io,sideEffects=None,admissionReviewVersions=v1
8594

8695
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
87-
func (r *CrdbCluster) ValidateCreate() error {
96+
func (r *CrdbCluster) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
97+
r = obj.(*CrdbCluster)
8898
webhookLog.Info("validate create", "name", r.Name)
8999
var errors []error
90100
if r.Spec.Ingress != nil {
@@ -102,20 +112,21 @@ func (r *CrdbCluster) ValidateCreate() error {
102112
}
103113

104114
if len(errors) != 0 {
105-
return kerrors.NewAggregate(errors)
115+
return nil, kerrors.NewAggregate(errors)
106116
}
107117

108-
return nil
118+
return nil, nil
109119
}
110120

111121
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
112-
func (r *CrdbCluster) ValidateUpdate(old runtime.Object) error {
122+
func (r *CrdbCluster) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
123+
r = newObj.(*CrdbCluster)
113124
webhookLog.Info("validate update", "name", r.Name)
114125
var errors []error
115126

116-
oldCluster, ok := old.(*CrdbCluster)
127+
oldCluster, ok := oldObj.(*CrdbCluster)
117128
if !ok {
118-
webhookLog.Info(fmt.Sprintf("unexpected old cluster type %T", old))
129+
webhookLog.Info(fmt.Sprintf("unexpected old cluster type %T", oldObj))
119130
} else {
120131
// Validate if labels changed.
121132
// k8s does not support changing selector/labels on sts:
@@ -136,18 +147,19 @@ func (r *CrdbCluster) ValidateUpdate(old runtime.Object) error {
136147
}
137148

138149
if len(errors) != 0 {
139-
return kerrors.NewAggregate(errors)
150+
return nil, kerrors.NewAggregate(errors)
140151
}
141152

142-
return nil
153+
return nil, nil
143154
}
144155

145156
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
146-
func (r *CrdbCluster) ValidateDelete() error {
157+
func (r *CrdbCluster) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
158+
r = obj.(*CrdbCluster)
147159
webhookLog.Info("validate delete", "name", r.Name)
148160

149161
// we're not validating anything on delete. This is just a placeholder for now to satisfy the Validator interface
150-
return nil
162+
return nil, nil
151163
}
152164

153165
// ValidateIngress validates the ingress configuration used to create ingress resource

apis/v1alpha1/webhook_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ limitations under the License.
1717
package v1alpha1_test
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"testing"
2223

2324
. "github.com/cockroachdb/cockroach-operator/apis/v1alpha1"
2425
"github.com/stretchr/testify/require"
2526
v1 "k8s.io/api/core/v1"
26-
"k8s.io/apimachinery/pkg/runtime"
2727
)
2828

2929
func TestCrdbClusterDefault(t *testing.T) {
@@ -43,7 +43,8 @@ func TestCrdbClusterDefault(t *testing.T) {
4343
Image: &PodImage{PullPolicyName: &policy},
4444
}
4545

46-
cluster.Default()
46+
ctx := context.Background()
47+
_ = cluster.Default(ctx, cluster)
4748
require.Equal(t, expected, cluster.Spec)
4849
}
4950

@@ -137,8 +138,9 @@ func TestCreateCrdbCluster(t *testing.T) {
137138
},
138139
}
139140

141+
ctx := context.Background()
140142
for _, testcase := range testcases {
141-
err := testcase.Cluster.ValidateCreate()
143+
_, err := testcase.Cluster.ValidateCreate(ctx, testcase.Cluster)
142144
if testcase.ErrMsg == "" {
143145
require.NoError(t, err)
144146
continue
@@ -176,8 +178,9 @@ func TestUpdateCrdbCluster(t *testing.T) {
176178
},
177179
}
178180

181+
ctx := context.Background()
179182
for _, testcase := range testcases {
180-
err := testcase.Cluster.ValidateUpdate(&oldCluster)
183+
_, err := testcase.Cluster.ValidateUpdate(ctx, &oldCluster, testcase.Cluster)
181184
require.Error(t, err)
182185
require.Equal(t, err.Error(), testcase.ErrMsg)
183186
}
@@ -258,8 +261,9 @@ func TestUpdateCrdbClusterLabels(t *testing.T) {
258261
},
259262
}
260263

264+
ctx := context.Background()
261265
for _, tc := range testcases {
262-
err := tc.Cluster.ValidateUpdate(runtime.Object(&oldCluster))
266+
_, err := tc.Cluster.ValidateUpdate(ctx, &oldCluster, tc.Cluster)
263267
if tc.ShouldError {
264268
require.Error(t, err)
265269
require.Equal(t, err.Error(), "mutating additionalLabels field is not supported")

cmd/cockroach-operator/BUILD.bazel

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template")
2-
load("@io_bazel_rules_go//go:def.bzl","go_library")
2+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
33
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_image_index")
44
load("@rules_pkg//:pkg.bzl", "pkg_tar")
55

@@ -44,6 +44,8 @@ go_library(
4444
"@io_k8s_sigs_controller_runtime//:go_default_library",
4545
"@io_k8s_sigs_controller_runtime//pkg/cache:go_default_library",
4646
"@io_k8s_sigs_controller_runtime//pkg/log/zap:go_default_library",
47+
"@io_k8s_sigs_controller_runtime//pkg/metrics/server:go_default_library",
48+
"@io_k8s_sigs_controller_runtime//pkg/webhook:go_default_library",
4749
],
4850
)
4951

@@ -67,26 +69,26 @@ oci_image(
6769
name = "cockroach_image_linux_amd64",
6870
# References container_pull from WORKSPACE
6971
base = "@redhat_ubi_minimal_linux_amd64",
72+
entrypoint = ["./cockroach-operator"],
7073
labels = ":labels",
7174
tars = [
7275
":licenses",
7376
"//cmd/cockroach-operator/linux-amd64:cockroach-linux-amd64-tar",
7477
":operator_image_linux_amd64",
7578
],
76-
entrypoint = ["./cockroach-operator"],
7779
)
7880

7981
oci_image(
8082
name = "cockroach_image_linux_arm64",
8183
# References container_pull from WORKSPACE
8284
base = "@redhat_ubi_minimal_linux_arm64",
85+
entrypoint = ["./cockroach-operator"],
8386
labels = ":labels",
8487
tars = [
8588
":licenses",
8689
"//cmd/cockroach-operator/linux-arm64:cockroach-linux-arm64-tar",
8790
":operator_image_linux_arm64",
8891
],
89-
entrypoint = ["./cockroach-operator"],
9092
)
9193

9294
pkg_tar(

cmd/cockroach-operator/main.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
ctrl "sigs.k8s.io/controller-runtime"
3232
"sigs.k8s.io/controller-runtime/pkg/cache"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34+
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
35+
"sigs.k8s.io/controller-runtime/pkg/webhook"
3436
)
3537

3638
const (
@@ -52,6 +54,7 @@ func init() {
5254
func main() {
5355
var metricsAddr, featureGatesString, leaderElectionID string
5456
var enableLeaderElection, skipWebhookConfig bool
57+
var err error
5558

5659
// use zap logging cli options
5760
opts := zap.Options{}
@@ -88,19 +91,31 @@ func main() {
8891
watchNamespace := os.Getenv(watchNamespaceEnvVar)
8992

9093
mgrOpts := ctrl.Options{
91-
Scheme: scheme,
92-
Namespace: watchNamespace,
93-
MetricsBindAddress: metricsAddr,
94-
LeaderElection: enableLeaderElection,
95-
LeaderElectionID: leaderElectionID,
96-
Port: 9443,
97-
CertDir: certDir,
94+
Scheme: scheme,
95+
Cache: cache.Options{
96+
DefaultNamespaces: map[string]cache.Config{
97+
watchNamespace: cache.Config{},
98+
},
99+
},
100+
Metrics: server.Options{
101+
BindAddress: metricsAddr,
102+
},
103+
LeaderElection: enableLeaderElection,
104+
LeaderElectionID: leaderElectionID,
105+
WebhookServer: webhook.NewServer(webhook.Options{
106+
Port: 9443,
107+
CertDir: certDir,
108+
}),
98109
}
99110

100111
if strings.Contains(watchNamespace, ",") {
101112
setupLog.Info("manager set up with multiple namespaces", "namespaces", watchNamespace)
102-
mgrOpts.Namespace = ""
103-
mgrOpts.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(watchNamespace, ","))
113+
namespaces := strings.Split(watchNamespace, ",")
114+
for _, ns := range namespaces {
115+
setupLog.Info("watching namespace", "namespace", ns)
116+
mgrOpts.Cache.DefaultNamespaces[ns] = cache.Config{}
117+
}
118+
mgrOpts.NewCache = cache.New
104119
}
105120

106121
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), mgrOpts)

0 commit comments

Comments
 (0)