...

Source file src/k8s.io/cli-runtime/pkg/printers/json.go

Documentation: k8s.io/cli-runtime/pkg/printers

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package printers
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"reflect"
    25  
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  )
    29  
    30  // JSONPrinter is an implementation of ResourcePrinter which outputs an object as JSON.
    31  type JSONPrinter struct{}
    32  
    33  // PrintObj is an implementation of ResourcePrinter.PrintObj which simply writes the object to the Writer.
    34  func (p *JSONPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
    35  	// we use reflect.Indirect here in order to obtain the actual value from a pointer.
    36  	// we need an actual value in order to retrieve the package path for an object.
    37  	// using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers.
    38  	if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
    39  		return fmt.Errorf(InternalObjectPrinterErr)
    40  	}
    41  
    42  	switch obj := obj.(type) {
    43  	case *metav1.WatchEvent:
    44  		if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj.Object.Object)).Type().PkgPath()) {
    45  			return fmt.Errorf(InternalObjectPrinterErr)
    46  		}
    47  		data, err := json.Marshal(obj)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		_, err = w.Write(data)
    52  		if err != nil {
    53  			return err
    54  		}
    55  		_, err = w.Write([]byte{'\n'})
    56  		return err
    57  	case *runtime.Unknown:
    58  		var buf bytes.Buffer
    59  		err := json.Indent(&buf, obj.Raw, "", "    ")
    60  		if err != nil {
    61  			return err
    62  		}
    63  		buf.WriteRune('\n')
    64  		_, err = buf.WriteTo(w)
    65  		return err
    66  	}
    67  
    68  	if obj.GetObjectKind().GroupVersionKind().Empty() {
    69  		return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type")
    70  	}
    71  
    72  	data, err := json.MarshalIndent(obj, "", "    ")
    73  	if err != nil {
    74  		return err
    75  	}
    76  	data = append(data, '\n')
    77  	_, err = w.Write(data)
    78  	return err
    79  }
    80  

View as plain text