-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathperformance_test.go
396 lines (350 loc) · 13 KB
/
performance_test.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"slices"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gmeasure"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
NamespaceTypeLabelKey string = "konflux-ci.dev/type"
NamespaceTypeUserLabelValue string = "user"
)
var _ = Describe("Authorizing requests", Serial, Ordered, func() {
username := "user"
var restConfig *rest.Config
var c client.Client
var ans []client.Object
var uns []client.Object
var cacheCfg *cacheConfig
BeforeAll(func(ctx context.Context) {
var err error
// prepare scheme
s := runtime.NewScheme()
utilruntime.Must(corev1.AddToScheme(s))
utilruntime.Must(rbacv1.AddToScheme(s))
// get kubernetes client config
restConfig = ctrl.GetConfigOrDie()
restConfig.QPS = 500
restConfig.Burst = 500
// build kubernetes client
c, err = client.New(restConfig, client.Options{Scheme: s})
utilruntime.Must(err)
// create resources
err, ans, uns = createResources(ctx, c, username, 300, 800, 1200)
utilruntime.Must(err)
})
BeforeEach(func(ctx context.Context) {
// create cache
ls, err := labels.Parse(fmt.Sprintf("%s=%s", NamespaceTypeLabelKey, NamespaceTypeUserLabelValue))
utilruntime.Must(err)
cacheCfg = &cacheConfig{restConfig: restConfig, namespacesLabelSector: ls}
})
It("efficiently authorize on a huge environment", Serial, Label("perf"), func(ctx context.Context) {
// new gomega experiment
experiment := gmeasure.NewExperiment("Authorizing Request")
// Register the experiment as a ReportEntry - this will cause Ginkgo's reporter infrastructure
// to print out the experiment's report and to include the experiment in any generated reports
AddReportEntry(experiment.Name, experiment)
// create cache, authorizer, namespacelister, and handler
cache, err := BuildAndStartResourceCache(ctx, cacheCfg)
utilruntime.Must(err)
authzr := NewAuthorizer(ctx, cache)
nl := NewNamespaceListerWithAuthorizer(cache, authzr)
lnh := NewListNamespacesHandler(nl)
// we sample a function repeatedly to get a statistically significant set of measurements
experiment.Sample(func(idx int) {
rctx := context.WithValue(context.Background(), ContextKeyUserDetails, &authenticator.Response{
User: &user.DefaultInfo{
Name: username,
},
})
r := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(rctx)
w := httptest.NewRecorder()
// measure http Handler
experiment.MeasureDuration("http listing", func() {
lnh.ServeHTTP(w, r)
})
}, gmeasure.SamplingConfig{N: 30, Duration: 2 * time.Minute})
// we'll sample the function up to 30 times or up to 2 minutes, whichever comes first.
// we sample a function repeatedly to get a statistically significant set of measurements
experiment.Sample(func(idx int) {
var err error
var nn *corev1.NamespaceList
// measure ListNamespaces
experiment.MeasureDuration("internal listing", func() {
nn, err = nl.ListNamespaces(ctx, username)
})
// check results
if err != nil {
panic(err)
}
if lnn := len(nn.Items); lnn != len(ans) {
panic(fmt.Errorf("expecting %d namespaces, received %d", len(ans), lnn))
}
}, gmeasure.SamplingConfig{N: 30, Duration: 2 * time.Minute})
// we'll sample the function up to 30 times or up to 2 minutes, whichever comes first.
// we sample a function repeatedly to get a statistically significant set of measurements
experiment.Sample(func(idx int) {
nsName := ans[0].GetName()
// measure how long it takes to allow a request and store the duration in a "authorization-allow" measurement
var d authorizer.Decision
var err error
r := authorizer.AttributesRecord{
User: &user.DefaultInfo{Name: username},
Verb: "get",
Resource: "namespaces",
APIGroup: corev1.GroupName,
APIVersion: corev1.SchemeGroupVersion.Version,
Name: nsName,
Namespace: nsName,
ResourceRequest: true,
}
// measure authorization
experiment.MeasureDuration("authorization-allow", func() {
d, _, err = authzr.Authorize(ctx, r)
})
// check results
if err != nil {
panic(err)
}
if d != authorizer.DecisionAllow {
panic(fmt.Sprintf("expected decision Allow, got %d (0 Deny, 1 Allowed, 2 NoOpinion)", d))
}
}, gmeasure.SamplingConfig{N: 30, Duration: 2 * time.Minute})
// we'll sample the function up to 30 times or up to 2 minutes, whichever comes first.
// we sample a function repeatedly to get a statistically significant set of measurements
experiment.Sample(func(idx int) {
nsName := uns[0].GetName()
// measure how long it takes to produce a NoOpinion decision to a request
// and store the duration in a "authorization-no-opinion" measurement
var d authorizer.Decision
var err error
r := authorizer.AttributesRecord{
User: &user.DefaultInfo{Name: username},
Verb: "get",
Resource: "namespaces",
APIGroup: corev1.GroupName,
APIVersion: corev1.SchemeGroupVersion.Version,
Name: nsName,
Namespace: nsName,
ResourceRequest: true,
}
// measure authorization
experiment.MeasureDuration("authorization-noopinion", func() {
d, _, err = authzr.Authorize(ctx, r)
})
// check results
if err != nil {
panic(err)
}
if d != authorizer.DecisionNoOpinion {
panic(fmt.Sprintf("expected decision NoOpinion, got %d (0 Deny, 1 Allowed, 2 NoOpinion)", d))
}
}, gmeasure.SamplingConfig{N: 30, Duration: 2 * time.Minute})
// we'll sample the function up to 30 times or up to 2 minutes, whichever comes first.
// we get the median listing duration from the experiment we just ran
httpListingStats := experiment.GetStats("http listing")
medianDuration := httpListingStats.DurationFor(gmeasure.StatMedian)
// and assert that it hasn't changed much from ~100ms
Expect(medianDuration).To(BeNumerically("~", 100*time.Millisecond, 70*time.Millisecond))
})
It("efficiently authorize on a huge environment with cached accesses", Serial, Label("perf"), func(ctx context.Context) {
// new gomega experiment
experiment := gmeasure.NewExperiment("Authorizing Request")
// Register the experiment as a ReportEntry - this will cause Ginkgo's reporter infrastructure
// to print out the experiment's report and to include the experiment in any generated reports
AddReportEntry(experiment.Name, experiment)
// create cache, namespacelister, and handler
cache, err := BuildAndStartResourceCache(ctx, cacheCfg)
utilruntime.Must(err)
c, err := buildAndStartAccessCache(ctx, cache)
utilruntime.Must(err)
nl := NewNamespaceListerForSubject(c)
lnh := NewListNamespacesHandler(nl)
// we sample a function repeatedly to get a statistically significant set of measurements
experiment.Sample(func(idx int) {
var err error
var nn *corev1.NamespaceList
// measure ListNamespaces
experiment.MeasureDuration("internal listing", func() {
nn, err = nl.ListNamespaces(ctx, username)
})
// check results
if err != nil {
panic(err)
}
if lnn := len(nn.Items); lnn != len(ans) {
panic(fmt.Errorf("expecting %d namespaces, received %d", len(ans), lnn))
}
}, gmeasure.SamplingConfig{N: 30, Duration: 2 * time.Minute})
// we'll sample the function up to 30 times or up to 2 minutes, whichever comes first.
// we sample a function repeatedly to get a statistically significant set of measurements
experiment.Sample(func(idx int) {
rctx := context.WithValue(context.Background(), ContextKeyUserDetails, &authenticator.Response{
User: &user.DefaultInfo{
Name: username,
},
})
r := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(rctx)
w := httptest.NewRecorder()
// measure http Handler
experiment.MeasureDuration("http listing", func() {
lnh.ServeHTTP(w, r)
})
}, gmeasure.SamplingConfig{N: 30, Duration: 2 * time.Minute})
// we'll sample the function up to 30 times or up to 2 minutes, whichever comes first.
})
})
func createResources(ctx context.Context, cli client.Client, user string, numAllowedNamespaces, numUnallowedNamespaces, numNonMatchingClusterRoles int) (error, []client.Object, []client.Object) {
// cluster scoped resources
mcr, nmcr := matchingClusterRoles(1), nonMatchingClusterRoles(numNonMatchingClusterRoles)
ans, uns := namespaces("allowed-tenant-", numAllowedNamespaces), namespaces("unallowed-tenant-", numUnallowedNamespaces)
if err := create(ctx, cli, slices.Concat(mcr, nmcr, ans, uns)); err != nil {
return fmt.Errorf("could not create cluster scoped resources: %w", err), nil, nil
}
// namespace scoped resources
atr := allowedTenants(user, ans, 10, "ClusterRole", mcr[0].GetName(), "ClusterRole", nmcr[0].GetName())
utr := unallowedTenants(user, uns, 10, "ClusterRole", mcr[0].GetName(), "ClusterRole", nmcr[0].GetName())
if err := create(ctx, cli, slices.Concat(atr, utr)); err != nil {
return fmt.Errorf("could not create namespaced scoped resources: %w", err), nil, nil
}
return nil, ans, uns
}
func create(ctx context.Context, cli client.Client, rr []client.Object) error {
for _, r := range rr {
if err := cli.Create(ctx, r); client.IgnoreAlreadyExists(err) != nil {
return err
}
}
return nil
}
func matchingClusterRoles(quantity int) []client.Object {
crr := make([]client.Object, quantity)
for i := range quantity {
cr := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("perf-cluster-role-matching-%d", i),
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{corev1.GroupName},
Resources: []string{"namespaces"},
Verbs: []string{"get"},
},
},
}
crr[i] = cr
}
return crr
}
func nonMatchingClusterRoles(quantity int) []client.Object {
crr := make([]client.Object, quantity)
for i := range quantity {
cr := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "perf-cluster-role-non-matching-",
},
}
crr[i] = cr
}
return crr
}
func namespaces(generateName string, quantity int) []client.Object {
rr := make([]client.Object, quantity)
for i := range quantity {
rr[i] = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: generateName,
Labels: map[string]string{
NamespaceTypeLabelKey: NamespaceTypeUserLabelValue,
},
},
}
}
return rr
}
func allowedTenants(user string, namespaces []client.Object, pollutingRoleBindings int, matchingRoleRefKind, matchingRoleRefName, nonMatchingRoleRefKind, nonMatchingRoleRefName string) []client.Object {
rr := make([]client.Object, 0, len(namespaces)*(pollutingRoleBindings+1))
for _, n := range namespaces {
// add access role binding
rr = append(rr, &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "allowed-tenant-",
Namespace: n.GetName(),
},
Subjects: []rbacv1.Subject{{Kind: "User", APIGroup: rbacv1.GroupName, Name: user}},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: matchingRoleRefKind,
Name: matchingRoleRefName,
},
})
// add pollution
for range pollutingRoleBindings {
rr = append(rr, &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "pollution-",
Namespace: n.GetName(),
},
Subjects: []rbacv1.Subject{{Kind: "User", APIGroup: rbacv1.GroupName, Name: user}},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: nonMatchingRoleRefKind,
Name: nonMatchingRoleRefName,
},
})
}
}
return rr
}
func unallowedTenants(user string, namespaces []client.Object, pollutingRoleBindings int, matchingRoleRefKind, matchingRoleRefName, nonMatchingRoleRefKind, nonMatchingRoleRefName string) []client.Object {
rr := make([]client.Object, 0, len(namespaces)*(pollutingRoleBindings+1))
for _, n := range namespaces {
// add access role binding
rr = append(rr, &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "non-allowed-tenant-",
Namespace: n.GetName(),
},
Subjects: []rbacv1.Subject{{Kind: "User", APIGroup: rbacv1.GroupName, Name: "not-the-perf-test-user"}},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: matchingRoleRefKind,
Name: matchingRoleRefName,
},
})
// add pollution
for range pollutingRoleBindings {
rr = append(rr, &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "pollution-",
Namespace: n.GetName(),
},
Subjects: []rbacv1.Subject{{Kind: "User", APIGroup: rbacv1.GroupName, Name: user}},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: nonMatchingRoleRefKind,
Name: nonMatchingRoleRefName,
},
})
}
}
return rr
}