Skip to content

Commit

Permalink
- Updated cache.Options with a DefaultTransform function that rem…
Browse files Browse the repository at this point in the history
…oves managed fields and the `"kubectl.kubernetes.io/last-applied-configuration"` annotation from objects before storing them in the cache. (#2300)

- Stripping managed fields reduces cache memory overhead, which improves performance in large clusters.
- Removing the `"last-applied-configuration"` annotation prevents unnecessary cache bloat and reduces redundant reconciliation triggers caused by changes to this field, which does not require for operation.

This enhancement minimizes cache size and resource usage, improving controller runtime performance and memory management.

Co-authored-by: afdesk <[email protected]>
  • Loading branch information
mjshastha and afdesk authored Nov 20, 2024
1 parent 265309e commit 1880d76
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
mp "github.com/aquasecurity/trivy/pkg/policy"
"github.com/bluele/gcache"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -73,6 +74,23 @@ func Start(ctx context.Context, buildInfo trivyoperator.BuildInfo, operatorConfi
},
},
},
Cache: cache.Options{
DefaultTransform: func(obj interface{}) (interface{}, error) {
obj, err := cache.TransformStripManagedFields()(obj)
if err != nil {
return obj, err
}
if metaObj, ok := obj.(metav1.ObjectMetaAccessor); ok {
annotations := metaObj.GetObjectMeta().GetAnnotations()
if annotations != nil {
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
metaObj.GetObjectMeta().SetAnnotations(annotations)
}
}

return obj, nil
},
},
Controller: controllerconfig.Controller{SkipNameValidation: &skipNameValidation},
}

Expand Down

0 comments on commit 1880d76

Please sign in to comment.