From 5cddce5031e30360bc9fe2241b6d75d491feaa93 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Thu, 16 Nov 2023 07:44:40 -0500 Subject: [PATCH] Remove ToUnstructured from syncer/test ...in lieu of resource.MustToUnstructured. Signed-off-by: Tom Pantelis --- pkg/fake/delete_colection_reactor.go | 4 ++-- pkg/resource/util.go | 5 ++--- pkg/syncer/resource_syncer_test.go | 26 +++++++++++++------------- pkg/syncer/test/util.go | 11 ++--------- pkg/util/create_or_update_test.go | 9 +++++---- test/e2e/util/util.go | 12 ++++++------ 6 files changed, 30 insertions(+), 37 deletions(-) diff --git a/pkg/fake/delete_colection_reactor.go b/pkg/fake/delete_colection_reactor.go index 64c1308e..d4f3bec5 100644 --- a/pkg/fake/delete_colection_reactor.go +++ b/pkg/fake/delete_colection_reactor.go @@ -25,7 +25,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/submariner-io/admiral/pkg/syncer/test" + "github.com/submariner-io/admiral/pkg/resource" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -73,7 +73,7 @@ func (r *DeleteCollectionReactor) react(action testing.Action) (bool, runtime.Ob return true, nil, err } - raw := test.ToUnstructured(obj) + raw := resource.MustToUnstructured(obj) items, found, err := unstructured.NestedSlice(raw.Object, "items") Expect(err).To(Succeed()) Expect(found).To(BeTrue()) diff --git a/pkg/resource/util.go b/pkg/resource/util.go index 6eecc82b..991d79e6 100644 --- a/pkg/resource/util.go +++ b/pkg/resource/util.go @@ -28,6 +28,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes/scheme" ) @@ -56,9 +57,7 @@ func MustToUnstructured(from runtime.Object) *unstructured.Unstructured { func MustToUnstructuredUsingScheme(from runtime.Object, usingScheme *runtime.Scheme) *unstructured.Unstructured { u, err := ToUnstructuredUsingScheme(from, usingScheme) - if err != nil { - panic(err) - } + utilruntime.Must(err) return u } diff --git a/pkg/syncer/resource_syncer_test.go b/pkg/syncer/resource_syncer_test.go index b47ea0cb..3caf86f9 100644 --- a/pkg/syncer/resource_syncer_test.go +++ b/pkg/syncer/resource_syncer_test.go @@ -355,11 +355,11 @@ func testTransformFunction() { }) verifyDistribute := func() { - d.federator.VerifyDistribute(test.SetClusterIDLabel(test.ToUnstructured(transformed), "remote")) + d.federator.VerifyDistribute(test.SetClusterIDLabel(resource.MustToUnstructured(transformed), "remote")) } verifyDelete := func() { - d.federator.VerifyDelete(test.SetClusterIDLabel(test.ToUnstructured(transformed), "remote")) + d.federator.VerifyDelete(test.SetClusterIDLabel(resource.MustToUnstructured(transformed), "remote")) } When("a resource is created in the datastore", func() { @@ -399,7 +399,7 @@ func testTransformFunction() { d.resource = test.NewPodWithImage(d.config.SourceNamespace, "updated") test.UpdateResource(d.sourceClient, test.NewPodWithImage(d.config.SourceNamespace, "updated")) - d.federator.VerifyDistribute(test.ToUnstructured(transformed)) + d.federator.VerifyDistribute(resource.MustToUnstructured(transformed)) Eventually(expOperation).Should(Receive(Equal(syncer.Update))) }) }) @@ -630,7 +630,7 @@ func testOnSuccessfulSyncFunction() { It("should invoke the OnSuccessfulSync function with the transformed resource", func() { test.CreateResource(d.sourceClient, d.resource) - d.federator.VerifyDistribute(test.ToUnstructured(expResource)) + d.federator.VerifyDistribute(resource.MustToUnstructured(expResource)) Eventually(expOperation).Should(Receive(Equal(syncer.Create))) }) }) @@ -883,7 +883,7 @@ func testUpdateSuppression() { }) It("should distribute it", func() { - d.federator.VerifyDistribute(test.ToUnstructured(d.resource)) + d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource)) }) }) @@ -894,7 +894,7 @@ func testUpdateSuppression() { }) It("should distribute it", func() { - d.federator.VerifyDistribute(test.ToUnstructured(d.resource)) + d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource)) }) }) }) @@ -931,7 +931,7 @@ func testUpdateSuppression() { }) It("should distribute it", func() { - d.federator.VerifyDistribute(test.ToUnstructured(d.resource)) + d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource)) }) }) @@ -941,7 +941,7 @@ func testUpdateSuppression() { }) It("should distribute it", func() { - d.federator.VerifyDistribute(test.ToUnstructured(d.resource)) + d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource)) }) }) }) @@ -960,7 +960,7 @@ func testUpdateSuppression() { }) It("should distribute it", func() { - d.federator.VerifyDistribute(test.ToUnstructured(d.resource)) + d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource)) }) }) }) @@ -976,7 +976,7 @@ func testUpdateSuppression() { }) It("should distribute it", func() { - d.federator.VerifyDistribute(test.ToUnstructured(d.resource)) + d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource)) }) }) @@ -1152,11 +1152,11 @@ func testRequeueResource() { }) It("should requeue it", func() { - d.federator.VerifyDistribute(test.ToUnstructured(transformed)) + d.federator.VerifyDistribute(resource.MustToUnstructured(transformed)) d.syncer.RequeueResource(d.resource.Name, d.resource.Namespace) - d.federator.VerifyDistribute(test.ToUnstructured(transformed)) + d.federator.VerifyDistribute(resource.MustToUnstructured(transformed)) }) }) @@ -1257,7 +1257,7 @@ func newTestDriver(sourceNamespace, localClusterID string, syncDirection syncer. } func (t *testDriver) addInitialResource(obj runtime.Object) { - t.initialResources = append(t.initialResources, test.ToUnstructured(obj)) + t.initialResources = append(t.initialResources, resource.MustToUnstructured(obj)) } func (t *testDriver) verifyDistributeOnCreateTest(clusterID string) { diff --git a/pkg/syncer/test/util.go b/pkg/syncer/test/util.go index a62cfcb6..8c0ee1e4 100644 --- a/pkg/syncer/test/util.go +++ b/pkg/syncer/test/util.go @@ -60,7 +60,7 @@ func GetResource(resourceInterface dynamic.ResourceInterface, obj runtime.Object } func CreateResource(resourceInterface dynamic.ResourceInterface, obj runtime.Object) *unstructured.Unstructured { - u := ToUnstructured(obj) + u := resource.MustToUnstructured(obj) u.SetResourceVersion("") created, err := resourceInterface.Create(context.TODO(), u, metav1.CreateOptions{}) @@ -181,7 +181,7 @@ func PrepInitialClientObjs(namespace, clusterID string, initObjs ...runtime.Obje newObjs := make([]runtime.Object, 0, len(initObjs)) for _, obj := range initObjs { - raw := ToUnstructured(obj) + raw := resource.MustToUnstructured(obj) raw.SetUID(uuid.NewUUID()) raw.SetResourceVersion("1") @@ -205,13 +205,6 @@ func PrepInitialClientObjs(namespace, clusterID string, initObjs ...runtime.Obje return newObjs } -func ToUnstructured(obj runtime.Object) *unstructured.Unstructured { - raw, err := resource.ToUnstructured(obj) - Expect(err).To(Succeed()) - - return raw -} - func SetClusterIDLabel(obj runtime.Object, clusterID string) runtime.Object { meta, err := metaapi.Accessor(obj) Expect(err).To(Succeed()) diff --git a/pkg/util/create_or_update_test.go b/pkg/util/create_or_update_test.go index c021ac00..f8a1b9d0 100644 --- a/pkg/util/create_or_update_test.go +++ b/pkg/util/create_or_update_test.go @@ -144,7 +144,8 @@ var _ = Describe("CreateOrUpdate function", func() { t := newCreateOrUpdateTestDiver() createOrUpdate := func(expResult util.OperationResult) error { - result, err := util.CreateOrUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client), test.ToUnstructured(t.pod), t.mutateFn) + result, err := util.CreateOrUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client), + resource.MustToUnstructured(t.pod), t.mutateFn) if err != nil && expResult != util.OperationResultNone { return err } @@ -246,7 +247,7 @@ var _ = Describe("Update function", func() { t := newCreateOrUpdateTestDiver() update := func() error { - return util.Update[runtime.Object](context.TODO(), resource.ForDynamic(t.client), test.ToUnstructured(t.pod), t.mutateFn) + return util.Update[runtime.Object](context.TODO(), resource.ForDynamic(t.client), resource.MustToUnstructured(t.pod), t.mutateFn) } When("the resource doesn't exist", func() { @@ -268,7 +269,7 @@ var _ = Describe("MustUpdate function", func() { t := newCreateOrUpdateTestDiver() mustUpdate := func() error { - return util.MustUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client), test.ToUnstructured(t.pod), t.mutateFn) + return util.MustUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client), resource.MustToUnstructured(t.pod), t.mutateFn) } When("the resource doesn't exist", func() { @@ -318,7 +319,7 @@ func newCreateOrUpdateTestDiver() *createOrUpdateTestDriver { }) t.mutateFn = func(existing runtime.Object) (runtime.Object, error) { - obj := test.ToUnstructured(t.pod) + obj := resource.MustToUnstructured(t.pod) obj.SetUID(resource.MustToMeta(existing).GetUID()) return util.Replace(obj)(nil) } diff --git a/test/e2e/util/util.go b/test/e2e/util/util.go index 26da43fa..96d2e504 100644 --- a/test/e2e/util/util.go +++ b/test/e2e/util/util.go @@ -24,7 +24,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/submariner-io/admiral/pkg/syncer/test" + "github.com/submariner-io/admiral/pkg/resource" testV1 "github.com/submariner-io/admiral/test/apis/admiral.submariner.io/v1" "github.com/submariner-io/shipyard/test/e2e/framework" metaapi "k8s.io/apimachinery/pkg/api/meta" @@ -48,16 +48,16 @@ func DeleteAllToasters(client dynamic.Interface, namespace, clusterName string) DeleteAllOf(client, ToasterGVR(), namespace, clusterName) } -func DeleteAllOf(client dynamic.Interface, gvr *schema.GroupVersionResource, namespace, clusterName string) { +func DeleteAllOf(dynClient dynamic.Interface, gvr *schema.GroupVersionResource, namespace, clusterName string) { By(fmt.Sprintf("Deleting all %s in namespace %q from %q", gvr.Resource, namespace, clusterName)) - resource := client.Resource(*gvr).Namespace(namespace) - Expect(resource.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})).To(Succeed()) + client := dynClient.Resource(*gvr).Namespace(namespace) + Expect(client.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})).To(Succeed()) framework.AwaitUntil(fmt.Sprintf("list %s in namespace %q from %q", gvr.Resource, namespace, clusterName), func() (i interface{}, e error, ) { - return resource.List(context.TODO(), metav1.ListOptions{}) + return client.List(context.TODO(), metav1.ListOptions{}) }, func(result interface{}) (bool, string, error) { list, ok := result.(*unstructured.UnstructuredList) Expect(ok).To(BeTrue()) @@ -73,7 +73,7 @@ func DeleteAllOf(client dynamic.Interface, gvr *schema.GroupVersionResource, nam func CreateToaster(client dynamic.Interface, toaster *testV1.Toaster, clusterName string) *testV1.Toaster { By(fmt.Sprintf("Creating Toaster %q in namespace %q in %q", toaster.Name, toaster.Namespace, clusterName)) - obj, err := client.Resource(*ToasterGVR()).Namespace(toaster.Namespace).Create(context.TODO(), test.ToUnstructured(toaster), + obj, err := client.Resource(*ToasterGVR()).Namespace(toaster.Namespace).Create(context.TODO(), resource.MustToUnstructured(toaster), metav1.CreateOptions{}) Expect(err).To(Succeed())