/
instance.go
351 lines (302 loc) · 9.01 KB
/
instance.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
// Copyright 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 kube
import (
"context"
"fmt"
"io"
"time"
"github.com/hashicorp/go-multierror"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"istio.io/istio/pkg/config/protocol"
"istio.io/istio/pkg/test"
echoClient "istio.io/istio/pkg/test/echo"
"istio.io/istio/pkg/test/echo/common/scheme"
"istio.io/istio/pkg/test/framework/components/cluster"
"istio.io/istio/pkg/test/framework/components/echo"
"istio.io/istio/pkg/test/framework/components/echo/check"
"istio.io/istio/pkg/test/framework/components/echo/common"
"istio.io/istio/pkg/test/framework/resource"
"istio.io/istio/pkg/test/util/retry"
"istio.io/istio/pkg/util/istiomultierror"
)
const (
tcpHealthPort = 3333
httpReadinessPort = 8080
)
var (
_ echo.Instance = &instance{}
_ io.Closer = &instance{}
startDelay = retry.BackoffDelay(time.Millisecond * 100)
)
type instance struct {
id resource.ID
cfg echo.Config
clusterIP string
clusterIPs []string
ctx resource.Context
cluster cluster.Cluster
workloadMgr *workloadManager
deployment *deployment
workloadFilter []echo.Workload
}
func newInstance(ctx resource.Context, originalCfg echo.Config) (out *instance, err error) {
cfg := originalCfg.DeepCopy()
c := &instance{
cfg: cfg,
ctx: ctx,
cluster: cfg.Cluster,
}
// Deploy echo to the cluster
c.deployment, err = newDeployment(ctx, cfg)
if err != nil {
return nil, err
}
// Create the manager for echo workloads for this instance.
c.workloadMgr, err = newWorkloadManager(ctx, cfg, c.deployment)
if err != nil {
return nil, err
}
// Now that we have the successfully created the workload manager, track this resource so
// that it will be closed when it goes out of scope.
c.id = ctx.TrackResource(c)
// Now retrieve the service information to find the ClusterIP
s, err := c.cluster.Kube().CoreV1().Services(cfg.Namespace.Name()).Get(context.TODO(), cfg.Service, metav1.GetOptions{})
if err != nil {
return nil, err
}
c.clusterIP = s.Spec.ClusterIP
c.clusterIPs = s.Spec.ClusterIPs
switch c.clusterIP {
case corev1.ClusterIPNone, "":
if !cfg.Headless {
return nil, fmt.Errorf("invalid ClusterIP %s for non-headless service %s/%s",
c.clusterIP,
c.cfg.Namespace.Name(),
c.cfg.Service)
}
c.clusterIP = ""
}
// Start the workload manager.
if err := c.workloadMgr.Start(); err != nil {
return nil, err
}
return c, nil
}
func (c *instance) ID() resource.ID {
return c.id
}
func (c *instance) Address() string {
return c.clusterIP
}
func (c *instance) Addresses() []string {
return c.clusterIPs
}
func (c *instance) Workloads() (echo.Workloads, error) {
wls, err := c.workloadMgr.ReadyWorkloads()
if err != nil {
return nil, err
}
var final []echo.Workload
for _, wl := range wls {
filtered := false
for _, filter := range c.workloadFilter {
if wl.Address() != filter.Address() {
filtered = true
break
}
}
if !filtered {
final = append(final, wl)
}
}
return final, nil
}
func (c *instance) WorkloadsOrFail(t test.Failer) echo.Workloads {
t.Helper()
out, err := c.Workloads()
if err != nil {
t.Fatal(err)
}
return out
}
func (c *instance) MustWorkloads() echo.Workloads {
out, err := c.Workloads()
if err != nil {
panic(err)
}
return out
}
func (c *instance) Clusters() cluster.Clusters {
return cluster.Clusters{c.cluster}
}
func (c *instance) Instances() echo.Instances {
return echo.Instances{c}
}
func (c *instance) Close() (err error) {
return c.workloadMgr.Close()
}
func (c *instance) NamespacedName() echo.NamespacedName {
return c.cfg.NamespacedName()
}
func (c *instance) PortForName(name string) echo.Port {
return c.cfg.Ports.MustForName(name)
}
func (c *instance) ServiceName() string {
return c.cfg.Service
}
func (c *instance) NamespaceName() string {
return c.cfg.NamespaceName()
}
func (c *instance) ServiceAccountName() string {
return c.cfg.ServiceAccountName()
}
func (c *instance) ClusterLocalFQDN() string {
return c.cfg.ClusterLocalFQDN()
}
func (c *instance) ClusterSetLocalFQDN() string {
return c.cfg.ClusterSetLocalFQDN()
}
func (c *instance) Config() echo.Config {
return c.cfg
}
func (c *instance) WithWorkloads(wls ...echo.Workload) echo.Instance {
n := *c
n.workloadFilter = wls
return &n
}
func (c *instance) Cluster() cluster.Cluster {
return c.cfg.Cluster
}
func (c *instance) Call(opts echo.CallOptions) (echo.CallResult, error) {
// Setup default check. This is done here rather than in echo core package to avoid import loops
if opts.Check == nil {
opts.Check = check.OK()
}
return c.aggregateResponses(opts)
}
func (c *instance) CallOrFail(t test.Failer, opts echo.CallOptions) echo.CallResult {
t.Helper()
r, err := c.Call(opts)
if err != nil {
t.Fatal(err)
}
return r
}
func (c *instance) GetWorkloadLabels(labels map[string]string) error {
for _, wl := range c.workloadMgr.workloads {
wl.mutex.Lock()
pod := wl.pod
wl.mutex.Unlock()
if pod.Name != "" {
pod.Labels = labels
_, err := wl.Cluster().Kube().CoreV1().Pods(c.NamespaceName()).Update(context.TODO(), &pod, metav1.UpdateOptions{})
return fmt.Errorf("update pod labels failed: %v", err)
}
}
return nil
}
func (c *instance) UpdateWorkloadLabel(add map[string]string, remove []string) error {
for _, wl := range c.workloadMgr.workloads {
wl.mutex.Lock()
pod := wl.pod
wl.mutex.Unlock()
if pod.Name != "" {
return retry.UntilSuccess(func() (err error) {
podName := pod.Name
podNamespace := pod.Namespace
pod, err := wl.Cluster().Kube().CoreV1().Pods(c.NamespaceName()).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("get pod %s/%s failed: %v", podNamespace, podName, err)
}
newLabels := make(map[string]string)
for k, v := range pod.GetLabels() {
newLabels[k] = v
}
for k, v := range add {
newLabels[k] = v
}
for _, k := range remove {
delete(newLabels, k)
}
pod.Labels = newLabels
_, err = wl.Cluster().Kube().CoreV1().Pods(c.NamespaceName()).Update(context.TODO(), pod, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("update pod labels failed: %v", err)
}
return nil
}, retry.Timeout(c.cfg.ReadinessTimeout), startDelay)
}
}
return nil
}
func (c *instance) Restart() error {
// Wait for all current workloads to become ready and preserve the original count.
origWorkloads, err := c.workloadMgr.WaitForReadyWorkloads()
if err != nil {
return fmt.Errorf("restart failed to get initial workloads: %v", err)
}
// Restart the deployment.
if err := c.deployment.Restart(); err != nil {
return err
}
// Wait until all pods are ready and match the original count.
return retry.UntilSuccess(func() (err error) {
// Get the currently ready workloads.
workloads, err := c.workloadMgr.WaitForReadyWorkloads()
if err != nil {
return fmt.Errorf("failed waiting for restarted pods for echo %s/%s: %v",
c.cfg.Namespace.Name(), c.cfg.Service, err)
}
// Make sure the number of pods matches the original.
if len(workloads) != len(origWorkloads) {
return fmt.Errorf("failed restarting echo %s/%s: number of pods %d does not match original %d",
c.cfg.Namespace.Name(), c.cfg.Service, len(workloads), len(origWorkloads))
}
return nil
}, retry.Timeout(c.cfg.ReadinessTimeout), startDelay)
}
// aggregateResponses forwards an echo request from all workloads belonging to this echo instance and aggregates the results.
func (c *instance) aggregateResponses(opts echo.CallOptions) (echo.CallResult, error) {
// TODO put this somewhere else, or require users explicitly set the protocol - quite hacky
if c.Config().IsProxylessGRPC() && (opts.Scheme == scheme.GRPC || opts.Port.Name == "grpc" || opts.Port.Protocol == protocol.GRPC) {
// for gRPC calls, use XDS resolver
opts.Scheme = scheme.XDS
}
resps := make(echoClient.Responses, 0)
workloads, err := c.Workloads()
if err != nil {
return echo.CallResult{}, err
}
aggErr := istiomultierror.New()
for _, w := range workloads {
clusterName := w.(*workload).cluster.Name()
serviceName := fmt.Sprintf("%s (cluster=%s)", c.cfg.Service, clusterName)
out, err := common.ForwardEcho(serviceName, c, opts, w.(*workload).Client)
if err != nil {
aggErr = multierror.Append(aggErr, err)
continue
}
resps = append(resps, out.Responses...)
}
if aggErr.ErrorOrNil() != nil {
return echo.CallResult{}, aggErr
}
return echo.CallResult{
From: c,
Opts: opts,
Responses: resps,
}, nil
}