forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.go
351 lines (308 loc) · 10.7 KB
/
install.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package helper
import (
"context"
"os"
"path/filepath"
"runtime"
"time"
"helm.sh/helm/v3/pkg/repo"
"github.com/avast/retry-go"
"github.com/pkg/errors"
"github.com/spf13/afero"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/rotisserie/eris"
"github.com/solo-io/go-utils/log"
"github.com/solo-io/go-utils/testutils/exec"
"github.com/solo-io/k8s-utils/testutils/kube"
)
const (
GATEWAY = "gateway"
INGRESS = "ingress"
KNATIVE = "knative"
)
// Default test configuration
var defaults = TestConfig{
TestAssetDir: "_test",
BuildAssetDir: "_output",
HelmRepoIndexFileName: "index.yaml",
DeployTestServer: true,
}
// supportedArchs is represents the list of architectures we build glooctl for
var supportedArchs = map[string]struct{}{
"arm64": {},
"amd64": {},
}
// returns true if supported, based on `supportedArchs`
func isSupportedArch() (string, bool) {
if goarch, ok := os.LookupEnv("GOARCH"); ok {
// if the environment's goarch is supported
_, ok := supportedArchs[goarch]
return goarch, ok
}
// if the runtime's goarch is supported
runtimeArch := runtime.GOARCH
_, ok := supportedArchs[runtimeArch]
return runtimeArch, ok
}
// Function to provide/override test configuration. Default values will be passed in.
type TestConfigFunc func(defaults TestConfig) TestConfig
type TestConfig struct {
// All relative paths will assume this as the base directory. This is usually the project base directory.
RootDir string
// The directory holding the test assets. Must be relative to RootDir.
TestAssetDir string
// The directory holding the build assets. Must be relative to RootDir.
BuildAssetDir string
// Helm chart name
HelmChartName string
// Name of the helm index file name
HelmRepoIndexFileName string
// The namespace gloo (and the test server) will be installed to. If empty, will use the helm chart version.
InstallNamespace string
// Name of the glooctl executable
GlooctlExecName string
// If provided, the licence key to install the enterprise version of Gloo
LicenseKey string
// Determines whether the test server pod gets deployed
DeployTestServer bool
// Install a released version of gloo. This is the value of the github tag that may have a leading 'v'
ReleasedVersion string
// If true, glooctl will be run with a -v flag
Verbose bool
// The version of the Helm chart. Calculated from either the chart or the released version. It will not have a leading 'v'
version string
}
type SoloTestHelper struct {
*TestConfig
TestUpstreamServer
}
// NewSoloTestHelper is meant to provide a standard way of deploying Gloo/GlooE to a k8s cluster during tests.
// It assumes that build and test assets are present in the `_output` and `_test` directories (these are configurable).
// Specifically, it expects the glooctl executable in the BuildAssetDir and a helm chart in TestAssetDir.
// It also assumes that a kubectl executable is on the PATH.
func NewSoloTestHelper(configFunc TestConfigFunc) (*SoloTestHelper, error) {
// Get and validate test config
testConfig := defaults
if configFunc != nil {
testConfig = configFunc(defaults)
}
// Depending on the testing tool used, GOARCH may always be set if not set already by detecting the local arch
// (`go test`), `ginkgo` and other testing tools may not do this requiring keeping the runtime.GOARCH check
if testConfig.GlooctlExecName == "" {
if arch, ok := isSupportedArch(); ok {
testConfig.GlooctlExecName = "glooctl-" + runtime.GOOS + "-" + arch
} else {
testConfig.GlooctlExecName = "glooctl-" + runtime.GOOS + "-amd64"
}
}
// Get chart version
if testConfig.ReleasedVersion == "" {
if err := validateConfig(testConfig); err != nil {
return nil, errors.Wrapf(err, "test config validation failed")
}
version, err := getChartVersion(testConfig)
if err != nil {
return nil, errors.Wrapf(err, "getting Helm chart version")
}
testConfig.version = version
} else {
// we use the version field as a chart version and tests assume it doesn't have a leading 'v'
if testConfig.ReleasedVersion[0] == 'v' {
testConfig.version = testConfig.ReleasedVersion[1:]
} else {
testConfig.version = testConfig.ReleasedVersion
}
}
// Default the install namespace to the chart version.
// Currently the test chart version built in CI contains the build id, so the namespace will be unique).
if testConfig.InstallNamespace == "" {
testConfig.InstallNamespace = testConfig.version
}
testHelper := &SoloTestHelper{
TestConfig: &testConfig,
}
// Optionally, initialize a test server
if testConfig.DeployTestServer {
testServer, err := NewTestServer(testConfig.InstallNamespace)
if err != nil {
return nil, errors.Wrapf(err, "initializing testserver")
}
testHelper.TestUpstreamServer = testServer
}
return testHelper, nil
}
// Return the version of the Helm chart
func (h *SoloTestHelper) ChartVersion() string {
return h.version
}
type InstallOption func(*InstallOptions)
type InstallOptions struct {
GlooctlCommand []string
Verbose bool
}
func ExtraArgs(args ...string) func(*InstallOptions) {
return func(io *InstallOptions) {
io.GlooctlCommand = append(io.GlooctlCommand, args...)
}
}
// Installs Gloo (and, optionally, the test server)
func (h *SoloTestHelper) InstallGloo(ctx context.Context, deploymentType string, timeout time.Duration, options ...InstallOption) error {
log.Printf("installing gloo in [%s] mode to namespace [%s]", deploymentType, h.InstallNamespace)
glooctlCommand := []string{
filepath.Join(h.BuildAssetDir, h.GlooctlExecName),
"install", deploymentType,
}
if h.LicenseKey != "" {
glooctlCommand = append(glooctlCommand, "enterprise", "--license-key", h.LicenseKey)
}
if h.ReleasedVersion != "" {
glooctlCommand = append(glooctlCommand, "-n", h.InstallNamespace, "--version", h.ReleasedVersion)
} else {
glooctlCommand = append(glooctlCommand,
"-n", h.InstallNamespace,
"-f", filepath.Join(h.TestAssetDir, h.HelmChartName+"-"+h.version+".tgz"))
}
if h.Verbose {
glooctlCommand = append(glooctlCommand, "-v")
}
variant := os.Getenv("IMAGE_VARIANT")
if variant != "" {
variantValuesFile, err := GenerateVariantValuesFile(variant)
if err != nil {
return err
}
options = append(options, ExtraArgs("--values", variantValuesFile))
}
io := &InstallOptions{
GlooctlCommand: glooctlCommand,
Verbose: true,
}
for _, opt := range options {
opt(io)
}
if err := glooctlInstallWithTimeout(h.RootDir, io, time.Minute*2); err != nil {
return errors.Wrapf(err, "error running glooctl install command")
}
if h.TestUpstreamServer != nil {
if err := waitForDefaultServiceAccount(ctx, h.InstallNamespace); err != nil {
return errors.Wrapf(err, "waiting for default service account")
}
if err := h.DeployServer(timeout); err != nil {
return errors.Wrapf(err, "deploying testserver")
}
}
return nil
}
// Wait for the glooctl install command to respond, err on timeout.
// The command returns as soon as certgen completes and all other
// deployments have been applied, which should only be delayed if
// there's an issue pulling the certgen docker image.
// Without this timeout, it would just hang indefinitely.
func glooctlInstallWithTimeout(rootDir string, io *InstallOptions, timeout time.Duration) error {
runResponse := make(chan error, 1)
go func() {
err := exec.RunCommand(rootDir, io.Verbose, io.GlooctlCommand...)
if err != nil {
runResponse <- errors.Wrapf(err, "error while installing gloo")
}
runResponse <- nil
}()
select {
case err := <-runResponse:
return err // can be nil
case <-time.After(timeout):
return errors.New("timeout - did something go wrong fetching the docker images?")
}
}
func waitForDefaultServiceAccount(ctx context.Context, installNamespace string) error {
kubeClient := kube.MustKubeClient()
getDefaultServiceAccount := func() error {
_, err := kubeClient.CoreV1().ServiceAccounts(installNamespace).Get(ctx, "default", metav1.GetOptions{})
return err
}
return retry.Do(getDefaultServiceAccount)
}
// passes the --all flag to glooctl uninstall
func (h *SoloTestHelper) UninstallGlooAll() error {
return h.uninstallGloo(true)
}
// does not pass the --all flag to glooctl uninstall
func (h *SoloTestHelper) UninstallGloo() error {
return h.uninstallGloo(false)
}
func (h *SoloTestHelper) uninstallGloo(all bool) error {
if conc := h.TestUpstreamServer.(*testServer); conc != nil {
log.Debugf("terminating %s...", TestServerName)
if err := h.TerminatePod(); err != nil {
// Just log a warning, we don't want to fail
log.Warnf("error terminating %s", TestServerName)
}
}
log.Printf("uninstalling gloo...")
cmdArgs := []string{
filepath.Join(h.BuildAssetDir, h.GlooctlExecName), "uninstall", "-n", h.InstallNamespace, "--delete-namespace",
}
if all {
cmdArgs = append(cmdArgs, "--all")
}
return exec.RunCommand(h.RootDir, true, cmdArgs...)
}
// Parses the Helm index file and returns the version of the chart.
func getChartVersion(config TestConfig) (string, error) {
// Find helm index file in test asset directory
helmIndexFile := filepath.Join(config.RootDir, config.TestAssetDir, config.HelmRepoIndexFileName)
helmIndex, err := repo.LoadIndexFile(helmIndexFile)
if err != nil {
return "", errors.Wrapf(err, "parsing Helm index file")
}
log.Printf("found Helm index file at: %s", helmIndexFile)
// Read and return version from helm index file
if chartVersions, ok := helmIndex.Entries[config.HelmChartName]; !ok {
return "", eris.Errorf("index file does not contain entry with key: %s", config.HelmChartName)
} else if len(chartVersions) == 0 || len(chartVersions) > 1 {
return "", eris.Errorf("expected a single entry with name [%s], found: %v", config.HelmChartName, len(chartVersions))
} else {
version := chartVersions[0].Version
log.Printf("version of [%s] Helm chart is: %s", config.HelmChartName, version)
return version, nil
}
}
func validateConfig(config TestConfig) error {
if err := validateDir(config.RootDir); err != nil {
return err
}
if err := validateDir(filepath.Join(config.RootDir, config.TestAssetDir)); err != nil {
return err
}
if err := validateDir(filepath.Join(config.RootDir, config.BuildAssetDir)); err != nil {
return err
}
return nil
}
func validateDir(dir string) error {
if stat, err := os.Stat(dir); err != nil {
return errors.Wrapf(err, "finding directory: %s", dir)
} else if !stat.IsDir() {
return eris.Errorf("expected a directory. Got: %s", dir)
}
return nil
}
func GenerateVariantValuesFile(variant string) (string, error) {
content := `global:
image:
variant: ` + variant
fs := afero.NewOsFs()
dir, err := afero.TempDir(fs, "", "")
if err != nil {
return "", err
}
tmpFile, err := afero.TempFile(fs, dir, "")
if err != nil {
return "", err
}
_, err = tmpFile.WriteString(content)
if err != nil {
return "", err
}
return tmpFile.Name(), nil
}