/
idle.go
333 lines (296 loc) · 11.9 KB
/
idle.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
package router
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
e2e "k8s.io/kubernetes/test/e2e/framework"
admissionapi "k8s.io/pod-security-admission/api"
utilpointer "k8s.io/utils/pointer"
g "github.com/onsi/ginkgo/v2"
o "github.com/onsi/gomega"
configv1 "github.com/openshift/api/config/v1"
routev1 "github.com/openshift/api/route/v1"
unidlingapi "github.com/openshift/api/unidling/v1alpha1"
exutil "github.com/openshift/origin/test/extended/util"
exutilimage "github.com/openshift/origin/test/extended/util/image"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var _ = g.Describe("[sig-network-edge][Conformance][Area:Networking][Feature:Router]", func() {
defer g.GinkgoRecover()
var (
oc = exutil.NewCLIWithPodSecurityLevel("router-idling", admissionapi.LevelBaseline)
)
// this hook must be registered before the framework namespace teardown
// hook
g.AfterEach(func() {
if g.CurrentSpecReport().Failed() {
exutil.DumpPodLogsStartingWithInNamespace("router", "openshift-ingress", oc.AsAdmin())
}
})
g.Describe("The HAProxy router", func() {
g.It("should be able to connect to a service that is idled because a GET on the route will unidle it", func() {
network, err := oc.AdminConfigClient().ConfigV1().Networks().Get(context.Background(), "cluster", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred(), "failed to get cluster network configuration")
if !(network.Status.NetworkType == "OVNKubernetes" || network.Status.NetworkType == "OpenShiftSDN") {
g.Skip("idle feature only supported on OVNKubernetes or OpenShiftSDN")
return
}
infra, err := oc.AdminConfigClient().ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred(), "failed to get cluster-wide infrastructure")
switch infra.Status.PlatformStatus.Type {
case configv1.OvirtPlatformType, configv1.KubevirtPlatformType, configv1.LibvirtPlatformType, configv1.VSpherePlatformType:
// Skip on platforms where the default
// router is not exposed by a load
// balancer service.
g.Skip("https://bugzilla.redhat.com/show_bug.cgi?id=1933114")
}
timeout := 15 * time.Minute
g.By("creating test fixtures")
ns := oc.KubeFramework().Namespace.Name
idleRoute := &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: "idle-test",
Labels: map[string]string{
"app": "idle-test",
},
},
Spec: routev1.RouteSpec{
Port: &routev1.RoutePort{
TargetPort: intstr.FromInt(8080),
},
To: routev1.RouteTargetReference{
Kind: "Service",
Name: "idle-test",
},
},
}
_, err = oc.RouteClient().RouteV1().Routes(ns).Create(context.Background(), idleRoute, metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
idleService := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "idle-test",
Labels: map[string]string{
"app": "idle-test",
},
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": "idle-test",
},
Ports: []corev1.ServicePort{
{
Port: 8080,
Name: "8080-http",
TargetPort: intstr.FromInt(8080),
Protocol: corev1.ProtocolTCP,
},
},
},
}
_, err = oc.AdminKubeClient().CoreV1().Services(ns).Create(context.Background(), idleService, metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
idleDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "idle-test",
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "idle-test",
},
},
Replicas: utilpointer.Int32(1),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "idle-test",
Labels: map[string]string{
"app": "idle-test",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Image: exutilimage.ShellImage(),
Name: "idle-test",
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt(8080),
},
},
InitialDelaySeconds: 3,
PeriodSeconds: 3,
},
Command: []string{
"/usr/bin/socat",
"TCP4-LISTEN:8080,reuseaddr,fork",
`EXEC:'/bin/bash -c \"printf \\\"HTTP/1.0 200 OK\r\n\r\n\\\"; sed -e \\\"/^\r/q\\\"\"'`,
},
Ports: []corev1.ContainerPort{
{
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
},
},
},
},
},
},
}
_, err = oc.AdminKubeClient().AppsV1().Deployments(ns).Create(context.Background(), idleDeployment, metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Waiting for pods to be running")
err = waitForRunningPods(oc, 1, exutil.ParseLabelsOrDie("app=idle-test"), timeout)
o.Expect(err).NotTo(o.HaveOccurred(), "pods not running")
g.By("Getting a 200 status code when accessing the route")
hostname, err := getHostnameForRoute(oc, "idle-test")
o.Expect(err).NotTo(o.HaveOccurred())
err = waitHTTPGetStatus(hostname, http.StatusOK, timeout)
o.Expect(err).NotTo(o.HaveOccurred(), "expected status 200 from the GET request")
g.By("Idling the service")
_, err = oc.Run("idle").Args("idle-test").Output()
o.Expect(err).NotTo(o.HaveOccurred(), "failed to idle the service")
var annotations map[string]string
g.By("Fetching the endpoints and checking the idle annotations are present")
err = wait.PollImmediate(time.Second, timeout, func() (bool, error) {
endpoints, err := oc.KubeClient().CoreV1().Endpoints(oc.Namespace()).Get(context.Background(), "idle-test", metav1.GetOptions{})
if err != nil {
e2e.Logf("Error getting endpoints: %v", err)
return false, nil
}
annotations = endpoints.Annotations
_, idledAt := annotations[unidlingapi.IdledAtAnnotation]
_, unidleTarget := annotations[unidlingapi.UnidleTargetAnnotation]
return idledAt && unidleTarget, nil
})
o.Expect(err).NotTo(o.HaveOccurred(), "failed to fetch the endpoints")
mustVerifyIdleAnnotationValues(annotations)
g.By("Fetching the service and checking the idle annotations are present")
err = wait.PollImmediate(time.Second, timeout, func() (bool, error) {
service, err := oc.KubeClient().CoreV1().Services(oc.Namespace()).Get(context.Background(), "idle-test", metav1.GetOptions{})
if err != nil {
e2e.Logf("Error getting service: %v", err)
return false, nil
}
annotations = service.Annotations
_, idledAt := annotations[unidlingapi.IdledAtAnnotation]
_, unidleTarget := annotations[unidlingapi.UnidleTargetAnnotation]
return idledAt && unidleTarget, nil
})
o.Expect(err).NotTo(o.HaveOccurred(), "failed to fetch the service")
mustVerifyIdleAnnotationValues(annotations)
// wait for target deployment to actually scale down
err = waitForRunningPods(oc, 0, exutil.ParseLabelsOrDie("app=idle-test"), timeout)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Unidling the service by making a GET request on the route")
err = waitHTTPGetStatus(hostname, http.StatusOK, timeout)
o.Expect(err).NotTo(o.HaveOccurred(), "expected status 200 from the GET request")
g.By("Validating that the idle annotations have been removed from the endpoints")
err = wait.PollImmediate(time.Second, timeout, func() (bool, error) {
endpoints, err := oc.KubeClient().CoreV1().Endpoints(oc.Namespace()).Get(context.Background(), "idle-test", metav1.GetOptions{})
if err != nil {
e2e.Logf("Error getting endpoints: %v", err)
return false, nil
}
_, idledAt := endpoints.Annotations[unidlingapi.IdledAtAnnotation]
_, unidleTarget := endpoints.Annotations[unidlingapi.UnidleTargetAnnotation]
return !idledAt && !unidleTarget, nil
})
o.Expect(err).NotTo(o.HaveOccurred(), "idle annotations not removed from endpoints")
g.By("Validating that the idle annotations have been removed from the service")
err = wait.PollImmediate(time.Second, timeout, func() (bool, error) {
service, err := oc.KubeClient().CoreV1().Services(oc.Namespace()).Get(context.Background(), "idle-test", metav1.GetOptions{})
if err != nil {
e2e.Logf("Error getting service: %v", err)
return false, nil
}
_, idledAt := service.Annotations[unidlingapi.IdledAtAnnotation]
_, unidleTarget := service.Annotations[unidlingapi.UnidleTargetAnnotation]
return !idledAt && !unidleTarget, nil
})
o.Expect(err).NotTo(o.HaveOccurred(), "idle annotations not removed from service")
})
})
})
func mustVerifyIdleAnnotationValues(annotations map[string]string) {
o.Expect(annotations).To(o.HaveKey(unidlingapi.IdledAtAnnotation))
o.Expect(annotations).To(o.HaveKey(unidlingapi.UnidleTargetAnnotation))
idledAtAnnotation := annotations[unidlingapi.IdledAtAnnotation]
idledAtTime, err := time.Parse(time.RFC3339, idledAtAnnotation)
o.Expect(err).ToNot(o.HaveOccurred())
o.Expect(idledAtTime).To(o.BeTemporally("~", time.Now(), 5*time.Minute))
g.By("Checking the idle targets")
unidleTargetAnnotation := annotations[unidlingapi.UnidleTargetAnnotation]
var unidleTargets []unidlingapi.RecordedScaleReference
err = json.Unmarshal([]byte(unidleTargetAnnotation), &unidleTargets)
o.Expect(err).ToNot(o.HaveOccurred())
o.Expect(unidleTargets).To(o.Equal([]unidlingapi.RecordedScaleReference{
{
Replicas: 1,
CrossGroupObjectReference: unidlingapi.CrossGroupObjectReference{
Kind: "Deployment",
Group: "apps",
Name: "idle-test",
},
},
}))
}
// waitForRunningPods waits for podCount pods matching podSelector are
// in the running state. It retries the request every second and will
// return an error if the conditions are not met after the specified
// timeout.
func waitForRunningPods(oc *exutil.CLI, podCount int, podLabels labels.Selector, timeout time.Duration) error {
ns := oc.KubeFramework().Namespace.Name
if err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
podList, err := oc.AdminKubeClient().CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{LabelSelector: podLabels.String()})
if err != nil {
e2e.Logf("Error listing pods: %v", err)
return false, nil
}
return len(podList.Items) == podCount, nil
}); err != nil {
return fmt.Errorf("failed to list pods: %v", err)
}
e2e.Logf("Waiting for %d pods in namespace %s", podCount, ns)
c := oc.AdminKubeClient()
pods, err := exutil.WaitForPods(c.CoreV1().Pods(ns), podLabels, exutil.CheckPodIsRunning, podCount, timeout)
if err != nil {
return fmt.Errorf("error in pod wait: %v", err)
} else if len(pods) < podCount {
return fmt.Errorf("only got %v out of %v pods in %s (timeout)", len(pods), podCount, timeout)
}
e2e.Logf("All expected pods in namespace %s are running", ns)
return nil
}
// waitHTTPGetStatus repeatedly makes a HTTP GET request to hostname
// until the GET response equals statusCode. It retries every second
// and will return an error if the conditions are not met after the
// specified timeout.
func waitHTTPGetStatus(hostname string, statusCode int, timeout time.Duration) error {
client := makeHTTPClient(false, 30*time.Second)
var attempt int
url := "http://" + hostname
return wait.Poll(time.Second, timeout, func() (bool, error) {
attempt += 1
resp, err := client.Get(url)
if err != nil {
e2e.Logf("GET#%v %q error=%v", attempt, url, err)
return false, nil // could be 503 if service not ready
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
e2e.Logf("GET#%v %q status=%v", attempt, url, resp.StatusCode)
return resp.StatusCode == statusCode, nil
})
}