diff --git a/internal/k8sdeps/kunstruct/kunstruct.go b/internal/k8sdeps/kunstruct/kunstruct.go index 5a7e8cecd..6bc8fad24 100644 --- a/internal/k8sdeps/kunstruct/kunstruct.go +++ b/internal/k8sdeps/kunstruct/kunstruct.go @@ -20,11 +20,8 @@ package kunstruct import ( "encoding/json" "fmt" - "reflect" "strings" - "github.com/pkg/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -83,30 +80,10 @@ func (fs *UnstructAdapter) SetMap(m map[string]interface{}) { // GetFieldValue returns value at the given fieldpath. func (fs *UnstructAdapter) GetFieldValue(path string) (string, error) { - s, err := getFieldValue(fs.UnstructuredContent(), strings.Split(path, ".")) - if err != nil { - return "", errors.Wrapf(err, "at path '%s'", path) + s, found, err := unstructured.NestedString( + fs.UnstructuredContent(), strings.Split(path, ".")...) + if found || err != nil { + return s, err } - return s, nil -} - -func getFieldValue(m map[string]interface{}, path []string) (string, error) { - if len(path) == 0 { - return "", fmt.Errorf("%v not found", path) - } - v, ok := m[path[0]] - if !ok { - return "", fmt.Errorf("no field named '%s'", path[0]) - } - if len(path) == 1 { - if s, ok := v.(string); ok { - return s, nil - } - return "", fmt.Errorf("value at '%v' not a string", path[0]) - } - if deeper, ok := v.(map[string]interface{}); ok { - return getFieldValue(deeper, path[1:]) - } - return "", fmt.Errorf("Expected map at %v, but got %s=%v", - path[0], reflect.TypeOf(v), v) + return "", fmt.Errorf("no field named '%s'", path) } diff --git a/internal/k8sdeps/kunstruct/kunstruct_test.go b/internal/k8sdeps/kunstruct/kunstruct_test.go index 61633b3c1..dc7eb8bb2 100644 --- a/internal/k8sdeps/kunstruct/kunstruct_test.go +++ b/internal/k8sdeps/kunstruct/kunstruct_test.go @@ -76,49 +76,49 @@ func TestGetFieldValue(t *testing.T) { name: "empty", pathToField: "", errorExpected: true, - errorMsg: "at path '': no field named ''", + errorMsg: "no field named ''", }, { name: "emptyDotEmpty", pathToField: ".", errorExpected: true, - errorMsg: "at path '.': no field named ''", + errorMsg: "no field named '.'", }, { name: "twoFieldsOneMissing", pathToField: "metadata.banana", errorExpected: true, - errorMsg: "at path 'metadata.banana': no field named 'banana'", + errorMsg: "no field named 'metadata.banana'", }, { name: "deeperMissingField", pathToField: "this.is.aDeep.field.that.does.not.exist", errorExpected: true, - errorMsg: "at path 'this.is.aDeep.field.that.does.not.exist': no field named 'aDeep'", + errorMsg: "no field named 'this.is.aDeep.field.that.does.not.exist'", }, { name: "emptyMap", pathToField: "this.is.anEmptyMap", errorExpected: true, - errorMsg: "at path 'this.is.anEmptyMap': value at 'anEmptyMap' not a string", + errorMsg: ".this.is.anEmptyMap accessor error: map[] is of the type map[string]interface {}, expected string", }, { name: "numberAsValue", pathToField: "this.is.aNumber", errorExpected: true, - errorMsg: "at path 'this.is.aNumber': value at 'aNumber' not a string", + errorMsg: ".this.is.aNumber accessor error: 1000 is of the type int, expected string", }, { name: "nilAsValue", pathToField: "this.is.aNilValue", errorExpected: true, - errorMsg: "at path 'this.is.aNilValue': value at 'aNilValue' not a string", + errorMsg: ".this.is.aNilValue accessor error: is of the type , expected string", }, { name: "unrecognizable", pathToField: "this.is.unrecognizable.Name", errorExpected: true, - errorMsg: "at path 'this.is.unrecognizable.Name': Expected map at unrecognizable, but got testing.InternalExample={fooBar false}", + errorMsg: ".this.is.unrecognizable.Name accessor error: {fooBar false} is of the type testing.InternalExample, expected map[string]interface{}", }, }