@@ -2,7 +2,9 @@ package v1beta1
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
7
+ "maps"
6
8
7
9
apierrors "k8s.io/apimachinery/pkg/api/errors"
8
10
"k8s.io/apimachinery/pkg/runtime"
@@ -23,8 +25,8 @@ func (q *Queue) SetupWebhookWithManager(mgr ctrl.Manager) error {
23
25
24
26
var _ webhook.CustomValidator = & Queue {}
25
27
26
- // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
27
- // either rabbitmqClusterReference.name or rabbitmqClusterReference.connectionSecret must be provided but not both
28
+ // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
29
+ // Either rabbitmqClusterReference.name or rabbitmqClusterReference.connectionSecret must be provided but not both
28
30
func (q * Queue ) ValidateCreate (_ context.Context , obj runtime.Object ) (warnings admission.Warnings , err error ) {
29
31
inQueue , ok := obj .(* Queue )
30
32
if ! ok {
@@ -38,10 +40,11 @@ func (q *Queue) ValidateCreate(_ context.Context, obj runtime.Object) (warnings
38
40
return nil , q .Spec .RabbitmqClusterReference .validate (inQueue .RabbitReference ())
39
41
}
40
42
41
- // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
42
- // returns error type 'forbidden' for updates that the controller chooses to disallow: queue name/vhost/rabbitmqClusterReference
43
- // returns error type 'invalid' for updates that will be rejected by rabbitmq server: queue types/autoDelete/durable
44
- // queue arguments not handled because implementation couldn't change
43
+ // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
44
+ //
45
+ // Returns error type 'forbidden' for updates that the controller chooses to disallow: queue name/vhost/rabbitmqClusterReference
46
+ //
47
+ // Returns error type 'invalid' for updates that will be rejected by rabbitmq server: queue types/autoDelete/durable
45
48
func (q * Queue ) ValidateUpdate (_ context.Context , oldObj , newObj runtime.Object ) (warnings admission.Warnings , err error ) {
46
49
oldQueue , ok := oldObj .(* Queue )
47
50
if ! ok {
@@ -94,10 +97,49 @@ func (q *Queue) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object)
94
97
))
95
98
}
96
99
100
+ if oldQueue .Spec .Arguments != nil && newQueue .Spec .Arguments == nil {
101
+ allErrs = append (allErrs , field .Invalid (
102
+ field .NewPath ("spec" , "arguments" ),
103
+ newQueue .Spec .Arguments ,
104
+ "queue arguments cannot be updated" ,
105
+ ))
106
+ }
107
+
108
+ if oldQueue .Spec .Arguments == nil && newQueue .Spec .Arguments != nil {
109
+ allErrs = append (allErrs , field .Invalid (
110
+ field .NewPath ("spec" , "arguments" ),
111
+ newQueue .Spec .Arguments ,
112
+ "queue arguments cannot be updated" ,
113
+ ))
114
+ }
115
+
116
+ if oldQueue .Spec .Arguments != nil && newQueue .Spec .Arguments != nil {
117
+ previousArgs := make (map [string ]any )
118
+ err := json .Unmarshal (oldQueue .Spec .Arguments .Raw , & previousArgs )
119
+ if err != nil {
120
+ return nil , apierrors .NewInternalError (fmt .Errorf ("error unmarshalling previous Queue arguments: %w" , err ))
121
+ }
122
+
123
+ updatedArgs := make (map [string ]any )
124
+ err = json .Unmarshal (newQueue .Spec .Arguments .Raw , & updatedArgs )
125
+ if err != nil {
126
+ return nil , apierrors .NewInternalError (fmt .Errorf ("error unmarshalling current Queue arguments: %w" , err ))
127
+ }
128
+
129
+ if ! maps .Equal (previousArgs , updatedArgs ) {
130
+ allErrs = append (allErrs , field .Invalid (
131
+ field .NewPath ("spec" , "arguments" ),
132
+ newQueue .Spec .Arguments ,
133
+ "queue arguments cannot be updated" ,
134
+ ))
135
+ }
136
+ }
137
+
97
138
if len (allErrs ) == 0 {
98
139
return nil , nil
99
140
}
100
141
142
+ //goland:noinspection GoDfaNilDereference
101
143
return nil , allErrs .ToAggregate ()
102
144
}
103
145
0 commit comments