Skip to content
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

perf: limit initSchema calls from openapi.IsNamespaceScoped #5076

Merged
merged 6 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions kyaml/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,40 @@ func GetSchema(s string, schema *spec.Schema) (*ResourceSchema, error) {
// be true if the resource is namespace-scoped, and false if the type is
// cluster-scoped.
func IsNamespaceScoped(typeMeta yaml.TypeMeta) (bool, bool) {
if res, f := precomputedIsNamespaceScoped[typeMeta]; f {
return res, true
if isNamespaceScoped, found := precomputedIsNamespaceScoped[typeMeta]; found {
return isNamespaceScoped, found
}
if isInitSchemaNeededForNamespaceScopeCheck() {
initSchema()
}
return isNamespaceScopedFromSchema(typeMeta)
}

func isNamespaceScopedFromSchema(typeMeta yaml.TypeMeta) (bool, bool) {
initSchema()
isNamespaceScoped, found := globalSchema.namespaceabilityByResourceType[typeMeta]
return isNamespaceScoped, found
}

// isInitSchemaNeededForNamespaceScopeCheck returns true if initSchema is needed
// to ensure globalSchema.namespaceabilityByResourceType is fully populated for
// cases where a custom or non-default built-in schema is in use.
func isInitSchemaNeededForNamespaceScopeCheck() bool {
schemaLock.Lock()
defer schemaLock.Unlock()

if globalSchema.schemaInit {
return false // globalSchema already is initialized.
}
if customSchema != nil {
return true // initSchema is needed.
}
if kubernetesOpenAPIVersion == "" || kubernetesOpenAPIVersion == kubernetesOpenAPIDefaultVersion {
// The default built-in schema is in use. Since
// precomputedIsNamespaceScoped aligns with the default built-in schema
// (verified by TestIsNamespaceScopedPrecompute), there is no need to
// call initSchema.
return false
}
// A non-default built-in schema is in use. initSchema is needed.
return true
}

// IsCertainlyClusterScoped returns true for Node, Namespace, etc. and
// false for Pod, Deployment, etc. and kinds that aren't recognized in the
// openapi data. See:
Expand Down
17 changes: 17 additions & 0 deletions kyaml/openapi/openapi_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package openapi

import (
"path/filepath"
"sigs.k8s.io/kustomize/kyaml/yaml"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move this import down with the second group of imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - thanks for the catch.

"strings"
"testing"

Expand All @@ -31,3 +32,19 @@ func BenchmarkProtoUnmarshal(t *testing.B) {
}
}
}

func BenchmarkPrecomputedIsNamespaceScoped(b *testing.B) {
ephesused marked this conversation as resolved.
Show resolved Hide resolved
testcases := map[string]yaml.TypeMeta{
"namespace scoped": {APIVersion: "apps/v1", Kind: "ControllerRevision"},
"cluster scoped": {APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
"unknown resource": {APIVersion: "custom.io/v1", Kind: "Custom"},
}
for name, testcase := range testcases {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
ResetOpenAPI()
_, _ = IsNamespaceScoped(testcase)
}
})
}
}