-
Notifications
You must be signed in to change notification settings - Fork 443
/
util.go
326 lines (289 loc) · 12.1 KB
/
util.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
package kube2e
import (
"context"
"os"
"path/filepath"
"runtime"
"time"
"github.com/solo-io/gloo/test/testutils"
"github.com/onsi/ginkgo/v2"
"github.com/solo-io/go-utils/stats"
"github.com/solo-io/gloo/test/gomega/assertions"
"github.com/solo-io/gloo/test/kube2e/upgrade"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/check"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/options"
clienthelpers "github.com/solo-io/gloo/projects/gloo/cli/pkg/helpers"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/k8s-utils/kubeutils"
"github.com/solo-io/k8s-utils/testutils/helper"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
. "github.com/onsi/gomega"
errors "github.com/rotisserie/eris"
"go.uber.org/zap/zapcore"
admissionregv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
// UniqueTestResourceLabel can be assigned to the resources used by kube2e tests
// This unique label per test run ensures that the generated snapshot is different on subsequent runs
// We have previously seen flakes where a resource is deleted and re-created with the same hash and thus
// the emitter can miss the update
UniqueTestResourceLabel = "gloo-kube2e-test-id"
)
func GetHttpEchoImage() string {
httpEchoImage := "hashicorp/http-echo"
if runtime.GOARCH == "arm64" {
httpEchoImage = "gcr.io/solo-test-236622/http-echo"
}
return httpEchoImage
}
func MustKubeClient() kubernetes.Interface {
restConfig, err := kubeutils.GetConfig("", "")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
kubeClient, err := kubernetes.NewForConfig(restConfig)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return kubeClient
}
// GlooctlCheckEventuallyHealthy will run up until proved timeoutInterval or until gloo is reported as healthy
func GlooctlCheckEventuallyHealthy(offset int, testHelper *helper.SoloTestHelper, timeoutInterval string) {
EventuallyWithOffset(offset, func() error {
contextWithCancel, cancel := context.WithCancel(context.Background())
defer cancel()
opts := &options.Options{
Metadata: core.Metadata{
Namespace: testHelper.InstallNamespace,
},
Top: options.Top{
Ctx: contextWithCancel,
},
}
err := check.CheckResources(opts)
if err != nil {
return errors.Wrap(err, "glooctl check detected a problem with the installation")
}
return nil
}, timeoutInterval, "5s").Should(BeNil())
}
func EventuallyReachesConsistentState(installNamespace string) {
// We port-forward the Gloo deployment stats port to inspect the metrics and log settings
glooStatsForwardConfig := assertions.StatsPortFwd{
ResourceName: "deployment/gloo",
ResourceNamespace: installNamespace,
LocalPort: stats.DefaultPort,
TargetPort: stats.DefaultPort,
}
// Gloo components are configured to log to the Info level by default
logLevelAssertion := assertions.LogLevelAssertion(zapcore.InfoLevel)
// The emitter at some point should stabilize and not continue to increase the number of snapshots produced
// We choose 4 here as a bit of a magic number, but we feel comfortable that if 4 consecutive polls of the metrics
// endpoint returns that same value, then we have stabilized
identicalResultInARow := 4
emitterMetricAssertion, _ := assertions.IntStatisticReachesConsistentValueAssertion("api_gloosnapshot_gloo_solo_io_emitter_snap_out", identicalResultInARow)
ginkgo.By("Gloo eventually reaches a consistent state")
offset := 1 // This method is called directly from a TestSuite
assertions.EventuallyWithOffsetStatisticsMatchAssertions(offset, glooStatsForwardConfig,
logLevelAssertion.WithOffset(offset),
emitterMetricAssertion.WithOffset(offset),
)
}
func UpdateDisableTransformationValidationSetting(ctx context.Context, shouldDisable bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGateway().GetValidation()).NotTo(BeNil())
settings.GetGateway().GetValidation().DisableTransformationValidation = &wrappers.BoolValue{Value: shouldDisable}
}, installNamespace)
}
// enable/disable strict validation
func UpdateAlwaysAcceptSetting(ctx context.Context, alwaysAccept bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGateway().GetValidation()).NotTo(BeNil())
settings.GetGateway().GetValidation().AlwaysAccept = &wrappers.BoolValue{Value: alwaysAccept}
}, installNamespace)
}
func UpdateAllowWarningsSetting(ctx context.Context, allowWarnings bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGateway().GetValidation()).NotTo(BeNil())
settings.GetGateway().GetValidation().AllowWarnings = &wrappers.BoolValue{Value: allowWarnings}
}, installNamespace)
}
func UpdateRestEdsSetting(ctx context.Context, enableRestEds bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGloo()).NotTo(BeNil())
settings.GetGloo().EnableRestEds = &wrappers.BoolValue{Value: enableRestEds}
}, installNamespace)
}
func UpdateReplaceInvalidRoutes(ctx context.Context, replaceInvalidRoutes bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGloo().GetInvalidConfigPolicy()).NotTo(BeNil())
settings.GetGloo().GetInvalidConfigPolicy().ReplaceInvalidRoutes = replaceInvalidRoutes
}, installNamespace)
}
func UpdateSettings(ctx context.Context, updateSettings func(settings *v1.Settings), installNamespace string) {
// when validation config changes, the validation server restarts -- give time for it to come up again.
// without the wait, the validation webhook may temporarily fallback to it's failurePolicy, which is not
// what we want to test.
// TODO (samheilbron) We should avoid relying on time.Sleep in our tests as these tend to cause flakes
waitForSettingsToPropagate := func() {
time.Sleep(3 * time.Second)
}
UpdateSettingsWithPropagationDelay(updateSettings, waitForSettingsToPropagate, ctx, installNamespace)
}
func UpdateSettingsWithPropagationDelay(updateSettings func(settings *v1.Settings), waitForSettingsToPropagate func(), ctx context.Context, installNamespace string) {
settingsClient := clienthelpers.MustSettingsClient(ctx)
settings, err := settingsClient.Read(installNamespace, "default", clients.ReadOpts{})
Expect(err).NotTo(HaveOccurred())
updateSettings(settings)
_, err = settingsClient.Write(settings, clients.WriteOpts{OverwriteExisting: true})
Expect(err).NotTo(HaveOccurred())
waitForSettingsToPropagate()
}
func ToFile(content string) string {
f, err := os.CreateTemp("", "")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
n, err := f.WriteString(content)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, n).To(Equal(len(content)))
_ = f.Close()
return f.Name()
}
// https://github.com/solo-io/gloo/issues/4043#issuecomment-772706604
// We should move tests away from using the testrunner, and instead depend on EphemeralContainers.
// The default response changed in later kube versions, which caused this value to change.
// Ideally the test utilities used by Gloo are maintained in the Gloo repo, so I opted to move
// this constant here.
// This response is given by the testrunner when the SimpleServer is started
func GetSimpleTestRunnerHttpResponse() string {
if runtime.GOARCH == "arm64" {
return SimpleTestRunnerHttpResponseArm
} else {
return SimpleTestRunnerHttpResponse
}
}
// For nightly runs, we want to install a released version rather than using a locally built chart
// To do this, set the environment variable RELEASED_VERSION with either a version name or "LATEST" to get the last release
func GetTestReleasedVersion(ctx context.Context, repoName string) string {
releasedVersion := os.Getenv(testutils.ReleasedVersion)
if releasedVersion == "" {
// In the case where the released version is empty, we return an empty string
// The function which consumes this value will then use the locally built chart
return releasedVersion
}
if releasedVersion == "LATEST" {
_, current, err := upgrade.GetUpgradeVersions(ctx, repoName)
Expect(err).NotTo(HaveOccurred())
return current.String()
}
// Assume that releasedVersion is a valid version, for a previously released version of Gloo Edge
return releasedVersion
}
func GetTestHelper(ctx context.Context, namespace string) (*helper.SoloTestHelper, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
if useVersion := GetTestReleasedVersion(ctx, "gloo"); useVersion != "" {
return helper.NewSoloTestHelper(func(defaults helper.TestConfig) helper.TestConfig {
defaults.RootDir = filepath.Join(cwd, "../../..")
defaults.HelmChartName = "gloo"
defaults.InstallNamespace = namespace
defaults.ReleasedVersion = useVersion
defaults.Verbose = true
return defaults
})
} else {
return helper.NewSoloTestHelper(func(defaults helper.TestConfig) helper.TestConfig {
defaults.RootDir = filepath.Join(cwd, "../../..")
defaults.HelmChartName = "gloo"
defaults.InstallNamespace = namespace
defaults.Verbose = true
return defaults
})
}
}
const SimpleTestRunnerHttpResponse = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="bin/">bin/</a>
<li><a href="boot/">boot/</a>
<li><a href="dev/">dev/</a>
<li><a href="etc/">etc/</a>
<li><a href="home/">home/</a>
<li><a href="lib/">lib/</a>
<li><a href="lib64/">lib64/</a>
<li><a href="media/">media/</a>
<li><a href="mnt/">mnt/</a>
<li><a href="opt/">opt/</a>
<li><a href="proc/">proc/</a>
<li><a href="product_name">product_name</a>
<li><a href="product_uuid">product_uuid</a>
<li><a href="root/">root/</a>
<li><a href="root.crt">root.crt</a>
<li><a href="run/">run/</a>
<li><a href="sbin/">sbin/</a>
<li><a href="srv/">srv/</a>
<li><a href="sys/">sys/</a>
<li><a href="tmp/">tmp/</a>
<li><a href="usr/">usr/</a>
<li><a href="var/">var/</a>
</ul>
<hr>
</body>
</html>`
const SimpleTestRunnerHttpResponseArm = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="bin/">bin/</a>
<li><a href="boot/">boot/</a>
<li><a href="dev/">dev/</a>
<li><a href="etc/">etc/</a>
<li><a href="home/">home/</a>
<li><a href="lib/">lib/</a>
<li><a href="lib64/">lib64/</a>
<li><a href="media/">media/</a>
<li><a href="mnt/">mnt/</a>
<li><a href="opt/">opt/</a>
<li><a href="proc/">proc/</a>
<li><a href="product_uuid">product_uuid</a>
<li><a href="root/">root/</a>
<li><a href="root.crt">root.crt</a>
<li><a href="run/">run/</a>
<li><a href="sbin/">sbin/</a>
<li><a href="srv/">srv/</a>
<li><a href="sys/">sys/</a>
<li><a href="tmp/">tmp/</a>
<li><a href="usr/">usr/</a>
<li><a href="var/">var/</a>
</ul>
<hr>
</body>
</html>`
func GetFailurePolicy(ctx context.Context, webhookName string) *admissionregv1.FailurePolicyType {
cfg := GetValidatingWebhookWithOffset(ctx, 2, webhookName)
ExpectWithOffset(1, cfg.Webhooks).To(HaveLen(1))
return cfg.Webhooks[0].FailurePolicy
}
func UpdateFailurePolicy(ctx context.Context, webhookName string, failurePolicy admissionregv1.FailurePolicyType) {
kubeClient := clienthelpers.MustKubeClient()
cfg := GetValidatingWebhookWithOffset(ctx, 2, webhookName)
ExpectWithOffset(1, cfg.Webhooks).To(HaveLen(1))
cfg.Webhooks[0].FailurePolicy = &failurePolicy
_, err := kubeClient.AdmissionregistrationV1().ValidatingWebhookConfigurations().Update(ctx, cfg, metav1.UpdateOptions{})
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
func GetValidatingWebhook(ctx context.Context, webhookName string) *admissionregv1.ValidatingWebhookConfiguration {
return GetValidatingWebhookWithOffset(ctx, 1, webhookName)
}
func GetValidatingWebhookWithOffset(ctx context.Context, offset int, webhookName string) *admissionregv1.ValidatingWebhookConfiguration {
kubeClient := clienthelpers.MustKubeClient()
cfg, err := kubeClient.AdmissionregistrationV1().ValidatingWebhookConfigurations().Get(ctx, webhookName, metav1.GetOptions{})
ExpectWithOffset(offset, err).NotTo(HaveOccurred())
return cfg
}