37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
controller.go 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
Erik Wilson 提交于 2018-11-06 18:04 . Store templateContent cache on disk
package catalog
import (
"context"
"os"
"path"
"time"
"github.com/rancher/rancher/pkg/catalog/manager"
"github.com/rancher/rancher/pkg/ticker"
"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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-rc7

搜索帮助

0d507c66 1850385 C8b1a773 1850385