Skip to content

Commit 0041df0

Browse files
feat: set transform to strip managedfields for informer (#2149)
* feat: set transform to strip managedfields for informer
1 parent f2869f7 commit 0041df0

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

cmd/yurt-manager/app/manager.go

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/spf13/cobra"
2525
"github.com/spf13/pflag"
26+
"k8s.io/apimachinery/pkg/api/meta"
2627
"k8s.io/apimachinery/pkg/runtime"
2728
"k8s.io/apimachinery/pkg/util/wait"
2829
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -34,6 +35,7 @@ import (
3435
"k8s.io/klog/v2"
3536
"k8s.io/klog/v2/klogr"
3637
ctrl "sigs.k8s.io/controller-runtime"
38+
"sigs.k8s.io/controller-runtime/pkg/cache"
3739
"sigs.k8s.io/controller-runtime/pkg/healthz"
3840
"sigs.k8s.io/controller-runtime/pkg/metrics"
3941
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -175,6 +177,14 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
175177
metricsServerOpts.ExtraHandlers[path] = handler
176178
}
177179

180+
trimManagedFields := func(obj interface{}) (interface{}, error) {
181+
if accessor, err := meta.Accessor(obj); err == nil {
182+
if accessor.GetManagedFields() != nil {
183+
accessor.SetManagedFields(nil)
184+
}
185+
}
186+
return obj, nil
187+
}
178188
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
179189
Scheme: scheme,
180190
Metrics: metricsServerOpts,
@@ -189,6 +199,9 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
189199
CertDir: util.GetCertDir(),
190200
}),
191201
Logger: setupLog,
202+
Cache: cache.Options{
203+
DefaultTransform: trimManagedFields,
204+
},
192205
})
193206
if err != nil {
194207
setupLog.Error(err, "unable to start manager")

cmd/yurthub/app/config/config.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646

4747
"github.com/openyurtio/openyurt/cmd/yurthub/app/options"
4848
"github.com/openyurtio/openyurt/pkg/projectinfo"
49+
pkgutil "github.com/openyurtio/openyurt/pkg/util"
4950
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
5051
"github.com/openyurtio/openyurt/pkg/yurthub/certificate"
5152
certificatemgr "github.com/openyurtio/openyurt/pkg/yurthub/certificate/manager"
@@ -277,19 +278,24 @@ func registerInformers(options *options.YurtHubOptions,
277278
informerFactory informers.SharedInformerFactory,
278279
workingMode util.WorkingMode,
279280
tenantNs string) {
281+
280282
// configmap informer is used by Yurthub filter approver
281283
newConfigmapInformer := func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
282284
tweakListOptions := func(options *metav1.ListOptions) {
283285
options.FieldSelector = fields.Set{"metadata.name": util.YurthubConfigMapName}.String()
284286
}
285-
return coreinformers.NewFilteredConfigMapInformer(client, options.YurtHubNamespace, resyncPeriod, nil, tweakListOptions)
287+
informer := coreinformers.NewFilteredConfigMapInformer(client, options.YurtHubNamespace, resyncPeriod, nil, tweakListOptions)
288+
informer.SetTransform(pkgutil.TransformStripManagedFields())
289+
return informer
286290
}
287291
informerFactory.InformerFor(&corev1.ConfigMap{}, newConfigmapInformer)
288292

289293
// secret informer is used by Tenant manager, this feature is not enabled in general.
290294
if tenantNs != "" {
291295
newSecretInformer := func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
292-
return coreinformers.NewFilteredSecretInformer(client, tenantNs, resyncPeriod, nil, nil)
296+
informer := coreinformers.NewFilteredSecretInformer(client, tenantNs, resyncPeriod, nil, nil)
297+
informer.SetTransform(pkgutil.TransformStripManagedFields())
298+
return informer
293299
}
294300
informerFactory.InformerFor(&corev1.Secret{}, newSecretInformer)
295301
}
@@ -300,10 +306,20 @@ func registerInformers(options *options.YurtHubOptions,
300306
listOptions := func(ops *metav1.ListOptions) {
301307
ops.FieldSelector = fields.Set{"spec.nodeName": options.NodeName}.String()
302308
}
303-
return coreinformers.NewFilteredPodInformer(client, "", resyncPeriod, nil, listOptions)
309+
informer := coreinformers.NewFilteredPodInformer(client, "", resyncPeriod, nil, listOptions)
310+
informer.SetTransform(pkgutil.TransformStripManagedFields())
311+
return informer
304312
}
305313
informerFactory.InformerFor(&corev1.Pod{}, newPodInformer)
306314
}
315+
316+
// service informer is used by serviceTopologyFilter
317+
newServiceInformer := func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
318+
informer := coreinformers.NewFilteredServiceInformer(client, "", resyncPeriod, nil, nil)
319+
informer.SetTransform(pkgutil.TransformStripManagedFields())
320+
return informer
321+
}
322+
informerFactory.InformerFor(&corev1.Service{}, newServiceInformer)
307323
}
308324

309325
func prepareServerServing(options *options.YurtHubOptions, certMgr certificate.YurtCertificateManager, cfg *YurtHubConfiguration) error {

pkg/util/util.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ limitations under the License.
1616

1717
package util
1818

19-
import "reflect"
19+
import (
20+
"reflect"
21+
22+
"k8s.io/apimachinery/pkg/api/meta"
23+
"k8s.io/client-go/tools/cache"
24+
)
2025

2126
func IsNil(i interface{}) bool {
2227
if i == nil {
@@ -30,6 +35,18 @@ func IsNil(i interface{}) bool {
3035
return false
3136
}
3237

38+
// Dropping `.metadata.managedFields` to improve memory usage
39+
func TransformStripManagedFields() cache.TransformFunc {
40+
return func(in any) (any, error) {
41+
// Nilcheck managed fields to avoid hitting https://github.com/kubernetes/kubernetes/issues/124337
42+
if obj, err := meta.Accessor(in); err == nil && obj.GetManagedFields() != nil {
43+
obj.SetManagedFields(nil)
44+
}
45+
46+
return in, nil
47+
}
48+
}
49+
3350
const (
3451
// HttpHeaderContentType HTTP request header keyword: Content-Type which is used in HTTP request and response
3552
// headers to specify the media type of the entity body

0 commit comments

Comments
 (0)