-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add performance tests for namespace-lister #38
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
345bd56
add performance tests for namespace-lister
filariow ad62cb6
add PERF_CLUSTER_PROVIDER_FLAGS
filariow 0e2d017
use OUT_DIR in performance tests
filariow d16b857
improve labelselector in performance_test
filariow 4b698e4
add keep going and retry for flakiness, ensure 1 proc is used
filariow 28f2f16
Merge branch 'main' into perf-test
filariow e63d9b0
Merge branch 'main' into perf-test
filariow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/konflux-ci/namespace-lister | ||
|
||
go 1.22.2 | ||
go 1.22.9 | ||
|
||
require ( | ||
github.com/go-logr/logr v1.4.2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,287 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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/user" | ||
"k8s.io/apiserver/pkg/authorization/authorizer" | ||
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", func() { | ||
BeforeEach(func() {}) | ||
|
||
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) | ||
|
||
// 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 | ||
c, err := client.New(restConfig, client.Options{Scheme: s}) | ||
utilruntime.Must(err) | ||
|
||
// create resources | ||
username := "user" | ||
err, ans, uns := createResources(ctx, c, username, 300, 800, 1200) | ||
utilruntime.Must(err) | ||
|
||
// create cache | ||
ls, err := labels.Parse(fmt.Sprintf("%s=%s", NamespaceTypeLabelKey, NamespaceTypeUserLabelValue)) | ||
utilruntime.Must(err) | ||
cacheConfig := cacheConfig{restConfig: restConfig, namespacesLabelSector: ls} | ||
cache, err := BuildAndStartCache(ctx, &cacheConfig) | ||
utilruntime.Must(err) | ||
|
||
// create authorizer and namespacelister | ||
authzr := NewAuthorizer(ctx, cache) | ||
nl := NewNamespaceLister(cache, authzr) | ||
|
||
// we sample a function repeatedly to get a statistically significant set of measurements | ||
experiment.Sample(func(idx int) { | ||
experiment.MeasureDuration("listing", func() { | ||
nn, err := nl.ListNamespaces(ctx, username) | ||
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 | ||
experiment.MeasureDuration("authorization-allow", func() { | ||
d, _, err := authzr.Authorize(ctx, authorizer.AttributesRecord{ | ||
User: &user.DefaultInfo{Name: username}, | ||
Verb: "get", | ||
Resource: "namespaces", | ||
APIGroup: corev1.GroupName, | ||
APIVersion: corev1.SchemeGroupVersion.Version, | ||
Name: nsName, | ||
Namespace: nsName, | ||
ResourceRequest: true, | ||
}) | ||
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 | ||
experiment.MeasureDuration("authorization-noopinion", func() { | ||
d, _, err := authzr.Authorize(ctx, authorizer.AttributesRecord{ | ||
User: &user.DefaultInfo{Name: username}, | ||
Verb: "get", | ||
Resource: "namespaces", | ||
APIGroup: corev1.GroupName, | ||
APIVersion: corev1.SchemeGroupVersion.Version, | ||
Name: nsName, | ||
Namespace: nsName, | ||
ResourceRequest: true, | ||
}) | ||
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 | ||
repaginationStats := experiment.GetStats("listing") | ||
medianDuration := repaginationStats.DurationFor(gmeasure.StatMedian) | ||
|
||
// and assert that it hasn't changed much from ~100ms | ||
Expect(medianDuration).To(BeNumerically("~", 100*time.Millisecond, 70*time.Millisecond)) | ||
}) | ||
}) | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a difference in behavior - why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I applied a fix for a complaint from my linter. When multiple files are copied, it's best practice to explicitly declare a folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then maybe we should have something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are already setting the WORKDIR to
/namespace-lister
. I'd prefer this to remain relative to the WORKDIR