Skip to content

Commit 6ba3eba

Browse files
committed
Move setting conditions to gateway.go
1 parent 5229ebb commit 6ba3eba

File tree

4 files changed

+132
-225
lines changed

4 files changed

+132
-225
lines changed

internal/controller/state/graph/gateway.go

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

33
import (
4+
"net"
5+
"reflect"
6+
47
"k8s.io/apimachinery/pkg/types"
58
"k8s.io/apimachinery/pkg/util/validation/field"
69
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -179,6 +182,27 @@ func validateGateway(gw *v1.Gateway, gc *GatewayClass, npCfg *NginxProxy) ([]con
179182
conds = append(conds, conditions.NewGatewayInvalid("GatewayClass is invalid")...)
180183
}
181184

185+
for _, address := range gw.Spec.Addresses {
186+
switch {
187+
case address.Type == nil:
188+
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:
197+
conds = append(conds, conditions.NewGatewayUnsupportedAddress("Only AddressType IPAddress is supported"))
198+
}
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+
}
204+
}
205+
182206
// we evaluate validity before validating parametersRef because an invalid parametersRef/NginxProxy does not
183207
// invalidate the entire Gateway.
184208
valid := len(conds) == 0

internal/controller/state/graph/gateway_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,114 @@ func TestBuildGateway(t *testing.T) {
14601460
},
14611461
name: "invalid gatewayclass and invalid NginxProxy",
14621462
},
1463+
{
1464+
name: "invalid gateway; gateway addresses type unspecified",
1465+
gateway: createGateway(gatewayCfg{
1466+
name: "gateway-addr-unspecified",
1467+
listeners: []v1.Listener{foo80Listener1},
1468+
addresses: []v1.GatewaySpecAddress{
1469+
{
1470+
Value: "198.0.0.1",
1471+
},
1472+
},
1473+
}),
1474+
gatewayClass: validGC,
1475+
expected: map[types.NamespacedName]*Gateway{
1476+
{Namespace: "test", Name: "gateway-addr-unspecified"}: {
1477+
Source: getLastCreatedGateway(),
1478+
DeploymentName: types.NamespacedName{
1479+
Namespace: "test",
1480+
Name: controller.CreateNginxResourceName("gateway-addr-unspecified", gcName),
1481+
},
1482+
Valid: false,
1483+
Conditions: []conditions.Condition{
1484+
conditions.NewGatewayUnsupportedAddress("AddressType must be specified"),
1485+
},
1486+
},
1487+
},
1488+
},
1489+
{
1490+
name: "invalid gateway; gateway addresses type unsupported",
1491+
gateway: createGateway(gatewayCfg{
1492+
name: "gateway-addr-unsupported",
1493+
listeners: []v1.Listener{foo80Listener1},
1494+
addresses: []v1.GatewaySpecAddress{
1495+
{
1496+
Type: helpers.GetPointer(v1.HostnameAddressType),
1497+
Value: "example.com",
1498+
},
1499+
},
1500+
}),
1501+
gatewayClass: validGC,
1502+
expected: map[types.NamespacedName]*Gateway{
1503+
{Namespace: "test", Name: "gateway-addr-unsupported"}: {
1504+
Source: getLastCreatedGateway(),
1505+
DeploymentName: types.NamespacedName{
1506+
Namespace: "test",
1507+
Name: controller.CreateNginxResourceName("gateway-addr-unsupported", gcName),
1508+
},
1509+
Valid: false,
1510+
Conditions: []conditions.Condition{
1511+
conditions.NewGatewayUnsupportedAddress("Only AddressType IPAddress is supported"),
1512+
},
1513+
},
1514+
},
1515+
},
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+
},
14631571
}
14641572

14651573
secretResolver := newSecretResolver(

internal/controller/status/prepare_requests.go

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

33
import (
44
"fmt"
5-
"net"
6-
"reflect"
75

86
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
97
"k8s.io/apimachinery/pkg/types"
@@ -331,27 +329,6 @@ func prepareGatewayRequest(
331329
)
332330
}
333331

334-
for _, address := range gateway.Source.Spec.Addresses {
335-
switch {
336-
case address.Type == nil:
337-
gwConds = append(gwConds, conditions.NewGatewayUnsupportedAddress("AddressType must be specified"))
338-
case *address.Type == v1.IPAddressType:
339-
ip := net.ParseIP(address.Value)
340-
// Address 198.51.100.0 is reserved for documentation.
341-
// This is needed to give the conformance tests an example unusable address.
342-
if address.Value != "" && (ip == nil || reflect.DeepEqual(ip, net.ParseIP("198.51.100.0"))) {
343-
gwConds = append(gwConds, conditions.NewGatewayUnusableAddress("Invalid IP address"))
344-
}
345-
default:
346-
gwConds = append(gwConds, conditions.NewGatewayUnsupportedAddress("Only AddressType IPAddress is supported"))
347-
}
348-
349-
if address.Value == "" {
350-
gwConds = append(gwConds, conditions.NewGatewayAddressNotAssigned("Dynamically assigned addresses for the "+
351-
"Gateway addresses field are not supported, value must be specified"))
352-
}
353-
}
354-
355332
apiGwConds := conditions.ConvertConditions(
356333
conditions.DeduplicateConditions(gwConds),
357334
gateway.Source.Generation,

0 commit comments

Comments
 (0)