-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package v1 | ||
|
|
||
| // GetSpec returns the Spec field of the ClusterExtension. | ||
| // This method is provided to satisfy generic interfaces that require a GetSpec() method. | ||
| func (c *ClusterExtension) GetSpec() ClusterExtensionSpec { | ||
| return c.Spec | ||
| } | ||
|
|
||
| // GetSpec returns the Spec field of the ClusterCatalog. | ||
| // This method is provided to satisfy generic interfaces that require a GetSpec() method. | ||
| func (c *ClusterCatalog) GetSpec() ClusterCatalogSpec { | ||
| return c.Spec | ||
| } | ||
|
|
||
| // GetSpec returns the Spec field of the ClusterExtensionRevision. | ||
| // This method is provided to satisfy generic interfaces that require a GetSpec() method. | ||
| func (c *ClusterExtensionRevision) GetSpec() ClusterExtensionRevisionSpec { | ||
| return c.Spec | ||
| } | ||
| 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 | ||
| } |
|
Contributor
Author
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. Not a fan of this package name, but I couldn't think of anything better? Suggestions welcome. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package k8s | ||
|
|
||
| import ( | ||
| "k8s.io/apimachinery/pkg/api/equality" | ||
| ) | ||
|
|
||
| // ObjectWithSpec represents any Kubernetes custom resource that embeds metav1.ObjectMeta | ||
| // and has a Spec field. The type parameter T should be the Spec type. | ||
| type ObjectWithSpec[T any] interface { | ||
| GetAnnotations() map[string]string | ||
| GetLabels() map[string]string | ||
| GetSpec() T | ||
| } | ||
|
Comment on lines
+9
to
+13
Contributor
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. How about that we do not introduce this interface and get access to func CheckForUnexpectedFieldChange[T metav1.Object](a, b T) booland we do not need to implement any additional interface on our API. |
||
|
|
||
| // 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) | ||
| // - Spec (via GetSpec, using semantic equality) | ||
| // | ||
| // Status and finalizers are intentionally not compared, as these are expected | ||
| // to change during reconciliation. | ||
| // | ||
| // Type parameters: | ||
| // - T: The Spec type for the object (e.g., ClusterExtensionSpec, ClusterCatalogSpec) | ||
| // - O: The object type that implements ObjectWithSpec[T] | ||
| // | ||
| // This function works with any object that implements the ObjectWithSpec interface, | ||
| // which requires GetAnnotations(), GetLabels(), and GetSpec() methods. | ||
| func CheckForUnexpectedFieldChange[T any, O ObjectWithSpec[T]](a, b O) bool { | ||
| if !equality.Semantic.DeepEqual(a.GetAnnotations(), b.GetAnnotations()) { | ||
| return true | ||
| } | ||
| if !equality.Semantic.DeepEqual(a.GetLabels(), b.GetLabels()) { | ||
| return true | ||
| } | ||
| return !equality.Semantic.DeepEqual(a.GetSpec(), b.GetSpec()) | ||
| } | ||
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.
It is recommended to avoid adding helper functions:
https://github.com/openshift/enhancements/blob/master/dev-guide/api-conventions.md#no-functions