Skip to content

Commit f2164da

Browse files
committed
Split setting conditions into respective files
1 parent 6ba3eba commit f2164da

File tree

5 files changed

+129
-74
lines changed

5 files changed

+129
-74
lines changed

internal/controller/state/graph/gateway.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package graph
22

33
import (
4-
"net"
5-
"reflect"
6-
74
"k8s.io/apimachinery/pkg/types"
85
"k8s.io/apimachinery/pkg/util/validation/field"
96
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -183,24 +180,11 @@ func validateGateway(gw *v1.Gateway, gc *GatewayClass, npCfg *NginxProxy) ([]con
183180
}
184181

185182
for _, address := range gw.Spec.Addresses {
186-
switch {
187-
case address.Type == nil:
183+
if address.Type == nil {
188184
conds = append(conds, conditions.NewGatewayUnsupportedAddress("AddressType must be specified"))
189-
case *address.Type == v1.IPAddressType:
190-
ip := net.ParseIP(address.Value)
191-
// Address 198.51.100.0 is reserved for documentation.
192-
// This is needed to give the conformance tests an example unusable address.
193-
if address.Value != "" && (ip == nil || reflect.DeepEqual(ip, net.ParseIP("198.51.100.0"))) {
194-
conds = append(conds, conditions.NewGatewayUnusableAddress("Invalid IP address"))
195-
}
196-
default:
185+
} else if *address.Type != v1.IPAddressType {
197186
conds = append(conds, conditions.NewGatewayUnsupportedAddress("Only AddressType IPAddress is supported"))
198187
}
199-
200-
if address.Value == "" {
201-
conds = append(conds, conditions.NewGatewayAddressNotAssigned("Dynamically assigned addresses for the "+
202-
"Gateway addresses field are not supported, value must be specified"))
203-
}
204188
}
205189

206190
// we evaluate validity before validating parametersRef because an invalid parametersRef/NginxProxy does not

internal/controller/state/graph/gateway_test.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,61 +1513,6 @@ func TestBuildGateway(t *testing.T) {
15131513
},
15141514
},
15151515
},
1516-
{
1517-
name: "invalid gateway; gateway addresses value unspecified",
1518-
gateway: createGateway(gatewayCfg{
1519-
name: "gateway-addr-value-unspecified",
1520-
listeners: []v1.Listener{foo80Listener1},
1521-
addresses: []v1.GatewaySpecAddress{
1522-
{
1523-
Type: helpers.GetPointer(v1.IPAddressType),
1524-
Value: "",
1525-
},
1526-
},
1527-
}),
1528-
gatewayClass: validGC,
1529-
expected: map[types.NamespacedName]*Gateway{
1530-
{Namespace: "test", Name: "gateway-addr-value-unspecified"}: {
1531-
Source: getLastCreatedGateway(),
1532-
DeploymentName: types.NamespacedName{
1533-
Namespace: "test",
1534-
Name: controller.CreateNginxResourceName("gateway-addr-value-unspecified", gcName),
1535-
},
1536-
Valid: false,
1537-
Conditions: []conditions.Condition{
1538-
conditions.NewGatewayAddressNotAssigned("Dynamically assigned addresses for the Gateway " +
1539-
"addresses field are not supported, value must be specified"),
1540-
},
1541-
},
1542-
},
1543-
},
1544-
{
1545-
name: "invalid gateway; gateway addresses value unusable",
1546-
gateway: createGateway(gatewayCfg{
1547-
name: "gateway-addr-unusable",
1548-
listeners: []v1.Listener{foo80Listener1},
1549-
addresses: []v1.GatewaySpecAddress{
1550-
{
1551-
Type: helpers.GetPointer(v1.IPAddressType),
1552-
Value: "<invalid-ip>",
1553-
},
1554-
},
1555-
}),
1556-
gatewayClass: validGC,
1557-
expected: map[types.NamespacedName]*Gateway{
1558-
{Namespace: "test", Name: "gateway-addr-unusable"}: {
1559-
Source: getLastCreatedGateway(),
1560-
DeploymentName: types.NamespacedName{
1561-
Namespace: "test",
1562-
Name: controller.CreateNginxResourceName("gateway-addr-unusable", gcName),
1563-
},
1564-
Valid: false,
1565-
Conditions: []conditions.Condition{
1566-
conditions.NewGatewayUnusableAddress("Invalid IP address"),
1567-
},
1568-
},
1569-
},
1570-
},
15711516
}
15721517

