forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.go
144 lines (125 loc) · 4.12 KB
/
catalog.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
package catalog
import (
"context"
"os"
"path/filepath"
"strings"
"time"
"github.com/bep/debounce"
helmlib "github.com/rancher/rancher/pkg/catalog/helm"
"github.com/rancher/rancher/pkg/ticker"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
type CacheCleaner struct {
catalogClient v3.CatalogInterface
projectCatalogClient v3.ProjectCatalogInterface
clusterCatalogClient v3.ClusterCatalogInterface
debounce func(func())
}
func Register(ctx context.Context, context *config.ScaledContext) {
cleaner := &CacheCleaner{
catalogClient: context.Management.Catalogs(""),
projectCatalogClient: context.Management.ProjectCatalogs(""),
clusterCatalogClient: context.Management.ClusterCatalogs(""),
debounce: debounce.New(time.Minute),
}
go cleaner.runPeriodicCatalogCacheCleaner(ctx, time.Hour)
context.Management.Catalogs("").Controller().AddHandler(ctx, "catalogCache", cleaner.destroyCatalogSync)
context.Management.ClusterCatalogs("").Controller().AddHandler(ctx, "clusterCatalogCache", cleaner.destroyClusterCatalogSync)
context.Management.ProjectCatalogs("").Controller().AddHandler(ctx, "projectCatalogCache", cleaner.destroyProjectCatalogSync)
}
func (c *CacheCleaner) runPeriodicCatalogCacheCleaner(ctx context.Context, interval time.Duration) {
c.GoCleanupLogError()
for range ticker.Context(ctx, interval) {
c.GoCleanupLogError()
}
}
func (c *CacheCleaner) destroyCatalogSync(key string, obj *v3.Catalog) (runtime.Object, error) {
c.debounce(c.GoCleanupLogError)
return nil, nil
}
func (c *CacheCleaner) destroyClusterCatalogSync(key string, obj *v3.ClusterCatalog) (runtime.Object, error) {
c.debounce(c.GoCleanupLogError)
return nil, nil
}
func (c *CacheCleaner) destroyProjectCatalogSync(key string, obj *v3.ProjectCatalog) (runtime.Object, error) {
c.debounce(c.GoCleanupLogError)
return nil, nil
}
func (c *CacheCleaner) GoCleanupLogError() {
go func() {
if err := c.Cleanup(); err != nil {
logrus.Errorf("Catalog-cache cleanup error: %s", err)
}
}()
}
func (c *CacheCleaner) Cleanup() error {
logrus.Debug("Catalog-cache running cleanup")
catalogCacheFiles, err := readDirNames(helmlib.CatalogCache)
if err != nil && !os.IsNotExist(err) {
return err
}
iconCacheFiles, err := readDirNames(helmlib.IconCache)
if err != nil && !os.IsNotExist(err) {
return err
}
if len(catalogCacheFiles) == 0 && len(iconCacheFiles) == 0 {
return nil
}
var catalogHashes = map[string]bool{}
catalogs, err := c.catalogClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, catalog := range catalogs.Items {
catalogHashes[helmlib.CatalogSHA256Hash(&catalog)] = true
}
clusterCatalogs, err := c.clusterCatalogClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, clusterCatalog := range clusterCatalogs.Items {
catalogHashes[helmlib.CatalogSHA256Hash(&clusterCatalog.Catalog)] = true
}
projectCatalogs, err := c.projectCatalogClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, projectCatalog := range projectCatalogs.Items {
catalogHashes[helmlib.CatalogSHA256Hash(&projectCatalog.Catalog)] = true
}
var cleanupCount int
cleanupCount += cleanupPath(helmlib.CatalogCache, catalogCacheFiles, catalogHashes)
cleanupCount += cleanupPath(helmlib.IconCache, iconCacheFiles, catalogHashes)
if cleanupCount > 0 {
logrus.Infof("Catalog-cache removed %d entries from disk", cleanupCount)
}
return nil
}
func readDirNames(dir string) ([]string, error) {
pathFile, err := os.Open(dir)
defer pathFile.Close()
if err != nil {
return nil, err
}
return pathFile.Readdirnames(0)
}
func cleanupPath(dir string, files []string, valid map[string]bool) int {
var count int
for _, file := range files {
if valid[file] || strings.HasPrefix(file, ".") {
continue
}
dirFile := filepath.Join(dir, file)
helmlib.Locker.Lock(file)
os.RemoveAll(dirFile)
helmlib.Locker.Unlock(file)
count++
logrus.Debugf("Catalog-cache removed entry from disk: %s", dirFile)
}
return count
}