...

Source file src/k8s.io/kubectl/pkg/cmd/get/table_printer.go

Documentation: k8s.io/kubectl/pkg/cmd/get

     1  /*
     2  Copyright 2019 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 get
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  	metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	"k8s.io/cli-runtime/pkg/printers"
    29  	"k8s.io/klog/v2"
    30  )
    31  
    32  // TablePrinter decodes table objects into typed objects before delegating to another printer.
    33  // Non-table types are simply passed through
    34  type TablePrinter struct {
    35  	Delegate printers.ResourcePrinter
    36  }
    37  
    38  func (t *TablePrinter) PrintObj(obj runtime.Object, writer io.Writer) error {
    39  	table, err := decodeIntoTable(obj)
    40  	if err == nil {
    41  		return t.Delegate.PrintObj(table, writer)
    42  	}
    43  	// if we are unable to decode server response into a v1beta1.Table,
    44  	// fallback to client-side printing with whatever info the server returned.
    45  	klog.V(2).Infof("Unable to decode server response into a Table. Falling back to hardcoded types: %v", err)
    46  	return t.Delegate.PrintObj(obj, writer)
    47  }
    48  
    49  var recognizedTableVersions = map[schema.GroupVersionKind]bool{
    50  	metav1beta1.SchemeGroupVersion.WithKind("Table"): true,
    51  	metav1.SchemeGroupVersion.WithKind("Table"):      true,
    52  }
    53  
    54  // assert the types are identical, since we're decoding both types into a metav1.Table
    55  var _ metav1.Table = metav1beta1.Table{}
    56  var _ metav1beta1.Table = metav1.Table{}
    57  
    58  func decodeIntoTable(obj runtime.Object) (runtime.Object, error) {
    59  	event, isEvent := obj.(*metav1.WatchEvent)
    60  	if isEvent {
    61  		obj = event.Object.Object
    62  	}
    63  
    64  	if !recognizedTableVersions[obj.GetObjectKind().GroupVersionKind()] {
    65  		return nil, fmt.Errorf("attempt to decode non-Table object")
    66  	}
    67  
    68  	unstr, ok := obj.(*unstructured.Unstructured)
    69  	if !ok {
    70  		return nil, fmt.Errorf("attempt to decode non-Unstructured object")
    71  	}
    72  	table := &metav1.Table{}
    73  	if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstr.Object, table); err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	for i := range table.Rows {
    78  		row := &table.Rows[i]
    79  		if row.Object.Raw == nil || row.Object.Object != nil {
    80  			continue
    81  		}
    82  		converted, err := runtime.Decode(unstructured.UnstructuredJSONScheme, row.Object.Raw)
    83  		if err != nil {
    84  			return nil, err
    85  		}
    86  		row.Object.Object = converted
    87  	}
    88  
    89  	if isEvent {
    90  		event.Object.Object = table
    91  		return event, nil
    92  	}
    93  	return table, nil
    94  }
    95  

View as plain text