From 304bce3cdf212a859b849552098e5d4dbc184049 Mon Sep 17 00:00:00 2001 From: David Eads Date: Wed, 16 May 2018 12:12:27 -0400 Subject: [PATCH] add protection for missing apiversion so we never serialize a bad object --- pkg/printers/json.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/printers/json.go b/pkg/printers/json.go index 8827e2e5e21..ec52bd6d2af 100644 --- a/pkg/printers/json.go +++ b/pkg/printers/json.go @@ -52,6 +52,10 @@ func (p *JSONPrinter) PrintObj(obj runtime.Object, w io.Writer) error { return err } + if obj.GetObjectKind().GroupVersionKind().Empty() { + return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type") + } + data, err := json.MarshalIndent(obj, "", " ") if err != nil { return err @@ -88,6 +92,10 @@ func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error { return err } + if obj.GetObjectKind().GroupVersionKind().Empty() { + return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type") + } + output, err := yaml.Marshal(obj) if err != nil { return err