Skip to content

Commit

Permalink
Handle updated GatewayClass
Browse files Browse the repository at this point in the history
  • Loading branch information
sjberman committed Jun 30, 2023
1 parent acf90b4 commit 9c677a9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
9 changes: 8 additions & 1 deletion internal/events/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions internal/manager/predicate/gatewayclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 7 additions & 3 deletions internal/manager/predicate/gatewayclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion internal/state/graph/gatewayclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9c677a9

Please sign in to comment.