Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: optimize informers transformers #1736

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
37 changes: 37 additions & 0 deletions pkg/util/transformer/metadata_transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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 (
"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
}
66 changes: 66 additions & 0 deletions pkg/util/transformer/metadata_transformer_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
4 changes: 4 additions & 0 deletions pkg/util/transformer/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If set PartialMetadataRemoveTransform as transformer here, it will replace the exist transformer. It's not the expected.
If you want to do this, you should wrap the transformFn with PartialMetadataRemoveTransform.

klog.Fatalf("Failed to PartialMetadataRemoveTransform in informer, resource: %v, err: %v", resource, err)
}
}
}
Loading