Skip to content

Commit 03d1726

Browse files
locker95mhofstetter
authored andcommitted
gateway-api: migrate net.IP usage to net/netip
Convert the remaining net.IP usage in the Gateway API operator to netip.Addr, as part of the tree-wide migration tracked in cilium#24246. In setAddressStatus, the NodePort branch now parses node addresses with netip.ParseAddr and sorts them with netip.Addr.Compare. Nodes whose first status address is not an IP address (e.g. a Hostname entry) are now skipped instead of being appended as a nil net.IP, which previously rendered as a bogus "<nil>" address in the Gateway status. Parsed addresses are Unmap()-ed so IPv4-mapped IPv6 inputs keep rendering in dotted-quad form as before. Note that mixed IPv4 and IPv6 addresses now sort IPv4-first instead of interleaving the IPv4-mapped form within the IPv6 space; the sort only exists to keep the assigned addresses deterministic, which is preserved. In verifyGatewayStaticAddresses, netip.ParseAddr replaces the net.ParseIP nil check. Parsing semantics are unchanged for valid IPv4/IPv6 literals; zoned IPv6 literals are additionally accepted. The nodeport testdata gains a node whose only status address is a Hostname entry, locking in the new skip behavior. Related: cilium#24246 Signed-off-by: Dean Chen <862469039@qq.com>
1 parent 3c27bc4 commit 03d1726

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

operator/pkg/gateway-api/gateway_reconcile.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
package gateway_api
55

66
import (
7-
"bytes"
87
"context"
98
"errors"
109
"fmt"
1110
"log/slog"
12-
"net"
13-
"sort"
11+
"net/netip"
12+
"slices"
1413
"strings"
1514

1615
"github.com/google/go-cmp/cmp"
@@ -1116,19 +1115,23 @@ func (r *gatewayReconciler) setAddressStatus(ctx context.Context, gw *gatewayv1.
11161115
return fmt.Errorf("unable to list nodes")
11171116
}
11181117

1119-
ips := make([]net.IP, 0)
1118+
ips := make([]netip.Addr, 0)
11201119
for _, node := range nodes.Items {
11211120
if len(node.Status.Addresses) == 0 {
11221121
continue
11231122
}
11241123
nodeAddress := node.Status.Addresses[0]
1125-
ips = append(ips, net.ParseIP(nodeAddress.Address))
1124+
ip, err := netip.ParseAddr(nodeAddress.Address)
1125+
if err != nil {
1126+
// the first address is not an IP address (e.g. a hostname),
1127+
// skip the node instead of reporting an invalid address.
1128+
continue
1129+
}
1130+
ips = append(ips, ip.Unmap())
11261131
}
11271132

11281133
// sort the addresses for consistent ip addresses assigned
1129-
sort.Slice(ips, func(i, j int) bool {
1130-
return bytes.Compare(ips[i], ips[j]) < 0
1131-
})
1134+
slices.SortFunc(ips, netip.Addr.Compare)
11321135

11331136
// allows for only a max of 16 addresses
11341137
if len(ips) > 16 {
@@ -1560,8 +1563,7 @@ func (r *gatewayReconciler) verifyGatewayStaticAddresses(gw *gatewayv1.Gateway)
15601563
if address.Value == "" {
15611564
return fmt.Errorf("address value is not set")
15621565
}
1563-
ip := net.ParseIP(address.Value)
1564-
if ip == nil {
1566+
if _, err := netip.ParseAddr(address.Value); err != nil {
15651567
return fmt.Errorf("invalid ip address")
15661568
}
15671569
}

operator/pkg/gateway-api/testdata/gateway/gatewayclassconfig-nodeport/input/nodeport-gateway.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ status:
8484
type: InternalIP
8585
- address: kind-worker
8686
type: Hostname
87+
---
88+
apiVersion: v1
89+
kind: Node
90+
metadata:
91+
name: kind-worker3
92+
spec:
93+
podCIDR: 10.244.2.0/24
94+
podCIDRs:
95+
- 10.244.2.0/24
96+
- fd00:10:244:2::/64
97+
status:
98+
addresses:
99+
- address: kind-worker3
100+
type: Hostname

0 commit comments

Comments
 (0)