Skip to content

Commit 3d71b58

Browse files
committed
Remove redundant warnings (nginx#502)
The warnings reported by dataplane.BuildConfiguration() are now redundant - they are reported as Conditions in the statuses of Gateway API resources, which includes warnings-related cases for: * invalid listener in an HTTPRoute * unresolved backend ref an HTTPRoute This commit removes the code, which generates and reports warnings. Additionally, the commit fixes a related test TestBuildConfiguration case "invalid listener": if a listener is invalid, it cannot include any Routes and AcceptedHostnames, so those fields are removed. Fixes nginx#467
1 parent abc008b commit 3d71b58

File tree

5 files changed

+9
-350
lines changed

5 files changed

+9
-350
lines changed

internal/state/change_processor.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,7 @@ func (c *ChangeProcessorImpl) Process(
168168
c.cfg.Validators,
169169
)
170170

171-
var warnings dataplane.Warnings
172-
conf, warnings = dataplane.BuildConfiguration(ctx, g, c.cfg.ServiceResolver)
173-
174-
for obj, objWarnings := range warnings {
175-
for _, w := range objWarnings {
176-
// FIXME(pleshakov): report warnings via Object status
177-
// https://github.com/nginxinc/nginx-kubernetes-gateway/issues/467
178-
c.cfg.Logger.Info("Got warning while building Graph",
179-
"kind", obj.GetObjectKind().GroupVersionKind().Kind,
180-
"namespace", obj.GetNamespace(),
181-
"name", obj.GetName(),
182-
"warning", w)
183-
}
184-
}
185-
171+
conf = dataplane.BuildConfiguration(ctx, g, c.cfg.ServiceResolver)
186172
statuses = buildStatuses(g)
187173

188174
return true, conf, statuses

internal/state/dataplane/configuration.go

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -97,33 +97,27 @@ func (r *MatchRule) GetMatch() v1beta1.HTTPRouteMatch {
9797

9898
// BuildConfiguration builds the Configuration from the Graph.
9999
// FIXME(pleshakov) For now we only handle paths with prefix matches. Handle exact and regex matches
100-
func BuildConfiguration(
101-
ctx context.Context,
102-
g *graph.Graph,
103-
resolver resolver.ServiceResolver,
104-
) (Configuration, Warnings) {
100+
func BuildConfiguration(ctx context.Context, g *graph.Graph, resolver resolver.ServiceResolver) Configuration {
105101
if g.GatewayClass == nil || !g.GatewayClass.Valid {
106-
return Configuration{}, nil
102+
return Configuration{}
107103
}
108104

109105
if g.Gateway == nil {
110-
return Configuration{}, nil
106+
return Configuration{}
111107
}
112108

113109
upstreamsMap := buildUpstreamsMap(ctx, g.Gateway.Listeners, resolver)
114110
httpServers, sslServers := buildServers(g.Gateway.Listeners)
115111
backendGroups := buildBackendGroups(g.Gateway.Listeners)
116112

117-
warnings := buildWarnings(g, upstreamsMap)
118-
119113
config := Configuration{
120114
HTTPServers: httpServers,
121115
SSLServers: sslServers,
122116
Upstreams: upstreamsMapToSlice(upstreamsMap),
123117
BackendGroups: backendGroups,
124118
}
125119

126-
return config, warnings
120+
return config
127121
}
128122

129123
func upstreamsMapToSlice(upstreamsMap map[string]Upstream) []Upstream {
@@ -139,55 +133,6 @@ func upstreamsMapToSlice(upstreamsMap map[string]Upstream) []Upstream {
139133
return upstreams
140134
}
141135

142-
func buildWarnings(graph *graph.Graph, upstreams map[string]Upstream) Warnings {
143-
warnings := newWarnings()
144-
145-
for _, l := range graph.Gateway.Listeners {
146-
for _, r := range l.Routes {
147-
if !l.Valid {
148-
warnings.AddWarningf(
149-
r.Source,
150-
"cannot configure routes for listener %s; listener is invalid",
151-
l.Source.Name,
152-
)
153-
154-
continue
155-
}
156-
157-
for _, group := range r.GetAllBackendGroups() {
158-
for _, backend := range group.Backends {
159-
if backend.Name != "" {
160-
upstream, ok := upstreams[backend.Name]
161-
162-
// this should never happen; but we check it just in case
163-
if !ok {
164-
warnings.AddWarningf(
165-
r.Source,
166-
"cannot resolve backend ref; internal error: upstream %s not found in map",
167-
backend.Name,
168-
)
169-
}
170-
171-
if upstream.ErrorMsg != "" {
172-
warnings.AddWarningf(
173-
r.Source,
174-
"cannot resolve backend ref: %s",
175-
upstream.ErrorMsg,
176-
)
177-
}
178-
}
179-
}
180-
}
181-
}
182-
}
183-
184-
if len(warnings) == 0 {
185-
return nil
186-
}
187-
188-
return warnings
189-
}
190-
191136
func buildBackendGroups(listeners map[string]*graph.Listener) []graph.BackendGroup {
192137
// There can be duplicate backend groups if a route is attached to multiple listeners.
193138
// We use a map to deduplicate them.

internal/state/dataplane/configuration_test.go

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,9 @@ func TestBuildConfiguration(t *testing.T) {
262262
secretPath := "/etc/nginx/secrets/secret"
263263

264264
tests := []struct {
265-
graph *graph.Graph
266-
expWarns Warnings
267-
msg string
268-
expConf Configuration
265+
graph *graph.Graph
266+
msg string
267+
expConf Configuration
269268
}{
270269
{
271270
graph: &graph.Graph{
@@ -371,15 +370,6 @@ func TestBuildConfiguration(t *testing.T) {
371370
"invalid-listener": {
372371
Source: invalidListener,
373372
Valid: false,
374-
Routes: map[types.NamespacedName]*graph.Route{
375-
{Namespace: "test", Name: "https-hr-1"}: httpsRouteHR1,
376-
{Namespace: "test", Name: "https-hr-2"}: httpsRouteHR2,
377-
},
378-
AcceptedHostnames: map[string]struct{}{
379-
"foo.example.com": {},
380-
"bar.example.com": {},
381-
},
382-
SecretPath: "",
383373
},
384374
},
385375
},
@@ -392,10 +382,6 @@ func TestBuildConfiguration(t *testing.T) {
392382
HTTPServers: []VirtualServer{},
393383
SSLServers: []VirtualServer{},
394384
},
395-
expWarns: Warnings{
396-
httpsHR1: []string{"cannot configure routes for listener invalid-listener; listener is invalid"},
397-
httpsHR2: []string{"cannot configure routes for listener invalid-listener; listener is invalid"},
398-
},
399385
msg: "invalid listener",
400386
},
401387
{
@@ -978,7 +964,7 @@ func TestBuildConfiguration(t *testing.T) {
978964

979965
for _, test := range tests {
980966
t.Run(test.msg, func(t *testing.T) {
981-
result, warns := BuildConfiguration(context.TODO(), test.graph, fakeResolver)
967+
result := BuildConfiguration(context.TODO(), test.graph, fakeResolver)
982968

983969
sort.Slice(result.BackendGroups, func(i, j int) bool {
984970
return result.BackendGroups[i].GroupName() < result.BackendGroups[j].GroupName()
@@ -991,10 +977,6 @@ func TestBuildConfiguration(t *testing.T) {
991977
if diff := cmp.Diff(test.expConf, result); diff != "" {
992978
t.Errorf("BuildConfiguration() %q mismatch for configuration (-want +got):\n%s", test.msg, diff)
993979
}
994-
995-
if diff := cmp.Diff(test.expWarns, warns); diff != "" {
996-
t.Errorf("BuildConfiguration() %q mismatch for warnings (-want +got):\n%s", test.msg, diff)
997-
}
998980
})
999981
}
1000982
}
@@ -1471,96 +1453,6 @@ func TestBuildBackendGroups(t *testing.T) {
14711453
g.Expect(result).To(ConsistOf(expGroups))
14721454
}
14731455

1474-
func TestBuildWarnings(t *testing.T) {
1475-
createBackendRefs := func(names ...string) []graph.BackendRef {
1476-
backends := make([]graph.BackendRef, len(names))
1477-
for idx, name := range names {
1478-
backends[idx] = graph.BackendRef{Name: name}
1479-
}
1480-
1481-
return backends
1482-
}
1483-
1484-
createBackendGroup := func(sourceName string, backends []graph.BackendRef) graph.BackendGroup {
1485-
return graph.BackendGroup{
1486-
Source: types.NamespacedName{Namespace: "test", Name: sourceName},
1487-
Backends: backends,
1488-
}
1489-
}
1490-
1491-
hrBackendGroup0 := createBackendGroup(
1492-
"hr",
1493-
createBackendRefs(""), // empty backend name should be skipped
1494-
)
1495-
1496-
hrBackendGroup1 := createBackendGroup(
1497-
"hr",
1498-
createBackendRefs("dne"),
1499-
)
1500-
1501-
hrInvalidGroup := createBackendGroup(
1502-
"hr-invalid",
1503-
createBackendRefs("invalid"),
1504-
)
1505-
1506-
hr := &v1beta1.HTTPRoute{ObjectMeta: metav1.ObjectMeta{Name: "hr", Namespace: "test"}}
1507-
hrInvalid := &v1beta1.HTTPRoute{ObjectMeta: metav1.ObjectMeta{Name: "hr-invalid", Namespace: "test"}}
1508-
1509-
invalidRoutes := map[types.NamespacedName]*graph.Route{
1510-
{Name: "invalid", Namespace: "test"}: {
1511-
Source: hrInvalid,
1512-
Rules: groupsToValidRules(hrInvalidGroup),
1513-
},
1514-
}
1515-
1516-
routes := map[types.NamespacedName]*graph.Route{
1517-
{Name: "hr", Namespace: "test"}: {
1518-
Source: hr,
1519-
Rules: groupsToValidRules(hrBackendGroup0, hrBackendGroup1),
1520-
},
1521-
}
1522-
1523-
upstreamMap := map[string]Upstream{
1524-
"foo": {},
1525-
"bar": {},
1526-
"resolve-error": {ErrorMsg: "resolve error"},
1527-
}
1528-
1529-
graph := &graph.Graph{
1530-
Gateway: &graph.Gateway{
1531-
Listeners: map[string]*graph.Listener{
1532-
"invalid-listener": {
1533-
Source: v1beta1.Listener{
1534-
Name: "invalid",
1535-
},
1536-
Valid: false,
1537-
Routes: invalidRoutes,
1538-
},
1539-
"listener": {
1540-
Source: v1beta1.Listener{
1541-
Name: "valid",
1542-
},
1543-
Valid: true,
1544-
Routes: routes,
1545-
},
1546-
},
1547-
},
1548-
}
1549-
1550-
expWarns := Warnings{
1551-
hr: []string{
1552-
"cannot resolve backend ref; internal error: upstream dne not found in map",
1553-
},
1554-
hrInvalid: []string{"cannot configure routes for listener invalid; listener is invalid"},
1555-
}
1556-
1557-
g := NewGomegaWithT(t)
1558-
1559-
warns := buildWarnings(graph, upstreamMap)
1560-
1561-
g.Expect(helpers.Diff(warns, expWarns)).To(BeEmpty())
1562-
}
1563-
15641456
func TestUpstreamsMapToSlice(t *testing.T) {
15651457
fooUpstream := Upstream{
15661458
Name: "foo",

internal/state/dataplane/warnings.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)