forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_quota_reset.go
47 lines (42 loc) · 1.13 KB
/
namespace_quota_reset.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
package resourcequota
import (
"fmt"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
corev1 "k8s.io/api/core/v1"
clientcache "k8s.io/client-go/tools/cache"
)
/*
quotaResetController is responsible for resetting resource quota on the namespace
when project resource quota gets reset
*/
type quotaResetController struct {
namespaces v1.NamespaceInterface
nsIndexer clientcache.Indexer
}
func (c *quotaResetController) resetNamespaceQuota(key string, project *v3.Project) error {
if project == nil || project.DeletionTimestamp != nil {
return nil
}
if project.Spec.ResourceQuota != nil {
return nil
}
projectID := fmt.Sprintf("%s:%s", project.Namespace, project.Name)
namespaces, err := c.nsIndexer.ByIndex(nsByProjectIndex, projectID)
if err != nil {
return err
}
for _, n := range namespaces {
ns := n.(*corev1.Namespace)
quota := getNamespaceResourceQuota(ns)
if quota == "" {
continue
}
toUpdate := ns.DeepCopy()
delete(toUpdate.Annotations, resourceQuotaAnnotation)
if _, err := c.namespaces.Update(toUpdate); err != nil {
return err
}
}
return nil
}