diff --git a/internal/events/handler.go b/internal/events/handler.go index f324d09658..6bbfc90f76 100644 --- a/internal/events/handler.go +++ b/internal/events/handler.go @@ -7,6 +7,7 @@ import ( "github.com/go-logr/logr" apiv1 "k8s.io/api/core/v1" discoveryV1 "k8s.io/api/discovery/v1" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/gateway-api/apis/v1beta1" "github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config" @@ -48,6 +49,8 @@ type EventHandlerConfig struct { StatusUpdater status.Updater // Logger is the logger to be used by the EventHandler. Logger logr.Logger + // ControllerName is the name of this controller. + ControllerName string } // EventHandlerImpl implements EventHandler. @@ -121,7 +124,11 @@ func (h *EventHandlerImpl) updateNginx(ctx context.Context, conf dataplane.Confi func (h *EventHandlerImpl) propagateUpsert(e *UpsertEvent) { switch r := e.Resource.(type) { case *v1beta1.GatewayClass: - h.cfg.Processor.CaptureUpsertChange(r) + if string(r.Spec.ControllerName) != h.cfg.ControllerName { + h.cfg.Processor.CaptureDeleteChange(r, client.ObjectKeyFromObject(r)) + } else { + h.cfg.Processor.CaptureUpsertChange(r) + } case *v1beta1.Gateway: h.cfg.Processor.CaptureUpsertChange(r) case *v1beta1.HTTPRoute: diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 994e0e40a2..34ba05a604 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -175,6 +175,7 @@ func Start(cfg config.Config) error { NginxFileMgr: nginxFileMgr, NginxRuntimeMgr: nginxRuntimeMgr, StatusUpdater: statusUpdater, + ControllerName: cfg.GatewayCtlrName, }) objects, objectLists := prepareFirstEventBatchPreparerArgs(cfg.GatewayClassName, cfg.GatewayNsName) diff --git a/internal/manager/predicate/gatewayclass.go b/internal/manager/predicate/gatewayclass.go index 13f07e27c2..56a792d259 100644 --- a/internal/manager/predicate/gatewayclass.go +++ b/internal/manager/predicate/gatewayclass.go @@ -29,14 +29,19 @@ func (gcp GatewayClassPredicate) Create(e event.CreateEvent) bool { // Update implements default UpdateEvent filter for validating a GatewayClass controllerName. func (gcp GatewayClassPredicate) Update(e event.UpdateEvent) bool { - if e.ObjectNew == nil { - return false + if e.ObjectOld != nil { + gcOld, ok := e.ObjectOld.(*v1beta1.GatewayClass) + if ok { + return string(gcOld.Spec.ControllerName) == gcp.ControllerName + } } - gc, ok := e.ObjectNew.(*v1beta1.GatewayClass) - if !ok { - return false + if e.ObjectNew != nil { + gcNew, ok := e.ObjectNew.(*v1beta1.GatewayClass) + if ok { + return string(gcNew.Spec.ControllerName) == gcp.ControllerName + } } - return string(gc.Spec.ControllerName) == gcp.ControllerName + return false } diff --git a/internal/manager/predicate/gatewayclass_test.go b/internal/manager/predicate/gatewayclass_test.go index 3413baaa32..febcdc0089 100644 --- a/internal/manager/predicate/gatewayclass_test.go +++ b/internal/manager/predicate/gatewayclass_test.go @@ -22,7 +22,11 @@ func TestGatewayClassPredicate(t *testing.T) { g.Expect(p.Create(event.CreateEvent{Object: gc})).To(BeTrue()) g.Expect(p.Update(event.UpdateEvent{ObjectNew: gc})).To(BeTrue()) - gc.Spec.ControllerName = "unknown" - g.Expect(p.Create(event.CreateEvent{Object: gc})).To(BeFalse()) - g.Expect(p.Update(event.UpdateEvent{ObjectNew: gc})).To(BeFalse()) + gc2 := &v1beta1.GatewayClass{ + Spec: v1beta1.GatewayClassSpec{ + ControllerName: "unknown", + }, + } + g.Expect(p.Create(event.CreateEvent{Object: gc2})).To(BeFalse()) + g.Expect(p.Update(event.UpdateEvent{ObjectOld: gc, ObjectNew: gc2})).To(BeTrue()) } diff --git a/internal/state/graph/gatewayclass.go b/internal/state/graph/gatewayclass.go index 15c002a3a5..8c932939c2 100644 --- a/internal/state/graph/gatewayclass.go +++ b/internal/state/graph/gatewayclass.go @@ -28,7 +28,7 @@ type processedGatewayClasses struct { // processGatewayClasses returns the "Winner" GatewayClass, which is defined in // the command-line argument and references this controller, and a list of "Ignored" GatewayClasses // that reference this controller, but are not named in the command-line argument. -// Also returns a boolean that says whether or not the GatewayClasa defined +// Also returns a boolean that says whether or not the GatewayClass defined // in the command-line argument exists, regardless of which controller it references. func processGatewayClasses( gcs map[types.NamespacedName]*v1beta1.GatewayClass,