forked from yunionio/kubecomps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory_data_clean.go
More file actions
81 lines (73 loc) · 2.49 KB
/
Copy pathhistory_data_clean.go
File metadata and controls
81 lines (73 loc) · 2.49 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package models
import (
"context"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/kubecomps/pkg/kubeserver/options"
)
func collectClusterK8sManagers(man ISyncableManager, out *[]db.IHistoryDataManager) {
for _, sub := range man.GetSubManagers() {
collectClusterK8sManagers(sub, out)
}
if hM, ok := man.(db.IHistoryDataManager); ok {
*out = append(*out, hM)
}
}
// CleanK8sHistoryData hard-deletes soft-deleted rows of in-cluster K8s resources
// (ClusterManager sub-tree). Cluster/Machine/Fed* are not included.
func CleanK8sHistoryData(ctx context.Context, timeBefore time.Time) {
managers := make([]db.IHistoryDataManager, 0)
for _, man := range GetClusterManager().GetSubManagers() {
collectClusterK8sManagers(man, &managers)
}
for _, hM := range managers {
keyword := ""
if km, ok := hM.(db.IModelManager); ok {
keyword = km.Keyword()
}
start := time.Now()
cnt, err := hM.HistoryDataClean(ctx, timeBefore)
if err != nil {
log.Errorf("clean %s history data error: %v", keyword, err)
continue
}
if cnt > 0 {
log.Infof("clean %d %s history data cost %s", cnt, keyword, time.Since(start).Round(time.Second))
}
}
}
// AutoCleanK8sHistoryData is the cron entry that purges soft-deleted K8s resources
// older than options.K8sHistoryDataKeepDays.
func AutoCleanK8sHistoryData(ctx context.Context, userCred mcclient.TokenCredential, startRun bool) {
keepDays := options.Options.K8sHistoryDataKeepDays
if keepDays <= 0 {
keepDays = 30
}
timeBefore := time.Now().AddDate(0, 0, -keepDays)
log.Infof("AutoCleanK8sHistoryData start, keep_days=%d, before=%s", keepDays, timeBefore.Format(time.RFC3339))
CleanK8sHistoryData(ctx, timeBefore)
}
// PerformHistoryDataClean is the clusters class action that cleans soft-deleted
// in-cluster K8s resources older than the given keep days.
func (m *SClusterManager) PerformHistoryDataClean(
ctx context.Context,
userCred mcclient.TokenCredential,
query, data jsonutils.JSONObject,
) (jsonutils.JSONObject, error) {
keepDays := options.Options.K8sHistoryDataKeepDays
if keepDays <= 0 {
keepDays = 30
}
if !gotypes.IsNil(data) && data.Contains("day") {
day, _ := data.Int("day")
keepDays = int(day)
}
timeBefore := time.Now().AddDate(0, 0, -keepDays)
log.Infof("PerformHistoryDataClean keep_days=%d, before=%s", keepDays, timeBefore.Format(time.RFC3339))
go CleanK8sHistoryData(ctx, timeBefore)
return nil, nil
}