-
Notifications
You must be signed in to change notification settings - Fork 15
Optimization fixes #126
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
Closed
Closed
Optimization fixes #126
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ import ( | |||||||||||
| "bytes" | ||||||||||||
| "fmt" | ||||||||||||
| "io" | ||||||||||||
| "sync" | ||||||||||||
|
|
||||||||||||
| nacv1alpha1 "github.com/migtools/oadp-non-admin/api/v1alpha1" | ||||||||||||
| "github.com/pkg/errors" | ||||||||||||
|
|
@@ -32,21 +33,33 @@ import ( | |||||||||||
| "k8s.io/apimachinery/pkg/runtime/serializer" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| var ( | ||||||||||||
| // Singleton scheme instance | ||||||||||||
| nonAdminScheme *runtime.Scheme | ||||||||||||
| nonAdminSchemeOnce sync.Once | ||||||||||||
|
|
||||||||||||
| // Singleton codec factory instance | ||||||||||||
| codecFactory serializer.CodecFactory | ||||||||||||
| codecFactoryOnce sync.Once | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| // NonAdminScheme returns a runtime.Scheme with NonAdmin types registered | ||||||||||||
| func NonAdminScheme() *runtime.Scheme { | ||||||||||||
|
Comment on lines
46
to
47
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| scheme := runtime.NewScheme() | ||||||||||||
| nonAdminSchemeOnce.Do(func() { | ||||||||||||
| nonAdminScheme = runtime.NewScheme() | ||||||||||||
|
|
||||||||||||
| // Add NonAdmin types | ||||||||||||
| if err := nacv1alpha1.AddToScheme(scheme); err != nil { | ||||||||||||
| panic(fmt.Sprintf("failed to add NonAdmin types to scheme: %v", err)) | ||||||||||||
| } | ||||||||||||
| // Add NonAdmin types | ||||||||||||
| if err := nacv1alpha1.AddToScheme(nonAdminScheme); err != nil { | ||||||||||||
| panic(fmt.Sprintf("failed to add NonAdmin types to scheme: %v", err)) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Add Velero types for compatibility | ||||||||||||
| if err := velerov1api.AddToScheme(scheme); err != nil { | ||||||||||||
| panic(fmt.Sprintf("failed to add Velero types to scheme: %v", err)) | ||||||||||||
| } | ||||||||||||
| // Add Velero types for compatibility | ||||||||||||
| if err := velerov1api.AddToScheme(nonAdminScheme); err != nil { | ||||||||||||
| panic(fmt.Sprintf("failed to add Velero types to scheme: %v", err)) | ||||||||||||
| } | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
| return scheme | ||||||||||||
| return nonAdminScheme | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // BindFlags wraps Velero's BindFlags to add output flags | ||||||||||||
|
|
@@ -123,8 +136,10 @@ func encodeTo(obj runtime.Object, format string, w io.Writer) error { | |||||||||||
| func encoderFor(format string, obj runtime.Object) (runtime.Encoder, error) { | ||||||||||||
| var encoder runtime.Encoder | ||||||||||||
|
|
||||||||||||
| // Use NonAdminScheme instead of Velero's scheme | ||||||||||||
| codecFactory := serializer.NewCodecFactory(NonAdminScheme()) | ||||||||||||
| // Initialize codec factory once using singleton scheme | ||||||||||||
| codecFactoryOnce.Do(func() { | ||||||||||||
| codecFactory = serializer.NewCodecFactory(NonAdminScheme()) | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
| desiredMediaType := fmt.Sprintf("application/%s", format) | ||||||||||||
| serializerInfo, found := runtime.SerializerInfoForMediaType(codecFactory.SupportedMediaTypes(), desiredMediaType) | ||||||||||||
|
|
||||||||||||
This file contains hidden or 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 hidden or 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 hidden or 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,115 @@ | ||
| /* | ||
| Copyright 2025 The OADP CLI Contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package shared | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestNewSchemeWithTypesCaching verifies that schemes are cached by configuration | ||
| func TestNewSchemeWithTypesCaching(t *testing.T) { | ||
| opts := ClientOptions{ | ||
| IncludeNonAdminTypes: true, | ||
| IncludeVeleroTypes: true, | ||
| } | ||
|
|
||
| scheme1, err1 := NewSchemeWithTypes(opts) | ||
| scheme2, err2 := NewSchemeWithTypes(opts) | ||
|
|
||
| if err1 != nil || err2 != nil { | ||
| t.Fatal("Unexpected error creating schemes") | ||
| } | ||
|
|
||
| if scheme1 != scheme2 { | ||
| t.Error("Expected same scheme instance for same options") | ||
| } | ||
| } | ||
|
|
||
| // TestNewSchemeWithTypesCachingDifferentOptions verifies different options yield different schemes | ||
| func TestNewSchemeWithTypesCachingDifferentOptions(t *testing.T) { | ||
| opts1 := ClientOptions{ | ||
| IncludeNonAdminTypes: true, | ||
| IncludeVeleroTypes: true, | ||
| } | ||
|
|
||
| opts2 := ClientOptions{ | ||
| IncludeNonAdminTypes: true, | ||
| IncludeVeleroTypes: false, | ||
| } | ||
|
|
||
| scheme1, err1 := NewSchemeWithTypes(opts1) | ||
| scheme2, err2 := NewSchemeWithTypes(opts2) | ||
|
|
||
| if err1 != nil || err2 != nil { | ||
| t.Fatal("Unexpected error creating schemes") | ||
| } | ||
|
|
||
| if scheme1 == scheme2 { | ||
| t.Error("Expected different scheme instances for different options") | ||
| } | ||
| } | ||
|
|
||
| // TestNewSchemeWithTypes verifies that the function creates schemes with correct types | ||
| func TestNewSchemeWithTypes(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| opts ClientOptions | ||
| }{ | ||
| { | ||
| name: "NonAdmin types only", | ||
| opts: ClientOptions{ | ||
| IncludeNonAdminTypes: true, | ||
| }, | ||
| }, | ||
| { | ||
| name: "Velero types only", | ||
| opts: ClientOptions{ | ||
| IncludeVeleroTypes: true, | ||
| }, | ||
| }, | ||
| { | ||
| name: "Core types only", | ||
| opts: ClientOptions{ | ||
| IncludeCoreTypes: true, | ||
| }, | ||
| }, | ||
| { | ||
| name: "All types", | ||
| opts: ClientOptions{ | ||
| IncludeNonAdminTypes: true, | ||
| IncludeVeleroTypes: true, | ||
| IncludeCoreTypes: true, | ||
| }, | ||
| }, | ||
| { | ||
| name: "No types", | ||
| opts: ClientOptions{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| scheme, err := NewSchemeWithTypes(tt.opts) | ||
| if err != nil { | ||
| t.Errorf("NewSchemeWithTypes() unexpected error: %v", err) | ||
| } | ||
| if scheme == nil { | ||
| t.Error("NewSchemeWithTypes() returned nil scheme") | ||
| } | ||
| }) | ||
| } | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here.. |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The schemeCache adds complexity (schemeKey struct, sync.Map, LoadOrStore semantics, shared mutable state) to cache something that is created exactly once per process lifetime. It provides no performance benefit for a short-lived CLI process. The sync.Once approach in
output.go is at least defensible since NonAdminScheme() could theoretically be called from multiple places within one command, but the sync.Map scheme cache in client.go solves a problem that doesn't exist.
do others agree?