forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
298 lines (258 loc) · 8.56 KB
/
controller.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
// 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 coredatamodel
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/gogo/protobuf/types"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/mcp/sink"
)
var errUnsupported = errors.New("this operation is not supported by mcp controller")
// CoreDataModel is a combined interface for ConfigStoreCache
// MCP Updater and ServiceDiscovery
type CoreDataModel interface {
model.ConfigStoreCache
sink.Updater
}
// Options stores the configurable attributes of a Control
type Options struct {
DomainSuffix string
ClearDiscoveryServerCache func()
}
// Controller is a temporary storage for the changes received
// via MCP server
type Controller struct {
configStoreMu sync.RWMutex
// keys [type][namespace][name]
configStore map[string]map[string]map[string]*model.Config
descriptorsByCollection map[string]model.ProtoSchema
options Options
eventHandlers map[string][]func(model.Config, model.Event)
syncedMu sync.Mutex
synced map[string]bool
}
// NewController provides a new CoreDataModel controller
func NewController(options Options) CoreDataModel {
descriptorsByMessageName := make(map[string]model.ProtoSchema, len(model.IstioConfigTypes))
synced := make(map[string]bool)
for _, descriptor := range model.IstioConfigTypes {
// don't register duplicate descriptors for the same collection
if _, ok := descriptorsByMessageName[descriptor.Collection]; !ok {
descriptorsByMessageName[descriptor.Collection] = descriptor
synced[descriptor.Collection] = false
}
}
return &Controller{
configStore: make(map[string]map[string]map[string]*model.Config),
options: options,
descriptorsByCollection: descriptorsByMessageName,
eventHandlers: make(map[string][]func(model.Config, model.Event)),
synced: synced,
}
}
// ConfigDescriptor returns all the ConfigDescriptors that this
// controller is responsible for
func (c *Controller) ConfigDescriptor() model.ConfigDescriptor {
return model.IstioConfigTypes
}
// List returns all the config that is stored by type and namespace
// if namespace is empty string it returns config for all the namespaces
func (c *Controller) List(typ, namespace string) (out []model.Config, err error) {
_, ok := c.ConfigDescriptor().GetByType(typ)
if !ok {
return nil, fmt.Errorf("list unknown type %s", typ)
}
c.configStoreMu.Lock()
byType, ok := c.configStore[typ]
c.configStoreMu.Unlock()
if !ok {
return nil, nil
}
if namespace == "" {
// ByType does not need locking since
// we replace the entire sub-map
for _, byNamespace := range byType {
for _, config := range byNamespace {
out = append(out, *config)
}
}
return out, nil
}
for _, config := range byType[namespace] {
out = append(out, *config)
}
return out, nil
}
// Apply receives changes from MCP server and creates the
// corresponding config
func (c *Controller) Apply(change *sink.Change) error {
descriptor, ok := c.descriptorsByCollection[change.Collection]
if !ok {
return fmt.Errorf("apply type not supported %s", change.Collection)
}
schema, valid := c.ConfigDescriptor().GetByType(descriptor.Type)
if !valid {
return fmt.Errorf("descriptor type not supported %s", change.Collection)
}
c.syncedMu.Lock()
c.synced[change.Collection] = true
c.syncedMu.Unlock()
// innerStore is [namespace][name]
innerStore := make(map[string]map[string]*model.Config)
for _, obj := range change.Objects {
namespace, name := extractNameNamespace(obj.Metadata.Name)
createTime := time.Now()
if obj.Metadata.CreateTime != nil {
var err error
if createTime, err = types.TimestampFromProto(obj.Metadata.CreateTime); err != nil {
// Do not return an error, instead discard the resources so that Pilot can process the rest.
log.Warnf("Discarding incoming MCP resource: invalid resource timestamp (%s/%s): %v", namespace, name, err)
continue
}
}
conf := &model.Config{
ConfigMeta: model.ConfigMeta{
Type: descriptor.Type,
Group: descriptor.Group,
Version: descriptor.Version,
Name: name,
Namespace: namespace,
ResourceVersion: obj.Metadata.Version,
CreationTimestamp: createTime,
Labels: obj.Metadata.Labels,
Annotations: obj.Metadata.Annotations,
Domain: c.options.DomainSuffix,
},
Spec: obj.Body,
}
if err := schema.Validate(conf.Name, conf.Namespace, conf.Spec); err != nil {
// Do not return an error, instead discard the resources so that Pilot can process the rest.
log.Warnf("Discarding incoming MCP resource: validation failed (%s/%s): %v", conf.Namespace, conf.Name, err)
continue
}
namedConfig, ok := innerStore[conf.Namespace]
if ok {
namedConfig[conf.Name] = conf
} else {
innerStore[conf.Namespace] = map[string]*model.Config{
conf.Name: conf,
}
}
}
var prevStore map[string]map[string]*model.Config
c.configStoreMu.Lock()
prevStore = c.configStore[descriptor.Type]
c.configStore[descriptor.Type] = innerStore
c.configStoreMu.Unlock()
if descriptor.Type == model.ServiceEntry.Type {
c.serviceEntryEvents(innerStore, prevStore)
} else {
c.options.ClearDiscoveryServerCache()
}
return nil
}
// HasSynced returns true if the first batch of items has been popped
func (c *Controller) HasSynced() bool {
var notReady []string
c.syncedMu.Lock()
for messageName, synced := range c.synced {
if !synced {
notReady = append(notReady, messageName)
}
}
c.syncedMu.Unlock()
if len(notReady) > 0 {
log.Infof("Configuration not synced: first push for %v not received", notReady)
return false
}
return true
}
// RegisterEventHandler registers a handler using the type as a key
func (c *Controller) RegisterEventHandler(typ string, handler func(model.Config, model.Event)) {
c.eventHandlers[typ] = append(c.eventHandlers[typ], handler)
}
// Run is not implemented
func (c *Controller) Run(stop <-chan struct{}) {
log.Warnf("Run: %s", errUnsupported)
}
// Get is not implemented
func (c *Controller) Get(typ, name, namespace string) *model.Config {
log.Warnf("get %s", errUnsupported)
return nil
}
// Update is not implemented
func (c *Controller) Update(config model.Config) (newRevision string, err error) {
log.Warnf("update %s", errUnsupported)
return "", errUnsupported
}
// Create is not implemented
func (c *Controller) Create(config model.Config) (revision string, err error) {
log.Warnf("create %s", errUnsupported)
return "", errUnsupported
}
// Delete is not implemented
func (c *Controller) Delete(typ, name, namespace string) error {
return errUnsupported
}
func (c *Controller) serviceEntryEvents(currentStore, prevStore map[string]map[string]*model.Config) {
dispatch := func(model model.Config, event model.Event) {}
if handlers, ok := c.eventHandlers[model.ServiceEntry.Type]; ok {
dispatch = func(model model.Config, event model.Event) {
log.Debugf("MCP event dispatch: key=%v event=%v", model.Key(), event.String())
for _, handler := range handlers {
handler(model, event)
}
}
}
// add/update
for namespace, byName := range currentStore {
for name, config := range byName {
if prevByNamespace, ok := prevStore[namespace]; ok {
if prevConfig, ok := prevByNamespace[name]; ok {
if config.ResourceVersion != prevConfig.ResourceVersion {
dispatch(*config, model.EventUpdate)
}
} else {
dispatch(*config, model.EventAdd)
}
} else {
dispatch(*config, model.EventAdd)
}
}
}
// remove
for namespace, prevByName := range prevStore {
for name, prevConfig := range prevByName {
if byNamespace, ok := currentStore[namespace]; ok {
if _, ok := byNamespace[name]; !ok {
dispatch(*prevConfig, model.EventDelete)
}
} else {
dispatch(*prevConfig, model.EventDelete)
}
}
}
}
func extractNameNamespace(metadataName string) (string, string) {
segments := strings.Split(metadataName, "/")
if len(segments) == 2 {
return segments[0], segments[1]
}
return "", segments[0]
}