From 52b1718a638ecaf03610c5dfdb3ed78e532354a2 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Tue, 20 Nov 2018 17:18:45 +0100 Subject: [PATCH] unstructured: return not-found if intermediate path is not found --- .../pkg/apis/meta/v1/unstructured/helpers.go | 3 +++ .../pkg/apis/meta/v1/unstructured/helpers_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go index fc138e75aa9..75ac693fe48 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go @@ -47,6 +47,9 @@ func NestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{ var val interface{} = obj for i, field := range fields { + if val == nil { + return nil, false, nil + } if m, ok := val.(map[string]interface{}); ok { val, ok = m[field] if !ok { diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go index d979962119b..529acab6bac 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers_test.go @@ -95,6 +95,19 @@ func TestNestedFieldNoCopy(t *testing.T) { assert.False(t, exists) assert.Nil(t, err) assert.Nil(t, res) + + // case 5: intermediate field does not exist + res, exists, err = NestedFieldNoCopy(obj, "a", "e", "f") + assert.False(t, exists) + assert.Nil(t, err) + assert.Nil(t, res) + + // case 6: intermediate field is null + // (background: happens easily in YAML) + res, exists, err = NestedFieldNoCopy(obj, "a", "c", "f") + assert.False(t, exists) + assert.Nil(t, err) + assert.Nil(t, res) } func TestNestedFieldCopy(t *testing.T) {