Skip to content

Commit 9c677a9

Browse files
committed
Handle updated GatewayClass
1 parent acf90b4 commit 9c677a9

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

internal/events/handler.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/go-logr/logr"
88
apiv1 "k8s.io/api/core/v1"
99
discoveryV1 "k8s.io/api/discovery/v1"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
1011
"sigs.k8s.io/gateway-api/apis/v1beta1"
1112

1213
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config"
@@ -48,6 +49,8 @@ type EventHandlerConfig struct {
4849
StatusUpdater status.Updater
4950
// Logger is the logger to be used by the EventHandler.
5051
Logger logr.Logger
52+
// ControllerName is the name of this controller.
53+
ControllerName string
5154
}
5255

5356
// EventHandlerImpl implements EventHandler.
@@ -121,7 +124,11 @@ func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Confi
121124
func (h *EventHandlerImpl) propagateUpsert(e *UpsertEvent) {
122125
switch r := e.Resource.(type) {
123126
case *v1beta1.GatewayClass:
124-
h.cfg.Processor.CaptureUpsertChange(r)
127+
if string(r.Spec.ControllerName) != h.cfg.ControllerName {
128+
h.cfg.Processor.CaptureDeleteChange(r, client.ObjectKeyFromObject(r))
129+
} else {
130+
h.cfg.Processor.CaptureUpsertChange(r)
131+
}
125132
case *v1beta1.Gateway:
126133
h.cfg.Processor.CaptureUpsertChange(r)
127134
case *v1beta1.HTTPRoute:

internal/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func Start(cfg config.Config) error {
175175
NginxFileMgr: nginxFileMgr,
176176
NginxRuntimeMgr: nginxRuntimeMgr,
177177
StatusUpdater: statusUpdater,
178+
ControllerName: cfg.GatewayCtlrName,
178179
})
179180

180181
objects, objectLists := prepareFirstEventBatchPreparerArgs(cfg.GatewayClassName, cfg.GatewayNsName)

internal/manager/predicate/gatewayclass.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,19 @@ func (gcp GatewayClassPredicate) Create(e event.CreateEvent) bool {
2929

3030
// Update implements default UpdateEvent filter for validating a GatewayClass controllerName.
3131
func (gcp GatewayClassPredicate) Update(e event.UpdateEvent) bool {
32-
if e.ObjectNew == nil {
33-
return false
32+
if e.ObjectOld != nil {
33+
gcOld, ok := e.ObjectOld.(*v1beta1.GatewayClass)
34+
if ok {
35+
return string(gcOld.Spec.ControllerName) == gcp.ControllerName
36+
}
3437
}
3538

36-
gc, ok := e.ObjectNew.(*v1beta1.GatewayClass)
37-
if !ok {
38-
return false
39+
if e.ObjectNew != nil {
40+
gcNew, ok := e.ObjectNew.(*v1beta1.GatewayClass)
41+
if ok {
42+
return string(gcNew.Spec.ControllerName) == gcp.ControllerName
43+
}
3944
}
4045

41-
return string(gc.Spec.ControllerName) == gcp.ControllerName
46+
return false
4247
}

internal/manager/predicate/gatewayclass_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ func TestGatewayClassPredicate(t *testing.T) {
2222
g.Expect(p.Create(event.CreateEvent{Object: gc})).To(BeTrue())
2323
g.Expect(p.Update(event.UpdateEvent{ObjectNew: gc})).To(BeTrue())
2424

25-
gc.Spec.ControllerName = "unknown"
26-
g.Expect(p.Create(event.CreateEvent{Object: gc})).To(BeFalse())
27-
g.Expect(p.Update(event.UpdateEvent{ObjectNew: gc})).To(BeFalse())
25+
gc2 := &v1beta1.GatewayClass{
26+
Spec: v1beta1.GatewayClassSpec{
27+
ControllerName: "unknown",
28+
},
29+
}
30+
g.Expect(p.Create(event.CreateEvent{Object: gc2})).To(BeFalse())
31+
g.Expect(p.Update(event.UpdateEvent{ObjectOld: gc, ObjectNew: gc2})).To(BeTrue())
2832
}

internal/state/graph/gatewayclass.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type processedGatewayClasses struct {
2828
// processGatewayClasses returns the "Winner" GatewayClass, which is defined in
2929
// the command-line argument and references this controller, and a list of "Ignored" GatewayClasses
3030
// that reference this controller, but are not named in the command-line argument.
31-
// Also returns a boolean that says whether or not the GatewayClasa defined
31+
// Also returns a boolean that says whether or not the GatewayClass defined
3232
// in the command-line argument exists, regardless of which controller it references.
3333
func processGatewayClasses(
3434
gcs map[types.NamespacedName]*v1beta1.GatewayClass,

0 commit comments

Comments
 (0)