forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
289 lines (240 loc) · 10 KB
/
mapper.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package clusterquotamapping
import (
"reflect"
"sync"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
quotav1 "github.com/openshift/api/quota/v1"
)
type ClusterQuotaMapper interface {
// GetClusterQuotasFor returns the list of clusterquota names that this namespace matches. It also
// returns the selectionFields associated with the namespace for the check so that callers can determine staleness
GetClusterQuotasFor(namespaceName string) ([]string, SelectionFields)
// GetNamespacesFor returns the list of namespace names that this cluster quota matches. It also
// returns the selector associated with the clusterquota for the check so that callers can determine staleness
GetNamespacesFor(quotaName string) ([]string, quotav1.ClusterResourceQuotaSelector)
AddListener(listener MappingChangeListener)
}
// MappingChangeListener is notified of changes to the mapping. It must not block.
type MappingChangeListener interface {
AddMapping(quotaName, namespaceName string)
RemoveMapping(quotaName, namespaceName string)
}
type SelectionFields struct {
Labels map[string]string
Annotations map[string]string
}
// clusterQuotaMapper gives thread safe access to the actual mappings that are being stored.
// Many method use a shareable read lock to check status followed by a non-shareable
// write lock which double checks the condition before proceeding. Since locks aren't escalatable
// you have to perform the recheck because someone could have beaten you in.
type clusterQuotaMapper struct {
lock sync.RWMutex
// requiredQuotaToSelector indicates the latest label selector this controller has observed for a quota
requiredQuotaToSelector map[string]quotav1.ClusterResourceQuotaSelector
// requiredNamespaceToLabels indicates the latest selectionFields this controller has observed for a namespace
requiredNamespaceToLabels map[string]SelectionFields
// completedQuotaToSelector indicates the latest label selector this controller has scanned against namespaces
completedQuotaToSelector map[string]quotav1.ClusterResourceQuotaSelector
// completedNamespaceToLabels indicates the latest selectionFields this controller has scanned against cluster quotas
completedNamespaceToLabels map[string]SelectionFields
quotaToNamespaces map[string]sets.String
namespaceToQuota map[string]sets.String
listeners []MappingChangeListener
}
func NewClusterQuotaMapper() *clusterQuotaMapper {
return &clusterQuotaMapper{
requiredQuotaToSelector: map[string]quotav1.ClusterResourceQuotaSelector{},
requiredNamespaceToLabels: map[string]SelectionFields{},
completedQuotaToSelector: map[string]quotav1.ClusterResourceQuotaSelector{},
completedNamespaceToLabels: map[string]SelectionFields{},
quotaToNamespaces: map[string]sets.String{},
namespaceToQuota: map[string]sets.String{},
}
}
func (m *clusterQuotaMapper) GetClusterQuotasFor(namespaceName string) ([]string, SelectionFields) {
m.lock.RLock()
defer m.lock.RUnlock()
quotas, ok := m.namespaceToQuota[namespaceName]
if !ok {
return []string{}, m.completedNamespaceToLabels[namespaceName]
}
return quotas.List(), m.completedNamespaceToLabels[namespaceName]
}
func (m *clusterQuotaMapper) GetNamespacesFor(quotaName string) ([]string, quotav1.ClusterResourceQuotaSelector) {
m.lock.RLock()
defer m.lock.RUnlock()
namespaces, ok := m.quotaToNamespaces[quotaName]
if !ok {
return []string{}, m.completedQuotaToSelector[quotaName]
}
return namespaces.List(), m.completedQuotaToSelector[quotaName]
}
func (m *clusterQuotaMapper) AddListener(listener MappingChangeListener) {
m.lock.Lock()
defer m.lock.Unlock()
m.listeners = append(m.listeners, listener)
}
// requireQuota updates the selector requirements for the given quota. This prevents stale updates to the mapping itself.
// returns true if a modification was made
func (m *clusterQuotaMapper) requireQuota(quota *quotav1.ClusterResourceQuota) bool {
m.lock.RLock()
selector, exists := m.requiredQuotaToSelector[quota.Name]
m.lock.RUnlock()
if selectorMatches(selector, exists, quota) {
return false
}
m.lock.Lock()
defer m.lock.Unlock()
selector, exists = m.requiredQuotaToSelector[quota.Name]
if selectorMatches(selector, exists, quota) {
return false
}
m.requiredQuotaToSelector[quota.Name] = quota.Spec.Selector
return true
}
// completeQuota updates the latest selector used to generate the mappings for this quota. The value is returned
// by the Get methods for the mapping so that callers can determine staleness
func (m *clusterQuotaMapper) completeQuota(quota *quotav1.ClusterResourceQuota) {
m.lock.Lock()
defer m.lock.Unlock()
m.completedQuotaToSelector[quota.Name] = quota.Spec.Selector
}
// removeQuota deletes a quota from all mappings
func (m *clusterQuotaMapper) removeQuota(quotaName string) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.requiredQuotaToSelector, quotaName)
delete(m.completedQuotaToSelector, quotaName)
delete(m.quotaToNamespaces, quotaName)
for namespaceName, quotas := range m.namespaceToQuota {
if quotas.Has(quotaName) {
quotas.Delete(quotaName)
for _, listener := range m.listeners {
listener.RemoveMapping(quotaName, namespaceName)
}
}
}
}
// requireNamespace updates the label requirements for the given namespace. This prevents stale updates to the mapping itself.
// returns true if a modification was made
func (m *clusterQuotaMapper) requireNamespace(namespace metav1.Object) bool {
m.lock.RLock()
selectionFields, exists := m.requiredNamespaceToLabels[namespace.GetName()]
m.lock.RUnlock()
if selectionFieldsMatch(selectionFields, exists, namespace) {
return false
}
m.lock.Lock()
defer m.lock.Unlock()
selectionFields, exists = m.requiredNamespaceToLabels[namespace.GetName()]
if selectionFieldsMatch(selectionFields, exists, namespace) {
return false
}
m.requiredNamespaceToLabels[namespace.GetName()] = GetSelectionFields(namespace)
return true
}
// completeNamespace updates the latest selectionFields used to generate the mappings for this namespace. The value is returned
// by the Get methods for the mapping so that callers can determine staleness
func (m *clusterQuotaMapper) completeNamespace(namespace metav1.Object) {
m.lock.Lock()
defer m.lock.Unlock()
m.completedNamespaceToLabels[namespace.GetName()] = GetSelectionFields(namespace)
}
// removeNamespace deletes a namespace from all mappings
func (m *clusterQuotaMapper) removeNamespace(namespaceName string) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.requiredNamespaceToLabels, namespaceName)
delete(m.completedNamespaceToLabels, namespaceName)
delete(m.namespaceToQuota, namespaceName)
for quotaName, namespaces := range m.quotaToNamespaces {
if namespaces.Has(namespaceName) {
namespaces.Delete(namespaceName)
for _, listener := range m.listeners {
listener.RemoveMapping(quotaName, namespaceName)
}
}
}
}
func selectorMatches(selector quotav1.ClusterResourceQuotaSelector, exists bool, quota *quotav1.ClusterResourceQuota) bool {
return exists && equality.Semantic.DeepEqual(selector, quota.Spec.Selector)
}
func selectionFieldsMatch(selectionFields SelectionFields, exists bool, namespace metav1.Object) bool {
return exists && reflect.DeepEqual(selectionFields, GetSelectionFields(namespace))
}
// setMapping maps (or removes a mapping) between a clusterquota and a namespace
// It returns whether the action worked, whether the quota is out of date, whether the namespace is out of date
// This allows callers to decide whether to pull new information from the cache or simply skip execution
func (m *clusterQuotaMapper) setMapping(quota *quotav1.ClusterResourceQuota, namespace metav1.Object, remove bool) (bool /*added*/, bool /*quota matches*/, bool /*namespace matches*/) {
m.lock.RLock()
selector, selectorExists := m.requiredQuotaToSelector[quota.Name]
selectionFields, selectionFieldsExist := m.requiredNamespaceToLabels[namespace.GetName()]
m.lock.RUnlock()
if !selectorMatches(selector, selectorExists, quota) {
return false, false, selectionFieldsMatch(selectionFields, selectionFieldsExist, namespace)
}
if !selectionFieldsMatch(selectionFields, selectionFieldsExist, namespace) {
return false, true, false
}
m.lock.Lock()
defer m.lock.Unlock()
selector, selectorExists = m.requiredQuotaToSelector[quota.Name]
selectionFields, selectionFieldsExist = m.requiredNamespaceToLabels[namespace.GetName()]
if !selectorMatches(selector, selectorExists, quota) {
return false, false, selectionFieldsMatch(selectionFields, selectionFieldsExist, namespace)
}
if !selectionFieldsMatch(selectionFields, selectionFieldsExist, namespace) {
return false, true, false
}
if remove {
mutated := false
namespaces, ok := m.quotaToNamespaces[quota.Name]
if !ok {
m.quotaToNamespaces[quota.Name] = sets.String{}
} else {
mutated = namespaces.Has(namespace.GetName())
namespaces.Delete(namespace.GetName())
}
quotas, ok := m.namespaceToQuota[namespace.GetName()]
if !ok {
m.namespaceToQuota[namespace.GetName()] = sets.String{}
} else {
mutated = mutated || quotas.Has(quota.Name)
quotas.Delete(quota.Name)
}
if mutated {
for _, listener := range m.listeners {
listener.RemoveMapping(quota.Name, namespace.GetName())
}
}
return true, true, true
}
mutated := false
namespaces, ok := m.quotaToNamespaces[quota.Name]
if !ok {
mutated = true
m.quotaToNamespaces[quota.Name] = sets.NewString(namespace.GetName())
} else {
mutated = !namespaces.Has(namespace.GetName())
namespaces.Insert(namespace.GetName())
}
quotas, ok := m.namespaceToQuota[namespace.GetName()]
if !ok {
mutated = true
m.namespaceToQuota[namespace.GetName()] = sets.NewString(quota.Name)
} else {
mutated = mutated || !quotas.Has(quota.Name)
quotas.Insert(quota.Name)
}
if mutated {
for _, listener := range m.listeners {
listener.AddMapping(quota.Name, namespace.GetName())
}
}
return true, true, true
}
func GetSelectionFields(namespace metav1.Object) SelectionFields {
return SelectionFields{Labels: namespace.GetLabels(), Annotations: namespace.GetAnnotations()}
}