Skip to content

Commit 3ef2f04

Browse files
committed
Update service changed predicate
1 parent 016ec74 commit 3ef2f04

File tree

4 files changed

+173
-16
lines changed

4 files changed

+173
-16
lines changed

internal/controller/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func registerControllers(
440440
objectType: &apiv1.Service{},
441441
name: "user-service", // unique controller names are needed and we have multiple Service ctlrs
442442
options: []controller.Option{
443-
controller.WithK8sPredicate(predicate.ServicePortsChangedPredicate{}),
443+
controller.WithK8sPredicate(predicate.ServiceChangedPredicate{}),
444444
},
445445
},
446446
{

internal/framework/controller/predicate/service.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ import (
77
"sigs.k8s.io/controller-runtime/pkg/predicate"
88
)
99

10-
// ServicePortsChangedPredicate implements an update predicate function based on the Ports of a Service.
11-
// This predicate will skip update events that have no change in the Service Ports and TargetPorts.
12-
type ServicePortsChangedPredicate struct {
10+
// ServiceChangedPredicate implements an update predicate function for a Service.
11+
// This predicate will skip update events that have no change in a Service's Ports, TargetPorts, or AppProtocols.
12+
type ServiceChangedPredicate struct {
1313
predicate.Funcs
1414
}
1515

16-
// ports contains the ports that the Gateway cares about.
17-
type ports struct {
16+
// portInfo contains the information that the Gateway cares about.
17+
type portInfo struct {
1818
targetPort intstr.IntOrString
19+
appProtocol string
1920
servicePort int32
2021
}
2122

22-
// Update implements default UpdateEvent filter for validating Service port changes.
23-
func (ServicePortsChangedPredicate) Update(e event.UpdateEvent) bool {
23+
// Update implements default UpdateEvent filter for validating Service port information changes.
24+
func (ServiceChangedPredicate) Update(e event.UpdateEvent) bool {
2425
if e.ObjectOld == nil {
2526
return false
2627
}
@@ -45,12 +46,30 @@ func (ServicePortsChangedPredicate) Update(e event.UpdateEvent) bool {
4546
return true
4647
}
4748

48-
oldPortSet := make(map[ports]struct{})
49-
newPortSet := make(map[ports]struct{})
49+
oldPortSet := make(map[portInfo]struct{})
50+
newPortSet := make(map[portInfo]struct{})
5051

5152
for i := range len(oldSvc.Spec.Ports) {
52-
oldPortSet[ports{servicePort: oldPorts[i].Port, targetPort: oldPorts[i].TargetPort}] = struct{}{}
53-
newPortSet[ports{servicePort: newPorts[i].Port, targetPort: newPorts[i].TargetPort}] = struct{}{}
53+
var oldAppProtocol, newAppProtocl string
54+
55+
if oldPorts[i].AppProtocol != nil {
56+
oldAppProtocol = *oldPorts[i].AppProtocol
57+
}
58+
59+
if newPorts[i].AppProtocol != nil {
60+
newAppProtocl = *newPorts[i].AppProtocol
61+
}
62+
63+
oldPortSet[portInfo{
64+
servicePort: oldPorts[i].Port,
65+
targetPort: oldPorts[i].TargetPort,
66+
appProtocol: oldAppProtocol,
67+
}] = struct{}{}
68+
newPortSet[portInfo{
69+
servicePort: newPorts[i].Port,
70+
targetPort: newPorts[i].TargetPort,
71+
appProtocol: newAppProtocl,
72+
}] = struct{}{}
5473
}
5574

5675
for pd := range oldPortSet {

internal/framework/controller/predicate/service_test.go

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"k8s.io/apimachinery/pkg/util/intstr"
99
"sigs.k8s.io/controller-runtime/pkg/client"
1010
"sigs.k8s.io/controller-runtime/pkg/event"
11+
12+
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
1113
)
1214

13-
func TestServicePortsChangedPredicate_Update(t *testing.T) {
15+
func TestServiceChangedPredicate_Update(t *testing.T) {
1416
t.Parallel()
1517
testcases := []struct {
1618
objectOld client.Object
@@ -221,9 +223,145 @@ func TestServicePortsChangedPredicate_Update(t *testing.T) {
221223
},
222224
expUpdate: false,
223225
},
226+
{
227+
msg: "appProtocol changed",
228+
objectOld: &v1.Service{
229+
Spec: v1.ServiceSpec{
230+
Ports: []v1.ServicePort{
231+
{
232+
AppProtocol: helpers.GetPointer("oldAppProtocol"),
233+
},
234+
},
235+
},
236+
},
237+
objectNew: &v1.Service{
238+
Spec: v1.ServiceSpec{
239+
Ports: []v1.ServicePort{
240+
{
241+
AppProtocol: helpers.GetPointer("newAppProtocol"),
242+
},
243+
},
244+
},
245+
},
246+
expUpdate: true,
247+
},
248+
{
249+
msg: "appProtocol stayed the same",
250+
objectOld: &v1.Service{
251+
Spec: v1.ServiceSpec{
252+
Ports: []v1.ServicePort{
253+
{
254+
AppProtocol: helpers.GetPointer("sameAppProtocol"),
255+
},
256+
},
257+
},
258+
},
259+
objectNew: &v1.Service{
260+
Spec: v1.ServiceSpec{
261+
Ports: []v1.ServicePort{
262+
{
263+
AppProtocol: helpers.GetPointer("sameAppProtocol"),
264+
},
265+
},
266+
},
267+
},
268+
expUpdate: false,
269+
},
270+
{
271+
msg: "multiple appProtocols stayed the same",
272+
objectOld: &v1.Service{
273+
Spec: v1.ServiceSpec{
274+
Ports: []v1.ServicePort{
275+
{
276+
Port: 80,
277+
TargetPort: intstr.FromInt(80),
278+
AppProtocol: helpers.GetPointer("sameAppProtocol80"),
279+
},
280+
{
281+
Port: 81,
282+
TargetPort: intstr.FromInt(81),
283+
AppProtocol: helpers.GetPointer("sameAppProtocol81"),
284+
},
285+
{
286+
Port: 82,
287+
TargetPort: intstr.FromInt(82),
288+
AppProtocol: helpers.GetPointer("sameAppProtocol82"),
289+
},
290+
},
291+
},
292+
},
293+
objectNew: &v1.Service{
294+
Spec: v1.ServiceSpec{
295+
Ports: []v1.ServicePort{
296+
{
297+
Port: 80,
298+
TargetPort: intstr.FromInt(80),
299+
AppProtocol: helpers.GetPointer("sameAppProtocol80"),
300+
},
301+
{
302+
Port: 81,
303+
TargetPort: intstr.FromInt(81),
304+
AppProtocol: helpers.GetPointer("sameAppProtocol81"),
305+
},
306+
{
307+
Port: 82,
308+
TargetPort: intstr.FromInt(82),
309+
AppProtocol: helpers.GetPointer("sameAppProtocol82"),
310+
},
311+
},
312+
},
313+
},
314+
expUpdate: false,
315+
},
316+
{
317+
msg: "multiple appProtocols with one changing",
318+
objectOld: &v1.Service{
319+
Spec: v1.ServiceSpec{
320+
Ports: []v1.ServicePort{
321+
{
322+
Port: 80,
323+
TargetPort: intstr.FromInt(80),
324+
AppProtocol: helpers.GetPointer("sameAppProtocol80"),
325+
},
326+
{
327+
Port: 81,
328+
TargetPort: intstr.FromInt(81),
329+
AppProtocol: helpers.GetPointer("sameAppProtocol81"),
330+
},
331+
{
332+
Port: 82,
333+
TargetPort: intstr.FromInt(82),
334+
AppProtocol: helpers.GetPointer("sameAppProtocol82"),
335+
},
336+
},
337+
},
338+
},
339+
objectNew: &v1.Service{
340+
Spec: v1.ServiceSpec{
341+
Ports: []v1.ServicePort{
342+
{
343+
Port: 80,
344+
TargetPort: intstr.FromInt(80),
345+
AppProtocol: helpers.GetPointer("sameAppProtocol80"),
346+
},
347+
{
348+
Port: 81,
349+
TargetPort: intstr.FromInt(81),
350+
AppProtocol: helpers.GetPointer("sameAppProtocol81"),
351+
},
352+
{
353+
Port: 82,
354+
TargetPort: intstr.FromInt(82),
355+
AppProtocol: helpers.GetPointer("differentAppProtocol"),
356+
},
357+
},
358+
},
359+
},
360+
expUpdate: true,
361+
},
224362
}
225363

226-
p := ServicePortsChangedPredicate{}
364+
p := ServiceChangedPredicate{}
227365

228366
for _, tc := range testcases {
229367
t.Run(tc.msg, func(t *testing.T) {
@@ -243,7 +381,7 @@ func TestServicePortsChangedPredicate(t *testing.T) {
243381
t.Parallel()
244382
g := NewWithT(t)
245383

246-
p := ServicePortsChangedPredicate{}
384+
p := ServiceChangedPredicate{}
247385

248386
g.Expect(p.Delete(event.DeleteEvent{Object: &v1.Service{}})).To(BeTrue())
249387
g.Expect(p.Create(event.CreateEvent{Object: &v1.Service{}})).To(BeTrue())

internal/framework/controller/register_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestRegister(t *testing.T) {
142142
test.fakes.mgr,
143143
eventCh,
144144
controller.WithNamespacedNameFilter(nsNameFilter),
145-
controller.WithK8sPredicate(predicate.ServicePortsChangedPredicate{}),
145+
controller.WithK8sPredicate(predicate.ServiceChangedPredicate{}),
146146
controller.WithFieldIndices(fieldIndexes),
147147
controller.WithNewReconciler(newReconciler),
148148
controller.WithOnlyMetadata(),

0 commit comments

Comments
 (0)