forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
accessor.go
165 lines (139 loc) · 6.15 KB
/
accessor.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package clusterresourcequota
import (
"time"
lru "github.com/hashicorp/golang-lru"
"k8s.io/apimachinery/pkg/api/equality"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
utilwait "k8s.io/apimachinery/pkg/util/wait"
etcd "k8s.io/apiserver/pkg/storage/etcd"
kapi "k8s.io/kubernetes/pkg/apis/core"
kcorelisters "k8s.io/kubernetes/pkg/client/listers/core/internalversion"
utilquota "k8s.io/kubernetes/pkg/quota"
quotaapi "github.com/openshift/origin/pkg/quota/apis/quota"
"github.com/openshift/origin/pkg/quota/controller/clusterquotamapping"
quotatypedclient "github.com/openshift/origin/pkg/quota/generated/internalclientset/typed/quota/internalversion"
quotalister "github.com/openshift/origin/pkg/quota/generated/listers/quota/internalversion"
)
type clusterQuotaAccessor struct {
clusterQuotaLister quotalister.ClusterResourceQuotaLister
namespaceLister kcorelisters.NamespaceLister
clusterQuotaClient quotatypedclient.ClusterResourceQuotasGetter
clusterQuotaMapper clusterquotamapping.ClusterQuotaMapper
// updatedClusterQuotas holds a cache of quotas that we've updated. This is used to pull the "really latest" during back to
// back quota evaluations that touch the same quota doc. This only works because we can compare etcd resourceVersions
// for the same resource as integers. Before this change: 22 updates with 12 conflicts. after this change: 15 updates with 0 conflicts
updatedClusterQuotas *lru.Cache
}
// newQuotaAccessor creates an object that conforms to the QuotaAccessor interface to be used to retrieve quota objects.
func newQuotaAccessor(
clusterQuotaLister quotalister.ClusterResourceQuotaLister,
namespaceLister kcorelisters.NamespaceLister,
clusterQuotaClient quotatypedclient.ClusterResourceQuotasGetter,
clusterQuotaMapper clusterquotamapping.ClusterQuotaMapper,
) *clusterQuotaAccessor {
updatedCache, err := lru.New(100)
if err != nil {
// this should never happen
panic(err)
}
return &clusterQuotaAccessor{
clusterQuotaLister: clusterQuotaLister,
namespaceLister: namespaceLister,
clusterQuotaClient: clusterQuotaClient,
clusterQuotaMapper: clusterQuotaMapper,
updatedClusterQuotas: updatedCache,
}
}
// UpdateQuotaStatus the newQuota coming in will be incremented from the original. The difference between the original
// and the new is the amount to add to the namespace total, but the total status is the used value itself
func (e *clusterQuotaAccessor) UpdateQuotaStatus(newQuota *kapi.ResourceQuota) error {
clusterQuota, err := e.clusterQuotaLister.Get(newQuota.Name)
if err != nil {
return err
}
clusterQuota = e.checkCache(clusterQuota)
// re-assign objectmeta
// make a copy
clusterQuota = clusterQuota.DeepCopy()
clusterQuota.ObjectMeta = newQuota.ObjectMeta
clusterQuota.Namespace = ""
// determine change in usage
usageDiff := utilquota.Subtract(newQuota.Status.Used, clusterQuota.Status.Total.Used)
// update aggregate usage
clusterQuota.Status.Total.Used = newQuota.Status.Used
// update per namespace totals
oldNamespaceTotals, _ := clusterQuota.Status.Namespaces.Get(newQuota.Namespace)
namespaceTotalCopy := oldNamespaceTotals.DeepCopy()
newNamespaceTotals := *namespaceTotalCopy
newNamespaceTotals.Used = utilquota.Add(oldNamespaceTotals.Used, usageDiff)
clusterQuota.Status.Namespaces.Insert(newQuota.Namespace, newNamespaceTotals)
updatedQuota, err := e.clusterQuotaClient.ClusterResourceQuotas().UpdateStatus(clusterQuota)
if err != nil {
return err
}
e.updatedClusterQuotas.Add(clusterQuota.Name, updatedQuota)
return nil
}
var etcdVersioner = etcd.APIObjectVersioner{}
// checkCache compares the passed quota against the value in the look-aside cache and returns the newer
// if the cache is out of date, it deletes the stale entry. This only works because of etcd resourceVersions
// being monotonically increasing integers
func (e *clusterQuotaAccessor) checkCache(clusterQuota *quotaapi.ClusterResourceQuota) *quotaapi.ClusterResourceQuota {
uncastCachedQuota, ok := e.updatedClusterQuotas.Get(clusterQuota.Name)
if !ok {
return clusterQuota
}
cachedQuota := uncastCachedQuota.(*quotaapi.ClusterResourceQuota)
if etcdVersioner.CompareResourceVersion(clusterQuota, cachedQuota) >= 0 {
e.updatedClusterQuotas.Remove(clusterQuota.Name)
return clusterQuota
}
return cachedQuota
}
func (e *clusterQuotaAccessor) GetQuotas(namespaceName string) ([]kapi.ResourceQuota, error) {
clusterQuotaNames, err := e.waitForReadyClusterQuotaNames(namespaceName)
if err != nil {
return nil, err
}
resourceQuotas := []kapi.ResourceQuota{}
for _, clusterQuotaName := range clusterQuotaNames {
clusterQuota, err := e.clusterQuotaLister.Get(clusterQuotaName)
if kapierrors.IsNotFound(err) {
continue
}
if err != nil {
return nil, err
}
clusterQuota = e.checkCache(clusterQuota)
// now convert to a ResourceQuota
convertedQuota := kapi.ResourceQuota{}
convertedQuota.ObjectMeta = clusterQuota.ObjectMeta
convertedQuota.Namespace = namespaceName
convertedQuota.Spec = clusterQuota.Spec.Quota
convertedQuota.Status = clusterQuota.Status.Total
resourceQuotas = append(resourceQuotas, convertedQuota)
}
return resourceQuotas, nil
}
func (e *clusterQuotaAccessor) waitForReadyClusterQuotaNames(namespaceName string) ([]string, error) {
var clusterQuotaNames []string
// wait for a valid mapping cache. The overall response can be delayed for up to 10 seconds.
err := utilwait.PollImmediate(100*time.Millisecond, 8*time.Second, func() (done bool, err error) {
var namespaceSelectionFields clusterquotamapping.SelectionFields
clusterQuotaNames, namespaceSelectionFields = e.clusterQuotaMapper.GetClusterQuotasFor(namespaceName)
namespace, err := e.namespaceLister.Get(namespaceName)
// if we can't find the namespace yet, just wait for the cache to update. Requests to non-existent namespaces
// may hang, but those people are doing something wrong and namespacelifecycle should reject them.
if kapierrors.IsNotFound(err) {
return false, nil
}
if err != nil {
return false, err
}
if equality.Semantic.DeepEqual(namespaceSelectionFields, clusterquotamapping.GetSelectionFields(namespace)) {
return true, nil
}
return false, nil
})
return clusterQuotaNames, err
}