Skip to content

Commit

Permalink
Add reusable TrimManagedFields transform function
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Apr 17, 2024
1 parent 5cfcef6 commit 516c1d1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
61 changes: 61 additions & 0 deletions pkg/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/resource"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/ptr"
)

var _ = Describe("EnsureValidName", func() {
Expand All @@ -44,3 +48,60 @@ var _ = Describe("EnsureValidName", func() {
})
})
})

var _ = Describe("TrimManagedFields", func() {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
Namespace: "ns",
Labels: map[string]string{"app": "test"},
Annotations: map[string]string{"foo": "bar"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Image: "image",
Name: "httpd",
},
},
},
}

When("the object has metadata", func() {
It("should succeed and trim the ManagedFields", func() {
podWithMF := pod.DeepCopy()
podWithMF.ManagedFields = []metav1.ManagedFieldsEntry{
{
Manager: "kubectl",
Operation: metav1.ManagedFieldsOperationApply,
APIVersion: "v1",
Time: ptr.To(metav1.Now()),
FieldsType: "FieldsV1",
FieldsV1: ptr.To(metav1.FieldsV1{}),
},
}

retObj, err := resource.TrimManagedFields(podWithMF)
Expect(err).To(Succeed())
Expect(podWithMF).To(Equal(pod))
Expect(retObj).To(BeIdenticalTo(podWithMF))

_, err = resource.TrimManagedFields(podWithMF)
Expect(err).To(Succeed())
Expect(podWithMF).To(Equal(pod))
})
})

When("the object does not have metadata", func() {
It("should succeed and return the object", func() {
obj := &cache.DeletedFinalStateUnknown{
Key: "key",
Obj: pod,
}

retObj, err := resource.TrimManagedFields(obj)
Expect(err).To(Succeed())
Expect(retObj).To(Equal(obj))
})
})
})
11 changes: 11 additions & 0 deletions pkg/resource/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,14 @@ func ToJSON(o any) string {
out, _ := json.MarshalIndent(o, "", " ")
return string(out)
}

// TrimManagedFields is a cache.TransformFunc that removes the ManagedFields metadata field if present. Note that if 'obj' does not
// implement metav1.Object then it is ignored and no error is returned.
func TrimManagedFields(obj interface{}) (interface{}, error) {
objMeta, err := meta.Accessor(obj)
if err == nil && len(objMeta.GetManagedFields()) > 0 {
objMeta.SetManagedFields(nil)
}

return obj, nil
}
9 changes: 1 addition & 8 deletions pkg/syncer/resource_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,7 @@ func NewResourceSyncer(config *ResourceSyncerConfig) (Interface, error) {
AddFunc: syncer.onCreate,
UpdateFunc: syncer.onUpdate,
DeleteFunc: syncer.onDelete,
}, func(obj interface{}) (interface{}, error) {
objMeta, err := meta.Accessor(obj)
if err == nil && len(objMeta.GetManagedFields()) > 0 {
objMeta.SetManagedFields(nil)
}

return obj, nil
})
}, resourceUtil.TrimManagedFields)

return syncer, nil
}
Expand Down

0 comments on commit 516c1d1

Please sign in to comment.