@@ -2,7 +2,9 @@ package k8s
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
7
+ "net"
6
8
"strings"
7
9
8
10
"github.com/cloud-bulldozer/k8s-netperf/pkg/config"
@@ -12,9 +14,12 @@ import (
12
14
corev1 "k8s.io/api/core/v1"
13
15
v1 "k8s.io/api/rbac/v1"
14
16
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15
18
"k8s.io/apimachinery/pkg/labels"
19
+ "k8s.io/apimachinery/pkg/runtime/schema"
16
20
"k8s.io/apimachinery/pkg/util/intstr"
17
21
"k8s.io/apimachinery/pkg/watch"
22
+ "k8s.io/client-go/dynamic"
18
23
"k8s.io/client-go/kubernetes"
19
24
"k8s.io/utils/pointer"
20
25
)
@@ -44,6 +49,13 @@ type ServiceParams struct {
44
49
DataPorts []int32
45
50
}
46
51
52
+ type PodNetworksData struct {
53
+ IPAddresses []string `json:"ip_addresses"`
54
+ MacAddress string `json:"mac_address"`
55
+ GatewayIPs []string `json:"gateway_ips"`
56
+ Role string `json:"role"`
57
+ }
58
+
47
59
const sa string = "netperf"
48
60
const namespace string = "netperf"
49
61
@@ -72,6 +84,7 @@ const clientAcrossRole = "client-across"
72
84
const hostNetServerRole = "host-server"
73
85
const hostNetClientRole = "host-client"
74
86
const k8sNetperfImage = "quay.io/cloud-bulldozer/k8s-netperf:latest"
87
+ const udnName = "udn-l2-primary"
75
88
76
89
// BuildInfra will create the infra for the SUT
77
90
func BuildInfra (client * kubernetes.Clientset ) error {
@@ -124,6 +137,40 @@ func BuildInfra(client *kubernetes.Clientset) error {
124
137
return nil
125
138
}
126
139
140
+ // Create a User Defined Network for the tests
141
+ func DeployL2Udn (dynamicClient * dynamic.DynamicClient ) error {
142
+ log .Infof ("Deploying L2 Primary UDN in the NS : %s" , namespace )
143
+ udn := & unstructured.Unstructured {
144
+ Object : map [string ]interface {}{
145
+ "apiVersion" : "k8s.ovn.org/v1" ,
146
+ "kind" : "UserDefinedNetwork" ,
147
+ "metadata" : map [string ]interface {}{
148
+ "name" : udnName ,
149
+ "namespace" : "netperf" ,
150
+ },
151
+ "spec" : map [string ]interface {}{
152
+ "topology" : "Layer2" ,
153
+ "layer2" : map [string ]interface {}{
154
+ "role" : "Primary" ,
155
+ "subnets" : []string {"10.0.0.0/24" , "2001:db8::/60" },
156
+ },
157
+ },
158
+ },
159
+ }
160
+
161
+ // Specify the GVR for UDN
162
+ gvr := schema.GroupVersionResource {
163
+ Group : "k8s.ovn.org" ,
164
+ Version : "v1" ,
165
+ Resource : "userdefinednetworks" ,
166
+ }
167
+ _ , err := dynamicClient .Resource (gvr ).Namespace (namespace ).Create (context .TODO (), udn , metav1.CreateOptions {})
168
+ if err != nil {
169
+ return err
170
+ }
171
+ return nil
172
+ }
173
+
127
174
// BuildSUT Build the k8s env to run network performance tests
128
175
func BuildSUT (client * kubernetes.Clientset , s * config.PerfScenarios ) error {
129
176
var netperfDataPorts []int32
@@ -449,6 +496,35 @@ func BuildSUT(client *kubernetes.Clientset, s *config.PerfScenarios) error {
449
496
return nil
450
497
}
451
498
499
+ // Extract the UDN Ip address of a pod from the annotations - Support only ipv4
500
+ func ExtractUdnIp (s config.PerfScenarios ) (string , error ) {
501
+ podNetworksJson := s .Server .Items [0 ].Annotations ["k8s.ovn.org/pod-networks" ]
502
+ //
503
+ var root map [string ]json.RawMessage
504
+ err := json .Unmarshal ([]byte (podNetworksJson ), & root )
505
+ if err != nil {
506
+ fmt .Println ("Error unmarshalling JSON:" , err )
507
+ return "" , err
508
+ }
509
+ //
510
+ var udnData PodNetworksData
511
+ err = json .Unmarshal (root ["netperf/" + udnName ], & udnData )
512
+ if err != nil {
513
+ return "" , err
514
+ }
515
+ // Extract the IPv4 address
516
+ var ipv4 net.IP
517
+ for _ , ip := range udnData .IPAddresses {
518
+ if strings .Contains (ip , "." ) { // Check if it's an IPv4 address
519
+ ipv4 , _ , err = net .ParseCIDR (ip )
520
+ if err != nil {
521
+ return "" , err
522
+ }
523
+ }
524
+ }
525
+ return ipv4 .String (), nil
526
+ }
527
+
452
528
// launchServerVM will create the ServerVM with the specific node and pod affinity.
453
529
func launchServerVM (perf * config.PerfScenarios , name string , podAff * corev1.PodAntiAffinity , nodeAff * corev1.NodeAffinity ) error {
454
530
_ , err := CreateVMServer (perf .KClient , serverRole , serverRole , * podAff , * nodeAff )
0 commit comments