From 2bcf82c6a4cedfa23e922d3b1d28bc3ee3cf4c14 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Wed, 15 Jan 2020 16:58:45 -0800 Subject: [PATCH] Exclude invalid resources from status --- cmd/resource/status/cmd/util.go | 22 +++++++++------ cmd/resource/status/cmd/util_test.go | 40 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 cmd/resource/status/cmd/util_test.go diff --git a/cmd/resource/status/cmd/util.go b/cmd/resource/status/cmd/util.go index bd745191f..6071f145d 100644 --- a/cmd/resource/status/cmd/util.go +++ b/cmd/resource/status/cmd/util.go @@ -64,14 +64,20 @@ func (f *CaptureIdentifiersFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, e if err != nil { return nil, err } - f.Identifiers = append(f.Identifiers, wait.ResourceIdentifier{ - Name: id.Name, - Namespace: id.Namespace, - GroupKind: schema.GroupKind{ - Group: gv.Group, - Kind: id.Kind, - }, - }) + if IsValidKubernetesResource(id) { + f.Identifiers = append(f.Identifiers, wait.ResourceIdentifier{ + Name: id.Name, + Namespace: id.Namespace, + GroupKind: schema.GroupKind{ + Group: gv.Group, + Kind: id.Kind, + }, + }) + } } return slice, nil } + +func IsValidKubernetesResource(id yaml.ResourceIdentifier) bool { + return id.GetKind() != "" && id.GetAPIVersion() != "" && id.GetName() != "" +} diff --git a/cmd/resource/status/cmd/util_test.go b/cmd/resource/status/cmd/util_test.go new file mode 100644 index 000000000..da081aa14 --- /dev/null +++ b/cmd/resource/status/cmd/util_test.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "github.com/stretchr/testify/assert" + "sigs.k8s.io/kustomize/kyaml/yaml" + "testing" +) + +func TestIsValidKubernetesResource(t *testing.T) { + + testCases := map[string]struct { + data yaml.ResourceIdentifier + expected bool + }{ + "invalid resource": { + data: yaml.ResourceIdentifier { + Name : "", + APIVersion : "", + Kind : "", + Namespace : "", + }, + expected: false, + }, + "valid resource": { + data: yaml.ResourceIdentifier { + Name : "SomeName", + APIVersion : "SomeVersion", + Kind : "SomeKind", + Namespace : "", + }, + expected: true, + }, + } + + for tn, tc := range testCases { + t.Run(tn, func(t *testing.T) { + assert.Equal(t, IsValidKubernetesResource(tc.data), tc.expected) + }) + } +}