-
Notifications
You must be signed in to change notification settings - Fork 432
/
util.go
169 lines (147 loc) · 5.29 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
package kube2e
import (
"context"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"time"
"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/go-utils/kubeutils"
"github.com/solo-io/go-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/gogo/protobuf/types"
. "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(),
},
}
ok, err := check.CheckResources(opts)
if err != nil {
return errors.Wrap(err, "unable to run glooctl check")
}
if ok {
return nil
}
return errors.New("glooctl check detected a problem with the installation")
}, 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
`))
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
}, "3s", "0.5s").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
}
// enable/disable strict validation
func UpdateAlwaysAcceptSetting(alwaysAccept bool, installNamespace string) {
UpdateSettings(func(settings *v1.Settings) {
Expect(settings.Gateway).NotTo(BeNil())
Expect(settings.Gateway.Validation).NotTo(BeNil())
settings.Gateway.Validation.AlwaysAccept = &types.BoolValue{Value: alwaysAccept}
}, installNamespace)
}
func UpdateSettings(f func(settings *v1.Settings), installNamespace string) {
settingsClient := clienthelpers.MustSettingsClient()
settings, err := settingsClient.Read(installNamespace, "default", clients.ReadOpts{})
Expect(err).NotTo(HaveOccurred())
f(settings)
_, err = settingsClient.Write(settings, clients.WriteOpts{OverwriteExisting: true})
Expect(err).NotTo(HaveOccurred())
// 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.
time.Sleep(3 * time.Second)
}