From 5947f696ff8bf6265146b8295aff8f1d7769a0e1 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 29 Oct 2018 14:55:53 -0700 Subject: [PATCH] make sure the objects loaded have name and kind --- k8sdeps/kunstruct/factory.go | 16 ++++++++++++++++ k8sdeps/kunstruct/factory_test.go | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/k8sdeps/kunstruct/factory.go b/k8sdeps/kunstruct/factory.go index 60bbc979a..b9b5b8ed7 100644 --- a/k8sdeps/kunstruct/factory.go +++ b/k8sdeps/kunstruct/factory.go @@ -18,6 +18,7 @@ package kunstruct import ( "bytes" + "fmt" "io" "strings" @@ -52,6 +53,10 @@ func (kf *KunstructurerFactoryImpl) SliceFromBytes( var out unstructured.Unstructured err = decoder.Decode(&out) if err == nil { + err = kf.validate(out) + if err != nil { + return nil, err + } result = append(result, &UnstructAdapter{Unstructured: out}) } } @@ -94,3 +99,14 @@ func (kf *KunstructurerFactoryImpl) Set(fs fs.FileSystem, ldr ifc.Loader) { kf.cmfactory = configmapandsecret.NewConfigMapFactory(fs, ldr) kf.secfactory = configmapandsecret.NewSecretFactory(fs, ldr.Root()) } + +// validate validates that u has kind and name +func (kf *KunstructurerFactoryImpl) validate(u unstructured.Unstructured) error { + if u.GetName() == "" { + return fmt.Errorf("Missing metadata.name in object %v", u) + } + if u.GetKind() == "" { + return fmt.Errorf("Missing kind in object %v", u) + } + return nil +} diff --git a/k8sdeps/kunstruct/factory_test.go b/k8sdeps/kunstruct/factory_test.go index 6b273431e..24dbb8a25 100644 --- a/k8sdeps/kunstruct/factory_test.go +++ b/k8sdeps/kunstruct/factory_test.go @@ -100,6 +100,18 @@ WOOOOOOOOOOOOOOOOOOOOOOOOT: woot expectedOut: []ifc.Kunstructured{}, expectedErr: true, }, + { + name: "Missing .metadata.name in object", + input: []byte(` +apiVersion: v1 +kind: Namespace +metadata: + annotations: + foo: bar +`), + expectedOut: nil, + expectedErr: true, + }, } for _, test := range tests {