Skip to content

Commit 8913e0b

Browse files
aswinsuryandfarrell07
authored andcommitted
Add Ipv6 support for OVN-IC setups
Signed-off-by: Aswin Suryanarayanan <[email protected]>
1 parent de50d63 commit 8913e0b

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

pkg/routeagent_driver/handlers/ovn/connection.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ import (
4242
"k8s.io/apimachinery/pkg/runtime/schema"
4343
"k8s.io/client-go/dynamic"
4444
clientset "k8s.io/client-go/kubernetes"
45-
"k8s.io/utils/net"
45+
k8snet "k8s.io/utils/net"
4646
)
4747

4848
type ConnectionHandler struct {
4949
k8sClientset clientset.Interface
5050
dynamicClient dynamic.Interface
5151
nbdb libovsdbclient.Client
52-
ipFamily net.IPFamily
52+
ipFamily k8snet.IPFamily
5353
}
5454

55-
func NewConnectionHandler(ipFamily net.IPFamily, k8sClientset clientset.Interface, dynamicClient dynamic.Interface) *ConnectionHandler {
55+
func NewConnectionHandler(ipFamily k8snet.IPFamily, k8sClientset clientset.Interface, dynamicClient dynamic.Interface) *ConnectionHandler {
5656
return &ConnectionHandler{
5757
k8sClientset: k8sClientset,
5858
dynamicClient: dynamicClient,
@@ -117,7 +117,7 @@ func (c *ConnectionHandler) createLibovsdbClient(ctx context.Context, dbModel mo
117117
// Will use empty zone if not found
118118
zoneName := annotations[constants.OvnZoneAnnotation]
119119

120-
dbAddress, err := discoverOvnKubernetesNetwork(ctx, c.k8sClientset, c.dynamicClient, zoneName)
120+
dbAddress, err := discoverOvnKubernetesNetwork(c.ipFamily, ctx, c.k8sClientset, c.dynamicClient, zoneName)
121121
if err != nil {
122122
return nil, errors.Wrap(err, "error getting the OVN NBDB Address")
123123
}
@@ -196,7 +196,7 @@ func getTLSConfig(ctx context.Context, k8sClientset clientset.Interface) (*tls.C
196196
return tlsConfig, nil
197197
}
198198

199-
func discoverOvnKubernetesNetwork(ctx context.Context, k8sClientset clientset.Interface,
199+
func discoverOvnKubernetesNetwork(family k8snet.IPFamily, ctx context.Context, k8sClientset clientset.Interface,
200200
dynamicClient dynamic.Interface, zoneName string,
201201
) (string, error) {
202202
openshiftNetwork, err := FindOpenshiftNetwork(ctx, dynamicClient)
@@ -208,7 +208,7 @@ func discoverOvnKubernetesNetwork(ctx context.Context, k8sClientset clientset.In
208208
return discoverOpenshiftOvnKubernetesNetwork(ctx, k8sClientset)
209209
}
210210

211-
return discoverKindOvnKubernetesNetwork(ctx, k8sClientset, zoneName)
211+
return discoverKindOvnKubernetesNetwork(family, ctx, k8sClientset, zoneName)
212212
}
213213

214214
/*
@@ -245,7 +245,9 @@ The discovery method varies based on the deployment type.
245245
kind ic with one node per zone: we will have no endpoints matching hence we will return default kind socket path
246246
*/
247247

248-
func discoverKindOvnKubernetesNetwork(ctx context.Context, k8sClientSet clientset.Interface, zoneName string) (string, error) {
248+
func discoverKindOvnKubernetesNetwork(family k8snet.IPFamily, ctx context.Context, k8sClientSet clientset.Interface,
249+
zoneName string,
250+
) (string, error) {
249251
ovnDBPod, err := FindPod(ctx, k8sClientSet, "name=ovnkube-db")
250252
if err != nil {
251253
return "", err
@@ -264,7 +266,7 @@ func discoverKindOvnKubernetesNetwork(ctx context.Context, k8sClientSet clientse
264266
return "", fmt.Errorf("error finding the pod with label %q", ovnPodLabel)
265267
}
266268

267-
return discoverKindOvnNodeClusterNetwork(ctx, k8sClientSet, zoneName, ovnPod)
269+
return discoverKindOvnNodeClusterNetwork(family, ctx, k8sClientSet, zoneName, ovnPod)
268270
}
269271

270272
func discoverKindOvnDBClusterNetwork(ctx context.Context, ovnDBPod *corev1.Pod, k8sClientSet clientset.Interface) (string, error) {
@@ -278,7 +280,7 @@ func discoverKindOvnDBClusterNetwork(ctx context.Context, ovnDBPod *corev1.Pod,
278280
return fmt.Sprintf("%s:%s.%s:%d", dbConnectionProtocol, ovnKubeService, ovnDBPod.Namespace, ovnNBDBDefaultPort), nil
279281
}
280282

281-
func discoverKindOvnNodeClusterNetwork(ctx context.Context, k8sClientset clientset.Interface,
283+
func discoverKindOvnNodeClusterNetwork(family k8snet.IPFamily, ctx context.Context, k8sClientset clientset.Interface,
282284
zoneName string, ovnPod *corev1.Pod,
283285
) (string, error) {
284286
endpointList, err := findEndpoint(ctx, k8sClientset, ovnPod.Namespace)
@@ -291,7 +293,7 @@ func discoverKindOvnNodeClusterNetwork(ctx context.Context, k8sClientset clients
291293
if endpointList == nil || len(endpointList.Items) == 0 {
292294
nbdbAddress = defaultOVNUnixSocket
293295
} else {
294-
nbdbAddress, err = createClusterNetworkWithEndpoints(endpointList.Items, zoneName)
296+
nbdbAddress, err = createClusterNetworkWithEndpoints(family, endpointList.Items, zoneName)
295297
if err != nil {
296298
return "", err
297299
}
@@ -300,21 +302,33 @@ func discoverKindOvnNodeClusterNetwork(ctx context.Context, k8sClientset clients
300302
return nbdbAddress, nil
301303
}
302304

303-
func createClusterNetworkWithEndpoints(endPoints []corev1.Endpoints, zoneName string) (string, error) {
304-
for index := range endPoints {
305-
for _, subset := range endPoints[index].Subsets {
306-
if strings.Contains(endPoints[index].Name, zoneName) {
305+
func createClusterNetworkWithEndpoints(family k8snet.IPFamily, endPoints []corev1.Endpoints, zoneName string) (string, error) {
306+
for i := range endPoints {
307+
if !strings.Contains(endPoints[i].Name, zoneName) {
308+
continue
309+
}
310+
311+
for _, subset := range endPoints[i].Subsets {
312+
for _, addr := range subset.Addresses {
313+
ip := addr.IP
314+
if k8snet.IPFamilyOfString(ip) != family {
315+
continue
316+
}
317+
307318
for _, port := range subset.Ports {
308-
if strings.Contains(port.Name, "north") && net.IsIPv4String(subset.Addresses[0].IP) {
309-
return fmt.Sprintf("%s:%s:%d",
310-
port.Protocol, subset.Addresses[0].IP, ovnNBDBDefaultPort), nil
319+
if strings.Contains(port.Name, "north") {
320+
if family == k8snet.IPv6 {
321+
return fmt.Sprintf("%s:[%s]:%d", port.Protocol, ip, ovnNBDBDefaultPort), nil
322+
}
323+
324+
return fmt.Sprintf("%s:%s:%d", port.Protocol, ip, ovnNBDBDefaultPort), nil
311325
}
312326
}
313327
}
314328
}
315329
}
316330

317-
return "", fmt.Errorf("error finding an endpoint for the zone %q", zoneName)
331+
return "", fmt.Errorf("error finding an endpoint for the zone %q with IP family %v", zoneName, family)
318332
}
319333

320334
func findEndpoint(ctx context.Context, k8sClientset clientset.Interface, endpointNameSpace string) (*corev1.EndpointsList, error) {

pkg/routeagent_driver/handlers/ovn/utils.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ func jsonToIP(jsonData string) (string, error) {
5555

5656
err := json.Unmarshal([]byte(jsonData), &data)
5757
if err != nil {
58-
return "", errors.Wrapf(err, "error unmarshalling the json ip")
58+
return "", errors.Wrapf(err, "error unmarshalling the JSON IP")
5959
}
6060

61-
ipStr, found := data["ipv4"]
62-
if !found {
63-
return "", errors.New("json data does not contain an 'ipv4' field")
61+
var ipStr string
62+
if val, found := data["ipv4"]; found {
63+
ipStr = val
64+
} else if val, found := data["ipv6"]; found {
65+
ipStr = val
66+
} else {
67+
return "", errors.New("JSON data does not contain 'ipv4' or 'ipv6' field")
6468
}
6569

6670
ip, _, err := net.ParseCIDR(ipStr)

0 commit comments

Comments
 (0)