Skip to content

Commit 2e13294

Browse files
committed
add more unit tests, update handler
1 parent adb0e1b commit 2e13294

File tree

7 files changed

+578
-102
lines changed

7 files changed

+578
-102
lines changed

internal/mode/static/handler.go

+52-46
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ type eventHandlerConfig struct {
8383
plus bool
8484
}
8585

86+
// deployment name gatewayname-gatewayclassname
87+
// cafe-nginx
88+
89+
// namespace and name of the gateway --> key
90+
8691
const (
8792
// groups for GroupStatusUpdater.
8893
groupAllExceptGateways = "all-graphs-except-gateways"
@@ -294,59 +299,60 @@ func (h *eventHandlerImpl) waitForStatusUpdates(ctx context.Context) {
294299
return
295300
}
296301

302+
// TODO(sberman): once we support multiple Gateways, we'll have to get
303+
// the correct Graph for the Deployment contained in the update message
297304
gr := h.cfg.processor.GetLatestGraph()
298305
if gr == nil {
299306
continue
300307
}
301308

302-
deploymentName := item.Deployment
303-
gw := gr.Gateways[deploymentName]
304-
305309
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(
310+
for _, gw := range gr.Gateways {
311+
switch {
312+
case item.Error != nil:
313+
h.cfg.logger.Error(item.Error, "Failed to update NGINX configuration")
314+
nginxReloadRes.Error = item.Error
315+
case gw != nil:
316+
h.cfg.logger.Info("NGINX configuration was successfully updated")
317+
}
318+
gw.LatestReloadResult = nginxReloadRes
319+
320+
switch item.UpdateType {
321+
case status.UpdateAll:
322+
h.updateStatuses(ctx, gr, gw)
323+
case status.UpdateGateway:
324+
gwAddresses, err := getGatewayAddresses(
325+
ctx,
326+
h.cfg.k8sClient,
330327
item.GatewayService,
331-
v1.EventTypeWarning,
332-
"GetServiceIPFailed",
333-
msg+": %s",
334-
err.Error(),
328+
gw,
329+
h.cfg.gatewayClassName,
330+
)
331+
if err != nil {
332+
msg := "error getting Gateway Service IP address"
333+
h.cfg.logger.Error(err, msg)
334+
h.cfg.eventRecorder.Eventf(
335+
item.GatewayService,
336+
v1.EventTypeWarning,
337+
"GetServiceIPFailed",
338+
msg+": %s",
339+
err.Error(),
340+
)
341+
continue
342+
}
343+
344+
transitionTime := metav1.Now()
345+
346+
gatewayStatuses := status.PrepareGatewayRequests(
347+
gw,
348+
transitionTime,
349+
gwAddresses,
350+
gw.LatestReloadResult,
335351
)
336-
continue
352+
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gatewayStatuses...)
353+
default:
354+
panic(fmt.Sprintf("unknown event type %T", item.UpdateType))
337355
}
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))
350356
}
351357
}
352358
}
@@ -377,7 +383,7 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
377383
gr.L4Routes,
378384
gr.Routes,
379385
transitionTime,
380-
gr.LatestReloadResult[gw.DeploymentName],
386+
gw.LatestReloadResult,
381387
h.cfg.gatewayCtlrName,
382388
)
383389

@@ -408,7 +414,7 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
408414
gw,
409415
transitionTime,
410416
gwAddresses,
411-
gr.LatestReloadResult[gw.DeploymentName],
417+
gw.LatestReloadResult,
412418
)
413419
h.cfg.statusUpdater.UpdateGroup(ctx, groupGateways, gwReqs...)
414420
}

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

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

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

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)

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)