forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
323 lines (258 loc) · 12.1 KB
/
services.go
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
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2020 Red Hat, Inc.
*
*/
package network
import (
"context"
"fmt"
"strconv"
"time"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"kubevirt.io/kubevirt/tests/util"
batchv1 "k8s.io/api/batch/v1"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/console"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/job"
netservice "kubevirt.io/kubevirt/tests/libnet/service"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
)
const (
cleaningK8sv1ServiceShouldSucceed = "cleaning up the k8sv1.Service entity should have succeeded."
cleaningK8sv1JobFuncShouldExist = "a k8sv1.Job cleaning up function should exist"
cleaningK8sv1JobShouldSucceed = "cleaning up the k8sv1.Job entity should have succeeded."
expectConnectivityToExposedService = "connectivity is expected to the exposed service"
)
var _ = SIGDescribe("Services", func() {
var virtClient kubecli.KubevirtClient
runTCPClientExpectingHelloWorldFromServer := func(host, port, namespace string, retries int32) *batchv1.Job {
job := job.NewHelloWorldJobTCP(host, port)
job.Spec.BackoffLimit = &retries
var err error
job, err = virtClient.BatchV1().Jobs(namespace).Create(context.Background(), job, k8smetav1.CreateOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return job
}
exposeExistingVMISpec := func(vmi *v1.VirtualMachineInstance, subdomain string, hostname string, selectorLabelKey string, selectorLabelValue string) *v1.VirtualMachineInstance {
vmi.Labels = map[string]string{selectorLabelKey: selectorLabelValue}
vmi.Spec.Subdomain = subdomain
vmi.Spec.Hostname = hostname
return vmi
}
readyVMI := func(vmi *v1.VirtualMachineInstance, loginTo console.LoginToFunction) *v1.VirtualMachineInstance {
createdVMI, err := virtClient.VirtualMachineInstance(util.NamespaceTestDefault).Create(context.Background(), vmi)
Expect(err).ToNot(HaveOccurred())
return libwait.WaitUntilVMIReady(createdVMI, loginTo)
}
cleanupVMI := func(virtClient kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance) {
By("Deleting the VMI")
Expect(virtClient.VirtualMachineInstance(util.NamespaceTestDefault).Delete(context.Background(), vmi.GetName(), &k8smetav1.DeleteOptions{})).To(Succeed())
By("Waiting for the VMI to be gone")
Eventually(func() error {
_, err := virtClient.VirtualMachineInstance(util.NamespaceTestDefault).Get(context.Background(), vmi.GetName(), &k8smetav1.GetOptions{})
return err
}, 2*time.Minute, time.Second).Should(SatisfyAll(HaveOccurred(), WithTransform(errors.IsNotFound, BeTrue())), "The VMI should be gone within the given timeout")
}
cleanupService := func(namespace string, serviceName string) error {
return virtClient.CoreV1().Services(namespace).Delete(context.Background(), serviceName, k8smetav1.DeleteOptions{})
}
assertConnectivityToService := func(serviceName, namespace string, servicePort int) (func() error, error) {
serviceFQDN := fmt.Sprintf("%s.%s", serviceName, namespace)
By(fmt.Sprintf("starting a job which tries to reach the vmi via service %s", serviceFQDN))
tcpJob := runTCPClientExpectingHelloWorldFromServer(serviceFQDN, strconv.Itoa(servicePort), namespace, 3)
By(fmt.Sprintf("waiting for the job to report a SUCCESSFUL connection attempt to service %s on port %d", serviceFQDN, servicePort))
err := job.WaitForJobToSucceed(tcpJob, 90*time.Second)
return func() error {
return virtClient.BatchV1().Jobs(util.NamespaceTestDefault).Delete(context.Background(), tcpJob.Name, k8smetav1.DeleteOptions{})
}, err
}
assertNoConnectivityToService := func(serviceName, namespace string, servicePort int) (func() error, error) {
serviceFQDN := fmt.Sprintf("%s.%s", serviceName, namespace)
By(fmt.Sprintf("starting a job which tries to reach the vmi via service %s", serviceFQDN))
tcpJob := runTCPClientExpectingHelloWorldFromServer(serviceFQDN, strconv.Itoa(servicePort), namespace, 0)
By(fmt.Sprintf("waiting for the job to report a FAILED connection attempt to service %s on port %d", serviceFQDN, servicePort))
err := job.WaitForJobToFail(tcpJob, 90*time.Second)
return func() error {
return virtClient.BatchV1().Jobs(util.NamespaceTestDefault).Delete(context.Background(), tcpJob.Name, k8smetav1.DeleteOptions{})
}, err
}
BeforeEach(func() {
virtClient = kubevirt.Client()
})
Context("bridge interface binding", func() {
var inboundVMI *v1.VirtualMachineInstance
var serviceName string
const (
selectorLabelKey = "expose"
selectorLabelValue = "me"
servicePort = 1500
)
createVMISpecWithBridgeInterface := func() *v1.VirtualMachineInstance {
return libvmi.NewCirros(
libvmi.WithInterface(libvmi.InterfaceDeviceWithBridgeBinding(libvmi.DefaultInterfaceName)),
libvmi.WithNetwork(v1.DefaultPodNetwork()))
}
createReadyVMIWithBridgeBindingAndExposedService := func(hostname string, subdomain string) *v1.VirtualMachineInstance {
return readyVMI(
exposeExistingVMISpec(
createVMISpecWithBridgeInterface(), subdomain, hostname, selectorLabelKey, selectorLabelValue),
console.LoginToCirros)
}
BeforeEach(func() {
libnet.SkipWhenClusterNotSupportIpv4()
subdomain := "vmi"
hostname := "inbound"
inboundVMI = createReadyVMIWithBridgeBindingAndExposedService(hostname, subdomain)
tests.StartTCPServer(inboundVMI, servicePort, console.LoginToCirros)
})
AfterEach(func() {
Expect(inboundVMI).NotTo(BeNil(), "the VMI object must exist in order to be deleted.")
cleanupVMI(virtClient, inboundVMI)
})
Context("with a service matching the vmi exposed", func() {
var jobCleanup func() error
BeforeEach(func() {
serviceName = "myservice"
service := netservice.BuildSpec(serviceName, servicePort, servicePort, selectorLabelKey, selectorLabelValue)
_, err := virtClient.CoreV1().Services(inboundVMI.Namespace).Create(context.Background(), service, k8smetav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
Expect(cleanupService(inboundVMI.GetNamespace(), serviceName)).To(Succeed(), cleaningK8sv1ServiceShouldSucceed)
})
AfterEach(func() {
Expect(jobCleanup).NotTo(BeNil(), cleaningK8sv1JobFuncShouldExist)
Expect(jobCleanup()).To(Succeed(), cleaningK8sv1JobShouldSucceed)
jobCleanup = nil
})
It("[test_id:1547] should be able to reach the vmi based on labels specified on the vmi", func() {
var err error
jobCleanup, err = assertConnectivityToService(serviceName, inboundVMI.Namespace, servicePort)
Expect(err).NotTo(HaveOccurred(), expectConnectivityToExposedService)
})
It("[test_id:1548] should fail to reach the vmi if an invalid servicename is used", func() {
var err error
jobCleanup, err = assertNoConnectivityToService("wrongservice", inboundVMI.Namespace, servicePort)
Expect(err).NotTo(HaveOccurred(), "connectivity is *not* expected, since there isn't an exposed service")
})
})
Context("with a subdomain and a headless service given", func() {
var jobCleanup func() error
BeforeEach(func() {
serviceName = inboundVMI.Spec.Subdomain
service := netservice.BuildHeadlessSpec(serviceName, servicePort, servicePort, selectorLabelKey, selectorLabelValue)
_, err := virtClient.CoreV1().Services(inboundVMI.Namespace).Create(context.Background(), service, k8smetav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
Expect(virtClient.CoreV1().Services(inboundVMI.Namespace).Delete(context.Background(), serviceName, k8smetav1.DeleteOptions{})).To(Succeed())
})
AfterEach(func() {
Expect(jobCleanup()).To(Succeed(), cleaningK8sv1ServiceShouldSucceed)
})
It("[test_id:1549]should be able to reach the vmi via its unique fully qualified domain name", func() {
var err error
serviceHostnameWithSubdomain := fmt.Sprintf("%s.%s", inboundVMI.Spec.Hostname, inboundVMI.Spec.Subdomain)
jobCleanup, err = assertConnectivityToService(serviceHostnameWithSubdomain, inboundVMI.Namespace, servicePort)
Expect(err).NotTo(HaveOccurred(), expectConnectivityToExposedService)
})
})
})
Context("Masquerade interface binding", func() {
var inboundVMI *v1.VirtualMachineInstance
const (
selectorLabelKey = "expose"
selectorLabelValue = "me"
servicePort = 1500
)
createReadyVMIWithMasqueradeBindingAndExposedService := func(hostname string, subdomain string) *v1.VirtualMachineInstance {
vmi := libvmi.NewFedora(
libvmi.WithMasqueradeNetworking()...,
)
return readyVMI(
exposeExistingVMISpec(vmi, subdomain, hostname, selectorLabelKey, selectorLabelValue),
console.LoginToFedora)
}
BeforeEach(func() {
subdomain := "vmi"
hostname := "inbound"
inboundVMI = createReadyVMIWithMasqueradeBindingAndExposedService(hostname, subdomain)
tests.StartTCPServer(inboundVMI, servicePort, console.LoginToFedora)
})
AfterEach(func() {
Expect(inboundVMI).NotTo(BeNil(), "the VMI object must exist in order to be deleted.")
cleanupVMI(virtClient, inboundVMI)
})
Context("with a service matching the vmi exposed", func() {
var jobCleanup func() error
var service *k8sv1.Service
AfterEach(func() {
Expect(jobCleanup).NotTo(BeNil(), cleaningK8sv1JobFuncShouldExist)
Expect(jobCleanup()).To(Succeed(), cleaningK8sv1JobShouldSucceed)
jobCleanup = nil
})
AfterEach(func() {
Expect(cleanupService(inboundVMI.GetNamespace(), service.Name)).To(Succeed(), cleaningK8sv1ServiceShouldSucceed)
})
DescribeTable("[Conformance] should be able to reach the vmi based on labels specified on the vmi", func(ipFamily k8sv1.IPFamily) {
serviceName := "myservice"
By("setting up resources to expose the VMI via a service", func() {
libnet.SkipWhenClusterNotSupportIPFamily(ipFamily)
if ipFamily == k8sv1.IPv6Protocol {
serviceName = serviceName + "v6"
service = netservice.BuildIPv6Spec(serviceName, servicePort, servicePort, selectorLabelKey, selectorLabelValue)
} else {
service = netservice.BuildSpec(serviceName, servicePort, servicePort, selectorLabelKey, selectorLabelValue)
}
_, err := virtClient.CoreV1().Services(inboundVMI.Namespace).Create(context.Background(), service, k8smetav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred(), "the k8sv1.Service entity should have been created.")
})
By("checking connectivity the exposed service")
var err error
jobCleanup, err = assertConnectivityToService(serviceName, inboundVMI.Namespace, servicePort)
Expect(err).NotTo(HaveOccurred(), expectConnectivityToExposedService)
},
Entry("when the service is exposed by an IPv4 address.", k8sv1.IPv4Protocol),
Entry("when the service is exposed by an IPv6 address.", k8sv1.IPv6Protocol),
)
})
Context("*without* a service matching the vmi exposed", func() {
var jobCleanup func() error
var serviceName string
AfterEach(func() {
Expect(jobCleanup).NotTo(BeNil(), cleaningK8sv1JobFuncShouldExist)
Expect(jobCleanup()).To(Succeed(), cleaningK8sv1JobShouldSucceed)
jobCleanup = nil
})
It("should fail to reach the vmi", func() {
var err error
serviceName = "missingservice"
jobCleanup, err = assertNoConnectivityToService(serviceName, inboundVMI.Namespace, servicePort)
Expect(err).NotTo(HaveOccurred(), "connectivity is *not* expected, since there isn't an exposed service")
})
})
})
})