@@ -42,17 +42,17 @@ import (
42
42
"k8s.io/apimachinery/pkg/runtime/schema"
43
43
"k8s.io/client-go/dynamic"
44
44
clientset "k8s.io/client-go/kubernetes"
45
- "k8s.io/utils/net"
45
+ k8snet "k8s.io/utils/net"
46
46
)
47
47
48
48
type ConnectionHandler struct {
49
49
k8sClientset clientset.Interface
50
50
dynamicClient dynamic.Interface
51
51
nbdb libovsdbclient.Client
52
- ipFamily net .IPFamily
52
+ ipFamily k8snet .IPFamily
53
53
}
54
54
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 {
56
56
return & ConnectionHandler {
57
57
k8sClientset : k8sClientset ,
58
58
dynamicClient : dynamicClient ,
@@ -117,7 +117,7 @@ func (c *ConnectionHandler) createLibovsdbClient(ctx context.Context, dbModel mo
117
117
// Will use empty zone if not found
118
118
zoneName := annotations [constants .OvnZoneAnnotation ]
119
119
120
- dbAddress , err := discoverOvnKubernetesNetwork (ctx , c .k8sClientset , c .dynamicClient , zoneName )
120
+ dbAddress , err := discoverOvnKubernetesNetwork (c . ipFamily , ctx , c .k8sClientset , c .dynamicClient , zoneName )
121
121
if err != nil {
122
122
return nil , errors .Wrap (err , "error getting the OVN NBDB Address" )
123
123
}
@@ -196,7 +196,7 @@ func getTLSConfig(ctx context.Context, k8sClientset clientset.Interface) (*tls.C
196
196
return tlsConfig , nil
197
197
}
198
198
199
- func discoverOvnKubernetesNetwork (ctx context.Context , k8sClientset clientset.Interface ,
199
+ func discoverOvnKubernetesNetwork (family k8snet. IPFamily , ctx context.Context , k8sClientset clientset.Interface ,
200
200
dynamicClient dynamic.Interface , zoneName string ,
201
201
) (string , error ) {
202
202
openshiftNetwork , err := FindOpenshiftNetwork (ctx , dynamicClient )
@@ -208,7 +208,7 @@ func discoverOvnKubernetesNetwork(ctx context.Context, k8sClientset clientset.In
208
208
return discoverOpenshiftOvnKubernetesNetwork (ctx , k8sClientset )
209
209
}
210
210
211
- return discoverKindOvnKubernetesNetwork (ctx , k8sClientset , zoneName )
211
+ return discoverKindOvnKubernetesNetwork (family , ctx , k8sClientset , zoneName )
212
212
}
213
213
214
214
/*
@@ -245,7 +245,9 @@ The discovery method varies based on the deployment type.
245
245
kind ic with one node per zone: we will have no endpoints matching hence we will return default kind socket path
246
246
*/
247
247
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 ) {
249
251
ovnDBPod , err := FindPod (ctx , k8sClientSet , "name=ovnkube-db" )
250
252
if err != nil {
251
253
return "" , err
@@ -264,7 +266,7 @@ func discoverKindOvnKubernetesNetwork(ctx context.Context, k8sClientSet clientse
264
266
return "" , fmt .Errorf ("error finding the pod with label %q" , ovnPodLabel )
265
267
}
266
268
267
- return discoverKindOvnNodeClusterNetwork (ctx , k8sClientSet , zoneName , ovnPod )
269
+ return discoverKindOvnNodeClusterNetwork (family , ctx , k8sClientSet , zoneName , ovnPod )
268
270
}
269
271
270
272
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,
278
280
return fmt .Sprintf ("%s:%s.%s:%d" , dbConnectionProtocol , ovnKubeService , ovnDBPod .Namespace , ovnNBDBDefaultPort ), nil
279
281
}
280
282
281
- func discoverKindOvnNodeClusterNetwork (ctx context.Context , k8sClientset clientset.Interface ,
283
+ func discoverKindOvnNodeClusterNetwork (family k8snet. IPFamily , ctx context.Context , k8sClientset clientset.Interface ,
282
284
zoneName string , ovnPod * corev1.Pod ,
283
285
) (string , error ) {
284
286
endpointList , err := findEndpoint (ctx , k8sClientset , ovnPod .Namespace )
@@ -291,7 +293,7 @@ func discoverKindOvnNodeClusterNetwork(ctx context.Context, k8sClientset clients
291
293
if endpointList == nil || len (endpointList .Items ) == 0 {
292
294
nbdbAddress = defaultOVNUnixSocket
293
295
} else {
294
- nbdbAddress , err = createClusterNetworkWithEndpoints (endpointList .Items , zoneName )
296
+ nbdbAddress , err = createClusterNetworkWithEndpoints (family , endpointList .Items , zoneName )
295
297
if err != nil {
296
298
return "" , err
297
299
}
@@ -300,21 +302,33 @@ func discoverKindOvnNodeClusterNetwork(ctx context.Context, k8sClientset clients
300
302
return nbdbAddress , nil
301
303
}
302
304
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
+
307
318
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
311
325
}
312
326
}
313
327
}
314
328
}
315
329
}
316
330
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 )
318
332
}
319
333
320
334
func findEndpoint (ctx context.Context , k8sClientset clientset.Interface , endpointNameSpace string ) (* corev1.EndpointsList , error ) {
0 commit comments