Skip to content

Commit 9f1b033

Browse files
tmshortclaude
andcommitted
♻️ Refactor: Share cache transform function between components
Extract the stripManagedFieldsAndAnnotations function to a shared utility package to eliminate code duplication between operator-controller and catalogd. Changes: - Created internal/shared/util/cache/transform.go with StripManagedFieldsAndAnnotations function - Updated cmd/operator-controller/main.go to use shared implementation - Updated cmd/catalogd/main.go to use shared implementation - Removed duplicate function definitions (46 lines of duplication removed) This improves maintainability by having a single source of truth for the cache transform logic used across both components. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 91d2602 commit 9f1b033

File tree

3 files changed

+34
-46
lines changed

3 files changed

+34
-46
lines changed

cmd/catalogd/main.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import (
5959
"github.com/operator-framework/operator-controller/internal/catalogd/storage"
6060
"github.com/operator-framework/operator-controller/internal/catalogd/webhook"
6161
sharedcontrollers "github.com/operator-framework/operator-controller/internal/shared/controllers"
62+
cacheutil "github.com/operator-framework/operator-controller/internal/shared/util/cache"
6263
fsutil "github.com/operator-framework/operator-controller/internal/shared/util/fs"
6364
httputil "github.com/operator-framework/operator-controller/internal/shared/util/http"
6465
imageutil "github.com/operator-framework/operator-controller/internal/shared/util/image"
@@ -255,7 +256,7 @@ func run(ctx context.Context) error {
255256
cacheOptions := crcache.Options{
256257
ByObject: map[client.Object]crcache.ByObject{},
257258
// Memory optimization: strip managed fields and large annotations from cached objects
258-
DefaultTransform: stripManagedFieldsAndAnnotations,
259+
DefaultTransform: cacheutil.StripManagedFieldsAndAnnotations,
259260
}
260261

261262
saKey, err := sautil.GetServiceAccount()
@@ -452,28 +453,6 @@ func run(ctx context.Context) error {
452453
return nil
453454
}
454455

455-
// stripManagedFieldsAndAnnotations is a cache transform function that removes
456-
// memory-heavy fields that aren't needed for controller operations.
457-
// This significantly reduces memory usage in informer caches.
458-
func stripManagedFieldsAndAnnotations(obj interface{}) (interface{}, error) {
459-
if metaObj, ok := obj.(client.Object); ok {
460-
// Remove managed fields - these can be several KB per object
461-
metaObj.SetManagedFields(nil)
462-
463-
// Remove the last-applied-configuration annotation which can be very large
464-
annotations := metaObj.GetAnnotations()
465-
if annotations != nil {
466-
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
467-
if len(annotations) == 0 {
468-
metaObj.SetAnnotations(nil)
469-
} else {
470-
metaObj.SetAnnotations(annotations)
471-
}
472-
}
473-
}
474-
return obj, nil
475-
}
476-
477456
func podNamespace() string {
478457
namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
479458
if err != nil {

cmd/operator-controller/main.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import (
7878
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/render/registryv1"
7979
"github.com/operator-framework/operator-controller/internal/operator-controller/scheme"
8080
sharedcontrollers "github.com/operator-framework/operator-controller/internal/shared/controllers"
81+
cacheutil "github.com/operator-framework/operator-controller/internal/shared/util/cache"
8182
fsutil "github.com/operator-framework/operator-controller/internal/shared/util/fs"
8283
httputil "github.com/operator-framework/operator-controller/internal/shared/util/http"
8384
imageutil "github.com/operator-framework/operator-controller/internal/shared/util/image"
@@ -233,7 +234,7 @@ func run() error {
233234
},
234235
DefaultLabelSelector: k8slabels.Nothing(),
235236
// Memory optimization: strip managed fields and large annotations from cached objects
236-
DefaultTransform: stripManagedFieldsAndAnnotations,
237+
DefaultTransform: cacheutil.StripManagedFieldsAndAnnotations,
237238
}
238239

239240
if features.OperatorControllerFeatureGate.Enabled(features.BoxcutterRuntime) {
@@ -688,28 +689,6 @@ func setupHelm(
688689
return nil
689690
}
690691

691-
// stripManagedFieldsAndAnnotations is a cache transform function that removes
692-
// memory-heavy fields that aren't needed for controller operations.
693-
// This significantly reduces memory usage in informer caches.
694-
func stripManagedFieldsAndAnnotations(obj interface{}) (interface{}, error) {
695-
if metaObj, ok := obj.(client.Object); ok {
696-
// Remove managed fields - these can be several KB per object
697-
metaObj.SetManagedFields(nil)
698-
699-
// Remove the last-applied-configuration annotation which can be very large
700-
annotations := metaObj.GetAnnotations()
701-
if annotations != nil {
702-
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
703-
if len(annotations) == 0 {
704-
metaObj.SetAnnotations(nil)
705-
} else {
706-
metaObj.SetAnnotations(annotations)
707-
}
708-
}
709-
}
710-
return obj, nil
711-
}
712-
713692
func main() {
714693
if err := operatorControllerCmd.Execute(); err != nil {
715694
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cache
2+
3+
import "sigs.k8s.io/controller-runtime/pkg/client"
4+
5+
// StripManagedFieldsAndAnnotations is a cache transform function that removes
6+
// memory-heavy fields that aren't needed for controller operations.
7+
// This significantly reduces memory usage in informer caches by removing:
8+
// - Managed fields (can be several KB per object)
9+
// - kubectl.kubernetes.io/last-applied-configuration annotation (can be very large)
10+
//
11+
// Use this function as a DefaultTransform in controller-runtime cache.Options
12+
// to reduce memory overhead across all cached objects.
13+
func StripManagedFieldsAndAnnotations(obj interface{}) (interface{}, error) {
14+
if metaObj, ok := obj.(client.Object); ok {
15+
// Remove managed fields - these can be several KB per object
16+
metaObj.SetManagedFields(nil)
17+
18+
// Remove the last-applied-configuration annotation which can be very large
19+
annotations := metaObj.GetAnnotations()
20+
if annotations != nil {
21+
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
22+
if len(annotations) == 0 {
23+
metaObj.SetAnnotations(nil)
24+
} else {
25+
metaObj.SetAnnotations(annotations)
26+
}
27+
}
28+
}
29+
return obj, nil
30+
}

0 commit comments

Comments
 (0)