-
Notifications
You must be signed in to change notification settings - Fork 13
/
discoverycache.go
289 lines (237 loc) · 7.14 KB
/
discoverycache.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
package agent
import (
"fmt"
"sync"
"github.com/Axway/agent-sdk/pkg/agent/handler"
"github.com/Axway/agent-sdk/pkg/migrate"
"github.com/Axway/agent-sdk/pkg/watchmanager/proto"
apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/util/log"
)
const (
apiServerPageSize = 100
)
type discoveryCache struct {
centralURL string
migrator migrate.Migrator
logger log.FieldLogger
handlers []handler.Handler
isMpEnabled bool
client resourceClient
additionalDiscoveryFuncs []discoverFunc
watchTopic *management.WatchTopic
}
type resourceClient interface {
GetAPIV1ResourceInstancesWithPageSize(query map[string]string, URL string, pageSize int) ([]*apiv1.ResourceInstance, error)
}
// discoverFunc is the func definition for discovering resources to cache
type discoverFunc func() error
// discoveryOpt is a func that updates fields on the discoveryCache
type discoveryOpt func(dc *discoveryCache)
func withAdditionalDiscoverFuncs(funcs ...discoverFunc) discoveryOpt {
return func(dc *discoveryCache) {
dc.additionalDiscoveryFuncs = append(dc.additionalDiscoveryFuncs, funcs...)
}
}
func withMigration(mig migrate.Migrator) discoveryOpt {
return func(dc *discoveryCache) {
dc.migrator = mig
}
}
func withMpEnabled(isEnabled bool) discoveryOpt {
return func(dc *discoveryCache) {
dc.isMpEnabled = isEnabled
}
}
func newDiscoveryCache(
cfg config.CentralConfig,
client resourceClient,
handlers []handler.Handler,
watchTopic *management.WatchTopic,
opts ...discoveryOpt,
) *discoveryCache {
logger := log.NewFieldLogger().
WithPackage("sdk.agent").
WithComponent("discoveryCache")
dc := &discoveryCache{
logger: logger,
handlers: handlers,
centralURL: cfg.GetURL(),
client: client,
additionalDiscoveryFuncs: make([]discoverFunc, 0),
watchTopic: watchTopic,
}
for _, opt := range opts {
opt(dc)
}
return dc
}
// execute rebuilds the discovery cache
func (dc *discoveryCache) execute() error {
dc.logger.Debug("executing discovery cache")
discoveryFuncs := dc.buildDiscoveryFuncs()
if dc.additionalDiscoveryFuncs != nil {
discoveryFuncs = append(discoveryFuncs, dc.additionalDiscoveryFuncs...)
}
err := dc.executeDiscoveryFuncs(discoveryFuncs)
if err != nil {
return err
}
// Now do the marketplace discovery funcs as the other functions have completed
// AccessRequest cache need the APIServiceInstance cache to be fully loaded.
if dc.isMpEnabled {
marketplaceDiscoveryFuncs := dc.buildMarketplaceDiscoveryFuncs()
err := dc.executeDiscoveryFuncs(marketplaceDiscoveryFuncs)
if err != nil {
return err
}
}
dc.logger.Debug("cache has been updated")
return nil
}
func (dc *discoveryCache) executeDiscoveryFuncs(discoveryFuncs []discoverFunc) error {
errCh := make(chan error, len(discoveryFuncs))
wg := &sync.WaitGroup{}
for _, fun := range discoveryFuncs {
wg.Add(1)
go func(f func() error) {
defer wg.Done()
err := f()
errCh <- err
}(fun)
}
wg.Wait()
close(errCh)
for e := range errCh {
if e != nil {
return e
}
}
return nil
}
func (dc *discoveryCache) buildDiscoveryFuncs() []discoverFunc {
resources := make(map[string]discoverFunc)
for _, filter := range dc.watchTopic.Spec.Filters {
dc.logger.Debugf("adding function %s to be executed", filter.Kind)
f := dc.buildResourceFunc(filter)
if !isMPResource(filter.Kind) {
resources[filter.Kind] = f
}
}
var funcs []discoverFunc
for _, f := range resources {
funcs = append(funcs, f)
}
return funcs
}
func (dc *discoveryCache) buildMarketplaceDiscoveryFuncs() []discoverFunc {
mpResources := make(map[string]discoverFunc)
for _, filter := range dc.watchTopic.Spec.Filters {
if isMPResource(filter.Kind) {
f := dc.buildResourceFunc(filter)
mpResources[filter.Kind] = f
}
}
var funcs []discoverFunc
marketplaceFuncs := dc.buildMarketplaceFuncs(mpResources)
funcs = append(funcs, dc.handleMarketplaceFuncs(marketplaceFuncs))
return funcs
}
func (dc *discoveryCache) buildMarketplaceFuncs(mpResources map[string]discoverFunc) []discoverFunc {
var marketplaceFuncs []discoverFunc
mApps, ok := mpResources[management.ManagedApplicationGVK().Kind]
if ok {
marketplaceFuncs = append(marketplaceFuncs, mApps)
}
accessReq, ok := mpResources[management.AccessRequestGVK().Kind]
if ok {
marketplaceFuncs = append(marketplaceFuncs, accessReq)
}
creds, ok := mpResources[management.CredentialGVK().Kind]
if ok {
marketplaceFuncs = append(marketplaceFuncs, creds)
}
return marketplaceFuncs
}
func (dc *discoveryCache) handleMarketplaceFuncs(marketplaceFuncs []discoverFunc) discoverFunc {
return func() error {
for _, f := range marketplaceFuncs {
if err := f(); err != nil {
return err
}
}
return nil
}
}
func (dc *discoveryCache) buildResourceFunc(filter management.WatchTopicSpecFilters) discoverFunc {
return func() error {
url := fmt.Sprintf("/%s/v1alpha1", filter.Group)
if filter.Scope != nil {
scopePlural, _ := apiv1.GetPluralFromKind(filter.Scope.Kind)
url = fmt.Sprintf("%s/%s/%s", url, scopePlural, filter.Scope.Name)
}
var kindPlural, _ = apiv1.GetPluralFromKind(filter.Kind)
url = fmt.Sprintf("%s/%s", url, kindPlural)
logger := dc.logger.WithField("kind", filter.Kind)
logger.Tracef("fetching %s and updating cache", filter.Kind)
url = dc.formatResourceURL(url)
resources, err := dc.client.GetAPIV1ResourceInstancesWithPageSize(nil, url, apiServerPageSize)
if err != nil {
return fmt.Errorf("failed to fetch resources of kind %s: %s", filter.Kind, err)
}
return dc.handleResourcesList(resources)
}
}
func (dc *discoveryCache) handleResourcesList(list []*apiv1.ResourceInstance) error {
for _, ri := range list {
if dc.migrator != nil {
var err error
ri, err = dc.migrator.Migrate(ri)
if err != nil {
dc.logger.WithError(err).Error("failed to migrate resource")
}
}
action := getAction(ri.Metadata.State)
if err := dc.handleResource(ri, action); err != nil {
dc.logger.
WithError(err).
WithField("kind", ri.Kind).
WithField("name", ri.Name).
Error("failed to handle resource")
}
}
return nil
}
func (dc *discoveryCache) handleResource(ri *apiv1.ResourceInstance, action proto.Event_Type) error {
ctx := handler.NewEventContext(action, nil, ri.Name, ri.Kind)
for _, h := range dc.handlers {
err := h.Handle(ctx, nil, ri)
if err != nil {
return err
}
}
return nil
}
func (dc *discoveryCache) formatResourceURL(s string) string {
return fmt.Sprintf("%s/apis%s", dc.centralURL, s)
}
func getAction(state string) proto.Event_Type {
if state == apiv1.ResourceDeleting {
return proto.Event_UPDATED
}
return proto.Event_CREATED
}
func isMPResource(kind string) bool {
switch kind {
case management.ManagedApplicationGVK().Kind:
return true
case management.AccessRequestGVK().Kind:
return true
case management.CredentialGVK().Kind:
return true
default:
return false
}
}