| 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 resourcequota |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"time" |
| 23 |
|
| 24 |
"golang.org/x/sync/singleflight" |
| 25 |
corev1 "k8s.io/api/core/v1" |
| 26 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 |
"k8s.io/apimachinery/pkg/labels" |
| 28 |
"k8s.io/apiserver/pkg/storage" |
| 29 |
"k8s.io/client-go/kubernetes" |
| 30 |
corev1listers "k8s.io/client-go/listers/core/v1" |
| 31 |
"k8s.io/utils/lru" |
| 32 |
) |
| 33 |
|
| 34 |
// QuotaAccessor abstracts the get/set logic from the rest of the Evaluator. This could be a test stub, a straight passthrough, |
| 35 |
// or most commonly a series of deconflicting caches. |
| 36 |
type QuotaAccessor interface { |
| 37 |
// UpdateQuotaStatus is called to persist final status. This method should write to persistent storage. |
| 38 |
// An error indicates that write didn't complete successfully. |
| 39 |
UpdateQuotaStatus(newQuota *corev1.ResourceQuota) error |
| 40 |
|
| 41 |
// GetQuotas gets all possible quotas for a given namespace |
| 42 |
GetQuotas(namespace string) ([]corev1.ResourceQuota, error) |
| 43 |
} |
| 44 |
|
| 45 |
type quotaAccessor struct { |
| 46 |
client kubernetes.Interface |
| 47 |
|
| 48 |
// lister can list/get quota objects from a shared informer's cache |
| 49 |
lister corev1listers.ResourceQuotaLister |
| 50 |
|
| 51 |
// hasSynced indicates whether the lister has completed its initial sync |
| 52 |
hasSynced func() bool |
| 53 |
|
| 54 |
// liveLookups holds the last few live lookups we've done to help ammortize cost on repeated lookup failures. |
| 55 |
// This lets us handle the case of latent caches, by looking up actual results for a namespace on cache miss/no results. |
| 56 |
// We track the lookup result here so that for repeated requests, we don't look it up very often. |
| 57 |
liveLookupCache *lru.Cache |
| 58 |
group singleflight.Group |
| 59 |
liveTTL time.Duration |
| 60 |
// updatedQuotas holds a cache of quotas that we've updated. This is used to pull the "really latest" during back to |
| 61 |
// back quota evaluations that touch the same quota doc. This only works because we can compare etcd resourceVersions |
| 62 |
// for the same resource as integers. Before this change: 22 updates with 12 conflicts. after this change: 15 updates with 0 conflicts |
| 63 |
updatedQuotas *lru.Cache |
| 64 |
} |
| 65 |
|
| 66 |
// newQuotaAccessor creates an object that conforms to the QuotaAccessor interface to be used to retrieve quota objects. |
| 67 |
func newQuotaAccessor() (*quotaAccessor, error) { |
| 68 |
liveLookupCache := lru.New(100) |
| 69 |
updatedCache := lru.New(100) |
| 70 |
|
| 71 |
// client and lister will be set when SetInternalKubeClientSet and SetInternalKubeInformerFactory are invoked |
| 72 |
return "aAccessor{ |
| 73 |
liveLookupCache: liveLookupCache, |
| 74 |
liveTTL: time.Duration(30 * time.Second), |
| 75 |
updatedQuotas: updatedCache, |
| 76 |
}, nil |
| 77 |
} |
| 78 |
|
| 79 |
func (e *quotaAccessor) UpdateQuotaStatus(newQuota *corev1.ResourceQuota) error { |
| 80 |
updatedQuota, err := e.client.CoreV1().ResourceQuotas(newQuota.Namespace).UpdateStatus(context.TODO(), newQuota, metav1.UpdateOptions{}) |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
|
| 85 |
key := newQuota.Namespace + "/" + newQuota.Name |
| 86 |
e.updatedQuotas.Add(key, updatedQuota) |
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
var etcdVersioner = storage.APIObjectVersioner{} |
| 91 |
|
| 92 |
// checkCache compares the passed quota against the value in the look-aside cache and returns the newer |
| 93 |
// if the cache is out of date, it deletes the stale entry. This only works because of etcd resourceVersions |
| 94 |
// being monotonically increasing integers |
| 95 |
func (e *quotaAccessor) checkCache(quota *corev1.ResourceQuota) *corev1.ResourceQuota { |
| 96 |
key := quota.Namespace + "/" + quota.Name |
| 97 |
uncastCachedQuota, ok := e.updatedQuotas.Get(key) |
| 98 |
if !ok { |
| 99 |
return quota |
| 100 |
} |
| 101 |
cachedQuota := uncastCachedQuota.(*corev1.ResourceQuota) |
| 102 |
|
| 103 |
if etcdVersioner.CompareResourceVersion(quota, cachedQuota) >= 0 { |
| 104 |
e.updatedQuotas.Remove(key) |
| 105 |
return quota |
| 106 |
} |
| 107 |
return cachedQuota |
| 108 |
} |
| 109 |
|
| 110 |
func (e *quotaAccessor) GetQuotas(namespace string) ([]corev1.ResourceQuota, error) { |
| 111 |
// determine if there are any quotas in this namespace |
| 112 |
// if there are no quotas, we don't need to do anything |
| 113 |
items, err := e.lister.ResourceQuotas(namespace).List(labels.Everything()) |
| 114 |
if err != nil { |
| 115 |
return nil, fmt.Errorf("error resolving quota: %v", err) |
| 116 |
} |
| 117 |
|
| 118 |
// if there are no items held in our unsynced lister, check our live-lookup LRU, if that misses, do the live lookup to prime it. |
| 119 |
if len(items) == 0 && !e.hasSynced() { |
| 120 |
lruItemObj, ok := e.liveLookupCache.Get(namespace) |
| 121 |
if !ok || lruItemObj.(liveLookupEntry).expiry.Before(time.Now()) { |
| 122 |
// use singleflight.Group to avoid flooding the apiserver with repeated |
| 123 |
// requests. See #22422 for details. |
| 124 |
lruItemObj, err, _ = e.group.Do(namespace, func() (interface{}, error) { |
| 125 |
liveList, err := e.client.CoreV1().ResourceQuotas(namespace).List(context.TODO(), metav1.ListOptions{}) |
| 126 |
if err != nil { |
| 127 |
return nil, err |
| 128 |
} |
| 129 |
newEntry := liveLookupEntry{expiry: time.Now().Add(e.liveTTL)} |
| 130 |
for i := range liveList.Items { |
| 131 |
newEntry.items = append(newEntry.items, &liveList.Items[i]) |
| 132 |
} |
| 133 |
e.liveLookupCache.Add(namespace, newEntry) |
| 134 |
return newEntry, nil |
| 135 |
}) |
| 136 |
if err != nil { |
| 137 |
return nil, err |
| 138 |
} |
| 139 |
} |
| 140 |
lruEntry := lruItemObj.(liveLookupEntry) |
| 141 |
for i := range lruEntry.items { |
| 142 |
items = append(items, lruEntry.items[i]) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
resourceQuotas := []corev1.ResourceQuota{} |
| 147 |
for i := range items { |
| 148 |
quota := items[i] |
| 149 |
quota = e.checkCache(quota) |
| 150 |
// always make a copy. We're going to muck around with this and we should never mutate the originals |
| 151 |
resourceQuotas = append(resourceQuotas, *quota) |
| 152 |
} |
| 153 |
|
| 154 |
return resourceQuotas, nil |
| 155 |
} |