/
cache.go
366 lines (325 loc) · 9.27 KB
/
cache.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
/*
Copyright 2021 The KubeVela 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 addon
import (
"context"
"fmt"
"strings"
"sync"
"time"
"k8s.io/klog/v2"
"github.com/oam-dev/kubevela/pkg/utils"
"github.com/oam-dev/kubevela/pkg/utils/common"
)
// We have three addon layer here
// 1. List metadata for all file path of one registry
// 2. UIData: Read file content that including README.md and other necessary things being used in UI apiserver
// 3. InstallPackage: All file content that used to be installation
// Cache package only cache for 1 and 2, we don't cache InstallPackage, and it only read for real installation
type Cache struct {
// uiData caches all the decoded UIData addons
// the key in the map is the registry name
uiData map[string][]*UIData
// registryMeta caches the addon metadata of every registry
// the key in the map is the registry name
registryMeta map[string]map[string]SourceMeta
registry map[string]Registry
versionedUIData map[string]map[string]*UIData
mutex *sync.RWMutex
ds RegistryDataStore
}
// NewCache will build a new cache instance
func NewCache(ds RegistryDataStore) *Cache {
return &Cache{
uiData: make(map[string][]*UIData),
registryMeta: make(map[string]map[string]SourceMeta),
registry: make(map[string]Registry),
versionedUIData: make(map[string]map[string]*UIData),
mutex: new(sync.RWMutex),
ds: ds,
}
}
// DiscoverAndRefreshLoop will run a loop to automatically discovery and refresh addons from registry
func (u *Cache) DiscoverAndRefreshLoop(ctx context.Context, cacheTime time.Duration) {
ticker := time.NewTicker(cacheTime)
defer ticker.Stop()
// This is infinite loop, we can receive a channel for close
for {
select {
case <-ticker.C:
u.discoverAndRefreshRegistry()
case <-ctx.Done():
return
}
}
}
// ListAddonMeta will list metadata from registry, if cache not found, it will find from source
func (u *Cache) ListAddonMeta(r Registry) (map[string]SourceMeta, error) {
registryMeta := u.getCachedAddonMeta(r.Name)
if registryMeta == nil {
return r.ListAddonMeta()
}
return registryMeta, nil
}
// GetUIData get addon data for UI display from cache, if cache not found, it will find from source
func (u *Cache) GetUIData(r Registry, addonName, version string) (*UIData, error) {
addon := u.getCachedUIData(r, addonName, version)
if addon != nil {
return addon, nil
}
var err error
if !IsVersionRegistry(r) {
registryMeta, err := u.ListAddonMeta(r)
if err != nil {
return nil, err
}
meta, ok := registryMeta[addonName]
if !ok {
return nil, ErrNotExist
}
addon, err = r.GetUIData(&meta, UIMetaOptions)
if err != nil {
return nil, err
}
} else {
versionedRegistry := BuildVersionedRegistry(r.Name, r.Helm.URL, &common.HTTPOption{
Username: r.Helm.Username,
Password: r.Helm.Password,
InsecureSkipTLS: r.Helm.InsecureSkipTLS,
})
addon, err = versionedRegistry.GetAddonUIData(context.Background(), addonName, version)
if err != nil {
klog.Errorf("fail to get addons from registry %s for cache updating, %v", utils.Sanitize(r.Name), err)
return nil, err
}
}
return addon, nil
}
// ListUIData will always list UIData from cache first, if not exist, read from source.
func (u *Cache) ListUIData(r Registry) ([]*UIData, error) {
var err error
var listAddons []*UIData
if !IsVersionRegistry(r) {
listAddons = u.listCachedUIData(r.Name)
if listAddons != nil {
return listAddons, nil
}
listAddons, err = u.listUIDataAndCache(r)
if err != nil {
return nil, err
}
} else {
listAddons = u.listVersionRegistryCachedUIData(r.Name)
if listAddons != nil {
return listAddons, nil
}
listAddons, err = u.listVersionRegistryUIDataAndCache(r)
if err != nil {
return nil, err
}
}
return listAddons, nil
}
func (u *Cache) getCachedUIData(registry Registry, addonName, version string) *UIData {
if !IsVersionRegistry(registry) {
addons := u.listCachedUIData(registry.Name)
for _, a := range addons {
if a.Name == addonName {
return a
}
}
} else {
if len(version) == 0 {
version = "latest"
}
return u.versionedUIData[registry.Name][fmt.Sprintf("%s-%s", addonName, version)]
}
return nil
}
// listCachedUIData will get cached addons from specified registry in cache
func (u *Cache) listCachedUIData(name string) []*UIData {
if u == nil {
return nil
}
u.mutex.RLock()
defer u.mutex.RUnlock()
d, ok := u.uiData[name]
if !ok {
return nil
}
return d
}
// listVersionRegistryCachedUIData will get cached addons from specified VersionRegistry in cache
func (u *Cache) listVersionRegistryCachedUIData(name string) []*UIData {
if u == nil {
return nil
}
u.mutex.RLock()
defer u.mutex.RUnlock()
d, ok := u.versionedUIData[name]
if !ok {
return nil
}
var uiDatas []*UIData
for version, uiData := range d {
if !strings.Contains(version, "-latest") {
uiDatas = append(uiDatas, uiData)
}
}
return uiDatas
}
// getCachedAddonMeta will get cached registry meta from specified registry in cache
func (u *Cache) getCachedAddonMeta(name string) map[string]SourceMeta {
if u == nil {
return nil
}
u.mutex.RLock()
defer u.mutex.RUnlock()
d, ok := u.registryMeta[name]
if !ok {
return nil
}
return d
}
func (u *Cache) putAddonUIData2Cache(name string, addons []*UIData) {
if u == nil {
return
}
u.mutex.Lock()
defer u.mutex.Unlock()
u.uiData[name] = addons
}
func (u *Cache) putAddonMeta2Cache(name string, addonMeta map[string]SourceMeta) {
if u == nil {
return
}
u.mutex.Lock()
defer u.mutex.Unlock()
u.registryMeta[name] = addonMeta
}
func (u *Cache) putRegistry2Cache(registry []Registry) {
if u == nil {
return
}
u.mutex.Lock()
defer u.mutex.Unlock()
for k := range u.registry {
var found = false
for _, r := range registry {
if r.Name == k {
found = true
break
}
}
// clean deleted registry
if !found {
delete(u.registry, k)
delete(u.registryMeta, k)
delete(u.uiData, k)
}
}
for _, r := range registry {
u.registry[r.Name] = r
}
}
func (u *Cache) putVersionedUIData2Cache(registryName, addonName, version string, uiData *UIData) {
if u == nil {
return
}
u.mutex.Lock()
defer u.mutex.Unlock()
if u.versionedUIData[registryName] == nil {
u.versionedUIData[registryName] = make(map[string]*UIData)
}
u.versionedUIData[registryName][fmt.Sprintf("%s-%s", addonName, version)] = uiData
}
func (u *Cache) discoverAndRefreshRegistry() {
registries, err := u.ds.ListRegistries(context.Background())
if err != nil {
klog.Errorf("fail to get registry %v", err)
return
}
u.putRegistry2Cache(registries)
for _, r := range registries {
if !IsVersionRegistry(r) {
_, err = u.listUIDataAndCache(r)
if err != nil {
continue
}
} else {
_, err = u.listVersionRegistryUIDataAndCache(r)
if err != nil {
continue
}
}
}
}
func (u *Cache) listUIDataAndCache(r Registry) ([]*UIData, error) {
registryMeta, err := r.ListAddonMeta()
if err != nil {
klog.Errorf("fail to list registry %s metadata, %v", r.Name, err)
return nil, err
}
u.putAddonMeta2Cache(r.Name, registryMeta)
uiData, err := r.ListUIData(registryMeta, UIMetaOptions)
if err != nil {
klog.Errorf("fail to get addons from registry %s for cache updating, %v", r.Name, err)
return nil, err
}
u.putAddonUIData2Cache(r.Name, uiData)
return uiData, nil
}
func (u *Cache) listVersionRegistryUIDataAndCache(r Registry) ([]*UIData, error) {
versionedRegistry := BuildVersionedRegistry(r.Name, r.Helm.URL, &common.HTTPOption{
Username: r.Helm.Username,
Password: r.Helm.Password,
InsecureSkipTLS: r.Helm.InsecureSkipTLS,
})
uiDatas, err := versionedRegistry.ListAddon()
if err != nil {
klog.Errorf("fail to get addons from registry %s for cache updating, %v", r.Name, err)
return nil, err
}
for _, addon := range uiDatas {
uiData, err := versionedRegistry.GetAddonUIData(context.Background(), addon.Name, addon.Version)
if err != nil {
klog.Errorf("fail to get addon from versioned registry %s, addon %s version %s for cache updating, %v", r.Name, addon.Name, addon.Version, err)
continue
}
// identity an addon from helm chart structure
if uiData.Name == "" {
addon.Name = ""
continue
}
u.putVersionedUIData2Cache(r.Name, addon.Name, addon.Version, uiData)
// we also no version key, if use get addonUIData without version will return this vale as latest data.
u.putVersionedUIData2Cache(r.Name, addon.Name, "latest", uiData)
}
// delete the addon which has been deleted from the addonRegistryCache
if addonUIData, ok := u.versionedUIData[r.Name]; ok {
for k := range addonUIData {
lastInd := strings.LastIndex(k, "-")
var needDelete = true
for _, addon := range uiDatas {
if k[:lastInd] == addon.Name {
needDelete = false
break
}
}
if needDelete {
delete(addonUIData, k)
}
}
}
return uiDatas, nil
}