@@ -42,7 +42,7 @@ import (
42
42
"k8s.io/client-go/tools/cache"
43
43
"k8s.io/client-go/util/workqueue"
44
44
"k8s.io/kubernetes/pkg/controller"
45
- quota "k8s.io/kubernetes/pkg/quota/v1"
45
+ "k8s.io/kubernetes/pkg/quota/v1"
46
46
)
47
47
48
48
// NamespacedResourcesFunc knows how to discover namespaced resources.
@@ -52,8 +52,8 @@ type NamespacedResourcesFunc func() ([]*metav1.APIResourceList, error)
52
52
// that may require quota to be recalculated.
53
53
type ReplenishmentFunc func (groupResource schema.GroupResource , namespace string )
54
54
55
- // ResourceQuotaControllerOptions holds options for creating a quota controller
56
- type ResourceQuotaControllerOptions struct {
55
+ // ControllerOptions holds options for creating a quota controller
56
+ type ControllerOptions struct {
57
57
// Must have authority to list all quotas, and update quota status
58
58
QuotaClient corev1client.ResourceQuotasGetter
59
59
// Shared informer for resource quotas
@@ -74,8 +74,8 @@ type ResourceQuotaControllerOptions struct {
74
74
ReplenishmentResyncPeriod controller.ResyncPeriodFunc
75
75
}
76
76
77
- // ResourceQuotaController is responsible for tracking quota usage status in the system
78
- type ResourceQuotaController struct {
77
+ // Controller is responsible for tracking quota usage status in the system
78
+ type Controller struct {
79
79
// Must have authority to list all resources in the system, and update quota status
80
80
rqClient corev1client.ResourceQuotasGetter
81
81
// A lister/getter of resource quota objects
@@ -100,10 +100,10 @@ type ResourceQuotaController struct {
100
100
workerLock sync.RWMutex
101
101
}
102
102
103
- // NewResourceQuotaController creates a quota controller with specified options
104
- func NewResourceQuotaController (options * ResourceQuotaControllerOptions ) (* ResourceQuotaController , error ) {
103
+ // NewController creates a quota controller with specified options
104
+ func NewController (options * ControllerOptions ) (* Controller , error ) {
105
105
// build the resource quota controller
106
- rq := & ResourceQuotaController {
106
+ rq := & Controller {
107
107
rqClient : options .QuotaClient ,
108
108
rqLister : options .ResourceQuotaInformer .Lister (),
109
109
informerSyncedFuncs : []cache.InformerSynced {options .ResourceQuotaInformer .Informer ().HasSynced },
@@ -175,7 +175,7 @@ func NewResourceQuotaController(options *ResourceQuotaControllerOptions) (*Resou
175
175
}
176
176
177
177
// enqueueAll is called at the fullResyncPeriod interval to force a full recalculation of quota usage statistics
178
- func (rq * ResourceQuotaController ) enqueueAll () {
178
+ func (rq * Controller ) enqueueAll () {
179
179
defer klog .V (4 ).Infof ("Resource quota controller queued all resource quota for full calculation of usage" )
180
180
rqs , err := rq .rqLister .List (labels .Everything ())
181
181
if err != nil {
@@ -185,15 +185,15 @@ func (rq *ResourceQuotaController) enqueueAll() {
185
185
for i := range rqs {
186
186
key , err := controller .KeyFunc (rqs [i ])
187
187
if err != nil {
188
- utilruntime .HandleError (fmt .Errorf ("Couldn 't get key for object %+v: %v" , rqs [i ], err ))
188
+ utilruntime .HandleError (fmt .Errorf ("couldn 't get key for object %+v: %v" , rqs [i ], err ))
189
189
continue
190
190
}
191
191
rq .queue .Add (key )
192
192
}
193
193
}
194
194
195
195
// obj could be an *v1.ResourceQuota, or a DeletionFinalStateUnknown marker item.
196
- func (rq * ResourceQuotaController ) enqueueResourceQuota (obj interface {}) {
196
+ func (rq * Controller ) enqueueResourceQuota (obj interface {}) {
197
197
key , err := controller .KeyFunc (obj )
198
198
if err != nil {
199
199
klog .Errorf ("Couldn't get key for object %+v: %v" , obj , err )
@@ -202,7 +202,7 @@ func (rq *ResourceQuotaController) enqueueResourceQuota(obj interface{}) {
202
202
rq .queue .Add (key )
203
203
}
204
204
205
- func (rq * ResourceQuotaController ) addQuota (obj interface {}) {
205
+ func (rq * Controller ) addQuota (obj interface {}) {
206
206
key , err := controller .KeyFunc (obj )
207
207
if err != nil {
208
208
klog .Errorf ("Couldn't get key for object %+v: %v" , obj , err )
@@ -220,7 +220,7 @@ func (rq *ResourceQuotaController) addQuota(obj interface{}) {
220
220
// if we declared a constraint that has no usage (which this controller can calculate, prioritize it)
221
221
for constraint := range resourceQuota .Status .Hard {
222
222
if _ , usageFound := resourceQuota .Status .Used [constraint ]; ! usageFound {
223
- matchedResources := []v1.ResourceName {v1 . ResourceName ( constraint ) }
223
+ matchedResources := []v1.ResourceName {constraint }
224
224
for _ , evaluator := range rq .registry .List () {
225
225
if intersection := evaluator .MatchingResources (matchedResources ); len (intersection ) > 0 {
226
226
rq .missingUsageQueue .Add (key )
@@ -235,7 +235,7 @@ func (rq *ResourceQuotaController) addQuota(obj interface{}) {
235
235
}
236
236
237
237
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
238
- func (rq * ResourceQuotaController ) worker (queue workqueue.RateLimitingInterface ) func () {
238
+ func (rq * Controller ) worker (queue workqueue.RateLimitingInterface ) func () {
239
239
workFunc := func () bool {
240
240
key , quit := queue .Get ()
241
241
if quit {
@@ -265,7 +265,7 @@ func (rq *ResourceQuotaController) worker(queue workqueue.RateLimitingInterface)
265
265
}
266
266
267
267
// Run begins quota controller using the specified number of workers
268
- func (rq * ResourceQuotaController ) Run (workers int , stopCh <- chan struct {}) {
268
+ func (rq * Controller ) Run (workers int , stopCh <- chan struct {}) {
269
269
defer utilruntime .HandleCrash ()
270
270
defer rq .queue .ShutDown ()
271
271
@@ -291,7 +291,7 @@ func (rq *ResourceQuotaController) Run(workers int, stopCh <-chan struct{}) {
291
291
}
292
292
293
293
// syncResourceQuotaFromKey syncs a quota key
294
- func (rq * ResourceQuotaController ) syncResourceQuotaFromKey (key string ) (err error ) {
294
+ func (rq * Controller ) syncResourceQuotaFromKey (key string ) (err error ) {
295
295
startTime := time .Now ()
296
296
defer func () {
297
297
klog .V (4 ).Infof ("Finished syncing resource quota %q (%v)" , key , time .Since (startTime ))
@@ -301,7 +301,7 @@ func (rq *ResourceQuotaController) syncResourceQuotaFromKey(key string) (err err
301
301
if err != nil {
302
302
return err
303
303
}
304
- quota , err := rq .rqLister .ResourceQuotas (namespace ).Get (name )
304
+ resourceQuota , err := rq .rqLister .ResourceQuotas (namespace ).Get (name )
305
305
if errors .IsNotFound (err ) {
306
306
klog .Infof ("Resource quota has been deleted %v" , key )
307
307
return nil
@@ -310,11 +310,11 @@ func (rq *ResourceQuotaController) syncResourceQuotaFromKey(key string) (err err
310
310
klog .Infof ("Unable to retrieve resource quota %v from store: %v" , key , err )
311
311
return err
312
312
}
313
- return rq .syncResourceQuota (quota )
313
+ return rq .syncResourceQuota (resourceQuota )
314
314
}
315
315
316
316
// syncResourceQuota runs a complete sync of resource quota status across all known kinds
317
- func (rq * ResourceQuotaController ) syncResourceQuota (resourceQuota * v1.ResourceQuota ) (err error ) {
317
+ func (rq * Controller ) syncResourceQuota (resourceQuota * v1.ResourceQuota ) (err error ) {
318
318
// quota is dirty if any part of spec hard limits differs from the status hard limits
319
319
statusLimitsDirty := ! apiequality .Semantic .DeepEqual (resourceQuota .Spec .Hard , resourceQuota .Status .Hard )
320
320
@@ -329,12 +329,12 @@ func (rq *ResourceQuotaController) syncResourceQuota(resourceQuota *v1.ResourceQ
329
329
}
330
330
hardLimits := quota .Add (v1.ResourceList {}, resourceQuota .Spec .Hard )
331
331
332
- errors := []error {}
332
+ var errs []error
333
333
334
334
newUsage , err := quota .CalculateUsage (resourceQuota .Namespace , resourceQuota .Spec .Scopes , hardLimits , rq .registry , resourceQuota .Spec .ScopeSelector )
335
335
if err != nil {
336
336
// if err is non-nil, remember it to return, but continue updating status with any resources in newUsage
337
- errors = append (errors , err )
337
+ errs = append (errs , err )
338
338
}
339
339
for key , value := range newUsage {
340
340
used [key ] = value
@@ -358,14 +358,14 @@ func (rq *ResourceQuotaController) syncResourceQuota(resourceQuota *v1.ResourceQ
358
358
if dirty {
359
359
_ , err = rq .rqClient .ResourceQuotas (usage .Namespace ).UpdateStatus (context .TODO (), usage , metav1.UpdateOptions {})
360
360
if err != nil {
361
- errors = append (errors , err )
361
+ errs = append (errs , err )
362
362
}
363
363
}
364
- return utilerrors .NewAggregate (errors )
364
+ return utilerrors .NewAggregate (errs )
365
365
}
366
366
367
367
// replenishQuota is a replenishment function invoked by a controller to notify that a quota should be recalculated
368
- func (rq * ResourceQuotaController ) replenishQuota (groupResource schema.GroupResource , namespace string ) {
368
+ func (rq * Controller ) replenishQuota (groupResource schema.GroupResource , namespace string ) {
369
369
// check if the quota controller can evaluate this groupResource, if not, ignore it altogether...
370
370
evaluator := rq .registry .Get (groupResource )
371
371
if evaluator == nil {
@@ -398,7 +398,7 @@ func (rq *ResourceQuotaController) replenishQuota(groupResource schema.GroupReso
398
398
}
399
399
400
400
// Sync periodically resyncs the controller when new resources are observed from discovery.
401
- func (rq * ResourceQuotaController ) Sync (discoveryFunc NamespacedResourcesFunc , period time.Duration , stopCh <- chan struct {}) {
401
+ func (rq * Controller ) Sync (discoveryFunc NamespacedResourcesFunc , period time.Duration , stopCh <- chan struct {}) {
402
402
// Something has changed, so track the new state and perform a sync.
403
403
oldResources := make (map [schema.GroupVersionResource ]struct {})
404
404
wait .Until (func () {
@@ -486,7 +486,7 @@ func waitForStopOrTimeout(stopCh <-chan struct{}, timeout time.Duration) <-chan
486
486
487
487
// resyncMonitors starts or stops quota monitors as needed to ensure that all
488
488
// (and only) those resources present in the map are monitored.
489
- func (rq * ResourceQuotaController ) resyncMonitors (resources map [schema.GroupVersionResource ]struct {}) error {
489
+ func (rq * Controller ) resyncMonitors (resources map [schema.GroupVersionResource ]struct {}) error {
490
490
if rq .quotaMonitor == nil {
491
491
return nil
492
492
}
@@ -510,7 +510,7 @@ func GetQuotableResources(discoveryFunc NamespacedResourcesFunc) (map[schema.Gro
510
510
quotableResources := discovery .FilteredBy (discovery.SupportsAllVerbs {Verbs : []string {"create" , "list" , "watch" , "delete" }}, possibleResources )
511
511
quotableGroupVersionResources , err := discovery .GroupVersionResources (quotableResources )
512
512
if err != nil {
513
- return nil , fmt .Errorf ("Failed to parse resources: %v" , err )
513
+ return nil , fmt .Errorf ("failed to parse resources: %v" , err )
514
514
}
515
515
// return the original discovery error (if any) in addition to the list
516
516
return quotableGroupVersionResources , discoveryErr
0 commit comments