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