Skip to content

Commit bdaa872

Browse files
Merge pull request #298 from abyrne55/OSD-21590
Fix improper rendering of Services in service-mutation webhook
2 parents a158ba3 + 05531bb commit bdaa872

File tree

2 files changed

+65
-28
lines changed

2 files changed

+65
-28
lines changed

pkg/webhooks/service/service.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,7 @@ func (s *ServiceWebhook) renderService(req admissionctl.Request) (*corev1.Servic
151151
}
152152
service := &corev1.Service{}
153153

154-
if len(req.OldObject.Raw) > 0 {
155-
err = decoder.DecodeRaw(req.OldObject, service)
156-
} else {
157-
err = decoder.Decode(req, service)
158-
}
154+
err = decoder.Decode(req, service)
159155
if err != nil {
160156
return nil, err
161157
}

pkg/webhooks/service/service_test.go

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ const testServiceJSONString string = `
4949
}`
5050

5151
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
5763
}
5864

5965
// createJSONByteArrayService returns a JSON byte-string of a mock Service with the given
@@ -84,15 +90,23 @@ func runServiceTest(t *testing.T, tArgs serviceTestArgs) {
8490
Resource: "services",
8591
}
8692

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+
87101
// Create JSON and raw representations of the original Service object
88102
originalServiceJSONByteArray := createJSONByteArrayService(tArgs.originalAnnotations)
89-
originalServiceRaw := runtime.RawExtension{Raw: originalServiceJSONByteArray}
103+
originalServiceRawPtr := &runtime.RawExtension{Raw: originalServiceJSONByteArray}
90104

91105
// Set up the Webhook under test
92106
// Note that this webhook doesn't care about RBAC, so we hardcode dummy RBAC parameter values
93107
hook := NewWebhook()
94108
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)
96110
if err != nil {
97111
t.Fatalf("Expected no error, got %s", err.Error())
98112
}
@@ -175,6 +189,13 @@ func TestServiceMutation(t *testing.T) {
175189
expectedAnnotations: map[string]string{"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags": "red-hat-managed=true"},
176190
shouldBeAllowed: true,
177191
},
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+
},
178199
{
179200
testID: "create-service-irrelevant-annotations",
180201
operation: admissionv1.Create,
@@ -183,11 +204,12 @@ func TestServiceMutation(t *testing.T) {
183204
shouldBeAllowed: true,
184205
},
185206
{
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,
191213
},
192214
{
193215
testID: "create-service-irrelevant-tags",
@@ -197,18 +219,28 @@ func TestServiceMutation(t *testing.T) {
197219
shouldBeAllowed: true,
198220
},
199221
{
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,
205228
},
206229
{
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,
212244
},
213245
{
214246
testID: "update-service-correct-tags",
@@ -402,14 +434,23 @@ func Test_hasRedHatManagedTag(t *testing.T) {
402434
want: false,
403435
},
404436
{
405-
name: "incorrect tag value",
437+
name: "incorrect tag values",
406438
args: args{
407439
serviceAnnotations: map[string]string{
408440
"service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags": "red-hat-managed=false,Foo=Bar,ABC=123",
409441
},
410442
},
411443
want: false,
412444
},
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+
},
413454
{
414455
name: "correct tag surrounded by garbage",
415456
args: args{

0 commit comments

Comments
 (0)