15731518
secretResolver := newSecretResolver(

internal/controller/status/prepare_requests.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package status
22

33
import (
44
"fmt"
5+
"net"
6+
"reflect"
57

68
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
79
"k8s.io/apimachinery/pkg/types"
@@ -17,6 +19,10 @@ import (
1719
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/kinds"
1820
)
1921

22+
// UnusableGatewayIPAddress 198.51.100.0 is a publicly reserved IP address specifically for documentation.
23+
// This is needed to give the conformance tests an example valid ip unusable address.
24+
const UnusableGatewayIPAddress = "198.51.100.0"
25+
2026
// PrepareRouteRequests prepares status UpdateRequests for the given Routes.
2127
func PrepareRouteRequests(
2228
l4routes map[graph.L4RouteKey]*graph.L4Route,
@@ -329,6 +335,18 @@ func prepareGatewayRequest(
329335
)
330336
}
331337

338+
for _, address := range gateway.Source.Spec.Addresses {
339+
if address.Value == "" {
340+
gwConds = append(gwConds, conditions.NewGatewayAddressNotAssigned("Dynamically assigned addresses for the "+
341+
"Gateway addresses field are not supported, value must be specified"))
342+
} else {
343+
ip := net.ParseIP(address.Value)
344+
if ip == nil || reflect.DeepEqual(ip, net.ParseIP(UnusableGatewayIPAddress)) {
345+
gwConds = append(gwConds, conditions.NewGatewayUnusableAddress("Invalid IP address"))
346+
}
347+
}
348+
}
349+
332350
apiGwConds := conditions.ConvertConditions(
333351
conditions.DeduplicateConditions(gwConds),
334352
gateway.Source.Generation,

internal/controller/status/prepare_requests_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,11 @@ func TestBuildGatewayStatuses(t *testing.T) {
850850
},
851851
}
852852
}
853+
createGatewayWithAddresses := func(addresses []v1.GatewaySpecAddress) *v1.Gateway {
854+
g := createGateway()
855+
g.Spec.Addresses = addresses
856+
return g
857+
}
853858

854859
transitionTime := helpers.PrepareTimeForFakeClient(metav1.Now())
855860

@@ -1341,6 +1346,105 @@ func TestBuildGatewayStatuses(t *testing.T) {
13411346
},
13421347
},
13431348
},
1349+
{
1350+
name: "valid gateway; valid listeners; gateway addresses value unspecified",
1351+
gateway: &graph.Gateway{
1352+
Source: createGatewayWithAddresses([]v1.GatewaySpecAddress{
1353+
{
1354+
Type: helpers.GetPointer(v1.IPAddressType),
1355+
Value: "",
1356+
},
1357+
}),
1358+
Listeners: []*graph.Listener{
1359+
{
1360+
Name: "listener-valid-1",
1361+
Valid: true,
1362+
Routes: map[graph.RouteKey]*graph.L7Route{routeKey: {}},
1363+
},
1364+
},
1365+
Valid: true,
1366+
},
1367+
expected: map[types.NamespacedName]v1.GatewayStatus{
1368+
{Namespace: "test", Name: "gateway"}: {
1369+
Addresses: addr,
1370+
Conditions: []metav1.Condition{
1371+
{
1372+
Type: string(v1.GatewayConditionAccepted),
1373+
Status: metav1.ConditionTrue,
1374+
ObservedGeneration: 2,
1375+
LastTransitionTime: transitionTime,
1376+
Reason: string(v1.GatewayReasonAccepted),
1377+
Message: "Gateway is accepted",
1378+
},
1379+
{
1380+
Type: string(v1.GatewayConditionProgrammed),
1381+
Status: metav1.ConditionFalse,
1382+
ObservedGeneration: 2,
1383+
LastTransitionTime: transitionTime,
1384+
Reason: string(v1.GatewayReasonAddressNotAssigned),
1385+
Message: "Dynamically assigned addresses for the Gateway addresses " +
1386+
"field are not supported, value must be specified",
1387+
},
1388+
},
1389+
Listeners: []v1.ListenerStatus{
1390+
{
1391+
Name: "listener-valid-1",
1392+
AttachedRoutes: 1,
1393+
Conditions: validListenerConditions,
1394+
},
1395+
},
1396+
},
1397+
},
1398+
},
1399+
{
1400+
name: "valid gateway; valid listeners; gateway addresses value unusable",
1401+
gateway: &graph.Gateway{
1402+
Source: createGatewayWithAddresses([]v1.GatewaySpecAddress{
1403+
{
1404+
Type: helpers.GetPointer(v1.IPAddressType),
1405+
Value: "<invalid-ip>",
1406+
},
1407+
}),
1408+
Listeners: []*graph.Listener{
1409+
{
1410+
Name: "listener-valid-1",
1411+
Valid: true,
1412+
Routes: map[graph.RouteKey]*graph.L7Route{routeKey: {}},
1413+
},
1414+
},
1415+
Valid: true,
1416+
},
1417+
expected: map[types.NamespacedName]v1.GatewayStatus{
1418+
{Namespace: "test", Name: "gateway"}: {
1419+
Addresses: addr,
1420+
Conditions: []metav1.Condition{
1421+
{
1422+
Type: string(v1.GatewayConditionAccepted),
1423+
Status: metav1.ConditionTrue,
1424+
ObservedGeneration: 2,
1425+
LastTransitionTime: transitionTime,
1426+
Reason: string(v1.GatewayReasonAccepted),
1427+
Message: "Gateway is accepted",
1428+
},
1429+
{
1430+
Type: string(v1.GatewayConditionProgrammed),
1431+
Status: metav1.ConditionFalse,
1432+
ObservedGeneration: 2,
1433+
LastTransitionTime: transitionTime,
1434+
Reason: string(v1.GatewayReasonAddressNotUsable),
1435+
Message: "Invalid IP address",
1436+
},
1437+
},
1438+
Listeners: []v1.ListenerStatus{
1439+
{
1440+
Name: "listener-valid-1",
1441+
AttachedRoutes: 1,
1442+
Conditions: validListenerConditions,
1443+
},
1444+
},
1445+
},
1446+
},
1447+
},
13441448
}
13451449

13461450
for _, test := range tests {

tests/conformance/conformance_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import (
3232
"sigs.k8s.io/yaml"
3333
)
3434

35+
// UnusableGatewayIPAddress 198.51.100.0 is a publicly reserved IP address specifically for documentation.
36+
// This is needed to give the conformance tests an example valid ip unusable address.
37+
const UnusableGatewayIPAddress = "198.51.100.0"
38+
3539
func TestConformance(t *testing.T) {
3640
g := NewWithT(t)
3741

@@ -46,7 +50,7 @@ func TestConformance(t *testing.T) {
4650
opts := conformance.DefaultOptions(t)
4751

4852
ipaddressType := v1.IPAddressType
49-
opts.UnusableNetworkAddresses = []v1beta1.GatewaySpecAddress{{Type: &ipaddressType, Value: "198.51.100.0"}}
53+
opts.UnusableNetworkAddresses = []v1beta1.GatewaySpecAddress{{Type: &ipaddressType, Value: UnusableGatewayIPAddress}}
5054
opts.UsableNetworkAddresses = []v1beta1.GatewaySpecAddress{{Type: &ipaddressType, Value: "192.0.2.1"}}
5155

5256
opts.Implementation = conf_v1.Implementation{

0 commit comments

Comments
 (0)