37 Star 403 Fork 75

GVPrancher/rancher

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
purge_daemon.go 1.29 KB
Copy Edit Raw Blame History
Dan Ramich authored 2019-06-12 13:38 . goimport linting changes
package tokens
import (
"context"
"time"
"github.com/rancher/norman/clientbase"
v3 "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"
"k8s.io/apimachinery/pkg/util/wait"
)
const intervalSeconds int64 = 3600
func StartPurgeDaemon(ctx context.Context, mgmt *config.ManagementContext) {
p := &purger{
tokenLister: mgmt.Management.Tokens("").Controller().Lister(),
tokens: mgmt.Management.Tokens(""),
}
go wait.JitterUntil(p.purge, time.Duration(intervalSeconds)*time.Second, .1, true, ctx.Done())
}
type purger struct {
tokenLister v3.TokenLister
tokens v3.TokenInterface
}
func (p *purger) purge() {
allTokens, err := p.tokenLister.List("", labels.Everything())
if err != nil {
logrus.Errorf("Error listing tokens during purge: %v", err)
}
var count int
for _, token := range allTokens {
if IsExpired(*token) {
err = p.tokens.Delete(token.ObjectMeta.Name, &metav1.DeleteOptions{})
if err != nil && !clientbase.IsNotFound(err) {
logrus.Errorf("Error: while deleting expired token %v: %v", err, token.ObjectMeta.Name)
continue
}
count++
}
}
if count > 0 {
logrus.Infof("Purged %v expired tokens", count)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.13

Search