-
Notifications
You must be signed in to change notification settings - Fork 0
/
purge_daemon.go
52 lines (44 loc) · 1.29 KB
/
purge_daemon.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
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)
}
}