fix(analyzer): fix clusterResource analyzer always uses the first object found (#1185)

* fix(analyzer): fix clusterResource analyzer  always uses the first object found
This commit is contained in:
Dexter Yan
2023-05-29 17:26:37 +12:00
committed by GitHub
parent acb1099bda
commit 830e357b48
2 changed files with 24 additions and 10 deletions

View File

@@ -94,11 +94,11 @@ func FindResource(kind string, clusterScoped bool, namespace string, name string
}
itemslice := items.([]interface{})
for _, item := range itemslice {
name, err := iutils.GetAtPath(item, "metadata.name")
resourceName, err := iutils.GetAtPath(item, "metadata.name")
if err != nil {
return nil, errors.Wrapf(err, "Failed to find resource with name: %s", name)
}
if name == name {
if resourceName == name {
return item, nil
}
}

View File

@@ -10,12 +10,14 @@ import (
func Test_clusterResource(t *testing.T) {
tests := []struct {
name string
isError bool
analyzer troubleshootv1beta2.ClusterResource
name string
isError bool
resourceExists bool
analyzer troubleshootv1beta2.ClusterResource
}{
{
name: "namespaced resource",
name: "namespaced resource",
resourceExists: true,
analyzer: troubleshootv1beta2.ClusterResource{
CollectorName: "Check namespaced resource",
Kind: "Deployment",
@@ -24,7 +26,8 @@ func Test_clusterResource(t *testing.T) {
},
},
{
name: "check default fallthrough",
name: "check default fallthrough",
resourceExists: true,
analyzer: troubleshootv1beta2.ClusterResource{
CollectorName: "Check namespaced resource",
Kind: "Deployment",
@@ -32,7 +35,8 @@ func Test_clusterResource(t *testing.T) {
},
},
{
name: "cluster scoped resource",
name: "cluster scoped resource",
resourceExists: true,
analyzer: troubleshootv1beta2.ClusterResource{
CollectorName: "Check namespaced resource",
Kind: "Node",
@@ -40,6 +44,16 @@ func Test_clusterResource(t *testing.T) {
Name: "repldev-marc",
},
},
{
name: "resource does not exist",
resourceExists: false,
analyzer: troubleshootv1beta2.ClusterResource{
CollectorName: "Check namespaced resource",
Kind: "Node",
ClusterScoped: true,
Name: "resource-does-not-exist",
},
},
}
for _, test := range tests {
@@ -49,9 +63,9 @@ func Test_clusterResource(t *testing.T) {
fcp := fileContentProvider{rootDir: rootDir}
analyzer := &test.analyzer
_, err := FindResource(analyzer.Kind, analyzer.ClusterScoped, analyzer.Namespace, analyzer.Name, fcp.getFileContents)
item, err := FindResource(analyzer.Kind, analyzer.ClusterScoped, analyzer.Namespace, analyzer.Name, fcp.getFileContents)
assert.Equal(t, test.resourceExists, item != nil)
assert.Nil(t, err)
})
}
}