-
Notifications
You must be signed in to change notification settings - Fork 333
/
unidling.go
409 lines (346 loc) · 16.6 KB
/
unidling.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package e2e
import (
"context"
"fmt"
"math/rand"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
e2enode "k8s.io/kubernetes/test/e2e/framework/node"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
e2epodoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
)
type serviceStatus int
const (
works serviceStatus = iota
rejects
failsWithNoReject
)
// Validate that Services with the well-known annotation k8s.ovn.org/idled-at
// generate a NeedPods Event if the service doesn´t have endpoints and
// OVN EmptyLB-Backends feature is enabled
var _ = ginkgo.Describe("Unidling", func() {
const (
serviceName = "empty-service"
podName = "execpod-noendpoints"
ovnServiceIdledAt = "k8s.ovn.org/idled-at"
port = 80
)
f := wrappedTestFramework("unidling")
var cs clientset.Interface
ginkgo.BeforeEach(func() {
cs = f.ClientSet
})
// We simulate the idling feature that is Openshift specific creating a service and removing the pods
ginkgo.It("Should generate a NeedPods event for traffic destined to idled services", func() {
namespace := f.Namespace.Name
jig := e2eservice.NewTestJig(cs, namespace, serviceName)
nodes, err := e2enode.GetBoundedReadySchedulableNodes(context.TODO(), cs, e2eservice.MaxNodesForEndpointsTests)
framework.ExpectNoError(err)
nodeName := nodes.Items[0].Name
ginkgo.By("creating an annotated service with no endpoints and idle annotation")
_, err = jig.CreateTCPServiceWithPort(context.TODO(), func(svc *v1.Service) {
svc.Annotations = map[string]string{ovnServiceIdledAt: "true"}
}, int32(port))
framework.ExpectNoError(err)
// Add a backend pod to the service in one node
ginkgo.By("creating a backend pod for the service " + serviceName)
serverPod := e2epod.NewAgnhostPod(namespace, "pod-backend", nil, nil, []v1.ContainerPort{{ContainerPort: 9376}}, "serve-hostname")
serverPod.Labels = jig.Labels
serverPod.Spec.NodeName = nodeName
e2epod.NewPodClient(f).CreateSync(context.TODO(), serverPod)
// Emulate the idling feature deleting the pod
e2epod.NewPodClient(f).DeleteSync(context.TODO(), serverPod.Name, metav1.DeleteOptions{}, e2epod.DefaultPodDeletionTimeout)
// Create exec pod to test the PodEvent is generated if it receives traffic to the idled service
ginkgo.By(fmt.Sprintf("creating %v on node %v", podName, nodeName))
execPod := e2epod.CreateExecPodOrFail(context.TODO(), f.ClientSet, namespace, podName, func(pod *v1.Pod) {
pod.Spec.NodeName = nodeName
})
serviceAddress := net.JoinHostPort(serviceName, strconv.Itoa(port))
framework.Logf("waiting up to %v to connect to %v", e2eservice.KubeProxyEndpointLagTimeout, serviceAddress)
cmd := fmt.Sprintf("/agnhost connect --timeout=3s %s", serviceAddress)
ginkgo.By(fmt.Sprintf("hitting service %v from pod %v on node %v", serviceAddress, podName, nodeName))
nonExpectedErr := "REFUSED"
if pollErr := wait.PollImmediate(framework.Poll, e2eservice.KubeProxyEndpointLagTimeout, func() (bool, error) {
_, err := e2epodoutput.RunHostCmd(execPod.Namespace, execPod.Name, cmd)
if err != nil && strings.Contains(err.Error(), nonExpectedErr) {
return false, fmt.Errorf("service is rejecting packets")
}
// An event like this must be generated
// oc.recorder.Eventf(&serviceRef, kapi.EventTypeNormal, "NeedPods", "The service %s needs pods", serviceName.Name)
events, err := cs.CoreV1().Events(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, e := range events.Items {
framework.Logf("At %v - event for %v: %v %v: %v", e.FirstTimestamp, e.InvolvedObject.Name, e.Source, e.Reason, e.Message)
if e.Reason == "NeedPods" && strings.Contains(e.Message, serviceName) {
return true, nil
}
}
return false, nil
}); pollErr != nil {
framework.ExpectNoError(pollErr)
}
})
// Check the creation of needPods events starting with annotated services.
// All the tests start with a given configuration, waits for the behaviour of the service to settle
// (if must reject or must timeout), then try to hit the service again and check if new events are
// generated or not according to the test scenario. This is done to avoid races and make sure that
// the right configuration is applied.
ginkgo.Context("With annotated service", func() {
var (
clientPod *v1.Pod
node string
cmd string
namespace string
serviceName string
service *v1.Service
jig *e2eservice.TestJig
)
ginkgo.BeforeEach(func() {
namespace = f.Namespace.Name
serviceName = "testservice" + randString(5)
jig = e2eservice.NewTestJig(cs, namespace, serviceName)
nodes, err := e2enode.GetBoundedReadySchedulableNodes(context.TODO(), cs, e2eservice.MaxNodesForEndpointsTests)
framework.ExpectNoError(err)
node = nodes.Items[0].Name
ginkgo.By("creating an annotated service with no endpoints and idle annotation")
service, err = jig.CreateTCPServiceWithPort(context.TODO(), func(svc *v1.Service) {
svc.Annotations = map[string]string{ovnServiceIdledAt: "true"}
}, int32(port))
framework.ExpectNoError(err)
ginkgo.By(fmt.Sprintf("creating %v on node %v", podName, node))
clientPod = e2epod.CreateExecPodOrFail(context.TODO(), f.ClientSet, namespace, podName, func(pod *v1.Pod) {
pod.Spec.NodeName = node
})
serviceAddress := net.JoinHostPort(serviceName, strconv.Itoa(port))
framework.Logf("waiting up to %v to connect to %v", e2eservice.KubeProxyEndpointLagTimeout, serviceAddress)
cmd = fmt.Sprintf("/agnhost connect --timeout=10s %s", serviceAddress)
})
ginkgo.It("Should generate a NeedPods event for traffic destined to idled services", func() {
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(failsWithNoReject), "Service is rejecting")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(true), "New events are not generated")
})
ginkgo.It("Should not generate a NeedPods event when removing the annotation", func() {
ginkgo.Skip("Not supported by OVN: Enable back when https://bugzilla.redhat.com/show_bug.cgi?id=2177173 is fixed")
_, err := jig.UpdateService(context.TODO(), func(service *v1.Service) {
service.Annotations = nil
})
framework.ExpectNoError(err)
// Service starts a grace period in which it doesn't reject connections.
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(failsWithNoReject), "Service is rejecting")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(false), "New events are generated")
})
ginkgo.It("Should not generate a NeedPods event when has backend", func() {
createBackend(f, serviceName, namespace, node, jig.Labels, port)
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(works), "Service is failing")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(false), "New events are generated")
})
ginkgo.It("Should generate a NeedPods event when backends were added and then removed", func() {
be := createBackend(f, serviceName, namespace, node, jig.Labels, port)
e2epod.NewPodClient(f).DeleteSync(context.TODO(), be.Name, metav1.DeleteOptions{}, e2epod.DefaultPodDeletionTimeout)
err := framework.WaitForServiceEndpointsNum(context.TODO(), f.ClientSet, f.Namespace.Name, serviceName, 0, time.Second, wait.ForeverTestTimeout)
framework.ExpectNoError(err)
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(failsWithNoReject), "Service is not timing out")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(true), "New events are not generated")
})
ginkgo.It("Should connect to an unidled backend at the first attempt", func() {
// Simulate service unidling
_, err := jig.UpdateService(context.TODO(), func(service *v1.Service) {
service.Annotations = nil
})
framework.ExpectNoError(err)
var wg sync.WaitGroup
done := make(chan struct{})
wg.Add(1)
go func() {
defer ginkgo.GinkgoRecover()
wg.Done()
time.Sleep(time.Second)
createBackend(f, serviceName, namespace, node, jig.Labels, port)
close(done)
}()
wg.Wait()
defer func() { <-done }()
// Connecting to the service should work at the first attempt
gomega.Expect(checkService(clientPod, cmd)).To(gomega.Equal(works))
})
})
// Check the creation of needPods events starting with non annotated services.
// All the tests start with a given configuration, waits for the behaviour of the service to settle
// (if must reject or must timeout), then try to hit the service again and check if new events are
// generated or not according to the test scenario. This is done to avoid races and make sure that
// the right configuration is applied.
ginkgo.Context("With non annotated service", func() {
var (
clientPod *v1.Pod
node string
cmd string
namespace string
serviceName string
service *v1.Service
jig *e2eservice.TestJig
)
ginkgo.BeforeEach(func() {
namespace = f.Namespace.Name
serviceName = "testservice" + randString(5)
jig = e2eservice.NewTestJig(cs, namespace, serviceName)
nodes, err := e2enode.GetBoundedReadySchedulableNodes(context.TODO(), cs, e2eservice.MaxNodesForEndpointsTests)
framework.ExpectNoError(err)
node = nodes.Items[0].Name
ginkgo.By("creating an annotated service with no endpoints and idle annotation")
service, err = jig.CreateTCPServiceWithPort(context.TODO(), func(svc *v1.Service) {
}, int32(port))
framework.ExpectNoError(err)
ginkgo.By(fmt.Sprintf("creating %v on node %v", podName, node))
clientPod = e2epod.CreateExecPodOrFail(context.TODO(), f.ClientSet, namespace, podName, func(pod *v1.Pod) {
pod.Spec.NodeName = node
})
serviceAddress := net.JoinHostPort(serviceName, strconv.Itoa(port))
framework.Logf("waiting up to %v to connect to %v", e2eservice.KubeProxyEndpointLagTimeout, serviceAddress)
cmd = fmt.Sprintf("/agnhost connect --timeout=3s %s", serviceAddress)
})
ginkgo.It("Should not generate a NeedPods event for traffic destined to idled services", func() {
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(rejects), "Service is not rejecting")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(false), "New events are generated")
})
ginkgo.It("Should generate a NeedPods event when adding the annotation", func() {
_, err := jig.UpdateService(context.TODO(), func(service *v1.Service) {
service.Annotations = map[string]string{ovnServiceIdledAt: "true"}
})
framework.ExpectNoError(err)
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(failsWithNoReject), "Service is not timing out")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(true), "New events are not generated")
})
ginkgo.It("Should not generate a NeedPods event when has backend", func() {
createBackend(f, serviceName, namespace, node, jig.Labels, port)
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(works), "Service is failing")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(false), "New events are generated")
})
ginkgo.It("Should not generate a NeedPods event when backends were added and then removed", func() {
be := createBackend(f, serviceName, namespace, node, jig.Labels, port)
e2epod.NewPodClient(f).DeleteSync(context.TODO(), be.Name, metav1.DeleteOptions{}, e2epod.DefaultPodDeletionTimeout)
err := framework.WaitForServiceEndpointsNum(context.TODO(), f.ClientSet, f.Namespace.Name, serviceName, 0, time.Second, wait.ForeverTestTimeout)
framework.ExpectNoError(err)
gomega.Eventually(func() serviceStatus {
return checkService(clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(rejects), "Service is not rejecting")
gomega.Eventually(func() bool {
return hittingGeneratesNewEvents(service, cs, clientPod, cmd)
}, 10*time.Second, 1*time.Second).Should(gomega.Equal(false), "New events are generated")
})
})
})
func createBackend(f *framework.Framework, serviceName, namespace, node string, labels map[string]string, port int32) *v1.Pod {
ginkgo.By("creating a backend pod for the service " + serviceName)
serverPod := e2epod.NewAgnhostPod(namespace, "pod-backend", nil, nil, []v1.ContainerPort{{ContainerPort: port}}, "netexec", "--http-port=80")
serverPod.Labels = labels
serverPod.Spec.NodeName = node
pod := e2epod.NewPodClient(f).CreateSync(context.TODO(), serverPod)
err := framework.WaitForServiceEndpointsNum(context.TODO(), f.ClientSet, f.Namespace.Name, serviceName, 1, time.Second, wait.ForeverTestTimeout)
framework.ExpectNoError(err)
return pod
}
// hittingGeneratesNewEvents tells if by hitting a service a brand new needPods event is generated
func hittingGeneratesNewEvents(service *v1.Service, cs clientset.Interface, clientPod *v1.Pod, cmd string) bool {
framework.Logf("checking if needPods events are emitted for service %s in namespace %s", service.Name, service.Namespace)
lastEventTime := lastIdlingEventForService(service, cs)
// the event time resolution is one second, which is what we use to check if there are new events
// we need to sleep 1 second to ensure two events are generated with different timestamps.
time.Sleep(1 * time.Second)
checkService(clientPod, cmd)
err := wait.PollImmediate(framework.Poll, 3*time.Second, func() (bool, error) {
newLastEvent := lastIdlingEventForService(service, cs)
if newLastEvent.After(lastEventTime.Time) {
return true, nil
}
return false, nil
})
if err != nil { // timed out
framework.Logf("hittingGeneratesNewEvents fails with error: %v\n", err)
return false
}
return true
}
// checkService tries to hit a service and tells the behaviour of the connection.
// The connection can be refused, can timeout or can work.
func checkService(clientPod *v1.Pod, cmd string) serviceStatus {
refusedError := "REFUSED"
stdout, stderr, err := e2epodoutput.RunHostCmdWithFullOutput(clientPod.Namespace, clientPod.Name, cmd)
framework.Logf("checking service with cmd \"%s\" from pod %s in ns %s returned stdout: %v stderr: %v", cmd,
clientPod.Name, clientPod.Namespace, stdout, stderr)
if err != nil && strings.Contains(err.Error(), refusedError) {
return rejects
}
if err != nil {
return failsWithNoReject
}
return works
}
// lastIdlingEventForService returns the most recent idling event for the given service
func lastIdlingEventForService(service *v1.Service, cs clientset.Interface) metav1.Time {
// An event like this must be generated
// oc.recorder.Eventf(&serviceRef, kapi.EventTypeNormal, "NeedPods", "The service %s needs pods", serviceName.Name)
fieldSelector := fmt.Sprintf("reason=NeedPods")
events, err := cs.CoreV1().Events(service.Namespace).List(context.Background(), metav1.ListOptions{FieldSelector: fieldSelector})
framework.ExpectNoError(err)
mostRecent := metav1.Time{}
for _, e := range events.Items {
if strings.Contains(e.Message, service.Name) && e.LastTimestamp.After(mostRecent.Time) {
mostRecent = e.LastTimestamp
}
}
return mostRecent
}
const charset = "abcdefghijklmnopqrstuvwxyz"
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func randString(length int) string {
return StringWithCharset(length, charset)
}