-
Notifications
You must be signed in to change notification settings - Fork 68
🐛 OPRUN-4239: Memory usage improvements #2290
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
Merged
openshift-merge-bot
merged 1 commit into
operator-framework:main
from
tmshort:memory-improvements
Nov 5, 2025
+114
−15
Merged
Changes from all commits
Commits
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
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
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,71 @@ | ||
| package cache | ||
|
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. nit: perhaps better location for this file is |
||
|
|
||
| import ( | ||
| "maps" | ||
|
|
||
| toolscache "k8s.io/client-go/tools/cache" | ||
| crcache "sigs.k8s.io/controller-runtime/pkg/cache" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| // stripAnnotations removes memory-heavy annotations that aren't needed for controller operations. | ||
| func stripAnnotations(obj interface{}) (interface{}, error) { | ||
| if metaObj, ok := obj.(client.Object); ok { | ||
| // Remove the last-applied-configuration annotation which can be very large | ||
| // Clone the annotations map to avoid modifying shared references | ||
| annotations := metaObj.GetAnnotations() | ||
| if annotations != nil { | ||
| annotations = maps.Clone(annotations) | ||
anik120 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| delete(annotations, "kubectl.kubernetes.io/last-applied-configuration") | ||
| if len(annotations) == 0 { | ||
| metaObj.SetAnnotations(nil) | ||
| } else { | ||
| metaObj.SetAnnotations(annotations) | ||
| } | ||
| } | ||
| } | ||
| return obj, nil | ||
| } | ||
|
|
||
| // StripManagedFieldsAndAnnotations returns a cache transform function that removes | ||
| // memory-heavy fields that aren't needed for controller operations. | ||
| // This significantly reduces memory usage in informer caches by removing: | ||
| // - Managed fields (can be several KB per object) | ||
| // - kubectl.kubernetes.io/last-applied-configuration annotation (can be very large) | ||
| // | ||
| // Use this function as a DefaultTransform in controller-runtime cache.Options | ||
| // to reduce memory overhead across all cached objects. | ||
| // | ||
| // Example: | ||
| // | ||
| // cacheOptions := cache.Options{ | ||
| // DefaultTransform: cacheutil.StripManagedFieldsAndAnnotations(), | ||
| // } | ||
| func StripManagedFieldsAndAnnotations() toolscache.TransformFunc { | ||
| // Use controller-runtime's built-in TransformStripManagedFields and compose it | ||
| // with our custom annotation stripping transform | ||
| managedFieldsTransform := crcache.TransformStripManagedFields() | ||
|
|
||
| return func(obj interface{}) (interface{}, error) { | ||
| // First strip managed fields using controller-runtime's transform | ||
| obj, err := managedFieldsTransform(obj) | ||
| if err != nil { | ||
| return obj, err | ||
| } | ||
|
|
||
| // Then strip the large annotations | ||
| return stripAnnotations(obj) | ||
| } | ||
| } | ||
|
|
||
| // ApplyStripTransform applies the strip transform directly to an object. | ||
| // This is a convenience function for cases where you need to strip fields | ||
| // from an object outside of the cache transform context. | ||
| // | ||
| // Note: This function never returns an error in practice, but returns error | ||
| // to satisfy the TransformFunc interface. | ||
| func ApplyStripTransform(obj client.Object) error { | ||
| transform := StripManagedFieldsAndAnnotations() | ||
| _, err := transform(obj) | ||
| return err | ||
| } | ||
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.
Suggestion:
Just a small nit — this isn’t valid only for Boxcutter since we’re not protecting it with a feature flag.
Could we clarify that in the title? As it stands, it gives the impression that the change only applies when the flag is enabled.
Alternatively, if we don’t want it to be used by default, we could protect the change with the flag.