Skip to content

Commit 33a4cb7

Browse files
committed
update handler to recieve status update for gateways
1 parent 0a3490b commit 33a4cb7

File tree

8 files changed

+774
-263
lines changed

8 files changed

+774
-263
lines changed

cmd/gateway/commands_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func TestCommonFlagsValidation(t *testing.T) {
6363
args: []string{
6464
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway",
6565
"--gatewayclass=nginx",
66+
"--gateway=nginx-gateway/nginx",
67+
"--config=nginx-gateway-config",
6668
},
6769
wantErr: false,
6870
},

internal/mode/static/handler.go

Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -294,64 +294,89 @@ func (h *eventHandlerImpl) waitForStatusUpdates(ctx context.Context) {
294294
return
295295
}
296296

297-
// TODO(sberman): once we support multiple Gateways, we'll have to get
298-
// the correct Graph for the Deployment contained in the update message
297+
// get the deploymentName from the item
298+
deploymentName := item.Deployment
299+
if deploymentName == (types.NamespacedName{}) {
300+
continue
301+
}
302+
303+
fmt.Println("Processing status update for deployment:", deploymentName)
304+
299305
gr := h.cfg.processor.GetLatestGraph()
300306
if gr == nil {
301307
continue
302308
}
303309

310+
gw := gr.Gateways[deploymentName]
311+
312+
// if gateway is nil,, we update the gateway class status
313+
if gw == nil {
314+
h.updateGatewayClassStatus(ctx, gr)
315+
continue
316+
}
317+
304318
var nginxReloadRes graph.NginxReloadResult
305-
for _, gw := range gr.Gateways {
306-
switch {
307-
case item.Error != nil:
308-
h.cfg.logger.Error(item.Error, "Failed to update NGINX configuration")
309-
nginxReloadRes.Error = item.Error
310-
case gw != nil:
311-
h.cfg.logger.Info("NGINX configuration was successfully updated")
312-
}
313-
gr.LatestReloadResult = nginxReloadRes
314-
315-
switch item.UpdateType {
316-
case status.UpdateAll:
317-
h.updateStatuses(ctx, gr, gw)
318-
case status.UpdateGateway:
319-
gwAddresses, err := getGatewayAddresses(
320-
ctx,
321-
h.cfg.k8sClient,
319+
switch {
320+
case item.Error != nil:
321+
h.cfg.logger.Error(item.Error, "Failed to update NGINX configuration")
322+
nginxReloadRes.Error = item.Error
323+
case gw != nil:
324+
h.cfg.logger.Info("NGINX configuration was successfully updated")
325+
}
326+
gr.LatestReloadResult[deploymentName] = nginxReloadRes
327+
328+
switch item.UpdateType {
329+
case status.UpdateAll:
330+
h.updateStatuses(ctx, gr, gw)
331+
case status.UpdateGateway:
332+
fmt.Println("update gateways status", gw)
333+
gwAddresses, err := getGatewayAddresses(
334+
ctx,
335+
h.cfg.k8sClient,
336+
item.GatewayService,
337+
gw,
338+
h.cfg.gatewayClassName,
339+
)
340+
if err != nil {
341+
msg := "error getting Gateway Service IP address"
342+
h.cfg.logger.Error(err, msg)
343+
h.cfg.eventRecorder.Eventf(
322344
item.GatewayService,
323-
gw,
324-
h.cfg.gatewayClassName,
345+
v1.EventTypeWarning,
346+
"GetServiceIPFailed",
347+
msg+": %s",
348+
err.Error(),
325349
)
326-
if err != nil {
327-
msg := "error getting Gateway Service IP address"
328-
h.cfg.logger.Error(err, msg)
329-
h.cfg.eventRecorder.Eventf(
330-
item.GatewayService,
331-
v1.EventTypeWarning,
332-
"GetServiceIPFailed",
333-
msg+": %s",
334-
err.Error(),
335-
)
336-
continue
337-
}
338-
339-
transitionTime := metav1.Now()
340-
341-
gatewayStatuses := status.PrepareGatewayRequests(
342-
gw,
343-
transitionTime,
344-
gwAddresses,
345-
gr.LatestReloadResult,
346-
)
347-
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gatewayStatuses...)
348-
default:
349-
panic(fmt.Sprintf("unknown event type %T", item.UpdateType))
350+
continue
350351
}
352+
353+
transitionTime := metav1.Now()
354+
355+
gatewayStatuses := status.PrepareGatewayRequests(
356+
gw,
357+
transitionTime,
358+
gwAddresses,
359+
gr.LatestReloadResult[deploymentName],
360+
)
361+
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gatewayStatuses...)
362+
default:
363+
panic(fmt.Sprintf("unknown event type %T", item.UpdateType))
351364
}
352365
}
353366
}
354367

368+
func (h *eventHandlerImpl) updateGatewayClassStatus(ctx context.Context, gr *graph.Graph) {
369+
transitionTime := metav1.Now()
370+
371+
gcReqs := status.PrepareGatewayClassRequests(gr.GatewayClass, gr.IgnoredGatewayClasses, transitionTime)
372+
373+
reqs := make([]frameworkStatus.UpdateRequest, 0, len(gcReqs))
374+
reqs = append(reqs, gcReqs...)
375+
376+
fmt.Println("Processing status update for GatewayClass", gr.GatewayClass)
377+
h.cfg.statusUpdater.UpdateGroup(ctx, groupAllExceptGateways, reqs...)
378+
}
379+
355380
func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph, gw *graph.Gateway) {
356381
gwAddresses, err := getGatewayAddresses(ctx, h.cfg.k8sClient, nil, gw, h.cfg.gatewayClassName)
357382
if err != nil {
@@ -373,7 +398,7 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
373398
gr.L4Routes,
374399
gr.Routes,
375400
transitionTime,
376-
gr.LatestReloadResult,
401+
gr.LatestReloadResult[gw.DeploymentName],
377402
h.cfg.gatewayCtlrName,
378403
)
379404

@@ -404,7 +429,7 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
404429
gw,
405430
transitionTime,
406431
gwAddresses,
407-
gr.LatestReloadResult,
432+
gr.LatestReloadResult[gw.DeploymentName],
408433
)
409434
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gwReqs...)
410435
}

internal/mode/static/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ var _ = Describe("eventHandler", func() {
381381
}).Should(Equal(2))
382382

383383
gr := handler.cfg.processor.GetLatestGraph()
384-
Expect(gr.LatestReloadResult.Error.Error()).To(Equal("status error"))
384+
Expect(gr.LatestReloadResult[types.NamespacedName{}].Error.Error()).To(Equal("status error"))
385385
})
386386

387387
It("should update Gateway status when receiving a queue event", func() {

internal/mode/static/state/graph/backend_refs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func addBackendRefsToRouteRules(
4949
gws map[types.NamespacedName]*Gateway,
5050
) {
5151
for _, gw := range gws {
52-
if gw == nil {
52+
if gw == nil || gw == (&Gateway{}) {
5353
continue
5454
}
5555
for _, r := range routes {

0 commit comments

Comments
 (0)