This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathdriver.go
349 lines (301 loc) · 9.81 KB
/
driver.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
// Copyright 2017 Istio 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 main
import (
"bufio"
"bytes"
"context"
"errors"
"flag"
"fmt"
"os"
"strconv"
"strings"
"text/template"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/golang/glog"
"github.com/golang/sync/errgroup"
multierror "github.com/hashicorp/go-multierror"
"k8s.io/client-go/kubernetes"
proxyconfig "istio.io/api/proxy/v1/config"
"istio.io/pilot/platform"
"istio.io/pilot/platform/kube"
"istio.io/pilot/platform/kube/inject"
"istio.io/pilot/test/util"
)
var (
params infra
// Enable/disable auth, or run both for the tests.
authmode string
verbose bool
count int
// The particular test to run, e.g. "HTTP reachability" or "routing rules"
testType string
kubeconfig string
client kubernetes.Interface
)
const (
// caImage specifies the default istio-ca docker image used for e2e testing *update manually*
caImage = "gcr.io/istio-testing/istio-ca:2baec6baacecbd516ea0880573b6fc3cd5736739"
// retry budget
budget = 90
mixerConfigFile = "/etc/istio/proxy/envoy_mixer.json"
mixerConfigAuthFile = "/etc/istio/proxy/envoy_mixer_auth.json"
pilotConfigFile = "/etc/istio/proxy/envoy_pilot.json"
pilotConfigAuthFile = "/etc/istio/proxy/envoy_pilot_auth.json"
)
func init() {
flag.StringVar(¶ms.Hub, "hub", "gcr.io/istio-testing", "Docker hub")
flag.StringVar(¶ms.Tag, "tag", "", "Docker tag")
flag.StringVar(¶ms.CaImage, "ca", caImage, "CA Docker image")
flag.StringVar(¶ms.IstioNamespace, "ns", "",
"Namespace in which to install Istio components (empty to create/delete temporary one)")
flag.StringVar(¶ms.Namespace, "n", "",
"Namespace in which to install the applications (empty to create/delete temporary one)")
flag.StringVar(¶ms.Registry, "registry", string(platform.KubernetesRegistry), "Pilot registry")
flag.BoolVar(&verbose, "verbose", false, "Debug level noise from proxies")
flag.BoolVar(¶ms.checkLogs, "logs", true, "Validate pod logs (expensive in long-running tests)")
flag.StringVar(&kubeconfig, "kubeconfig", "platform/kube/config",
"kube config file (missing or empty file makes the test use in-cluster kube config instead)")
flag.IntVar(&count, "count", 1, "Number of times to run the tests after deploying")
flag.StringVar(&authmode, "auth", "both", "Enable / disable auth, or test both.")
// If specified, only run one test
flag.StringVar(&testType, "testtype", "", "Select test to run (default is all tests)")
// Keep disabled until default no-op initializer is distributed
// and running in test clusters.
flag.BoolVar(¶ms.UseInitializer, "use-initializer", false, "Use k8s sidecar initializer")
flag.BoolVar(¶ms.UseAdmissionWebhook, "use-admission-webhook", false,
"Use k8s external admission webhook for config validation")
// TODO(github.com/kubernetes/kubernetes/issues/49987) - use
// `istio-pilot-external` for the registered service name and
// provide `istio-pilot` as --service-name argument to
// platform/kube/admit/webhook-workaround.sh. Once this bug is
// fixed (and for non-GKE k8s) the admission-service-name should
// be `istio-pilot`.
flag.StringVar(¶ms.AdmissionServiceName, "admission-service-name", "istio-pilot-external",
"Name of admission webhook service name")
flag.IntVar(¶ms.DebugPort, "debugport", 0, "Debugging port")
flag.BoolVar(¶ms.debugImagesAndMode, "debug", true, "Use debug images and mode (false for prod)")
}
type test interface {
String() string
setup() error
run() error
teardown()
}
func main() {
flag.Parse()
if params.Tag == "" {
glog.Fatal("No docker tag specified")
}
if verbose {
params.Verbosity = 3
} else {
params.Verbosity = 2
}
params.Name = "(default infra)"
params.Auth = proxyconfig.MeshConfig_NONE
params.Mixer = true
params.Ingress = true
params.Zipkin = true
params.MixerCustomConfigFile = mixerConfigFile
params.PilotCustomConfigFile = pilotConfigFile
if len(params.Namespace) != 0 && authmode == "both" {
glog.Infof("When namespace(=%s) is specified, auth mode(=%s) must be one of enable or disable.",
params.Namespace, authmode)
return
}
var err error
_, client, err = kube.CreateInterface(kubeconfig)
if err != nil {
glog.Fatal(err)
}
switch authmode {
case "enable":
runTests(setAuth(params))
case "disable":
runTests(params)
case "both":
runTests(params, setAuth(params))
default:
glog.Infof("Invald auth flag: %s. Please choose from: enable/disable/both.", authmode)
}
}
func setAuth(params infra) infra {
out := params
out.Name = "(auth infra)"
out.Auth = proxyconfig.MeshConfig_MUTUAL_TLS
out.ControlPlaneAuthPolicy = proxyconfig.AuthenticationPolicy_MUTUAL_TLS
out.MixerCustomConfigFile = mixerConfigAuthFile
out.PilotCustomConfigFile = pilotConfigAuthFile
return out
}
func log(header, s string) {
glog.Infof("\n\n=================== %s =====================\n%s\n\n", header, s)
}
func logError(header, s string) {
glog.Errorf("\n\n=================== %s =====================\n%s\n\n", header, s)
}
func runTests(envs ...infra) {
var result error
for _, istio := range envs {
var errs error
log("Deploying infrastructure", spew.Sdump(istio))
if err := istio.setup(); err != nil {
result = multierror.Append(result, err)
continue
}
if err := istio.deployApps(); err != nil {
result = multierror.Append(result, err)
continue
}
nslist := []string{istio.IstioNamespace, istio.Namespace}
istio.apps, errs = util.GetAppPods(client, nslist)
if errs != nil {
result = multierror.Append(result, errs)
break
}
tests := []test{
&http{infra: &istio},
&grpc{infra: &istio},
&tcp{infra: &istio},
&headless{infra: &istio},
&ingress{infra: &istio},
&egressRules{infra: &istio},
&routing{infra: &istio},
&zipkin{infra: &istio},
}
for _, test := range tests {
// If the user has specified a test, skip all other tests
if len(testType) > 0 && testType != test.String() {
continue
}
for i := 0; i < count; i++ {
log("Test run", strconv.Itoa(i))
if err := test.setup(); err != nil {
errs = multierror.Append(errs, multierror.Prefix(err, test.String()))
} else {
log("Running test", test.String())
if err := test.run(); err != nil {
errs = multierror.Append(errs, multierror.Prefix(err, fmt.Sprintf("%v run %d", test, i)))
} else {
log("Success!", test.String())
}
}
log("Tearing down test", test.String())
test.teardown()
}
}
// spill all logs on error
if errs != nil {
for _, pod := range util.GetPods(client, istio.Namespace) {
if strings.HasPrefix(pod, "istio-pilot") {
log("Discovery log", pod)
glog.Info(util.FetchLogs(client, pod, istio.IstioNamespace, "discovery"))
} else if strings.HasPrefix(pod, "istio-mixer") {
log("Mixer log", pod)
glog.Info(util.FetchLogs(client, pod, istio.IstioNamespace, "mixer"))
} else if strings.HasPrefix(pod, "istio-ingress") {
log("Ingress log", pod)
glog.Info(util.FetchLogs(client, pod, istio.IstioNamespace, inject.ProxyContainerName))
} else {
log("Proxy log", pod)
glog.Info(util.FetchLogs(client, pod, istio.Namespace, inject.ProxyContainerName))
}
}
}
// always remove infra even if the tests fail
log("Tearing down infrastructure", istio.Name)
istio.teardown()
if errs == nil {
log("Passed all tests!", fmt.Sprintf("tests: %v, count: %d", tests, count))
} else {
logError("Failed tests!", errs.Error())
result = multierror.Append(result, multierror.Prefix(errs, istio.Name))
}
}
if result == nil {
log("Passed infrastructure tests!", spew.Sdump(envs))
} else {
logError("Failed infrastructure tests!", result.Error())
os.Exit(1)
}
}
// fill a file based on a template
func fill(inFile string, values interface{}) (string, error) {
var bytes bytes.Buffer
w := bufio.NewWriter(&bytes)
tmpl, err := template.ParseFiles("test/integration/testdata/" + inFile)
if err != nil {
return "", err
}
if err := tmpl.Execute(w, values); err != nil {
return "", err
}
if err := w.Flush(); err != nil {
return "", err
}
return bytes.String(), nil
}
type status error
var (
errAgain status = errors.New("try again")
)
// run in parallel with retries. all funcs must succeed for the function to succeed
func parallel(fs map[string]func() status) error {
g, ctx := errgroup.WithContext(context.Background())
repeat := func(name string, f func() status) func() error {
return func() error {
for n := 0; n < budget; n++ {
glog.Infof("%s (attempt %d)", name, n)
err := f()
switch err {
case nil:
// success
return nil
case errAgain:
// do nothing
default:
return fmt.Errorf("failed %s at attempt %d: %v", name, n, err)
}
select {
case <-time.After(time.Second):
// try again
case <-ctx.Done():
return nil
}
}
return fmt.Errorf("failed all %d attempts for %s", budget, name)
}
}
for name, f := range fs {
g.Go(repeat(name, f))
}
return g.Wait()
}
// repeat a check up to budget until it does not return an error
func repeat(f func() error, budget int, delay time.Duration) error {
var errs error
for i := 0; i < budget; i++ {
err := f()
if err == nil {
return nil
}
errs = multierror.Append(errs, multierror.Prefix(err, fmt.Sprintf("attempt %d", i)))
glog.Infof("attempt #%d failed with %v", i, err)
time.Sleep(delay)
}
return errs
}