/
config.go
157 lines (137 loc) · 4.02 KB
/
config.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
// 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 aggregate implements a read-only aggregator for config stores.
package aggregate
import (
"errors"
"fmt"
multierror "github.com/hashicorp/go-multierror"
"istio.io/istio/pilot/pkg/model"
)
var errorUnsupported = errors.New("unsupported operation: the config aggregator is read-only")
// Make creates an aggregate config store from several config stores and
// unifies their descriptors
func Make(stores []model.ConfigStore) (model.ConfigStore, error) {
union := model.ConfigDescriptor{}
storeTypes := make(map[string][]model.ConfigStore)
for _, store := range stores {
for _, descriptor := range store.ConfigDescriptor() {
if len(storeTypes[descriptor.Type]) == 0 {
union = append(union, descriptor)
}
storeTypes[descriptor.Type] = append(storeTypes[descriptor.Type], store)
}
}
if err := union.Validate(); err != nil {
return nil, err
}
return &store{
descriptor: union,
stores: storeTypes,
}, nil
}
// MakeCache creates an aggregate config store cache from several config store
// caches.
func MakeCache(caches []model.ConfigStoreCache) (model.ConfigStoreCache, error) {
stores := make([]model.ConfigStore, 0, len(caches))
for _, cache := range caches {
stores = append(stores, cache)
}
store, err := Make(stores)
if err != nil {
return nil, err
}
return &storeCache{
ConfigStore: store,
caches: caches,
}, nil
}
type store struct {
// descriptor is the unified
descriptor model.ConfigDescriptor
// stores is a mapping from config type to a store
stores map[string][]model.ConfigStore
}
func (cr *store) ConfigDescriptor() model.ConfigDescriptor {
return cr.descriptor
}
// Get the first config found in the stores.
func (cr *store) Get(typ, name, namespace string) *model.Config {
for _, store := range cr.stores[typ] {
config := store.Get(typ, name, namespace)
if config != nil {
return config
}
}
return nil
}
// List all configs in the stores.
func (cr *store) List(typ, namespace string) ([]model.Config, error) {
if len(cr.stores[typ]) == 0 {
return nil, fmt.Errorf("missing type %q", typ)
}
var errs *multierror.Error
var configs []model.Config
// Used to remove duplicated config
configMap := make(map[string]struct{})
for _, store := range cr.stores[typ] {
storeConfigs, err := store.List(typ, namespace)
if err != nil {
errs = multierror.Append(errs, err)
}
for _, config := range storeConfigs {
key := config.Type + config.Namespace + config.Name
if _, exist := configMap[key]; exist {
continue
}
configs = append(configs, config)
configMap[key] = struct{}{}
}
}
return configs, errs.ErrorOrNil()
}
func (cr *store) Delete(typ, name, namespace string) error {
return errorUnsupported
}
func (cr *store) Create(config model.Config) (string, error) {
return "", errorUnsupported
}
func (cr *store) Update(config model.Config) (string, error) {
return "", errorUnsupported
}
type storeCache struct {
model.ConfigStore
caches []model.ConfigStoreCache
}
func (cr *storeCache) HasSynced() bool {
for _, cache := range cr.caches {
if !cache.HasSynced() {
return false
}
}
return true
}
func (cr *storeCache) RegisterEventHandler(typ string, handler func(model.Config, model.Event)) {
for _, cache := range cr.caches {
if _, exists := cache.ConfigDescriptor().GetByType(typ); exists {
cache.RegisterEventHandler(typ, handler)
}
}
}
func (cr *storeCache) Run(stop <-chan struct{}) {
for _, cache := range cr.caches {
go cache.Run(stop)
}
<-stop
}