-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathswitch_lb_rule.go
More file actions
395 lines (351 loc) · 13.7 KB
/
switch_lb_rule.go
File metadata and controls
395 lines (351 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package switch_lb_rule
import (
"context"
"fmt"
"strconv"
"time"
"github.com/onsi/ginkgo/v2"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
e2epodoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/request"
"github.com/kubeovn/kube-ovn/pkg/util"
"github.com/kubeovn/kube-ovn/test/e2e/framework"
)
func generateSwitchLBRuleName(ruleName string) string {
return "lr-" + ruleName
}
func generateServiceName(slrName string) string {
return "slr-" + slrName
}
func generateVpcName(name string) string {
return "vpc-" + name
}
func generateSubnetName(name string) string {
return "subnet-" + name
}
func curlSvc(f *framework.Framework, clientPodName, vip string, port int32) {
ginkgo.GinkgoHelper()
cmd := "curl -q -s --connect-timeout 5 --max-time 5 " + util.JoinHostPort(vip, port)
ginkgo.By(fmt.Sprintf(`Executing %q in pod %s/%s`, cmd, f.Namespace.Name, clientPodName))
framework.WaitUntil(2*time.Second, 30*time.Second, func(_ context.Context) (bool, error) {
_, err := e2epodoutput.RunHostCmd(f.Namespace.Name, clientPodName, cmd)
return err == nil, nil
}, fmt.Sprintf("%s:%d is reachable", vip, port))
}
func isMultusInstalled(f *framework.Framework) bool {
_, err := f.ExtClientSet.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), "network-attachment-definitions.k8s.cni.cncf.io", metav1.GetOptions{})
return err == nil
}
var _ = framework.Describe("[group:slr]", func() {
f := framework.NewDefaultFramework("slr")
var (
switchLBRuleClient *framework.SwitchLBRuleClient
endpointsClient *framework.EndpointsClient
serviceClient *framework.ServiceClient
stsClient *framework.StatefulSetClient
podClient *framework.PodClient
subnetClient *framework.SubnetClient
vpcClient *framework.VpcClient
nadClient *framework.NetworkAttachmentDefinitionClient
namespaceName, suffix string
vpcName, subnetName, clientPodName, label string
stsName, stsSvcName string
selSlrName, selSvcName string
epSlrName, epSvcName string
nadName string
overlaySubnetCidr, vip string
// TODO:// slr support dual-stack
frontPort, selSlrFrontPort, epSlrFrontPort, backendPort int32
)
ginkgo.BeforeEach(func() {
switchLBRuleClient = f.SwitchLBRuleClient()
endpointsClient = f.EndpointClient()
serviceClient = f.ServiceClient()
stsClient = f.StatefulSetClient()
podClient = f.PodClient()
subnetClient = f.SubnetClient()
vpcClient = f.VpcClient()
nadClient = f.NetworkAttachmentDefinitionClient()
suffix = framework.RandomSuffix()
namespaceName = f.Namespace.Name
selSlrName = "sel-" + generateSwitchLBRuleName(suffix)
selSvcName = generateServiceName(selSlrName)
epSlrName = "ep-" + generateSwitchLBRuleName(suffix)
epSvcName = generateServiceName(epSlrName)
stsName = "sts-" + suffix
stsSvcName = stsName
label = "slr"
clientPodName = "client-" + suffix
subnetName = generateSubnetName(suffix)
nadName = subnetName
vpcName = generateVpcName(suffix)
frontPort = 8090
selSlrFrontPort = 8091
epSlrFrontPort = 8092
backendPort = 80
vip = ""
overlaySubnetCidr = framework.RandomCIDR(f.ClusterIPFamily)
ginkgo.By("Creating custom vpc")
vpc := framework.MakeVpc(vpcName, "", false, false, []string{namespaceName})
_ = vpcClient.CreateSync(vpc)
})
ginkgo.AfterEach(func() {
ginkgo.By("Deleting client pod " + clientPodName)
podClient.DeleteSync(clientPodName)
ginkgo.By("Deleting statefulset " + stsName)
stsClient.DeleteSync(stsName)
ginkgo.By("Deleting switch-lb-rule " + selSlrName)
switchLBRuleClient.DeleteSync(selSlrName)
ginkgo.By("Deleting switch-lb-rule " + epSlrName)
switchLBRuleClient.DeleteSync(epSlrName)
ginkgo.By("Deleting service " + stsSvcName)
serviceClient.DeleteSync(stsSvcName)
ginkgo.By("Deleting subnet " + subnetName)
subnetClient.DeleteSync(subnetName)
ginkgo.By("Deleting vpc " + vpcName)
vpcClient.DeleteSync(vpcName)
ginkgo.By("Deleting network attachment definition " + nadName)
nadClient.Delete(nadName)
})
ginkgo.DescribeTable("Test SLR connectivity", ginkgo.Label("Conformance"), func(customProvider bool) {
f.SkipVersionPriorTo(1, 12, "This feature was introduced in v1.12")
var provider string
annotations := make(map[string]string)
if customProvider {
f.SkipVersionPriorTo(1, 15, "This feature was introduced in v1.15")
if !isMultusInstalled(f) { // Multus must be installed for some tests
ginkgo.Skip("Multus must be activated to run the SLR tests with custom providers")
}
provider = fmt.Sprintf("%s.%s.%s", subnetName, f.Namespace.Name, util.OvnProvider)
annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, provider)] = subnetName
annotations[util.DefaultNetworkAnnotation] = fmt.Sprintf("%s/%s", f.Namespace.Name, nadName)
} else {
annotations[util.LogicalSwitchAnnotation] = subnetName
}
ginkgo.By("Creating custom overlay subnet")
overlaySubnet := framework.MakeSubnet(subnetName, "", overlaySubnetCidr, "", vpcName, provider, nil, nil, nil)
_ = subnetClient.CreateSync(overlaySubnet)
if customProvider {
nad := framework.MakeOVNNetworkAttachmentDefinition(subnetName, f.Namespace.Namespace, provider, []request.Route{})
nadClient.Create(nad)
}
ginkgo.By("Creating client pod " + clientPodName)
newClientPod := framework.MakePod(namespaceName, clientPodName, nil, annotations, framework.AgnhostImage, nil, nil)
podClient.CreateSync(newClientPod)
ginkgo.By("1. Creating sts svc with slr")
var (
clientPod *corev1.Pod
err error
stsSvc, selSvc, epSvc *corev1.Service
selSlrEps, epSlrEps *corev1.Endpoints
)
replicas := 1
labels := map[string]string{"app": label}
ginkgo.By("Creating statefulset " + stsName + " with subnet " + subnetName)
sts := framework.MakeStatefulSet(stsName, stsSvcName, int32(replicas), labels, framework.AgnhostImage)
ginkgo.By("Creating sts " + stsName)
sts.Spec.Template.Annotations = annotations
portStr := strconv.Itoa(80)
webServerCmd := []string{"/agnhost", "netexec", "--http-port", portStr}
sts.Spec.Template.Spec.Containers[0].Command = webServerCmd
_ = stsClient.CreateSync(sts)
ginkgo.By("Creating service " + stsSvcName)
ports := []corev1.ServicePort{{
Name: "http",
Protocol: corev1.ProtocolTCP,
Port: frontPort,
TargetPort: intstr.FromInt32(80),
}}
selector := map[string]string{"app": label}
svcAnnotations := map[string]string{
util.LogicalSwitchAnnotation: subnetName,
}
stsSvc = framework.MakeService(stsSvcName, corev1.ServiceTypeClusterIP, svcAnnotations, selector, ports, corev1.ServiceAffinityNone)
stsSvc = serviceClient.CreateSync(stsSvc, func(s *corev1.Service) (bool, error) {
return len(s.Spec.ClusterIPs) != 0, nil
}, "cluster ips are not empty")
ginkgo.By("Waiting for sts service " + stsSvcName + " to be ready")
framework.WaitUntil(2*time.Second, time.Minute, func(_ context.Context) (bool, error) {
stsSvc, err = serviceClient.ServiceInterface.Get(context.TODO(), stsSvcName, metav1.GetOptions{})
if err == nil {
return true, nil
}
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, err
}, fmt.Sprintf("service %s is created", stsSvcName))
framework.ExpectNotNil(stsSvc)
ginkgo.By("Get client pod " + clientPodName)
clientPod, err = podClient.Get(context.TODO(), clientPodName, metav1.GetOptions{})
framework.ExpectNil(err)
framework.ExpectNotNil(clientPod)
ginkgo.By("Checking sts service " + stsSvc.Name)
for _, ip := range stsSvc.Spec.ClusterIPs {
curlSvc(f, clientPodName, ip, frontPort)
}
vip = stsSvc.Spec.ClusterIP
ginkgo.By("2. Creating switch-lb-rule with selector with lb front vip " + vip)
ginkgo.By("Creating selector SwitchLBRule " + epSlrName)
var (
selRule *kubeovnv1.SwitchLBRule
slrSelector []string
slrPorts, epPorts []kubeovnv1.SwitchLBRulePort
sessionAffinity corev1.ServiceAffinity
)
sessionAffinity = corev1.ServiceAffinityNone
slrPorts = []kubeovnv1.SwitchLBRulePort{
{
Name: "http",
Port: selSlrFrontPort,
TargetPort: backendPort,
Protocol: "TCP",
},
}
slrSelector = []string{"app:" + label}
selRule = framework.MakeSwitchLBRule(selSlrName, namespaceName, vip, sessionAffinity, nil, slrSelector, nil, slrPorts)
_ = switchLBRuleClient.Create(selRule)
ginkgo.By("Waiting for switch-lb-rule " + selSlrName + " to be ready")
framework.WaitUntil(2*time.Second, time.Minute, func(_ context.Context) (bool, error) {
_, err = switchLBRuleClient.SwitchLBRuleInterface.Get(context.TODO(), selSlrName, metav1.GetOptions{})
if err == nil {
return true, nil
}
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, err
}, fmt.Sprintf("switch-lb-rule %s is created", selSlrName))
ginkgo.By("Waiting for headless service " + selSvcName + " to be ready")
framework.WaitUntil(2*time.Second, time.Minute, func(_ context.Context) (bool, error) {
selSvc, err = serviceClient.ServiceInterface.Get(context.TODO(), selSvcName, metav1.GetOptions{})
if err == nil {
return true, nil
}
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, err
}, fmt.Sprintf("service %s is created", selSvcName))
framework.ExpectNotNil(selSvc)
ginkgo.By("Waiting for endpoints " + selSvcName + " to be ready")
framework.WaitUntil(2*time.Second, time.Minute, func(_ context.Context) (bool, error) {
selSlrEps, err = endpointsClient.EndpointsInterface.Get(context.TODO(), selSvcName, metav1.GetOptions{})
if err == nil {
return true, nil
}
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, err
}, fmt.Sprintf("endpoints %s is created", selSvcName))
framework.ExpectNotNil(selSlrEps)
pods := stsClient.GetPods(sts)
framework.ExpectHaveLen(pods.Items, replicas)
for i, subset := range selSlrEps.Subsets {
var (
ips []string
tps []int32
protocols = make(map[int32]string)
)
ginkgo.By("Checking endpoint address")
for _, address := range subset.Addresses {
ips = append(ips, address.IP)
}
framework.ExpectContainElement(ips, pods.Items[i].Status.PodIP)
ginkgo.By("Checking endpoint ports")
for _, port := range subset.Ports {
tps = append(tps, port.Port)
protocols[port.Port] = string(port.Protocol)
}
for _, port := range slrPorts {
framework.ExpectContainElement(tps, port.TargetPort)
framework.ExpectHaveKeyWithValue(protocols, port.TargetPort, port.Protocol)
}
}
ginkgo.By("Checking selector switch lb service " + selSvc.Name)
curlSvc(f, clientPodName, vip, selSlrFrontPort)
ginkgo.By("3. Creating switch-lb-rule with endpoints with lb front vip " + vip)
ginkgo.By("Creating endpoint SwitchLBRule " + epSlrName)
sessionAffinity = corev1.ServiceAffinityClientIP
epPorts = []kubeovnv1.SwitchLBRulePort{
{
Name: "http",
Port: epSlrFrontPort,
TargetPort: backendPort,
Protocol: "TCP",
},
}
presetEndpoints := []string{}
for _, pod := range pods.Items {
presetEndpoints = append(presetEndpoints, pod.Status.PodIP)
}
epRule := framework.MakeSwitchLBRule(epSlrName, namespaceName, vip, sessionAffinity, annotations, nil, presetEndpoints, epPorts)
_ = switchLBRuleClient.Create(epRule)
ginkgo.By("Waiting for switch-lb-rule " + epSlrName + " to be ready")
framework.WaitUntil(2*time.Second, time.Minute, func(_ context.Context) (bool, error) {
_, err := switchLBRuleClient.SwitchLBRuleInterface.Get(context.TODO(), epSlrName, metav1.GetOptions{})
if err == nil {
return true, nil
}
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, err
}, fmt.Sprintf("switch-lb-rule %s is created", epSlrName))
ginkgo.By("Waiting for headless service " + epSvcName + " to be ready")
framework.WaitUntil(2*time.Second, time.Minute, func(_ context.Context) (bool, error) {
epSvc, err = serviceClient.ServiceInterface.Get(context.TODO(), epSvcName, metav1.GetOptions{})
if err == nil {
return true, nil
}
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, err
}, fmt.Sprintf("service %s is created", epSvcName))
framework.ExpectNotNil(epSvc)
ginkgo.By("Waiting for endpoints " + epSvcName + " to be ready")
framework.WaitUntil(2*time.Second, time.Minute, func(_ context.Context) (bool, error) {
epSlrEps, err = endpointsClient.EndpointsInterface.Get(context.TODO(), epSvcName, metav1.GetOptions{})
if err == nil {
return true, nil
}
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, err
}, fmt.Sprintf("endpoints %s is created", epSvcName))
framework.ExpectNotNil(epSlrEps)
for i, subset := range epSlrEps.Subsets {
var (
ips []string
tps []int32
protocols = make(map[int32]string)
)
ginkgo.By("Checking endpoint address")
for _, address := range subset.Addresses {
ips = append(ips, address.IP)
}
framework.ExpectContainElement(ips, pods.Items[i].Status.PodIP)
ginkgo.By("Checking endpoint ports")
for _, port := range subset.Ports {
tps = append(tps, port.Port)
protocols[port.Port] = string(port.Protocol)
}
for _, port := range epPorts {
framework.ExpectContainElement(tps, port.TargetPort)
framework.ExpectHaveKeyWithValue(protocols, port.TargetPort, port.Protocol)
}
}
ginkgo.By("Checking endpoint switch lb service " + epSvc.Name)
curlSvc(f, clientPodName, vip, epSlrFrontPort)
},
ginkgo.Entry("SLR with default provider", false),
ginkgo.Entry("SLR with custom provider", true),
)
})