Debian Codegraph @

pkg/admission/plugin/resourcequota/resource_access_test.go

raw
1 /*
2 Copyright 2020 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 "fmt"
21 "reflect"
22 "sync"
23 "sync/atomic"
24 "testing"
25 "time"
26
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 "k8s.io/apimachinery/pkg/runtime"
29
30 corev1 "k8s.io/api/core/v1"
31 "k8s.io/client-go/informers"
32 "k8s.io/client-go/kubernetes/fake"
33 core "k8s.io/client-go/testing"
34 "k8s.io/utils/lru"
35 )
36
37 func TestLRUCacheLookup(t *testing.T) {
38 namespace := "foo"
39 resourceQuota := &corev1.ResourceQuota{
40 ObjectMeta: metav1.ObjectMeta{
41 Name: "foo",
42 Namespace: namespace,
43 },
44 }
45
46 testcases := []struct {
47 description string
48 cacheInput []*corev1.ResourceQuota
49 clientInput []runtime.Object
50 ttl time.Duration
51 namespace string
52 expectedQuota *corev1.ResourceQuota
53 }{
54 {
55 description: "object is found via cache",
56 cacheInput: []*corev1.ResourceQuota{resourceQuota},
57 ttl: 30 * time.Second,
58 namespace: namespace,
59 expectedQuota: resourceQuota,
60 },
61 {
62 description: "object is outdated and not found with client",
63 cacheInput: []*corev1.ResourceQuota{resourceQuota},
64 ttl: -30 * time.Second,
65 namespace: namespace,
66 expectedQuota: nil,
67 },
68 {
69 description: "object is outdated but is found with client",
70 cacheInput: []*corev1.ResourceQuota{resourceQuota},
71 clientInput: []runtime.Object{resourceQuota},
72 ttl: -30 * time.Second,
73 namespace: namespace,
74 expectedQuota: resourceQuota,
75 },
76 {
77 description: "object does not exist in cache and is not found with client",
78 cacheInput: []*corev1.ResourceQuota{resourceQuota},
79 ttl: 30 * time.Second,
80 expectedQuota: nil,
81 },
82 {
83 description: "object does not exist in cache and is found with client",
84 cacheInput: []*corev1.ResourceQuota{},
85 clientInput: []runtime.Object{resourceQuota},
86 namespace: namespace,
87 expectedQuota: resourceQuota,
88 },
89 }
90
91 for _, tc := range testcases {
92 t.Run(tc.description, func(t *testing.T) {
93 liveLookupCache := lru.New(1)
94 kubeClient := fake.NewSimpleClientset(tc.clientInput...)
95 informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
96
97 accessor, _ := newQuotaAccessor()
98 accessor.client = kubeClient
99 accessor.lister = informerFactory.Core().V1().ResourceQuotas().Lister()
100 accessor.hasSynced = func() bool { return false }
101 accessor.liveLookupCache = liveLookupCache
102
103 for _, q := range tc.cacheInput {
104 quota := q
105 liveLookupCache.Add(quota.Namespace, liveLookupEntry{expiry: time.Now().Add(tc.ttl), items: []*corev1.ResourceQuota{quota}})
106 }
107
108 quotas, err := accessor.GetQuotas(tc.namespace)
109 if err != nil {
110 t.Errorf("Unexpected error: %v", err)
111 }
112
113 if tc.expectedQuota != nil {
114 if count := len(quotas); count != 1 {
115 t.Fatalf("Expected 1 object but got %d", count)
116 }
117
118 if !reflect.DeepEqual(quotas[0], *tc.expectedQuota) {
119 t.Errorf("Retrieved object does not match")
120 }
121 return
122 }
123
124 if count := len(quotas); count > 0 {
125 t.Errorf("Expected 0 objects but got %d", count)
126 }
127 })
128 }
129 }
130
131 // TestGetQuotas ensures we do not have multiple LIST calls to the apiserver
132 // in-flight at any one time. This is to ensure the issue described in #22422 do
133 // not happen again.
134 func TestGetQuotas(t *testing.T) {
135 var (
136 testNamespace1 = "test-a"
137 testNamespace2 = "test-b"
138 listCallCountTestNamespace1 int64
139 listCallCountTestNamespace2 int64
140 )
141 resourceQuota := &corev1.ResourceQuota{
142 ObjectMeta: metav1.ObjectMeta{
143 Name: "foo",
144 },
145 }
146
147 resourceQuotas := []*corev1.ResourceQuota{resourceQuota}
148
149 kubeClient := &fake.Clientset{}
150 informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
151
152 accessor, _ := newQuotaAccessor()
153 accessor.client = kubeClient
154 accessor.lister = informerFactory.Core().V1().ResourceQuotas().Lister()
155 accessor.hasSynced = func() bool { return false }
156
157 kubeClient.AddReactor("list", "resourcequotas", func(action core.Action) (bool, runtime.Object, error) {
158 switch action.GetNamespace() {
159 case testNamespace1:
160 atomic.AddInt64(&listCallCountTestNamespace1, 1)
161 case testNamespace2:
162 atomic.AddInt64(&listCallCountTestNamespace2, 1)
163 default:
164 t.Error("unexpected namespace")
165 }
166
167 resourceQuotaList := &corev1.ResourceQuotaList{
168 ListMeta: metav1.ListMeta{
169 ResourceVersion: fmt.Sprintf("%d", len(resourceQuotas)),
170 },
171 }
172 for i, quota := range resourceQuotas {
173 quota.ResourceVersion = fmt.Sprintf("%d", i)
174 quota.Namespace = action.GetNamespace()
175 resourceQuotaList.Items = append(resourceQuotaList.Items, *quota)
176 }
177 // make the handler slow so concurrent calls exercise the singleflight
178 time.Sleep(time.Second)
179 return true, resourceQuotaList, nil
180 })
181
182 wg := sync.WaitGroup{}
183 for i := 0; i < 10; i++ {
184 wg.Add(2)
185 // simulating concurrent calls after a cache failure
186 go func() {
187 defer wg.Done()
188 quotas, err := accessor.GetQuotas(testNamespace1)
189 if err != nil {
190 t.Errorf("unexpected error: %v", err)
191 }
192 if len(quotas) != len(resourceQuotas) {
193 t.Errorf("Expected %d resource quotas, got %d", len(resourceQuotas), len(quotas))
194 }
195 for _, q := range quotas {
196 if q.Namespace != testNamespace1 {
197 t.Errorf("Expected %s namespace, got %s", testNamespace1, q.Namespace)
198 }
199 }
200 }()
201
202 // simulation of different namespaces is a call for a different group key, but not shared with the first namespace
203 go func() {
204 defer wg.Done()
205 quotas, err := accessor.GetQuotas(testNamespace2)
206 if err != nil {
207 t.Errorf("unexpected error: %v", err)
208 }
209 if len(quotas) != len(resourceQuotas) {
210 t.Errorf("Expected %d resource quotas, got %d", len(resourceQuotas), len(quotas))
211 }
212 for _, q := range quotas {
213 if q.Namespace != testNamespace2 {
214 t.Errorf("Expected %s namespace, got %s", testNamespace2, q.Namespace)
215 }
216 }
217 }()
218 }
219
220 // and here we wait for all the goroutines
221 wg.Wait()
222 // since all the calls with the same namespace will be held, they must
223 // be caught on the singleflight group. there are two different sets of
224 // namespace calls hence only 2.
225 if listCallCountTestNamespace1 != 1 {
226 t.Errorf("Expected 1 resource quota call, got %d", listCallCountTestNamespace1)
227 }
228 if listCallCountTestNamespace2 != 1 {
229 t.Errorf("Expected 1 resource quota call, got %d", listCallCountTestNamespace2)
230 }
231
232 // invalidate the cache
233 accessor.liveLookupCache.Remove(testNamespace1)
234 quotas, err := accessor.GetQuotas(testNamespace1)
235 if err != nil {
236 t.Errorf("unexpected error: %v", err)
237 }
238 if len(quotas) != len(resourceQuotas) {
239 t.Errorf("Expected %d resource quotas, got %d", len(resourceQuotas), len(quotas))
240 }
241
242 if listCallCountTestNamespace1 != 2 {
243 t.Errorf("Expected 2 resource quota call, got %d", listCallCountTestNamespace1)
244 }
245 if listCallCountTestNamespace2 != 1 {
246 t.Errorf("Expected 1 resource quota call, got %d", listCallCountTestNamespace2)
247 }
248 }