Skip to content

Commit fe53db0

Browse files
committed
authz: add benchmark for webhook authorizer
Signed-off-by: Rita Zhang <[email protected]>
1 parent f927d5b commit fe53db0

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook/webhook_v1_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,111 @@ func TestStructuredAuthzConfigFeatureEnablement(t *testing.T) {
782782
}
783783
}
784784

785+
func BenchmarkNoCELExpressionFeatureOff(b *testing.B) {
786+
benchmarkWebhookAuthorizer(b, []apiserver.WebhookMatchCondition{}, false)
787+
}
788+
789+
func BenchmarkNoCELExpressionFeatureOn(b *testing.B) {
790+
benchmarkWebhookAuthorizer(b, []apiserver.WebhookMatchCondition{}, true)
791+
}
792+
func BenchmarkWithOneCELExpressions(b *testing.B) {
793+
expressions := []apiserver.WebhookMatchCondition{
794+
{
795+
Expression: "request.user == 'alice'",
796+
},
797+
}
798+
benchmarkWebhookAuthorizer(b, expressions, true)
799+
}
800+
func BenchmarkWithTwoCELExpressions(b *testing.B) {
801+
expressions := []apiserver.WebhookMatchCondition{
802+
{
803+
Expression: "request.user == 'alice'",
804+
},
805+
{
806+
Expression: "request.uid == '1'",
807+
},
808+
}
809+
benchmarkWebhookAuthorizer(b, expressions, true)
810+
}
811+
func BenchmarkWithTwoComplexCELExpressions(b *testing.B) {
812+
expressions := []apiserver.WebhookMatchCondition{
813+
{
814+
Expression: "request.user == 'alice'",
815+
},
816+
{
817+
Expression: "has(request.resourceAttributes) && request.resourceAttributes.namespace == 'kittensandponies'",
818+
},
819+
}
820+
benchmarkWebhookAuthorizer(b, expressions, true)
821+
}
822+
func BenchmarkWithManyCELExpressions(b *testing.B) {
823+
expressions := []apiserver.WebhookMatchCondition{
824+
{
825+
Expression: "request.user == 'alice'",
826+
},
827+
{
828+
Expression: "request.uid == '1'",
829+
},
830+
{
831+
Expression: "('group1' in request.groups)",
832+
},
833+
{
834+
Expression: "('key1' in request.extra)",
835+
},
836+
{
837+
Expression: "!('key2' in request.extra)",
838+
},
839+
{
840+
Expression: "('a' in request.extra['key1'])",
841+
},
842+
{
843+
Expression: "!('z' in request.extra['key1'])",
844+
},
845+
{
846+
Expression: "has(request.resourceAttributes) && request.resourceAttributes.namespace == 'kittensandponies'",
847+
},
848+
}
849+
benchmarkWebhookAuthorizer(b, expressions, true)
850+
}
851+
852+
func benchmarkWebhookAuthorizer(b *testing.B, expressions []apiserver.WebhookMatchCondition, featureEnabled bool) {
853+
attr := authorizer.AttributesRecord{
854+
User: &user.DefaultInfo{
855+
Name: "alice",
856+
UID: "1",
857+
Groups: []string{"group1", "group2"},
858+
Extra: map[string][]string{"key1": {"a", "b", "c"}},
859+
},
860+
ResourceRequest: true,
861+
Namespace: "kittensandponies",
862+
Verb: "get",
863+
}
864+
service := new(mockV1Service)
865+
service.statusCode = 200
866+
service.Allow()
867+
s, err := NewV1TestServer(service, serverCert, serverKey, caCert)
868+
if err != nil {
869+
b.Fatal(err)
870+
}
871+
defer s.Close()
872+
defer featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.StructuredAuthorizationConfiguration, featureEnabled)()
873+
874+
b.ResetTimer()
875+
for i := 0; i < b.N; i++ {
876+
// Create an authorizer with or without expressions to compile
877+
wh, err := newV1Authorizer(s.URL, clientCert, clientKey, caCert, 0, noopAuthorizerMetrics(), expressions)
878+
if err != nil {
879+
b.Fatal(err)
880+
}
881+
// Call authorize may or may not require cel evaluations
882+
_, _, err = wh.Authorize(context.Background(), attr)
883+
if err != nil {
884+
b.Fatal(err)
885+
}
886+
}
887+
b.StopTimer()
888+
}
889+
785890
// TestV1WebhookMatchConditions verifies cel expressions are compiled and evaluated correctly
786891
func TestV1WebhookMatchConditions(t *testing.T) {
787892
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.StructuredAuthorizationConfiguration, true)()

0 commit comments

Comments
 (0)