37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
clustereventscleanup.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
package clusterevents
import (
"context"
"time"
"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/labels"
)
const (
syncInterval = 1 * time.Hour
TTL = 24 * time.Hour
)
type Cleaner struct {
clusterEvents v3.ClusterEventInterface
clusterEventsLister v3.ClusterEventLister
}
func Register(ctx context.Context, management *config.ManagementContext) {
c := &Cleaner{
clusterEvents: management.Management.ClusterEvents(""),
clusterEventsLister: management.Management.ClusterEvents("").Controller().Lister(),
}
go c.sync(ctx, syncInterval)
}
func (c *Cleaner) sync(ctx context.Context, syncInterval time.Duration) {
for range ticker.Context(ctx, syncInterval) {
err := c.cleanup()
if err != nil {
logrus.Errorf("Error running cluster events cleanup thread %v", err)
}
}
}
func (c *Cleaner) cleanup() error {
logrus.Infof("Running cluster events cleanup")
events, err := c.clusterEventsLister.List("", labels.NewSelector())
if err != nil {
return err
}
for _, event := range events {
created := event.CreationTimestamp.Time
if time.Now().Sub(created) >= TTL {
logrus.Debugf("Cleaninig up cluster event %s", event.Message)
err := c.clusterEvents.Delete(event.Name, &metav1.DeleteOptions{})
if err != nil {
// just log the error, retry will happen as a part of the next run
logrus.Errorf("Error deleting cluster event %s: %v", event.Message, err)
}
}
}
logrus.Infof("Done running cluster events cleanup")
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.6

搜索帮助