Merge pull request #522 from Liujingfang1/name

make sure the objects loaded have name and kind
This commit is contained in:
Jeff Regan
2018-11-07 12:18:50 -08:00
committed by GitHub
2 changed files with 28 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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 {