@@ -49,11 +49,17 @@ const testServiceJSONString string = `
49
49
}`
50
50
51
51
type serviceTestArgs struct {
52
- testID string
53
- operation admissionv1.Operation
54
- originalAnnotations map [string ]string
55
- expectedAnnotations map [string ]string
56
- shouldBeAllowed bool
52
+ testID string
53
+ operation admissionv1.Operation
54
+ // oldObjectAnnotations will trigger the creation of a Service to fill AdmissionRequest.OldObject,
55
+ // which is typically only populated for DELETE and UPDATE operations (other operations can leave
56
+ // this field nil). For example, in an UPDATE operation, OldObject represents the Service as it
57
+ // was before the user requested an UPDATE, and Object (which will be annotated using the
58
+ // originalAnnotations field) represents the state of the Service desired by the user post-UPDATE
59
+ oldObjectAnnotations map [string ]string
60
+ originalAnnotations map [string ]string
61
+ expectedAnnotations map [string ]string
62
+ shouldBeAllowed bool
57
63
}
58
64
59
65
// createJSONByteArrayService returns a JSON byte-string of a mock Service with the given
@@ -84,15 +90,23 @@ func runServiceTest(t *testing.T, tArgs serviceTestArgs) {
84
90
Resource : "services" ,
85
91
}
86
92
93
+ // For certain ops, create JSON and raw representations of the "old" Service object
94
+ // See docstring for serviceTestArgs.oldObjectAnnotations
95
+ var oldObjectServiceRawPtr * runtime.RawExtension
96
+ if tArgs .oldObjectAnnotations != nil {
97
+ oldObjectServiceJSONByteArray := createJSONByteArrayService (tArgs .oldObjectAnnotations )
98
+ oldObjectServiceRawPtr = & runtime.RawExtension {Raw : oldObjectServiceJSONByteArray }
99
+ }
100
+
87
101
// Create JSON and raw representations of the original Service object
88
102
originalServiceJSONByteArray := createJSONByteArrayService (tArgs .originalAnnotations )
89
- originalServiceRaw := runtime.RawExtension {Raw : originalServiceJSONByteArray }
103
+ originalServiceRawPtr := & runtime.RawExtension {Raw : originalServiceJSONByteArray }
90
104
91
105
// Set up the Webhook under test
92
106
// Note that this webhook doesn't care about RBAC, so we hardcode dummy RBAC parameter values
93
107
hook := NewWebhook ()
94
108
httprequest , err := testutils .CreateHTTPRequest (hook .GetURI (),
95
- tArgs .testID , gvk , gvr , tArgs .operation , "my_user" , []string {"my_group" }, "my_ns" , & originalServiceRaw , nil )
109
+ tArgs .testID , gvk , gvr , tArgs .operation , "my_user" , []string {"my_group" }, "my_ns" , originalServiceRawPtr , oldObjectServiceRawPtr )
96
110
if err != nil {
97
111
t .Fatalf ("Expected no error, got %s" , err .Error ())
98
112
}
@@ -175,6 +189,13 @@ func TestServiceMutation(t *testing.T) {
175
189
expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" },
176
190
shouldBeAllowed : true ,
177
191
},
192
+ {
193
+ testID : "create-service-incorrect-annotation-single" ,
194
+ operation : admissionv1 .Create ,
195
+ originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=foo" },
196
+ expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" },
197
+ shouldBeAllowed : true ,
198
+ },
178
199
{
179
200
testID : "create-service-irrelevant-annotations" ,
180
201
operation : admissionv1 .Create ,
@@ -183,11 +204,12 @@ func TestServiceMutation(t *testing.T) {
183
204
shouldBeAllowed : true ,
184
205
},
185
206
{
186
- testID : "update-service-correct-tag-irrelevant-annotations" ,
187
- operation : admissionv1 .Update ,
188
- originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" , "foo" : "bar" , "ABC" : "123" },
189
- expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" , "foo" : "bar" , "ABC" : "123" },
190
- shouldBeAllowed : true ,
207
+ testID : "update-service-correct-tag-add-irrelevant-annotation" ,
208
+ operation : admissionv1 .Update ,
209
+ oldObjectAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" , "foo" : "bar" },
210
+ originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" , "foo" : "bar" , "ABC" : "123" },
211
+ expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" , "foo" : "bar" , "ABC" : "123" },
212
+ shouldBeAllowed : true ,
191
213
},
192
214
{
193
215
testID : "create-service-irrelevant-tags" ,
@@ -197,18 +219,28 @@ func TestServiceMutation(t *testing.T) {
197
219
shouldBeAllowed : true ,
198
220
},
199
221
{
200
- testID : "update-service-relevant-tags" ,
201
- operation : admissionv1 .Update ,
202
- originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=false,Foo=Bar,ABC=123" },
203
- expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true,Foo=Bar,ABC=123" },
204
- shouldBeAllowed : true ,
222
+ testID : "update-service-correct-tag-deletion-attempt" , // OSD-21590 regression test
223
+ operation : admissionv1 .Update ,
224
+ oldObjectAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true,Foo=Bar,ABC=123" },
225
+ originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "Foo=Bar,ABC=123" },
226
+ expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true,Foo=Bar,ABC=123" },
227
+ shouldBeAllowed : true ,
205
228
},
206
229
{
207
- testID : "create-service-correct-tag" ,
208
- operation : admissionv1 .Create ,
209
- originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" },
210
- expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" },
211
- shouldBeAllowed : true ,
230
+ testID : "update-service-correct-tag-modification-attempt" , // OSD-21590 regression test
231
+ operation : admissionv1 .Update ,
232
+ oldObjectAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true,Foo=Bar,ABC=123" },
233
+ originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=foobar,Foo=Bar,ABC=123" },
234
+ expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true,Foo=Bar,ABC=123" },
235
+ shouldBeAllowed : true ,
236
+ },
237
+ {
238
+ testID : "create-service-correct-tag" ,
239
+ operation : admissionv1 .Create ,
240
+ oldObjectAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" },
241
+ originalAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" },
242
+ expectedAnnotations : map [string ]string {"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=true" },
243
+ shouldBeAllowed : true ,
212
244
},
213
245
{
214
246
testID : "update-service-correct-tags" ,
@@ -402,14 +434,23 @@ func Test_hasRedHatManagedTag(t *testing.T) {
402
434
want : false ,
403
435
},
404
436
{
405
- name : "incorrect tag value " ,
437
+ name : "incorrect tag values " ,
406
438
args : args {
407
439
serviceAnnotations : map [string ]string {
408
440
"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=false,Foo=Bar,ABC=123" ,
409
441
},
410
442
},
411
443
want : false ,
412
444
},
445
+ {
446
+ name : "incorrect tag value alone" ,
447
+ args : args {
448
+ serviceAnnotations : map [string ]string {
449
+ "service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags" : "red-hat-managed=foo" ,
450
+ },
451
+ },
452
+ want : false ,
453
+ },
413
454
{
414
455
name : "correct tag surrounded by garbage" ,
415
456
args : args {
0 commit comments