Skip to content

Commit de677e2

Browse files
committed
Bump controller runtime to k8s api 1.27
- controller runtime v0.15 contains breaking changes where Validator interfaces have been modified to include warning in return types: kubernetes-sigs/controller-runtime#2014 other breaking changes do not affect us
1 parent 3860450 commit de677e2

Some content is hidden

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

41 files changed

+377
-830
lines changed

api/v1alpha1/superstream_webhook.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/apimachinery/pkg/util/validation/field"
1717
ctrl "sigs.k8s.io/controller-runtime"
1818
"sigs.k8s.io/controller-runtime/pkg/webhook"
19+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1920
)
2021

2122
func (s *SuperStream) SetupWebhookWithManager(mgr ctrl.Manager) error {
@@ -30,48 +31,48 @@ var _ webhook.Validator = &SuperStream{}
3031

3132
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
3233
// either rabbitmqClusterReference.name or rabbitmqClusterReference.connectionSecret must be provided but not both
33-
func (s *SuperStream) ValidateCreate() error {
34+
func (s *SuperStream) ValidateCreate() (admission.Warnings, error) {
3435
return s.Spec.RabbitmqClusterReference.ValidateOnCreate(s.GroupResource(), s.Name)
3536
}
3637

3738
// ValidateUpdate returns error type 'forbidden' for updates on superstream name, vhost and rabbitmqClusterReference
38-
func (s *SuperStream) ValidateUpdate(old runtime.Object) error {
39+
func (s *SuperStream) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
3940
oldSuperStream, ok := old.(*SuperStream)
4041
if !ok {
41-
return apierrors.NewBadRequest(fmt.Sprintf("expected a superstream but got a %T", old))
42+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a superstream but got a %T", old))
4243
}
4344

4445
detailMsg := "updates on name, vhost and rabbitmqClusterReference are all forbidden"
4546
if s.Spec.Name != oldSuperStream.Spec.Name {
46-
return apierrors.NewForbidden(s.GroupResource(), s.Name,
47+
return nil, apierrors.NewForbidden(s.GroupResource(), s.Name,
4748
field.Forbidden(field.NewPath("spec", "name"), detailMsg))
4849
}
4950
if s.Spec.Vhost != oldSuperStream.Spec.Vhost {
50-
return apierrors.NewForbidden(s.GroupResource(), s.Name,
51+
return nil, apierrors.NewForbidden(s.GroupResource(), s.Name,
5152
field.Forbidden(field.NewPath("spec", "vhost"), detailMsg))
5253
}
5354

5455
if !oldSuperStream.Spec.RabbitmqClusterReference.Matches(&s.Spec.RabbitmqClusterReference) {
55-
return apierrors.NewForbidden(s.GroupResource(), s.Name,
56+
return nil, apierrors.NewForbidden(s.GroupResource(), s.Name,
5657
field.Forbidden(field.NewPath("spec", "rabbitmqClusterReference"), detailMsg))
5758
}
5859

5960
if !routingKeyUpdatePermitted(oldSuperStream.Spec.RoutingKeys, s.Spec.RoutingKeys) {
60-
return apierrors.NewForbidden(s.GroupResource(), s.Name,
61+
return nil, apierrors.NewForbidden(s.GroupResource(), s.Name,
6162
field.Forbidden(field.NewPath("spec", "routingKeys"), "updates may only add to the existing list of routing keys"))
6263
}
6364

6465
if s.Spec.Partitions < oldSuperStream.Spec.Partitions {
65-
return apierrors.NewForbidden(s.GroupResource(), s.Name,
66+
return nil, apierrors.NewForbidden(s.GroupResource(), s.Name,
6667
field.Forbidden(field.NewPath("spec", "partitions"), "updates may only increase the partition count, and may not decrease it"))
6768
}
6869

69-
return nil
70+
return nil, nil
7071
}
7172

7273
// ValidateDelete no validation on delete
73-
func (s *SuperStream) ValidateDelete() error {
74-
return nil
74+
func (s *SuperStream) ValidateDelete() (admission.Warnings, error) {
75+
return nil, nil
7576
}
7677

7778
// routingKeyUpdatePermitted allows updates only if adding additional keys at the end of the list of keys

api/v1alpha1/superstream_webhook_test.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -31,73 +31,84 @@ var _ = Describe("superstream webhook", func() {
3131
It("does not allow both spec.rabbitmqClusterReference.name and spec.rabbitmqClusterReference.connectionSecret be configured", func() {
3232
notAllowed := superstream.DeepCopy()
3333
notAllowed.Spec.RabbitmqClusterReference.ConnectionSecret = &corev1.LocalObjectReference{Name: "some-secret"}
34-
Expect(apierrors.IsForbidden(notAllowed.ValidateCreate())).To(BeTrue())
34+
_, err := notAllowed.ValidateCreate()
35+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
3536
})
3637

3738
It("spec.rabbitmqClusterReference.name and spec.rabbitmqClusterReference.connectionSecret cannot both be empty", func() {
3839
notAllowed := superstream.DeepCopy()
3940
notAllowed.Spec.RabbitmqClusterReference.Name = ""
4041
notAllowed.Spec.RabbitmqClusterReference.ConnectionSecret = nil
41-
Expect(apierrors.IsForbidden(notAllowed.ValidateCreate())).To(BeTrue())
42+
_, err := notAllowed.ValidateCreate()
43+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
4244
})
4345
})
4446

4547
Context("ValidateUpdate", func() {
4648
It("does not allow updates on superstream name", func() {
4749
newSuperStream := superstream.DeepCopy()
4850
newSuperStream.Spec.Name = "new-name"
49-
Expect(apierrors.IsForbidden(newSuperStream.ValidateUpdate(&superstream))).To(BeTrue())
51+
_, err := newSuperStream.ValidateUpdate(&superstream)
52+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
5053
})
5154

5255
It("does not allow updates on superstream vhost", func() {
5356
newSuperStream := superstream.DeepCopy()
5457
newSuperStream.Spec.Vhost = "new-vhost"
55-
Expect(apierrors.IsForbidden(newSuperStream.ValidateUpdate(&superstream))).To(BeTrue())
58+
_, err := newSuperStream.ValidateUpdate(&superstream)
59+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
5660
})
5761

5862
It("does not allow updates on RabbitmqClusterReference", func() {
5963
newSuperStream := superstream.DeepCopy()
6064
newSuperStream.Spec.RabbitmqClusterReference = topologyv1beta1.RabbitmqClusterReference{
6165
Name: "new-cluster",
6266
}
63-
Expect(apierrors.IsForbidden(newSuperStream.ValidateUpdate(&superstream))).To(BeTrue())
67+
_, err := newSuperStream.ValidateUpdate(&superstream)
68+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
6469
})
6570

6671
It("does not allow updates on rabbitmqClusterReference.connectionSecret", func() {
6772
newSuperStream := superstream.DeepCopy()
6873
newSuperStream.Spec.RabbitmqClusterReference = topologyv1beta1.RabbitmqClusterReference{ConnectionSecret: &corev1.LocalObjectReference{Name: "a-secret"}}
69-
Expect(apierrors.IsForbidden(newSuperStream.ValidateUpdate(&superstream))).To(BeTrue())
74+
_, err := newSuperStream.ValidateUpdate(&superstream)
75+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
7076
})
7177

7278
It("does not allow updates on superstream.spec.routingKeys", func() {
7379
newSuperStream := superstream.DeepCopy()
7480
newSuperStream.Spec.RoutingKeys = []string{"a1", "d6"}
75-
Expect(apierrors.IsForbidden(newSuperStream.ValidateUpdate(&superstream))).To(BeTrue())
81+
_, err := newSuperStream.ValidateUpdate(&superstream)
82+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
7683
})
7784

7885
It("if the superstream previously had routing keys and the update only appends, the update succeeds", func() {
7986
newSuperStream := superstream.DeepCopy()
8087
newSuperStream.Spec.RoutingKeys = []string{"a1", "b2", "f17", "z66"}
81-
Expect(newSuperStream.ValidateUpdate(&superstream)).To(Succeed())
88+
_, err := newSuperStream.ValidateUpdate(&superstream)
89+
Expect(err).NotTo(HaveOccurred())
8290
})
8391

8492
It("if the superstream previously had no routing keys but now does, the update fails", func() {
8593
superstream.Spec.RoutingKeys = nil
8694
newSuperStream := superstream.DeepCopy()
8795
newSuperStream.Spec.RoutingKeys = []string{"a1", "b2", "f17"}
88-
Expect(apierrors.IsForbidden(newSuperStream.ValidateUpdate(&superstream))).To(BeTrue())
96+
_, err := newSuperStream.ValidateUpdate(&superstream)
97+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
8998
})
9099

91100
It("allows superstream.spec.partitions to be increased", func() {
92101
newSuperStream := superstream.DeepCopy()
93102
newSuperStream.Spec.Partitions = 1000
94-
Expect(newSuperStream.ValidateUpdate(&superstream)).To(Succeed())
103+
_, err := newSuperStream.ValidateUpdate(&superstream)
104+
Expect(err).NotTo(HaveOccurred())
95105
})
96106

97107
It("does not allow superstream.spec.partitions to be decreased", func() {
98108
newSuperStream := superstream.DeepCopy()
99109
newSuperStream.Spec.Partitions = 1
100-
Expect(apierrors.IsForbidden(newSuperStream.ValidateUpdate(&superstream))).To(BeTrue())
110+
_, err := newSuperStream.ValidateUpdate(&superstream)
111+
Expect(apierrors.IsForbidden(err)).To(BeTrue())
101112
})
102113
})
103114
})

api/v1beta1/binding_webhook.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"reflect"
99
ctrl "sigs.k8s.io/controller-runtime"
1010
"sigs.k8s.io/controller-runtime/pkg/webhook"
11+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1112
)
1213

1314
func (b *Binding) SetupWebhookWithManager(mgr ctrl.Manager) error {
@@ -22,27 +23,27 @@ var _ webhook.Validator = &Binding{}
2223

2324
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
2425
// either rabbitmqClusterReference.name or rabbitmqClusterReference.connectionSecret must be provided but not both
25-
func (b *Binding) ValidateCreate() error {
26+
func (b *Binding) ValidateCreate() (admission.Warnings, error) {
2627
return b.Spec.RabbitmqClusterReference.ValidateOnCreate(b.GroupResource(), b.Name)
2728
}
2829

2930
// ValidateUpdate updates on vhost and rabbitmqClusterReference are forbidden
30-
func (b *Binding) ValidateUpdate(old runtime.Object) error {
31+
func (b *Binding) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
3132
oldBinding, ok := old.(*Binding)
3233
if !ok {
33-
return apierrors.NewBadRequest(fmt.Sprintf("expected a binding but got a %T", old))
34+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a binding but got a %T", old))
3435
}
3536

3637
var allErrs field.ErrorList
3738
detailMsg := "updates on vhost and rabbitmqClusterReference are all forbidden"
3839

3940
if b.Spec.Vhost != oldBinding.Spec.Vhost {
40-
return apierrors.NewForbidden(b.GroupResource(), b.Name,
41+
return nil, apierrors.NewForbidden(b.GroupResource(), b.Name,
4142
field.Forbidden(field.NewPath("spec", "vhost"), detailMsg))
4243
}
4344

4445
if !oldBinding.Spec.RabbitmqClusterReference.Matches(&b.Spec.RabbitmqClusterReference) {
45-
return apierrors.NewForbidden(b.GroupResource(), b.Name,
46+
return nil, apierrors.NewForbidden(b.GroupResource(), b.Name,
4647
field.Forbidden(field.NewPath("spec", "rabbitmqClusterReference"), detailMsg))
4748
}
4849

@@ -87,12 +88,12 @@ func (b *Binding) ValidateUpdate(old runtime.Object) error {
8788
}
8889

8990
if len(allErrs) == 0 {
90-
return nil
91+
return nil, nil
9192
}
9293

93-
return apierrors.NewInvalid(GroupVersion.WithKind("Binding").GroupKind(), b.Name, allErrs)
94+
return nil, apierrors.NewInvalid(GroupVersion.WithKind("Binding").GroupKind(), b.Name, allErrs)
9495
}
9596

96-
func (b *Binding) ValidateDelete() error {
97-
return nil
97+
func (b *Binding) ValidateDelete() (admission.Warnings, error) {
98+
return nil, nil
9899
}

api/v1beta1/binding_webhook_test.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,30 @@ var _ = Describe("Binding webhook", func() {
3030
It("does not allow both spec.rabbitmqClusterReference.name and spec.rabbitmqClusterReference.connectionSecret be configured", func() {
3131
notAllowed := oldBinding.DeepCopy()
3232
notAllowed.Spec.RabbitmqClusterReference.ConnectionSecret = &corev1.LocalObjectReference{Name: "some-secret"}
33-
Expect(apierrors.IsForbidden(notAllowed.ValidateCreate())).To(BeTrue())
33+
Expect(apierrors.IsForbidden(ignoreNilWarning(notAllowed.ValidateCreate()))).To(BeTrue())
3434
})
3535

3636
It("spec.rabbitmqClusterReference.name and spec.rabbitmqClusterReference.connectionSecret cannot both be empty", func() {
3737
notAllowed := oldBinding.DeepCopy()
3838
notAllowed.Spec.RabbitmqClusterReference.Name = ""
3939
notAllowed.Spec.RabbitmqClusterReference.ConnectionSecret = nil
40-
Expect(apierrors.IsForbidden(notAllowed.ValidateCreate())).To(BeTrue())
40+
Expect(apierrors.IsForbidden(ignoreNilWarning(notAllowed.ValidateCreate()))).To(BeTrue())
4141
})
4242
})
4343

4444
Context("ValidateUpdate", func() {
4545
It("does not allow updates on vhost", func() {
4646
newBinding := oldBinding.DeepCopy()
4747
newBinding.Spec.Vhost = "/new-vhost"
48-
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
48+
Expect(apierrors.IsForbidden(ignoreNilWarning(newBinding.ValidateUpdate(&oldBinding)))).To(BeTrue())
4949
})
5050

5151
It("does not allow updates on RabbitmqClusterReference", func() {
5252
newBinding := oldBinding.DeepCopy()
5353
newBinding.Spec.RabbitmqClusterReference = RabbitmqClusterReference{
5454
Name: "new-cluster",
5555
}
56-
Expect(apierrors.IsForbidden(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
56+
Expect(apierrors.IsForbidden(ignoreNilWarning(newBinding.ValidateUpdate(&oldBinding)))).To(BeTrue())
5757
})
5858

5959
It("does not allow updates on rabbitmqClusterReference.connectionSecret", func() {
@@ -71,38 +71,37 @@ var _ = Describe("Binding webhook", func() {
7171
}
7272
new := connectionScr.DeepCopy()
7373
new.Spec.RabbitmqClusterReference.ConnectionSecret.Name = "new-secret"
74-
Expect(apierrors.IsForbidden(new.ValidateUpdate(&connectionScr))).To(BeTrue())
74+
Expect(apierrors.IsForbidden(ignoreNilWarning(new.ValidateUpdate(&connectionScr)))).To(BeTrue())
7575
})
7676

7777
It("does not allow updates on source", func() {
7878
newBinding := oldBinding.DeepCopy()
7979
newBinding.Spec.Source = "updated-source"
80-
Expect(apierrors.IsInvalid(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
80+
Expect(apierrors.IsInvalid(ignoreNilWarning(newBinding.ValidateUpdate(&oldBinding)))).To(BeTrue())
8181
})
8282

8383
It("does not allow updates on destination", func() {
8484
newBinding := oldBinding.DeepCopy()
8585
newBinding.Spec.Destination = "updated-des"
86-
Expect(apierrors.IsInvalid(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
86+
Expect(apierrors.IsInvalid(ignoreNilWarning(newBinding.ValidateUpdate(&oldBinding)))).To(BeTrue())
8787
})
8888

8989
It("does not allow updates on destination type", func() {
9090
newBinding := oldBinding.DeepCopy()
9191
newBinding.Spec.DestinationType = "exchange"
92-
Expect(apierrors.IsInvalid(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
92+
Expect(apierrors.IsInvalid(ignoreNilWarning(newBinding.ValidateUpdate(&oldBinding)))).To(BeTrue())
9393
})
9494

9595
It("does not allow updates on routing key", func() {
9696
newBinding := oldBinding.DeepCopy()
9797
newBinding.Spec.RoutingKey = "not-allowed"
98-
Expect(apierrors.IsInvalid(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
98+
Expect(apierrors.IsInvalid(ignoreNilWarning(newBinding.ValidateUpdate(&oldBinding)))).To(BeTrue())
9999
})
100100

101101
It("does not allow updates on binding arguments", func() {
102102
newBinding := oldBinding.DeepCopy()
103103
newBinding.Spec.Arguments = &runtime.RawExtension{Raw: []byte(`{"new":"new-value"}`)}
104-
Expect(apierrors.IsInvalid(newBinding.ValidateUpdate(&oldBinding))).To(BeTrue())
104+
Expect(apierrors.IsInvalid(ignoreNilWarning(newBinding.ValidateUpdate(&oldBinding)))).To(BeTrue())
105105
})
106106
})
107-
108107
})

api/v1beta1/exchange_webhook.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"k8s.io/apimachinery/pkg/util/validation/field"
88
ctrl "sigs.k8s.io/controller-runtime"
99
"sigs.k8s.io/controller-runtime/pkg/webhook"
10+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1011
)
1112

1213
func (r *Exchange) SetupWebhookWithManager(mgr ctrl.Manager) error {
@@ -21,34 +22,34 @@ var _ webhook.Validator = &Exchange{}
2122

2223
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
2324
// either rabbitmqClusterReference.name or rabbitmqClusterReference.connectionSecret must be provided but not both
24-
func (e *Exchange) ValidateCreate() error {
25+
func (e *Exchange) ValidateCreate() (admission.Warnings, error) {
2526
return e.Spec.RabbitmqClusterReference.ValidateOnCreate(e.GroupResource(), e.Name)
2627
}
2728

2829
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
2930
// returns error type 'forbidden' for updates that the controller chooses to disallow: exchange name/vhost/rabbitmqClusterReference
3031
// returns error type 'invalid' for updates that will be rejected by rabbitmq server: exchange types/autoDelete/durable
3132
// exchange.spec.arguments can be updated
32-
func (e *Exchange) ValidateUpdate(old runtime.Object) error {
33+
func (e *Exchange) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
3334
oldExchange, ok := old.(*Exchange)
3435
if !ok {
35-
return apierrors.NewBadRequest(fmt.Sprintf("expected an exchange but got a %T", old))
36+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an exchange but got a %T", old))
3637
}
3738

3839
var allErrs field.ErrorList
3940
detailMsg := "updates on name, vhost, and rabbitmqClusterReference are all forbidden"
4041
if e.Spec.Name != oldExchange.Spec.Name {
41-
return apierrors.NewForbidden(e.GroupResource(), e.Name,
42+
return nil, apierrors.NewForbidden(e.GroupResource(), e.Name,
4243
field.Forbidden(field.NewPath("spec", "name"), detailMsg))
4344
}
4445

4546
if e.Spec.Vhost != oldExchange.Spec.Vhost {
46-
return apierrors.NewForbidden(e.GroupResource(), e.Name,
47+
return nil, apierrors.NewForbidden(e.GroupResource(), e.Name,
4748
field.Forbidden(field.NewPath("spec", "vhost"), detailMsg))
4849
}
4950

5051
if !oldExchange.Spec.RabbitmqClusterReference.Matches(&e.Spec.RabbitmqClusterReference) {
51-
return apierrors.NewForbidden(e.GroupResource(), e.Name,
52+
return nil, apierrors.NewForbidden(e.GroupResource(), e.Name,
5253
field.Forbidden(field.NewPath("spec", "rabbitmqClusterReference"), detailMsg))
5354
}
5455

@@ -77,12 +78,12 @@ func (e *Exchange) ValidateUpdate(old runtime.Object) error {
7778
}
7879

7980
if len(allErrs) == 0 {
80-
return nil
81+
return nil, nil
8182
}
8283

83-
return apierrors.NewInvalid(GroupVersion.WithKind("Exchange").GroupKind(), e.Name, allErrs)
84+
return nil, apierrors.NewInvalid(GroupVersion.WithKind("Exchange").GroupKind(), e.Name, allErrs)
8485
}
8586

86-
func (e *Exchange) ValidateDelete() error {
87-
return nil
87+
func (e *Exchange) ValidateDelete() (admission.Warnings, error) {
88+
return nil, nil
8889
}

0 commit comments

Comments
 (0)