-
Notifications
You must be signed in to change notification settings - Fork 444
/
util.go
200 lines (174 loc) · 6.9 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
package kube2e
import (
"context"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"time"
"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"
"k8s.io/client-go/kubernetes"
)
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
}
// Check that everything is OK by running `glooctl check`
func GlooctlCheckEventuallyHealthy(offset int, testHelper *helper.SoloTestHelper, timeoutInterval string) {
EventuallyWithOffset(offset, func() error {
opts := &options.Options{
Metadata: core.Metadata{
Namespace: testHelper.InstallNamespace,
},
Top: options.Top{
Ctx: context.Background(),
},
}
err := check.CheckResources(opts)
if err != nil {
return errors.New("glooctl check detected a problem with the installation")
}
return nil
}, timeoutInterval, "5s").Should(BeNil())
}
func GetHelmValuesOverrideFile() (filename string, cleanup func()) {
values, err := ioutil.TempFile("", "values-*.yaml")
Expect(err).NotTo(HaveOccurred())
// disabling usage statistics is not important to the functionality of the tests,
// but we don't want to report usage in CI since we only care about how our users are actually using Gloo.
// install to a single namespace so we can run multiple invocations of the regression tests against the
// same cluster in CI.
_, err = values.Write([]byte(`
global:
image:
pullPolicy: IfNotPresent
glooRbac:
namespaced: true
nameSuffix: e2e-test-rbac-suffix
settings:
singleNamespace: true
create: true
replaceInvalidRoutes: true
gloo:
deployment:
disableUsageStatistics: true
gatewayProxies:
gatewayProxy:
healthyPanicThreshold: 0
`))
Expect(err).NotTo(HaveOccurred())
err = values.Close()
Expect(err).NotTo(HaveOccurred())
return values.Name(), func() { _ = os.Remove(values.Name()) }
}
func EventuallyReachesConsistentState(installNamespace string) {
metricsPort := strconv.Itoa(9091)
portFwd := exec.Command("kubectl", "port-forward", "-n", installNamespace,
"deployment/gloo", metricsPort)
portFwd.Stdout = os.Stderr
portFwd.Stderr = os.Stderr
err := portFwd.Start()
Expect(err).ToNot(HaveOccurred())
defer func() {
if portFwd.Process != nil {
portFwd.Process.Kill()
}
}()
// make sure we eventually reach an eventually consistent state
lastSnapOut := getSnapOut(metricsPort)
eventuallyConsistentPollingInterval := 7 * time.Second // >= 5s for metrics reporting, which happens every 5s
time.Sleep(eventuallyConsistentPollingInterval)
Eventually(func() bool {
currentSnapOut := getSnapOut(metricsPort)
consistent := lastSnapOut == currentSnapOut
lastSnapOut = currentSnapOut
return consistent
}, "30s", eventuallyConsistentPollingInterval).Should(Equal(true))
Consistently(func() string {
currentSnapOut := getSnapOut(metricsPort)
return currentSnapOut
}, "30s", eventuallyConsistentPollingInterval).Should(Equal(lastSnapOut))
}
// needs a port-forward of the metrics port before a call to this will work
func getSnapOut(metricsPort string) string {
var bodyResp string
Eventually(func() string {
res, err := http.Post("http://localhost:"+metricsPort+"/metrics", "", nil)
if err != nil || res.StatusCode != 200 {
return ""
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
Expect(err).ToNot(HaveOccurred())
bodyResp = string(body)
return bodyResp
}, "5s", "1s").ShouldNot(BeEmpty())
Expect(bodyResp).To(ContainSubstring("api_gloo_solo_io_emitter_snap_out"))
findSnapOut := regexp.MustCompile("api_gloo_solo_io_emitter_snap_out ([\\d]+)")
matches := findSnapOut.FindAllStringSubmatch(bodyResp, -1)
Expect(matches).To(HaveLen(1))
snapOut := matches[0][1]
return snapOut
}
func UpdateDisableTransformationValidationSetting(ctx context.Context, shouldDisable bool, installNamespace string) {
UpdateSettings(func(settings *v1.Settings) {
Expect(settings.GetGateway()).NotTo(BeNil())
Expect(settings.GetGateway().GetValidation()).NotTo(BeNil())
settings.GetGateway().GetValidation().DisableTransformationValidation = &wrappers.BoolValue{Value: shouldDisable}
}, ctx, installNamespace)
}
// enable/disable strict validation
func UpdateAlwaysAcceptSetting(ctx context.Context, alwaysAccept bool, installNamespace string) {
UpdateSettings(func(settings *v1.Settings) {
Expect(settings.GetGateway()).NotTo(BeNil())
Expect(settings.GetGateway().GetValidation()).NotTo(BeNil())
settings.GetGateway().GetValidation().AlwaysAccept = &wrappers.BoolValue{Value: alwaysAccept}
}, ctx, installNamespace)
}
func UpdateRestEdsSetting(ctx context.Context, enableRestEds bool, installNamespace string) {
UpdateSettings(func(settings *v1.Settings) {
Expect(settings.GetGloo()).NotTo(BeNil())
settings.GetGloo().EnableRestEds = &wrappers.BoolValue{Value: enableRestEds}
}, ctx, installNamespace)
}
func UpdateReplaceInvalidRoutes(ctx context.Context, replaceInvalidRoutes bool, installNamespace string) {
UpdateSettings(func(settings *v1.Settings) {
Expect(settings.GetGloo()).NotTo(BeNil())
Expect(settings.GetGloo().GetInvalidConfigPolicy()).NotTo(BeNil())
settings.GetGloo().GetInvalidConfigPolicy().ReplaceInvalidRoutes = replaceInvalidRoutes
}, ctx, installNamespace)
}
func UpdateSettings(updateSettings func(settings *v1.Settings), ctx context.Context, 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()
}