-
Notifications
You must be signed in to change notification settings - Fork 68
🌱 Use Patch instead of Update for finalizer operations #2328
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 2 commits into
operator-framework:main
from
tmshort:apply-finalizers
Dec 2, 2025
+405
−25
Merged
Changes from all commits
Commits
Show all changes
2 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
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,91 @@ | ||
| package cache | ||
|
|
||
| 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) | ||
| 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) | ||
| } | ||
| } | ||
|
|
||
| // StripAnnotations returns a cache transform function that removes | ||
| // memory-heavy annotation fields that aren't needed for controller operations. | ||
| // This significantly reduces memory usage in informer caches by removing: | ||
| // - 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.StripAnnotations(), | ||
| // } | ||
| func StripAnnotations() toolscache.TransformFunc { | ||
| return func(obj interface{}) (interface{}, error) { | ||
| // Strip the large annotations | ||
| return stripAnnotations(obj) | ||
| } | ||
| } | ||
|
|
||
| // ApplyStripAnnotationsTransform 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 ApplyStripAnnotationsTransform(obj client.Object) error { | ||
| transform := StripAnnotations() | ||
| _, err := transform(obj) | ||
| return err | ||
| } |
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,54 @@ | ||
| package k8s | ||
|
|
||
| import ( | ||
| "reflect" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/equality" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // CheckForUnexpectedFieldChange compares two Kubernetes objects and returns true | ||
| // if their annotations, labels, or spec have changed. This is useful for detecting | ||
| // unexpected modifications during reconciliation. | ||
| // | ||
| // The function compares: | ||
| // - Annotations (via GetAnnotations) | ||
| // - Labels (via GetLabels) | ||
pedjak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // - Spec (using reflection to access the Spec field, with semantic equality) | ||
| // | ||
| // Status and finalizers are intentionally not compared, as these are expected | ||
| // to change during reconciliation. | ||
| // | ||
| // This function uses reflection to access the Spec field, so no explicit GetSpec() | ||
| // method is required. The objects must have a field named "Spec". | ||
| func CheckForUnexpectedFieldChange(a, b metav1.Object) bool { | ||
| if !equality.Semantic.DeepEqual(a.GetAnnotations(), b.GetAnnotations()) { | ||
| return true | ||
| } | ||
| if !equality.Semantic.DeepEqual(a.GetLabels(), b.GetLabels()) { | ||
| return true | ||
| } | ||
|
|
||
| // Use reflection to access the Spec field | ||
| aVal := reflect.ValueOf(a) | ||
| bVal := reflect.ValueOf(b) | ||
|
|
||
| // Handle pointer types | ||
| if aVal.Kind() == reflect.Ptr { | ||
| aVal = aVal.Elem() | ||
| } | ||
| if bVal.Kind() == reflect.Ptr { | ||
| bVal = bVal.Elem() | ||
| } | ||
|
|
||
| // Get the Spec field from both objects | ||
| aSpec := aVal.FieldByName("Spec") | ||
| bSpec := bVal.FieldByName("Spec") | ||
|
|
||
| // If either Spec field is invalid, return false (no change detected) | ||
| if !aSpec.IsValid() || !bSpec.IsValid() { | ||
| return false | ||
| } | ||
|
|
||
| return !equality.Semantic.DeepEqual(aSpec.Interface(), bSpec.Interface()) | ||
| } | ||
Oops, something went wrong.
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.
Not a fan of this package name, but I couldn't think of anything better? Suggestions welcome.