Skip to content

Commit 91d2602

Browse files
tmshortclaude
andcommitted
⚡ Apply memory optimizations to catalogd
Apply the same memory optimization patterns used in operator-controller to catalogd for consistent memory management across the codebase: 1. Add cache transform function to strip managed fields and annotations - Removes managed fields from all cached objects - Strips kubectl.kubernetes.io/last-applied-configuration annotations - Applied to all catalogd informer caches 2. Pre-allocate slices with correct capacity - localdir.go: Pre-allocate metaChans with len(storeMetaFuncs) - garbage_collector.go: Pre-allocate removed slice with len(cacheDirEntries) - Reduces allocations and GC pressure during catalog operations These optimizations follow the same patterns that reduced operator-controller memory usage by 38% and should provide similar benefits for catalogd. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 25adc19 commit 91d2602

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

cmd/catalogd/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ func run(ctx context.Context) error {
254254

255255
cacheOptions := crcache.Options{
256256
ByObject: map[client.Object]crcache.ByObject{},
257+
// Memory optimization: strip managed fields and large annotations from cached objects
258+
DefaultTransform: stripManagedFieldsAndAnnotations,
257259
}
258260

259261
saKey, err := sautil.GetServiceAccount()
@@ -450,6 +452,28 @@ func run(ctx context.Context) error {
450452
return nil
451453
}
452454

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+
453477
func podNamespace() string {
454478
namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
455479
if err != nil {

internal/catalogd/garbagecollection/garbage_collector.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ func runGarbageCollection(ctx context.Context, cachePath string, metaClient meta
7979
if err != nil {
8080
return nil, fmt.Errorf("error reading cache directory: %w", err)
8181
}
82-
removed := []string{}
82+
// Pre-allocate removed slice with estimated capacity to avoid reallocation
83+
removed := make([]string, 0, len(cacheDirEntries))
8384
for _, cacheDirEntry := range cacheDirEntries {
8485
if cacheDirEntry.IsDir() && expectedCatalogs.Has(cacheDirEntry.Name()) {
8586
continue

internal/catalogd/storage/localdir.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func (s *LocalDirV1) Store(ctx context.Context, catalog string, fsys fs.FS) erro
6565
}
6666

6767
eg, egCtx := errgroup.WithContext(ctx)
68-
metaChans := []chan *declcfg.Meta{}
68+
// Pre-allocate metaChans with correct capacity to avoid reallocation
69+
metaChans := make([]chan *declcfg.Meta, 0, len(storeMetaFuncs))
6970

7071
for range storeMetaFuncs {
7172
metaChans = append(metaChans, make(chan *declcfg.Meta, 1))

0 commit comments

Comments
 (0)