...

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

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

     1  /*
     2  Copyright 2021 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  	"fmt"
    21  	"io"
    22  	"reflect"
    23  	"sync/atomic"
    24  
    25  	"sigs.k8s.io/yaml"
    26  
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  )
    30  
    31  // YAMLPrinter is an implementation of ResourcePrinter which outputs an object as YAML.
    32  // The input object is assumed to be in the internal version of an API and is converted
    33  // to the given version first.
    34  // If PrintObj() is called multiple times, objects are separated with a '---' separator.
    35  type YAMLPrinter struct {
    36  	printCount int64
    37  }
    38  
    39  // PrintObj prints the data as YAML.
    40  func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
    41  	// we use reflect.Indirect here in order to obtain the actual value from a pointer.
    42  	// we need an actual value in order to retrieve the package path for an object.
    43  	// using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers.
    44  	if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
    45  		return fmt.Errorf(InternalObjectPrinterErr)
    46  	}
    47  
    48  	count := atomic.AddInt64(&p.printCount, 1)
    49  	if count > 1 {
    50  		if _, err := w.Write([]byte("---\n")); err != nil {
    51  			return err
    52  		}
    53  	}
    54  
    55  	switch obj := obj.(type) {
    56  	case *metav1.WatchEvent:
    57  		if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj.Object.Object)).Type().PkgPath()) {
    58  			return fmt.Errorf(InternalObjectPrinterErr)
    59  		}
    60  		data, err := yaml.Marshal(obj)
    61  		if err != nil {
    62  			return err
    63  		}
    64  		_, err = w.Write(data)
    65  		return err
    66  	case *runtime.Unknown:
    67  		data, err := yaml.JSONToYAML(obj.Raw)
    68  		if err != nil {
    69  			return err
    70  		}
    71  		_, err = w.Write(data)
    72  		return err
    73  	}
    74  
    75  	if obj.GetObjectKind().GroupVersionKind().Empty() {
    76  		return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type")
    77  	}
    78  
    79  	output, err := yaml.Marshal(obj)
    80  	if err != nil {
    81  		return err
    82  	}
    83  	_, err = fmt.Fprint(w, string(output))
    84  	return err
    85  }
    86  

View as plain text