代码拉取完成,页面将自动刷新
package clusterevents
import (
"context"
"time"
"github.com/rancher/rancher/pkg/cluster/utils"
"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 utils.TickerContext(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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。