-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
105 lines (89 loc) · 3.14 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
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
package catalog
import (
"context"
"os"
"path"
"time"
"github.com/rancher/rancher/pkg/catalog/manager"
"github.com/rancher/rancher/pkg/ticker"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/labels"
)
func Register(ctx context.Context, management *config.ManagementContext) {
// remove old disk cache
oldCache := path.Join("management-state", "catalog-controller")
os.RemoveAll(oldCache)
// TODO: Get values from settings
if err := Run(ctx, 3600, management); err != nil {
panic(err)
}
}
func runRefreshCatalog(ctx context.Context, interval int, controller v3.CatalogController, m *manager.Manager) {
for range ticker.Context(ctx, time.Duration(interval)*time.Second) {
catalogs, err := m.CatalogLister.List("", labels.NewSelector())
if err != nil {
logrus.Error(err)
continue
}
for _, catalog := range catalogs {
controller.Enqueue("", catalog.Name)
}
}
}
func runRefreshProjectCatalog(ctx context.Context, interval int, controller v3.ProjectCatalogController, m *manager.Manager) {
for range ticker.Context(ctx, time.Duration(interval)*time.Second) {
projectCatalogs, err := m.ProjectCatalogLister.List("", labels.NewSelector())
if err != nil {
logrus.Error(err)
continue
}
for _, pc := range projectCatalogs {
controller.Enqueue(pc.Namespace, pc.Name)
}
}
}
func runRefreshClusterCatalog(ctx context.Context, interval int, controller v3.ClusterCatalogController, m *manager.Manager) {
for range ticker.Context(ctx, time.Duration(interval)*time.Second) {
clusterCatalogs, err := m.ClusterCatalogLister.List("", labels.NewSelector())
if err != nil {
logrus.Error(err)
continue
}
for _, cc := range clusterCatalogs {
controller.Enqueue(cc.Namespace, cc.Name)
}
}
}
func doUntilSucceeds(ctx context.Context, retryPeriod time.Duration, f func() bool) {
for {
if f() {
return
}
select {
case <-time.After(retryPeriod):
case <-ctx.Done():
return
}
}
}
func Run(ctx context.Context, refreshInterval int, management *config.ManagementContext) error {
logrus.Infof("Starting catalog controller")
m := manager.New(management)
controller := management.Management.Catalogs("").Controller()
controller.AddHandler(ctx, "catalog", m.Sync)
logrus.Infof("Starting project-level catalog controller")
projectCatalogController := management.Management.ProjectCatalogs("").Controller()
projectCatalogController.AddHandler(ctx, "projectCatalog", m.ProjectCatalogSync)
logrus.Infof("Starting cluster-level catalog controller")
clusterCatalogController := management.Management.ClusterCatalogs("").Controller()
clusterCatalogController.AddHandler(ctx, "clusterCatalog", m.ClusterCatalogSync)
var failureRetryPeriod = 15 * time.Minute
go doUntilSucceeds(ctx, failureRetryPeriod, m.DeleteOldTemplateContent)
go doUntilSucceeds(ctx, failureRetryPeriod, m.DeleteBadCatalogTemplates)
go runRefreshCatalog(ctx, refreshInterval, controller, m)
go runRefreshProjectCatalog(ctx, refreshInterval, projectCatalogController, m)
go runRefreshClusterCatalog(ctx, refreshInterval, clusterCatalogController, m)
return nil
}