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
60 changes: 60 additions & 0 deletions pkg/util/transformer/metadata_transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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 (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
)

var metadataTransformers = []func(meta *metav1.PartialObjectMetadata){
TransformPartialMetadataRemoveResources,
}

func InstallMetadataTransformer(informer cache.SharedIndexInformer) {
if err := informer.SetTransform(TransformPod); err != nil {

Check failure on line 30 in pkg/util/transformer/metadata_transformer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: TransformPod) (typecheck)

Check failure on line 30 in pkg/util/transformer/metadata_transformer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: TransformPod) (typecheck)

Check failure on line 30 in pkg/util/transformer/metadata_transformer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: TransformPod) (typecheck)

Check failure on line 30 in pkg/util/transformer/metadata_transformer.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: TransformPod (typecheck)

Check failure on line 30 in pkg/util/transformer/metadata_transformer.go

View workflow job for this annotation

GitHub Actions / unit-tests(Verify govet)

undefined: TransformPod

Check failure on line 30 in pkg/util/transformer/metadata_transformer.go

View workflow job for this annotation

GitHub Actions / unit-tests(Run Go build)

undefined: TransformPod

Check failure on line 30 in pkg/util/transformer/metadata_transformer.go

View workflow job for this annotation

GitHub Actions / unit-tests(Run Go test)

undefined: TransformPod
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)
}

if unknown, ok := obj.(cache.DeletedFinalStateUnknown); ok {
unknown.Obj = meta
return unknown, nil
}
return meta, nil
}

func TransformPartialMetadataRemoveResources(partialMeta *metav1.PartialObjectMetadata) {
partialMeta.ManagedFields = nil
}
71 changes: 71 additions & 0 deletions pkg/util/transformer/metadata_transformer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
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 := TransformMeta(tt.pod)
assert.NoError(t, err)
assert.Equal(t, tt.wantPod, obj)
})
}
}

func TestTransformMetadataError(t *testing.T) {
_, err := TransformMeta(&metav1.PartialObjectMetadata{})
assert.Nil(t, err)
}
2 changes: 2 additions & 0 deletions pkg/util/transformer/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,6 +31,7 @@ import (
var transformers = map[schema.GroupVersionResource]cache.TransformFunc{
corev1.SchemeGroupVersion.WithResource("nodes"): TransformNode,
schedulingv1alpha1.SchemeGroupVersion.WithResource("devices"): TransformDevice,
metav1.SchemeGroupVersion.WithResource("metas"): TransformMeta,
}

type TransformFactory func() cache.TransformFunc
Expand Down
Loading