forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_quota_cleanup.go
55 lines (49 loc) · 1.62 KB
/
resource_quota_cleanup.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
package resourcequota
import (
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
/*
cleanupController watches resource quota resource and cleans up those not created by Rancher
if the resource belongs to a project with resourceQuota set
*/
type cleanupController struct {
projectLister v3.ProjectLister
namespaceLister v1.NamespaceLister
resourceQuotas v1.ResourceQuotaInterface
resourceQuotaLister v1.ResourceQuotaLister
}
func (c *cleanupController) cleanup(key string, quota *corev1.ResourceQuota) (runtime.Object, error) {
if quota == nil || quota.DeletionTimestamp != nil {
return nil, nil
}
cleanup, err := c.needToCleanup(quota)
if err != nil || !cleanup {
return nil, err
}
return nil, c.cleanupResourcequota(key, quota)
}
func (c *cleanupController) needToCleanup(quota *corev1.ResourceQuota) (bool, error) {
if quota.Labels != nil {
if _, ok := quota.Labels[resourceQuotaLabel]; ok {
return false, nil
}
}
ns, err := c.namespaceLister.Get("", quota.Namespace)
if err != nil {
return false, err
}
projectLimit, _, err := getProjectResourceQuotaLimit(ns, c.projectLister)
if err != nil {
return false, err
}
return projectLimit != nil, nil
}
func (c *cleanupController) cleanupResourcequota(key string, quota *corev1.ResourceQuota) error {
logrus.Infof("Deleting resource quota [%s] created natively", key)
return c.resourceQuotas.DeleteNamespaced(quota.Namespace, quota.Name, &metav1.DeleteOptions{})
}