/
backend.go
394 lines (322 loc) · 9.86 KB
/
backend.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2018 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 mcp
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"strings"
"sync"
"time"
"google.golang.org/grpc"
"k8s.io/apimachinery/pkg/runtime/schema"
mcp "istio.io/api/mcp/v1alpha1"
"istio.io/istio/galley/pkg/metadata/kube"
"istio.io/istio/mixer/pkg/config/store"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/mcp/client"
"istio.io/istio/pkg/mcp/configz"
"istio.io/istio/pkg/mcp/creds"
"istio.io/istio/pkg/mcp/monitoring"
"istio.io/istio/pkg/mcp/sink"
"istio.io/istio/pkg/probe"
)
var scope = log.RegisterScope("mcp", "Mixer MCP client stack", 0)
const (
// TODO(nmittler): Need to decide the correct NodeID for mixer.
mixerNodeID = "default"
eventChannelSize = 4096
requiredCertCheckFreq = 500 * time.Millisecond
)
// Register registers this module as a StoreBackend.
// Do not use 'init()' for automatic registration; linker will drop
// the whole module because it looks unused.
func Register(builders map[string]store.Builder) {
var builder store.Builder = func(u *url.URL, _ *schema.GroupVersion, credOptions *creds.Options, _ []string) (store.Backend, error) {
return newStore(u, credOptions, nil)
}
builders["mcp"] = builder
builders["mcps"] = builder
}
// newStore creates a new Store instance.
func newStore(u *url.URL, credOptions *creds.Options, fn updateHookFn) (store.Backend, error) {
insecure := true
if u.Scheme == "mcps" {
insecure = false
if credOptions == nil {
return nil, errors.New("no credentials specified with secure MCP scheme")
}
}
return &backend{
serverAddress: u.Host,
insecure: insecure,
credOptions: credOptions,
Probe: probe.NewProbe(),
updateHook: fn,
}, nil
}
// updateHookFn is a testing hook function
type updateHookFn func()
// backend is StoreBackend implementation using MCP.
type backend struct {
// mapping of CRD <> collections.
mapping *mapping
// Use insecure communication for gRPC.
insecure bool
// address of the MCP server.
serverAddress string
// MCP credential options
credOptions *creds.Options
// The cancellation function that is used to cancel gRPC/MCP operations.
cancel context.CancelFunc
mcpReporter monitoring.Reporter
// The in-memory state, where resources are kept for out-of-band get and list calls.
state *state
// channel for publishing backend events. chLock is used to protect against ch lifecycle
// events (e.g. creation of the channel).
// It also protects against a race, where a channel could be created while there is incoming
// data/changes.
chLock sync.Mutex
ch chan store.BackendEvent
// The update hook that was registered. This is for testing.
updateHook updateHookFn
*probe.Probe
}
var _ store.Backend = &backend{}
var _ probe.SupportsProbe = &backend{}
var _ sink.Updater = &backend{}
// state is the in-memory cache.
type state struct {
sync.RWMutex
// items stored by kind, then by key.
items map[string]map[store.Key]*store.BackEndResource
synced map[string]bool // by collection
}
// Init implements store.Backend.Init.
func (b *backend) Init(kinds []string) error {
m, err := constructMapping(kinds, kube.Types)
if err != nil {
return err
}
b.mapping = m
collections := b.mapping.collections()
scope.Infof("Requesting following collections:")
for i, name := range collections {
scope.Infof(" [%d] %s", i, name)
}
// nolint: govet
ctx, cancel := context.WithCancel(context.Background())
securityOption := grpc.WithInsecure()
if !b.insecure {
address := b.serverAddress
if strings.Contains(address, ":") {
address = strings.Split(address, ":")[0]
}
requiredFiles := []string{b.credOptions.CertificateFile, b.credOptions.KeyFile, b.credOptions.CACertificateFile}
log.Infof("Secure MCP configured. Waiting for required certificate files to become available: %v", requiredFiles)
for len(requiredFiles) > 0 {
if _, err := os.Stat(requiredFiles[0]); os.IsNotExist(err) {
log.Infof("%v not found. Checking again in %v", requiredFiles[0], requiredCertCheckFreq)
select {
case <-ctx.Done():
// nolint: govet
cancel()
return ctx.Err()
case <-time.After(requiredCertCheckFreq):
// retry
}
continue
}
log.Infof("%v found", requiredFiles[0])
requiredFiles = requiredFiles[1:]
}
watcher, err := creds.WatchFiles(ctx.Done(), b.credOptions)
if err != nil {
cancel()
return err
}
credentials := creds.CreateForClient(address, watcher)
securityOption = grpc.WithTransportCredentials(credentials)
}
conn, err := grpc.DialContext(ctx, b.serverAddress, securityOption)
if err != nil {
cancel()
scope.Errorf("Error connecting to server: %v\n", err)
return err
}
b.mcpReporter = monitoring.NewStatsContext("mixer/mcp/sink")
options := &sink.Options{
CollectionOptions: sink.CollectionOptionsFromSlice(collections),
Updater: b,
ID: mixerNodeID,
Reporter: b.mcpReporter,
}
// TODO - temporarily support both the new and old stack during transition
if os.Getenv("USE_MCP_LEGACY") == "1" {
log.Infof("USE_MCP_LEGACY=1 - using legacy MCP client stack")
cl := mcp.NewAggregatedMeshConfigServiceClient(conn)
c := client.New(cl, options)
configz.Register(c)
go c.Run(ctx)
} else {
log.Infof("Using new MCP client sink stack")
cl := mcp.NewResourceSourceClient(conn)
c := sink.NewClient(cl, options)
configz.Register(c)
go c.Run(ctx)
}
b.state = &state{
items: make(map[string]map[store.Key]*store.BackEndResource),
synced: make(map[string]bool),
}
for _, collection := range collections {
b.state.synced[collection] = false
}
b.cancel = cancel
return nil
}
// WaitForSynced implements store.Backend interface.
func (b *backend) WaitForSynced(timeout time.Duration) error {
stop := time.After(timeout)
tick := time.NewTicker(10 * time.Millisecond)
defer tick.Stop()
for {
select {
case <-stop:
return fmt.Errorf("exceeded timeout %v", timeout)
case <-tick.C:
ready := true
for _, synced := range b.state.synced {
if !synced {
ready = false
break
}
}
if ready {
return nil
}
}
}
}
// Stop implements store.Backend.Stop.
func (b *backend) Stop() {
if b.cancel != nil {
b.cancel()
b.cancel = nil
}
b.mcpReporter.Close()
}
// Watch creates a channel to receive the events.
func (b *backend) Watch() (<-chan store.BackendEvent, error) {
b.chLock.Lock()
defer b.chLock.Unlock()
if b.ch != nil {
return nil, fmt.Errorf("watch was already called")
}
b.ch = make(chan store.BackendEvent, eventChannelSize)
return b.ch, nil
}
// Get returns a resource's spec to the key.
func (b *backend) Get(key store.Key) (*store.BackEndResource, error) {
b.state.RLock()
defer b.state.RUnlock()
perTypeState, found := b.state.items[key.Kind]
if !found {
return nil, store.ErrNotFound
}
item, found := perTypeState[key]
if !found {
return nil, store.ErrNotFound
}
return item, nil
}
// List returns the whole mapping from key to resource specs in the store.
func (b *backend) List() map[store.Key]*store.BackEndResource {
b.state.RLock()
defer b.state.RUnlock()
result := make(map[store.Key]*store.BackEndResource)
for _, perTypeItems := range b.state.items {
for k, v := range perTypeItems {
result[k] = v
}
}
return result
}
// Apply implements client.Updater.Apply
func (b *backend) Apply(change *sink.Change) error {
b.state.Lock()
defer b.state.Unlock()
defer b.callUpdateHook()
newTypeStates := make(map[string]map[store.Key]*store.BackEndResource)
b.state.synced[change.Collection] = true
scope.Debugf("Received update for: collection:%s, count:%d", change.Collection, len(change.Objects))
for _, o := range change.Objects {
if scope.DebugEnabled() {
scope.Debugf("Processing incoming resource: %q @%s [%s]",
o.Metadata.Name, o.Metadata.Version, o.TypeURL)
}
name := o.Metadata.Name
kind := b.mapping.kind(change.Collection)
contents := o.Body
labels := o.Metadata.Labels
annotations := o.Metadata.Annotations
collection, found := newTypeStates[kind]
if !found {
collection = make(map[store.Key]*store.BackEndResource)
newTypeStates[kind] = collection
}
// Map it to Mixer's store model, and put it in the new collection.
key := toKey(kind, name)
resource, err := toBackendResource(key, labels, annotations, contents, o.Metadata.Version)
if err != nil {
return err
}
collection[key] = resource
}
// Lock the channel state, as we will start publishing events soon.
b.chLock.Lock()
defer b.chLock.Unlock()
// Now, diff against the in-memory state and generate store events.
for kind, newTypeState := range newTypeStates {
oldTypeState, found := b.state.items[kind]
// Replace the old collection with the new one.
// We can do this, because there is no error that can be raised from now on.
b.state.items[kind] = newTypeState
// If the downstream users haven't started listening yet, we don't need to
// send any events.
if b.ch == nil {
continue
}
// Otherwise, start pumping events by diffing old and new states.
if found {
for k := range oldTypeState {
if _, exists := newTypeState[k]; !exists {
b.ch <- store.BackendEvent{Key: k, Type: store.Delete}
continue
}
}
}
for k, v := range newTypeState {
b.ch <- store.BackendEvent{Key: k, Type: store.Update, Value: v}
}
}
return nil
}
func (b *backend) callUpdateHook() {
if b.updateHook != nil {
b.updateHook()
}
}