From 0bbcd9a56317cfdb7b098ec68e5596a9a10d51d6 Mon Sep 17 00:00:00 2001 From: Priya Modali Date: Thu, 5 Nov 2020 22:56:02 -0800 Subject: [PATCH] Added logic to handle nil during interface conversion of namespace. (#5001) * Added logic to Handle nil during interface conversion of namespace. Added unit test. * Variable rename. --- .../kubernetes/manifest/namespace_test.go | 28 +++++++++++++++++++ .../kubernetes/manifest/namespaces.go | 6 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/skaffold/kubernetes/manifest/namespace_test.go b/pkg/skaffold/kubernetes/manifest/namespace_test.go index c508afa88a5..4d42c15b0b2 100644 --- a/pkg/skaffold/kubernetes/manifest/namespace_test.go +++ b/pkg/skaffold/kubernetes/manifest/namespace_test.go @@ -125,6 +125,34 @@ spec: containers: - image: gcr.io/k8s-skaffold/example name: example +`)}, + expected: []string{}, + }, { + description: "single Pod manifest with nil namespace", + manifests: ManifestList{[]byte(` +apiVersion: v1 +kind: Pod +metadata: + name: getting-started + namespace: +spec: + containers: + - image: gcr.io/k8s-skaffold/example + name: example +`)}, + expected: []string{}, + }, { + description: "single Pod manifest with empty namespace", + manifests: ManifestList{[]byte(` +apiVersion: v1 +kind: Pod +metadata: + name: getting-started + namespace: "" +spec: + containers: + - image: gcr.io/k8s-skaffold/example + name: example `)}, expected: []string{}, }, { diff --git a/pkg/skaffold/kubernetes/manifest/namespaces.go b/pkg/skaffold/kubernetes/manifest/namespaces.go index 55b2687ba1c..625e2355ea9 100644 --- a/pkg/skaffold/kubernetes/manifest/namespaces.go +++ b/pkg/skaffold/kubernetes/manifest/namespaces.go @@ -58,7 +58,11 @@ func (r *namespaceCollector) Visit(o map[string]interface{}, k string, v interfa return true } if nsValue, present := metadata["namespace"]; present { - if ns := strings.TrimSpace(nsValue.(string)); ns != "" { + nsString, ok := nsValue.(string) + if !ok || nsString == "" { + return true + } + if ns := strings.TrimSpace(nsString); ns != "" { r.namespaces[ns] = true } }