-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
e2e.go
211 lines (190 loc) · 7.42 KB
/
e2e.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
/*
Copyright 2019 The Knative Authors
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.
*/
package e2e
import (
"context"
"fmt"
"strconv"
"testing"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
// Mysteriously required to support GCP auth (required by k8s libs).
// Apparently just importing it is enough. @_@ side effects @_@.
// https://github.com/kubernetes/client-go/issues/242
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
appsv1 "k8s.io/api/apps/v1"
"knative.dev/pkg/kmeta"
"knative.dev/pkg/system"
pkgTest "knative.dev/pkg/test"
"knative.dev/serving/pkg/apis/autoscaling"
"knative.dev/serving/pkg/apis/serving"
v1 "knative.dev/serving/pkg/apis/serving/v1"
asconfig "knative.dev/serving/pkg/autoscaler/config"
"knative.dev/serving/pkg/autoscaler/config/autoscalerconfig"
"knative.dev/serving/pkg/networking"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
)
// Setup creates the client objects needed in the e2e tests.
func Setup(t *testing.T) *test.Clients {
return test.Setup(t)
}
// SetupAlternativeNamespace creates the client objects needed in e2e tests
// under the alternative namespace.
func SetupAlternativeNamespace(t *testing.T) *test.Clients {
return test.Setup(t, test.Options{Namespace: test.ServingFlags.AltTestNamespace})
}
// autoscalerCM returns the current autoscaler config map deployed to the
// test cluster.
func autoscalerCM(clients *test.Clients) (*autoscalerconfig.Config, error) {
autoscalerCM, err := clients.KubeClient.CoreV1().ConfigMaps(system.Namespace()).Get(
context.Background(), asconfig.ConfigName, metav1.GetOptions{})
if err != nil {
return nil, err
}
return asconfig.NewConfigFromMap(autoscalerCM.Data)
}
// WaitForScaleToZero will wait for the specified deployment to scale to 0 replicas.
// Will wait up to 6 times the configured ScaleToZeroGracePeriod before failing.
func WaitForScaleToZero(t *testing.T, deploymentName string, clients *test.Clients) error {
t.Helper()
t.Logf("Waiting for %q to scale to zero", deploymentName)
cfg, err := autoscalerCM(clients)
if err != nil {
return fmt.Errorf("failed to get autoscaler configmap: %w", err)
}
return pkgTest.WaitForDeploymentState(
context.Background(),
clients.KubeClient,
deploymentName,
func(d *appsv1.Deployment) (bool, error) {
return (d.Spec.Replicas == nil || *d.Spec.Replicas == 0) &&
d.Status.Replicas == 0 &&
d.Status.UpdatedReplicas == 0 &&
d.Status.ReadyReplicas == 0 &&
d.Status.AvailableReplicas == 0 &&
d.Status.UnavailableReplicas == 0,
nil
},
"DeploymentIsScaledDown",
test.ServingFlags.TestNamespace,
cfg.ScaleToZeroGracePeriod*6,
)
}
// waitForActivatorEndpoints waits for the Service endpoints to match that of activator.
func waitForActivatorEndpoints(ctx *TestContext) error {
var (
aset, svcSet sets.Set[string]
wantAct int
)
if rerr := wait.PollUntilContextTimeout(context.Background(), 250*time.Millisecond, time.Minute, true, func(context.Context) (bool, error) {
// We need to fetch the activator endpoints at every check, since it can change.
actEps, err := ctx.clients.KubeClient.CoreV1().Endpoints(
system.Namespace()).Get(context.Background(), networking.ActivatorServiceName, metav1.GetOptions{})
if err != nil {
return false, nil
}
sks, err := ctx.clients.NetworkingClient.ServerlessServices.Get(context.Background(), ctx.resources.Revision.Name, metav1.GetOptions{})
if err != nil {
return false, nil
}
svcEps, err := ctx.clients.KubeClient.CoreV1().Endpoints(test.ServingFlags.TestNamespace).Get(
context.Background(), ctx.resources.Revision.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
wantAct = int(sks.Spec.NumActivators)
aset = make(sets.Set[string], wantAct)
for _, ss := range actEps.Subsets {
for i := 0; i < len(ss.Addresses); i++ {
aset.Insert(ss.Addresses[i].IP)
}
}
svcSet = make(sets.Set[string], wantAct)
for _, ss := range svcEps.Subsets {
for i := 0; i < len(ss.Addresses); i++ {
svcSet.Insert(ss.Addresses[i].IP)
}
}
// Subset wants this many activators, but there might not be as many,
// so reduce the expectation.
if aset.Len() < wantAct {
wantAct = aset.Len()
}
// If public endpoints have not yet been updated with the new values.
if svcSet.Len() < wantAct {
return false, nil
}
return svcSet.Intersection(aset).Len() > 0, nil
}); rerr != nil {
ctx.t.Logf("Did not see activator endpoints in public service for %s."+
"Last received values: Activator: %v "+
"PubSvc: %v, WantActivators %d",
ctx.resources.Revision.Name, sets.List(aset), sets.List(svcSet), wantAct)
return rerr
}
return nil
}
// CreateAndVerifyInitialScaleConfiguration creates a Configuration with the
// `initialScale` annotation set and validates the `wantPods` number of pods
// are created.
func CreateAndVerifyInitialScaleConfiguration(t *testing.T, clients *test.Clients, names test.ResourceNames, wantPods int) {
t.Log("Creating a new Configuration.", "configuration", names.Config)
_, err := v1test.CreateConfiguration(t, clients, names, func(configuration *v1.Configuration) {
configuration.Spec.Template.Annotations = kmeta.UnionMaps(
configuration.Spec.Template.Annotations, map[string]string{
autoscaling.InitialScaleAnnotationKey: strconv.Itoa(wantPods),
})
})
if err != nil {
t.Fatal("Failed creating initial configuration:", err)
}
t.Logf("Waiting for Configuration %q to transition to Ready with %d number of pods.", names.Config, wantPods)
selector := fmt.Sprintf("%s=%s", serving.ConfigurationLabelKey, names.Config)
if err := v1test.WaitForConfigurationState(clients.ServingClient, names.Config, func(s *v1.Configuration) (b bool, e error) {
pods := clients.KubeClient.CoreV1().Pods(test.ServingFlags.TestNamespace)
podList, err := pods.List(context.Background(), metav1.ListOptions{
LabelSelector: selector,
// Include both running and terminating pods, because we will scale down from
// initial scale immediately if there's no traffic coming in.
FieldSelector: "status.phase!=Pending",
})
if err != nil {
return false, err
}
gotPods := len(podList.Items)
switch {
case gotPods == wantPods:
return s.IsReady(), nil
case gotPods > wantPods:
return false, fmt.Errorf("expected %d pods created, got %d", wantPods, gotPods)
default:
return false, nil
}
}, "ConfigurationIsReadyWithWantPods"); err != nil {
t.Fatal("Configuration does not have the desired number of pods running:", err)
}
}
// Get revision name from configuration.
func RevisionFromConfiguration(clients *test.Clients, configName string) (string, error) {
config, err := clients.ServingClient.Configs.Get(context.Background(), configName, metav1.GetOptions{})
if err != nil {
return "", err
}
if config.Status.LatestCreatedRevisionName != "" {
return config.Status.LatestCreatedRevisionName, nil
}
return "", fmt.Errorf("no valid revision name found in configuration %s", configName)
}