forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
56 lines (46 loc) · 1.31 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
package catalog
import (
"context"
"path"
"time"
"github.com/rancher/rancher/pkg/catalog/manager"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
)
func Register(ctx context.Context, management *config.ManagementContext) {
// TODO: Get values from settings
if err := Run(ctx, "", 3600, management); err != nil {
panic(err)
}
}
func runRefresh(ctx context.Context, interval int, controller v3.CatalogController, m *manager.Manager) {
ticker := time.NewTicker(time.Duration(interval) * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
break
case <-ticker.C:
catalogs, err := m.GetCatalogs()
if err != nil {
logrus.Error(err)
continue
}
for _, catalog := range catalogs {
controller.Enqueue("", catalog.Name)
}
}
}
}
func Run(ctx context.Context, cacheRoot string, refreshInterval int, management *config.ManagementContext) error {
if cacheRoot == "" {
cacheRoot = path.Join("./management-state", "catalog-controller", "cache")
}
logrus.Infof("Starting catalog controller")
m := manager.New(management, cacheRoot)
controller := management.Management.Catalogs("").Controller()
controller.AddHandler("catalog", m.Sync)
go runRefresh(ctx, refreshInterval, controller, m)
return nil
}