-
Notifications
You must be signed in to change notification settings - Fork 216
/
controller.go
129 lines (108 loc) · 3.54 KB
/
controller.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Package content purges orphaned content objects by inspecting bundledeployments in all namespaces. Runs every 5 minutes. (fleetcontroller)
package content
import (
"context"
"github.com/sirupsen/logrus"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/durations"
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/kv"
"github.com/rancher/wrangler/pkg/ticker"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type handler struct {
content fleetcontrollers.ContentController
bundleDeployment fleetcontrollers.BundleDeploymentController
namespaces corecontrollers.NamespaceClient
}
type contentRef struct {
safeToDelete bool
markForDeletion bool
bundleCount int
}
func Register(ctx context.Context,
content fleetcontrollers.ContentController,
bundleDeployment fleetcontrollers.BundleDeploymentController,
namespaces corecontrollers.NamespaceController) {
h := &handler{
content: content,
bundleDeployment: bundleDeployment,
namespaces: namespaces,
}
go func() {
h.purgeOrphaned(ctx)
}()
}
func (h *handler) purgeOrphaned(ctx context.Context) {
deleteRefs := make(map[string]*contentRef)
for range ticker.Context(ctx, durations.ContentPurgeInterval) {
logrus.Debugf("Checking for orphaned content objects")
namespaces, err := h.namespaces.List(metav1.ListOptions{})
if err != nil {
logrus.Warnf("Error reading namespaces %v", err)
continue
}
var bundleDeployments []fleet.BundleDeployment
for _, ns := range namespaces.Items {
nsBundleDeployments, err := h.bundleDeployment.List(ns.Name, metav1.ListOptions{})
if err != nil {
logrus.Warnf("Error listing bundle deployments %v", err)
continue
}
bundleDeployments = append(bundleDeployments, nsBundleDeployments.Items...)
}
contentRefs := make(map[string]*contentRef)
contents, err := h.content.List(metav1.ListOptions{})
if err != nil {
logrus.Warnf("Error reading contents %v", err)
continue
}
for _, content := range contents.Items {
contentRefs[content.Name] = &contentRef{
safeToDelete: false,
bundleCount: 0,
}
}
for _, bd := range bundleDeployments {
deployManifestID, _ := kv.Split(bd.Spec.DeploymentID, ":")
if val, ok := contentRefs[deployManifestID]; ok {
val.bundleCount++
}
stagedManifestID, _ := kv.Split(bd.Spec.StagedDeploymentID, ":")
if val, ok := contentRefs[stagedManifestID]; ok && stagedManifestID != deployManifestID {
val.bundleCount++
}
}
for contentName, cr := range contentRefs {
_, deleteCandidate := deleteRefs[contentName]
if cr.bundleCount > 0 {
if deleteCandidate {
delete(deleteRefs, contentName)
}
} else {
if deleteCandidate {
deleteRefs[contentName].safeToDelete = true
} else {
logrus.Infof("Marking orphaned content[%s] for deletion", contentName)
deleteRefs[contentName] = &contentRef{
bundleCount: 0,
markForDeletion: true,
safeToDelete: false,
}
}
}
}
for contentName, dr := range deleteRefs {
if dr.safeToDelete {
logrus.Infof("Deleting orphaned content[%s]", contentName)
if err := h.content.Delete(contentName, &metav1.DeleteOptions{}); err != nil && !errors.IsNotFound(err) {
logrus.Warnf("Error deleting contentbundle %v", err)
} else {
delete(deleteRefs, contentName)
}
}
}
}
}