From 01cc5fa64236ed205ab89e64600ecc7007684114 Mon Sep 17 00:00:00 2001 From: jdesouza Date: Wed, 14 Jul 2021 10:17:40 -0300 Subject: [PATCH] Fixed repeated names on dashboard (#586) * Fixed repeated names on dashboard * Fixed unit test --- pkg/kube/resource.go | 14 ++++++-------- pkg/kube/resources.go | 4 ++-- pkg/validator/arbitrary_test.go | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/pkg/kube/resource.go b/pkg/kube/resource.go index 427c6265e..a0aee41c5 100644 --- a/pkg/kube/resource.go +++ b/pkg/kube/resource.go @@ -25,24 +25,23 @@ type GenericResource struct { } // NewGenericResourceFromUnstructured creates a workload from an unstructured.Unstructured -func NewGenericResourceFromUnstructured(unst *unstructured.Unstructured) (GenericResource, error) { +func NewGenericResourceFromUnstructured(unst unstructured.Unstructured) (GenericResource, error) { workload := GenericResource{ Kind: unst.GetKind(), - Resource: *unst, + Resource: unst, } - objMeta, err := meta.Accessor(unst) + objMeta, err := meta.Accessor(&unst) if err != nil { return workload, err } workload.ObjectMeta = objMeta - b, err := json.Marshal(unst) + b, err := json.Marshal(&unst) if err != nil { return workload, err } workload.OriginalObjectJSON = b - m := make(map[string]interface{}) err = json.Unmarshal(b, &m) if err != nil { @@ -61,7 +60,6 @@ func NewGenericResourceFromUnstructured(unst *unstructured.Unstructured) (Generi } workload.PodSpec = &podSpec } - return workload, nil } @@ -101,7 +99,7 @@ func NewGenericResourceFromBytes(contentBytes []byte) (GenericResource, error) { if err != nil { return GenericResource{}, err } - return NewGenericResourceFromUnstructured(&unst) + return NewGenericResourceFromUnstructured(unst) } // ResolveControllerFromPod builds a new workload for a given Pod @@ -161,7 +159,7 @@ func resolveControllerFromPod(ctx context.Context, podResource kubeAPICoreV1.Pod if lastKey != "" { unst := objectCache[lastKey] - return NewGenericResourceFromUnstructured(&unst) + return NewGenericResourceFromUnstructured(unst) } workload, err := NewGenericResourceFromPod(podResource, podResource) if err != nil { diff --git a/pkg/kube/resources.go b/pkg/kube/resources.go index 5c86ba4cc..97903cab8 100644 --- a/pkg/kube/resources.go +++ b/pkg/kube/resources.go @@ -168,7 +168,7 @@ func CreateResourceProviderFromResource(ctx context.Context, workload string) (* logrus.Errorf("Could not find workload %s: %v", workload, err) return nil, err } - workloadObj, err := NewGenericResourceFromUnstructured(obj) + workloadObj, err := NewGenericResourceFromUnstructured(*obj) if err != nil { logrus.Errorf("Could not parse workload %s: %v", workload, err) return nil, err @@ -298,7 +298,7 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac return nil, err } for _, obj := range objects.Items { - res, err := NewGenericResourceFromUnstructured(&obj) + res, err := NewGenericResourceFromUnstructured(obj) if err != nil { return nil, err } diff --git a/pkg/validator/arbitrary_test.go b/pkg/validator/arbitrary_test.go index e0b321f3d..2724fb09b 100644 --- a/pkg/validator/arbitrary_test.go +++ b/pkg/validator/arbitrary_test.go @@ -33,7 +33,7 @@ func TestValidatePDB(t *testing.T) { }, } pdb := unstructured.Unstructured{} - res, err := kube.NewGenericResourceFromUnstructured(&pdb) + res, err := kube.NewGenericResourceFromUnstructured(pdb) res.Kind = "PodDisruptionBudget" actualResult, err := applyNonControllerSchemaChecks(&c, nil, res) @@ -70,7 +70,7 @@ func TestValidateIngress(t *testing.T) { if err != nil { panic(err) } - res, err := kube.NewGenericResourceFromUnstructured(&unst) + res, err := kube.NewGenericResourceFromUnstructured(unst) if err != nil { panic(err) }