Skip to content

Commit 7840a5c

Browse files
salonichf5sjberman
authored andcommitted
update handler to recieve status update for gateways
1 parent f4a5ae3 commit 7840a5c

File tree

11 files changed

+3385
-2434
lines changed

11 files changed

+3385
-2434
lines changed

Diff for: cmd/gateway/commands_test.go

+2
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
},

Diff for: examples/cafe-example/cafe-routes.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ spec:
66
parentRefs:
77
- name: gateway
88
sectionName: http
9+
- name: gateway3
10+
sectionName: http
911
hostnames:
1012
- "cafe.example.com"
1113
rules:
@@ -25,6 +27,8 @@ spec:
2527
parentRefs:
2628
- name: gateway
2729
sectionName: http
30+
- name: gateway3
31+
sectionName: http
2832
hostnames:
2933
- "cafe.example.com"
3034
rules:

Diff for: examples/cafe-example/gateway.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,27 @@ spec:
99
port: 80
1010
protocol: HTTP
1111
hostname: "*.example.com"
12+
---
13+
apiVersion: gateway.networking.k8s.io/v1
14+
kind: Gateway
15+
metadata:
16+
name: gateway2
17+
spec:
18+
gatewayClassName: nginx
19+
listeners:
20+
- name: http
21+
port: 80
22+
protocol: HTTP
23+
hostname: "*.example.com"
24+
---
25+
apiVersion: gateway.networking.k8s.io/v1
26+
kind: Gateway
27+
metadata:
28+
name: gateway3
29+
spec:
30+
gatewayClassName: nginx
31+
listeners:
32+
- name: http
33+
port: 80
34+
protocol: HTTP
35+
hostname: "*.example.com"

Diff for: internal/mode/static/handler.go

+54-50
Original file line numberDiff line numberDiff line change
@@ -294,65 +294,72 @@ 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
299297
gr := h.cfg.processor.GetLatestGraph()
300298
if gr == nil {
301299
continue
302300
}
303301

302+
deploymentName := item.Deployment
303+
gw := gr.Gateways[deploymentName]
304+
304305
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,
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[deploymentName] = 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,
322+
item.GatewayService,
323+
gw,
324+
h.cfg.gatewayClassName,
325+
)
326+
if err != nil {
327+
msg := "error getting Gateway Service IP address"
328+
h.cfg.logger.Error(err, msg)
329+
h.cfg.eventRecorder.Eventf(
322330
item.GatewayService,
323-
gw,
324-
h.cfg.gatewayClassName,
325-
)
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,
331+
v1.EventTypeWarning,
332+
"GetServiceIPFailed",
333+
msg+": %s",
334+
err.Error(),
346335
)
347-
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gatewayStatuses...)
348-
default:
349-
panic(fmt.Sprintf("unknown event type %T", item.UpdateType))
336+
continue
350337
}
338+
339+
transitionTime := metav1.Now()
340+
341+
gatewayStatuses := status.PrepareGatewayRequests(
342+
gw,
343+
transitionTime,
344+
gwAddresses,
345+
gr.LatestReloadResult[deploymentName],
346+
)
347+
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gatewayStatuses...)
348+
default:
349+
panic(fmt.Sprintf("unknown event type %T", item.UpdateType))
351350
}
352351
}
353352
}
354353

355354
func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph, gw *graph.Gateway) {
355+
transitionTime := metav1.Now()
356+
gcReqs := status.PrepareGatewayClassRequests(gr.GatewayClass, gr.IgnoredGatewayClasses, transitionTime)
357+
358+
if gw == nil || gw.DeploymentName == (types.NamespacedName{}) {
359+
h.cfg.statusUpdater.UpdateGroup(ctx, groupAllExceptGateways, gcReqs...)
360+
return
361+
}
362+
356363
gwAddresses, err := getGatewayAddresses(ctx, h.cfg.k8sClient, nil, gw, h.cfg.gatewayClassName)
357364
if err != nil {
358365
msg := "error getting Gateway Service IP address"
@@ -366,14 +373,11 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
366373
)
367374
}
368375

369-
transitionTime := metav1.Now()
370-
371-
gcReqs := status.PrepareGatewayClassRequests(gr.GatewayClass, gr.IgnoredGatewayClasses, transitionTime)
372376
routeReqs := status.PrepareRouteRequests(
373377
gr.L4Routes,
374378
gr.Routes,
375379
transitionTime,
376-
gr.LatestReloadResult,
380+
gr.LatestReloadResult[gw.DeploymentName],
377381
h.cfg.gatewayCtlrName,
378382
)
379383

@@ -404,7 +408,7 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
404408
gw,
405409
transitionTime,
406410
gwAddresses,
407-
gr.LatestReloadResult,
411+
gr.LatestReloadResult[gw.DeploymentName],
408412
)
409413
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gwReqs...)
410414
}

Diff for: internal/mode/static/handler_test.go

+1-1
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() {

0 commit comments

Comments
 (0)