-
Notifications
You must be signed in to change notification settings - Fork 0
/
workload_manager.go
261 lines (226 loc) · 6.37 KB
/
workload_manager.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
// 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 (
"errors"
"fmt"
"io"
"sync"
"github.com/hashicorp/go-multierror"
corev1 "k8s.io/api/core/v1"
"istio.io/istio/pkg/config/protocol"
echoCommon "istio.io/istio/pkg/test/echo/common"
"istio.io/istio/pkg/test/framework/components/echo"
"istio.io/istio/pkg/test/framework/resource"
"istio.io/istio/pkg/test/util/retry"
)
var (
_ echo.Instance = &instance{}
_ io.Closer = &instance{}
)
type workloadHandler interface {
WorkloadReady(w *workload)
WorkloadNotReady(w *workload)
}
type workloadManager struct {
workloads []*workload
mutex sync.Mutex
podController *podController
cfg echo.Config
ctx resource.Context
grpcPort uint16
tls *echoCommon.TLSSettings
closing bool
stopCh chan struct{}
handler workloadHandler
}
func newWorkloadManager(ctx resource.Context, cfg echo.Config, handler workloadHandler) (*workloadManager, error) {
// Get the gRPC port and TLS settings.
var grpcInstancePort int
var tls *echoCommon.TLSSettings
if cfg.IsProxylessGRPC() {
grpcInstancePort = grpcMagicPort
}
if grpcInstancePort == 0 {
if grpcPort, found := cfg.Ports.ForProtocol(protocol.GRPC); found {
if grpcPort.TLS {
tls = cfg.TLSSettings
}
grpcInstancePort = grpcPort.WorkloadPort
}
}
if grpcInstancePort == 0 {
return nil, errors.New("unable to find GRPC command port")
}
m := &workloadManager{
cfg: cfg,
ctx: ctx,
handler: handler,
grpcPort: uint16(grpcInstancePort),
tls: tls,
stopCh: make(chan struct{}, 1),
}
m.podController = newPodController(cfg, podHandlers{
added: m.onPodAddOrUpdate,
updated: m.onPodAddOrUpdate,
deleted: m.onPodDeleted,
})
return m, nil
}
// WaitForReadyWorkloads waits until all known workloads are ready.
func (m *workloadManager) WaitForReadyWorkloads() (out echo.Workloads, err error) {
err = retry.UntilSuccess(func() error {
m.mutex.Lock()
out, err = m.readyWorkloads()
if err == nil && len(out) != len(m.workloads) {
err = fmt.Errorf("failed waiting for workloads for echo %s/%s to be ready",
m.cfg.Namespace.Name(),
m.cfg.Service)
}
m.mutex.Unlock()
return err
}, retry.Timeout(m.cfg.ReadinessTimeout), startDelay)
return
}
func (m *workloadManager) readyWorkloads() (echo.Workloads, error) {
out := make(echo.Workloads, 0, len(m.workloads))
var connErrs error
for _, w := range m.workloads {
if w.IsReady() {
out = append(out, w)
} else if w.connectErr != nil {
connErrs = multierror.Append(connErrs, w.connectErr)
}
}
if len(out) == 0 {
err := fmt.Errorf("no workloads ready for echo %s/%s", m.cfg.Namespace.Name(), m.cfg.Service)
if connErrs != nil {
err = fmt.Errorf("%v: failed connecting: %v", err, connErrs)
}
return nil, err
}
return out, nil
}
// ReadyWorkloads returns all ready workloads in ascending order by pod name.
func (m *workloadManager) ReadyWorkloads() (echo.Workloads, error) {
m.mutex.Lock()
out, err := m.readyWorkloads()
m.mutex.Unlock()
return out, err
}
func (m *workloadManager) Start() error {
// Run the pod controller.
go m.podController.Run(m.stopCh)
// Wait for the cache to sync.
if !m.podController.WaitForSync(m.stopCh) {
return fmt.Errorf(
"failed syncing cache for echo %s/%s: controller stopping",
m.cfg.Namespace.Name(),
m.cfg.Service)
}
// Wait until all pods are ready.
_, err := m.WaitForReadyWorkloads()
return err
}
func (m *workloadManager) onPodAddOrUpdate(pod *corev1.Pod) error {
m.mutex.Lock()
// After the method returns, notify the handler the ready state of the workload changed.
var workloadReady *workload
var workloadNotReady *workload
defer func() {
m.mutex.Unlock()
if workloadReady != nil {
m.handler.WorkloadReady(workloadReady)
}
if workloadNotReady != nil {
m.handler.WorkloadNotReady(workloadNotReady)
}
}()
// First, check to see if we already have a workload for the pod. If we do, just update it.
for _, w := range m.workloads {
if w.pod.Name == pod.Name {
prevReady := w.IsReady()
if err := w.Update(*pod); err != nil {
return err
}
// Defer notifying the handler until after we release the mutex.
if !prevReady && w.IsReady() {
workloadReady = w
} else if prevReady && !w.IsReady() {
workloadNotReady = w
}
return nil
}
}
// Add the pod to the end of the workload list.
newWorkload, err := newWorkload(workloadConfig{
pod: *pod,
hasSidecar: workloadHasSidecar(pod),
cluster: m.cfg.Cluster,
grpcPort: m.grpcPort,
tls: m.tls,
stop: m.stopCh,
}, m.ctx)
if err != nil {
return err
}
m.workloads = append(m.workloads, newWorkload)
if newWorkload.IsReady() {
workloadReady = newWorkload
}
return nil
}
func (m *workloadManager) onPodDeleted(pod *corev1.Pod) (err error) {
m.mutex.Lock()
// After the method returns, notify the handler the ready state of the workload changed.
var workloadNotReady *workload
defer func() {
m.mutex.Unlock()
if workloadNotReady != nil {
m.handler.WorkloadNotReady(workloadNotReady)
}
}()
newWorkloads := make([]*workload, 0, len(m.workloads))
for _, w := range m.workloads {
if w.pod.Name == pod.Name {
// Close the workload and remove it from the list. If an
// error occurs, just continue.
if w.IsReady() {
workloadNotReady = w
}
err = w.Close()
} else {
// Just add all other workloads.
newWorkloads = append(newWorkloads, w)
}
}
m.workloads = newWorkloads
return err
}
func (m *workloadManager) Close() (err error) {
m.mutex.Lock()
// Indicate we're closing.
m.closing = true
// Stop the controller and queue.
close(m.stopCh)
// Clear out the workloads array
workloads := m.workloads
m.workloads = nil
m.mutex.Unlock()
// Close the workloads.
for _, w := range workloads {
err = multierror.Append(err, w.Close()).ErrorOrNil()
}
return
}