k8s.io/apiserver@v0.31.1/pkg/quota/v1/generic/registry.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package generic 18 19 import ( 20 "sync" 21 22 "k8s.io/apimachinery/pkg/runtime/schema" 23 quota "k8s.io/apiserver/pkg/quota/v1" 24 ) 25 26 // implements a basic registry 27 type simpleRegistry struct { 28 lock sync.RWMutex 29 // evaluators tracked by the registry 30 evaluators map[schema.GroupResource]quota.Evaluator 31 } 32 33 // NewRegistry creates a simple registry with initial list of evaluators 34 func NewRegistry(evaluators []quota.Evaluator) quota.Registry { 35 return &simpleRegistry{ 36 evaluators: evaluatorsByGroupResource(evaluators), 37 } 38 } 39 40 func (r *simpleRegistry) Add(e quota.Evaluator) { 41 r.lock.Lock() 42 defer r.lock.Unlock() 43 r.evaluators[e.GroupResource()] = e 44 } 45 46 func (r *simpleRegistry) Remove(e quota.Evaluator) { 47 r.lock.Lock() 48 defer r.lock.Unlock() 49 delete(r.evaluators, e.GroupResource()) 50 } 51 52 func (r *simpleRegistry) Get(gr schema.GroupResource) quota.Evaluator { 53 r.lock.RLock() 54 defer r.lock.RUnlock() 55 return r.evaluators[gr] 56 } 57 58 func (r *simpleRegistry) List() []quota.Evaluator { 59 r.lock.RLock() 60 defer r.lock.RUnlock() 61 62 return evaluatorsList(r.evaluators) 63 } 64 65 // evaluatorsByGroupResource converts a list of evaluators to a map by group resource. 66 func evaluatorsByGroupResource(items []quota.Evaluator) map[schema.GroupResource]quota.Evaluator { 67 result := map[schema.GroupResource]quota.Evaluator{} 68 for _, item := range items { 69 result[item.GroupResource()] = item 70 } 71 return result 72 } 73 74 // evaluatorsList converts a map of evaluators to list 75 func evaluatorsList(input map[schema.GroupResource]quota.Evaluator) []quota.Evaluator { 76 var result []quota.Evaluator 77 for _, item := range input { 78 result = append(result, item) 79 } 80 return result 81 }