From 5986173a1308b3d7865edd6e070bdba738f7e60e Mon Sep 17 00:00:00 2001 From: dongjiang1989 Date: Fri, 10 Nov 2023 15:34:43 +0800 Subject: [PATCH 1/5] optimize informers transformers Signed-off-by: dongjiang1989 --- pkg/util/transformer/metadata_transformer.go | 37 ++++++++++++++++++++ pkg/util/transformer/transformers.go | 4 +++ 2 files changed, 41 insertions(+) create mode 100644 pkg/util/transformer/metadata_transformer.go diff --git a/pkg/util/transformer/metadata_transformer.go b/pkg/util/transformer/metadata_transformer.go new file mode 100644 index 000000000..200dc3ba3 --- /dev/null +++ b/pkg/util/transformer/metadata_transformer.go @@ -0,0 +1,37 @@ +/* +Copyright 2023 The Koordinator Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package transformer + +import ( + "fmt" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/cache" +) + +var _ cache.TransformFunc = PartialMetadataRemoveTransform + +// partialMetadataRemoveAll implements a cache.TransformFunc that removes managed +// fields from PartialObjectMetadata. +func PartialMetadataRemoveTransform(obj interface{}) (interface{}, error) { + partialMeta, ok := obj.(*metav1.PartialObjectMetadata) + if !ok { + return nil, fmt.Errorf("internal error: cannot cast object %#+v to PartialObjectMetadata", obj) + } + partialMeta.ManagedFields = nil + return partialMeta, nil +} diff --git a/pkg/util/transformer/transformers.go b/pkg/util/transformer/transformers.go index f2833f093..040930b51 100644 --- a/pkg/util/transformer/transformers.go +++ b/pkg/util/transformer/transformers.go @@ -45,5 +45,9 @@ func SetupTransformers(informerFactory informers.SharedInformerFactory, koordInf if err := informer.Informer().SetTransform(transformFn); err != nil { klog.Fatalf("Failed to SetTransform in informer, resource: %v, err: %v", resource, err) } + // clean up partial metadata + if err := informer.Informer().SetTransform(PartialMetadataRemoveTransform); err != nil { + klog.Fatalf("Failed to PartialMetadataRemoveTransform in informer, resource: %v, err: %v", resource, err) + } } } From 6a0352caa26ae5f8badd579446160cce1289e0b8 Mon Sep 17 00:00:00 2001 From: dongjiang1989 Date: Fri, 10 Nov 2023 16:15:45 +0800 Subject: [PATCH 2/5] fix license-header-checker Signed-off-by: dongjiang1989 --- pkg/util/transformer/metadata_transformer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/util/transformer/metadata_transformer.go b/pkg/util/transformer/metadata_transformer.go index 200dc3ba3..50bb076ec 100644 --- a/pkg/util/transformer/metadata_transformer.go +++ b/pkg/util/transformer/metadata_transformer.go @@ -1,5 +1,5 @@ /* -Copyright 2023 The Koordinator Authors. +Copyright 2022 The Koordinator Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 1f11ff03e6a4cc24ee2e4caa4a99f794c9024dd1 Mon Sep 17 00:00:00 2001 From: dongjiang1989 Date: Mon, 13 Nov 2023 14:33:20 +0800 Subject: [PATCH 3/5] add unittest case Signed-off-by: dongjiang1989 --- .../transformer/metadata_transformer_test.go | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 pkg/util/transformer/metadata_transformer_test.go diff --git a/pkg/util/transformer/metadata_transformer_test.go b/pkg/util/transformer/metadata_transformer_test.go new file mode 100644 index 000000000..9380ec363 --- /dev/null +++ b/pkg/util/transformer/metadata_transformer_test.go @@ -0,0 +1,66 @@ +/* +Copyright 2022 The Koordinator Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package transformer + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestTransformMetadata(t *testing.T) { + tests := []struct { + name string + pod *metav1.PartialObjectMetadata + wantPod *metav1.PartialObjectMetadata + }{ + { + name: "normal pod metadata transform", + pod: &metav1.PartialObjectMetadata{ + TypeMeta: metav1.TypeMeta{ + Kind: "Pod", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + ManagedFields: []metav1.ManagedFieldsEntry{}, + }, + }, + + wantPod: &metav1.PartialObjectMetadata{ + TypeMeta: metav1.TypeMeta{ + Kind: "Pod", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + ManagedFields: nil, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + obj, err := PartialMetadataRemoveTransform(tt.pod) + assert.NoError(t, err) + assert.Equal(t, tt.wantPod, obj) + }) + } +} From c7786e2cedf030b47dbc9e983ee3d0b48bca9336 Mon Sep 17 00:00:00 2001 From: dongjiang1989 Date: Mon, 13 Nov 2023 15:57:16 +0800 Subject: [PATCH 4/5] update metadata_transformer_test.go Signed-off-by: dongjiang1989 --- pkg/util/transformer/metadata_transformer_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/util/transformer/metadata_transformer_test.go b/pkg/util/transformer/metadata_transformer_test.go index 9380ec363..d40bc583d 100644 --- a/pkg/util/transformer/metadata_transformer_test.go +++ b/pkg/util/transformer/metadata_transformer_test.go @@ -64,3 +64,8 @@ func TestTransformMetadata(t *testing.T) { }) } } + +func TestTransformMetadataError(t *testing.T) { + _, err := PartialMetadataRemoveTransform(&metav1.TypeMeta{}) + assert.Error(t, err) +} From f4337b6e0cc7c5b41c48b402d4b12da475388bf1 Mon Sep 17 00:00:00 2001 From: dongjiang1989 Date: Sun, 4 Feb 2024 16:12:35 +0800 Subject: [PATCH 5/5] update metadata transformer Signed-off-by: dongjiang1989 --- pkg/util/transformer/metadata_transformer.go | 43 ++++++++++++++----- .../transformer/metadata_transformer_test.go | 6 +-- pkg/util/transformer/transformers.go | 6 +-- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/pkg/util/transformer/metadata_transformer.go b/pkg/util/transformer/metadata_transformer.go index 50bb076ec..7dbcf720e 100644 --- a/pkg/util/transformer/metadata_transformer.go +++ b/pkg/util/transformer/metadata_transformer.go @@ -17,21 +17,44 @@ limitations under the License. package transformer import ( - "fmt" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/cache" + "k8s.io/klog/v2" ) -var _ cache.TransformFunc = PartialMetadataRemoveTransform +var metadataTransformers = []func(meta *metav1.PartialObjectMetadata){ + TransformPartialMetadataRemoveResources, +} + +func InstallMetadataTransformer(informer cache.SharedIndexInformer) { + if err := informer.SetTransform(TransformPod); err != nil { + klog.Fatalf("Failed to SetTransform with metadata, err: %v", err) + } +} + +func TransformMeta(obj interface{}) (interface{}, error) { + var meta *metav1.PartialObjectMetadata + switch t := obj.(type) { + case *metav1.PartialObjectMetadata: + meta = t + case cache.DeletedFinalStateUnknown: + meta, _ = t.Obj.(*metav1.PartialObjectMetadata) + } + if meta == nil { + return obj, nil + } + + for _, fn := range metadataTransformers { + fn(meta) + } -// partialMetadataRemoveAll implements a cache.TransformFunc that removes managed -// fields from PartialObjectMetadata. -func PartialMetadataRemoveTransform(obj interface{}) (interface{}, error) { - partialMeta, ok := obj.(*metav1.PartialObjectMetadata) - if !ok { - return nil, fmt.Errorf("internal error: cannot cast object %#+v to PartialObjectMetadata", obj) + if unknown, ok := obj.(cache.DeletedFinalStateUnknown); ok { + unknown.Obj = meta + return unknown, nil } + return meta, nil +} + +func TransformPartialMetadataRemoveResources(partialMeta *metav1.PartialObjectMetadata) { partialMeta.ManagedFields = nil - return partialMeta, nil } diff --git a/pkg/util/transformer/metadata_transformer_test.go b/pkg/util/transformer/metadata_transformer_test.go index d40bc583d..5886e76e1 100644 --- a/pkg/util/transformer/metadata_transformer_test.go +++ b/pkg/util/transformer/metadata_transformer_test.go @@ -58,7 +58,7 @@ func TestTransformMetadata(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - obj, err := PartialMetadataRemoveTransform(tt.pod) + obj, err := TransformMeta(tt.pod) assert.NoError(t, err) assert.Equal(t, tt.wantPod, obj) }) @@ -66,6 +66,6 @@ func TestTransformMetadata(t *testing.T) { } func TestTransformMetadataError(t *testing.T) { - _, err := PartialMetadataRemoveTransform(&metav1.TypeMeta{}) - assert.Error(t, err) + _, err := TransformMeta(&metav1.PartialObjectMetadata{}) + assert.Nil(t, err) } diff --git a/pkg/util/transformer/transformers.go b/pkg/util/transformer/transformers.go index 040930b51..4c4972ed1 100644 --- a/pkg/util/transformer/transformers.go +++ b/pkg/util/transformer/transformers.go @@ -18,6 +18,7 @@ package transformer import ( corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/informers" "k8s.io/client-go/tools/cache" @@ -31,6 +32,7 @@ var transformers = map[schema.GroupVersionResource]cache.TransformFunc{ corev1.SchemeGroupVersion.WithResource("nodes"): TransformNode, corev1.SchemeGroupVersion.WithResource("pods"): TransformPod, schedulingv1alpha1.SchemeGroupVersion.WithResource("devices"): TransformDevice, + metav1.SchemeGroupVersion.WithResource("metas"): TransformMeta, } func SetupTransformers(informerFactory informers.SharedInformerFactory, koordInformerFactory koordinformers.SharedInformerFactory) { @@ -45,9 +47,5 @@ func SetupTransformers(informerFactory informers.SharedInformerFactory, koordInf if err := informer.Informer().SetTransform(transformFn); err != nil { klog.Fatalf("Failed to SetTransform in informer, resource: %v, err: %v", resource, err) } - // clean up partial metadata - if err := informer.Informer().SetTransform(PartialMetadataRemoveTransform); err != nil { - klog.Fatalf("Failed to PartialMetadataRemoveTransform in informer, resource: %v, err: %v", resource, err) - } } }