Skip to content

Commit 46705fe

Browse files
salonichf5sjberman
authored andcommitted
add more unit tests, update handler
1 parent 7840a5c commit 46705fe

File tree

9 files changed

+573
-130
lines changed

9 files changed

+573
-130
lines changed

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

-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ spec:
66
parentRefs:
77
- name: gateway
88
sectionName: http
9-
- name: gateway3
10-
sectionName: http
119
hostnames:
1210
- "cafe.example.com"
1311
rules:
@@ -27,8 +25,6 @@ spec:
2725
parentRefs:
2826
- name: gateway
2927
sectionName: http
30-
- name: gateway3
31-
sectionName: http
3228
hostnames:
3329
- "cafe.example.com"
3430
rules:

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

-24
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,3 @@ 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

+47-46
Original file line numberDiff line numberDiff line change
@@ -294,59 +294,60 @@ 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
297299
gr := h.cfg.processor.GetLatestGraph()
298300
if gr == nil {
299301
continue
300302
}
301303

302-
deploymentName := item.Deployment
303-
gw := gr.Gateways[deploymentName]
304-
305304
var nginxReloadRes graph.NginxReloadResult
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(
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+
gw.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,
330322
item.GatewayService,
331-
v1.EventTypeWarning,
332-
"GetServiceIPFailed",
333-
msg+": %s",
334-
err.Error(),
323+
gw,
324+
h.cfg.gatewayClassName,
335325
)
336-
continue
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+
gw.LatestReloadResult,
346+
)
347+
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gatewayStatuses...)
348+
default:
349+
panic(fmt.Sprintf("unknown event type %T", item.UpdateType))
337350
}
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))
350351
}
351352
}
352353
}
@@ -377,7 +378,7 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
377378
gr.L4Routes,
378379
gr.Routes,
379380
transitionTime,
380-
gr.LatestReloadResult[gw.DeploymentName],
381+
gw.LatestReloadResult,
381382
h.cfg.gatewayCtlrName,
382383
)
383384

@@ -408,7 +409,7 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
408409
gw,
409410
transitionTime,
410411
gwAddresses,
411-
gr.LatestReloadResult[gw.DeploymentName],
412+
gw.LatestReloadResult,
412413
)
413414
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gwReqs...)
414415
}

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

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

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

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

Diff for: internal/mode/static/state/change_processor_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,6 @@ var _ = Describe("ChangeProcessor", func() {
10071007
GatewayNsNames: map[types.NamespacedName]struct{}{{Namespace: "test", Name: "gateway-1"}: {}},
10081008
},
10091009
},
1010-
LatestReloadResult: map[types.NamespacedName]graph.NginxReloadResult{},
10111010
}
10121011

10131012
expGraph2 = &graph.Graph{
@@ -1134,7 +1133,6 @@ var _ = Describe("ChangeProcessor", func() {
11341133
GatewayNsNames: map[types.NamespacedName]struct{}{{Namespace: "test", Name: "gateway-1"}: {}},
11351134
},
11361135
},
1137-
LatestReloadResult: map[types.NamespacedName]graph.NginxReloadResult{},
11381136
}
11391137
})
11401138
When("no upsert has occurred", func() {

Diff for: internal/mode/static/state/graph/gateway.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
// Gateway represents the winning Gateway resource.
1616
type Gateway struct {
17+
LatestReloadResult NginxReloadResult
1718
Source *v1.Gateway
1819
NginxProxy *NginxProxy
1920
EffectiveNginxProxy *EffectiveNginxProxy

Diff for: internal/mode/static/state/graph/graph.go

-8
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ type Graph struct {
7676
SnippetsFilters map[types.NamespacedName]*SnippetsFilter
7777
// PlusSecrets holds the secrets related to NGINX Plus licensing.
7878
PlusSecrets map[types.NamespacedName][]PlusSecretFile
79-
// LatestReloadResult is holds latest result of applying config to nginx for all gateways.
80-
LatestReloadResult map[types.NamespacedName]NginxReloadResult
8179
}
8280

8381
// NginxReloadResult describes the result of an NGINX reload.
@@ -291,11 +289,6 @@ func BuildGraph(
291289

292290
setPlusSecretContent(state.Secrets, plusSecrets)
293291

294-
var latestReloadResult map[types.NamespacedName]NginxReloadResult
295-
if gws != nil {
296-
latestReloadResult = make(map[types.NamespacedName]NginxReloadResult, len(gws))
297-
}
298-
299292
g := &Graph{
300293
GatewayClass: gc,
301294
Gateways: gws,
@@ -311,7 +304,6 @@ func BuildGraph(
311304
NGFPolicies: processedPolicies,
312305
SnippetsFilters: processedSnippetsFilters,
313306
PlusSecrets: plusSecrets,
314-
LatestReloadResult: latestReloadResult,
315307
}
316308

317309
g.attachPolicies(controllerName)

Diff for: internal/mode/static/state/graph/route_common.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ func getListenerHostPortMap(listeners []*Listener, gw *Gateway) map[string]hostP
399399
Namespace: gw.Source.Namespace,
400400
}
401401
for _, l := range listeners {
402-
listenerHostPortMap[l.Name] = hostPort{
402+
key := fmt.Sprintf("%s,%s,%s", l.Name, gw.Source.Name, gw.Source.Namespace)
403+
listenerHostPortMap[key] = hostPort{
403404
hostname: getHostname(l.Source.Hostname),
404405
port: l.Source.Port,
405406
gwNsNames: gwNsNames,
@@ -447,6 +448,7 @@ func isolateHostnamesForParentRefs(parentRef []ParentRef, listenerHostnameMap ma
447448
}
448449
for _, h := range hostnames {
449450
for lName, lHostPort := range listenerHostnameMap {
451+
// skip comparison if not part of the same gateway
450452
if lHostPort.gwNsNames != ref.Gateway {
451453
continue
452454
}
@@ -456,8 +458,12 @@ func isolateHostnamesForParentRefs(parentRef []ParentRef, listenerHostnameMap ma
456458
continue
457459
}
458460

459-
// for L7Routes, we compare the hostname, port and gatewayNamespace-gatewayName-listenerName combination
461+
// for L7Routes, we compare the hostname, port and listenerName combination
460462
// to identify if hostname needs to be isolated.
463+
splitLName := strings.Split(lName, ",")
464+
if len(splitLName) > 1 {
465+
lName = splitLName[0]
466+
}
461467
if h == lHostPort.hostname && listenerName != lName {
462468
// for L4Routes, we only compare the hostname and listener name combination
463469
// because we do not allow l4Routes to attach to the same listener

0 commit comments

Comments
 (0)