@@ -27,7 +27,6 @@ import (
27
27
28
28
"k8s.io/apimachinery/pkg/api/meta"
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
31
30
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
32
31
"k8s.io/apimachinery/pkg/labels"
33
32
"k8s.io/apimachinery/pkg/runtime"
@@ -40,7 +39,6 @@ type TablePrinter interface {
40
39
}
41
40
42
41
type PrintHandler interface {
43
- Handler (columns , columnsWithWide []string , printFunc interface {}) error
44
42
TableHandler (columns []metav1beta1.TableColumnDefinition , printFunc interface {}) error
45
43
DefaultTableHandler (columns []metav1beta1.TableColumnDefinition , printFunc interface {}) error
46
44
}
@@ -49,7 +47,6 @@ var withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print
49
47
50
48
type handlerEntry struct {
51
49
columnDefinitions []metav1beta1.TableColumnDefinition
52
- printRows bool
53
50
printFunc reflect.Value
54
51
args []reflect.Value
55
52
}
@@ -65,19 +62,15 @@ type HumanReadablePrinter struct {
65
62
lastType interface {}
66
63
lastColumns []metav1beta1.TableColumnDefinition
67
64
skipTabWriter bool
68
- encoder runtime.Encoder
69
- decoder runtime.Decoder
70
65
}
71
66
72
67
var _ PrintHandler = & HumanReadablePrinter {}
73
68
74
69
// NewHumanReadablePrinter creates a HumanReadablePrinter.
75
- // If encoder and decoder are provided, an attempt to convert unstructured types to internal types is made.
76
- func NewHumanReadablePrinter (decoder runtime.Decoder , options PrintOptions ) * HumanReadablePrinter {
70
+ func NewHumanReadablePrinter (options PrintOptions ) * HumanReadablePrinter {
77
71
printer := & HumanReadablePrinter {
78
72
handlerMap : make (map [reflect.Type ]* handlerEntry ),
79
73
options : options ,
80
- decoder : decoder ,
81
74
}
82
75
return printer
83
76
}
@@ -112,53 +105,6 @@ func (h *HumanReadablePrinter) EnsurePrintHeaders() {
112
105
h .lastType = nil
113
106
}
114
107
115
- // Handler adds a print handler with a given set of columns to HumanReadablePrinter instance.
116
- // See ValidatePrintHandlerFunc for required method signature.
117
- func (h * HumanReadablePrinter ) Handler (columns , columnsWithWide []string , printFunc interface {}) error {
118
- var columnDefinitions []metav1beta1.TableColumnDefinition
119
- for i , column := range columns {
120
- format := ""
121
- if i == 0 && strings .EqualFold (column , "name" ) {
122
- format = "name"
123
- }
124
-
125
- columnDefinitions = append (columnDefinitions , metav1beta1.TableColumnDefinition {
126
- Name : column ,
127
- Description : column ,
128
- Type : "string" ,
129
- Format : format ,
130
- })
131
- }
132
- for _ , column := range columnsWithWide {
133
- columnDefinitions = append (columnDefinitions , metav1beta1.TableColumnDefinition {
134
- Name : column ,
135
- Description : column ,
136
- Type : "string" ,
137
- Priority : 1 ,
138
- })
139
- }
140
-
141
- printFuncValue := reflect .ValueOf (printFunc )
142
- if err := ValidatePrintHandlerFunc (printFuncValue ); err != nil {
143
- utilruntime .HandleError (fmt .Errorf ("unable to register print function: %v" , err ))
144
- return err
145
- }
146
-
147
- entry := & handlerEntry {
148
- columnDefinitions : columnDefinitions ,
149
- printFunc : printFuncValue ,
150
- }
151
-
152
- objType := printFuncValue .Type ().In (0 )
153
- if _ , ok := h .handlerMap [objType ]; ok {
154
- err := fmt .Errorf ("registered duplicate printer for %v" , objType )
155
- utilruntime .HandleError (err )
156
- return err
157
- }
158
- h .handlerMap [objType ] = entry
159
- return nil
160
- }
161
-
162
108
// TableHandler adds a print handler with a given set of columns to HumanReadablePrinter instance.
163
109
// See ValidateRowPrintHandlerFunc for required method signature.
164
110
func (h * HumanReadablePrinter ) TableHandler (columnDefinitions []metav1beta1.TableColumnDefinition , printFunc interface {}) error {
@@ -169,7 +115,6 @@ func (h *HumanReadablePrinter) TableHandler(columnDefinitions []metav1beta1.Tabl
169
115
}
170
116
entry := & handlerEntry {
171
117
columnDefinitions : columnDefinitions ,
172
- printRows : true ,
173
118
printFunc : printFuncValue ,
174
119
}
175
120
@@ -194,7 +139,6 @@ func (h *HumanReadablePrinter) DefaultTableHandler(columnDefinitions []metav1bet
194
139
}
195
140
entry := & handlerEntry {
196
141
columnDefinitions : columnDefinitions ,
197
- printRows : true ,
198
142
printFunc : printFuncValue ,
199
143
}
200
144
@@ -312,12 +256,6 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
312
256
return PrintTable (table , output , localOptions )
313
257
}
314
258
315
- // check if the object is unstructured. If so, let's attempt to convert it to a type we can understand before
316
- // trying to print, since the printers are keyed by type. This is extremely expensive.
317
- if h .decoder != nil {
318
- obj , _ = decodeUnknownObject (obj , h .decoder )
319
- }
320
-
321
259
// print with a registered handler
322
260
t := reflect .TypeOf (obj )
323
261
if handler := h .handlerMap [t ]; handler != nil {
@@ -516,9 +454,6 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
516
454
if ! ok {
517
455
return nil , fmt .Errorf ("no table handler registered for this type %v" , t )
518
456
}
519
- if ! handler .printRows {
520
- return h .legacyPrinterToTable (obj , handler )
521
- }
522
457
523
458
args := []reflect.Value {reflect .ValueOf (obj ), reflect .ValueOf (options )}
524
459
results := handler .printFunc .Call (args )
@@ -567,12 +502,11 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
567
502
// or an error, if any.
568
503
func printRowsForHandlerEntry (output io.Writer , handler * handlerEntry , obj runtime.Object , options PrintOptions , includeHeaders bool ) error {
569
504
var results []reflect.Value
570
- if handler .printRows {
571
- args := []reflect.Value {reflect .ValueOf (obj ), reflect .ValueOf (options )}
572
- results = handler .printFunc .Call (args )
573
- if ! results [1 ].IsNil () {
574
- return results [1 ].Interface ().(error )
575
- }
505
+
506
+ args := []reflect.Value {reflect .ValueOf (obj ), reflect .ValueOf (options )}
507
+ results = handler .printFunc .Call (args )
508
+ if ! results [1 ].IsNil () {
509
+ return results [1 ].Interface ().(error )
576
510
}
577
511
578
512
if includeHeaders {
@@ -592,16 +526,6 @@ func printRowsForHandlerEntry(output io.Writer, handler *handlerEntry, obj runti
592
526
printHeader (headers , output )
593
527
}
594
528
595
- if ! handler .printRows {
596
- // TODO: this code path is deprecated and will be removed when all handlers are row printers
597
- args := []reflect.Value {reflect .ValueOf (obj ), reflect .ValueOf (output ), reflect .ValueOf (options )}
598
- resultValue := handler .printFunc .Call (args )[0 ]
599
- if resultValue .IsNil () {
600
- return nil
601
- }
602
- return resultValue .Interface ().(error )
603
- }
604
-
605
529
if results [1 ].IsNil () {
606
530
rows := results [0 ].Interface ().([]metav1beta1.TableRow )
607
531
printRows (output , rows , options )
@@ -649,67 +573,6 @@ func printRows(output io.Writer, rows []metav1beta1.TableRow, options PrintOptio
649
573
}
650
574
}
651
575
652
- // legacyPrinterToTable uses the old printFunc with tabbed writer to generate a table.
653
- // TODO: remove when all legacy printers are removed.
654
- func (h * HumanReadablePrinter ) legacyPrinterToTable (obj runtime.Object , handler * handlerEntry ) (* metav1beta1.Table , error ) {
655
- printFunc := handler .printFunc
656
- table := & metav1beta1.Table {
657
- ColumnDefinitions : handler .columnDefinitions ,
658
- }
659
-
660
- options := PrintOptions {
661
- NoHeaders : true ,
662
- Wide : true ,
663
- }
664
- buf := & bytes.Buffer {}
665
- args := []reflect.Value {reflect .ValueOf (obj ), reflect .ValueOf (buf ), reflect .ValueOf (options )}
666
-
667
- if meta .IsListType (obj ) {
668
- listInterface , ok := obj .(metav1.ListInterface )
669
- if ok {
670
- table .ListMeta .SelfLink = listInterface .GetSelfLink ()
671
- table .ListMeta .ResourceVersion = listInterface .GetResourceVersion ()
672
- table .ListMeta .Continue = listInterface .GetContinue ()
673
- }
674
-
675
- // TODO: this uses more memory than it has to, as we refactor printers we should remove the need
676
- // for this.
677
- args [0 ] = reflect .ValueOf (obj )
678
- resultValue := printFunc .Call (args )[0 ]
679
- if ! resultValue .IsNil () {
680
- return nil , resultValue .Interface ().(error )
681
- }
682
- data := buf .Bytes ()
683
- i := 0
684
- items , err := meta .ExtractList (obj )
685
- if err != nil {
686
- return nil , err
687
- }
688
- for len (data ) > 0 {
689
- cells , remainder := tabbedLineToCells (data , len (table .ColumnDefinitions ))
690
- table .Rows = append (table .Rows , metav1beta1.TableRow {
691
- Cells : cells ,
692
- Object : runtime.RawExtension {Object : items [i ]},
693
- })
694
- data = remainder
695
- i ++
696
- }
697
- } else {
698
- args [0 ] = reflect .ValueOf (obj )
699
- resultValue := printFunc .Call (args )[0 ]
700
- if ! resultValue .IsNil () {
701
- return nil , resultValue .Interface ().(error )
702
- }
703
- data := buf .Bytes ()
704
- cells , _ := tabbedLineToCells (data , len (table .ColumnDefinitions ))
705
- table .Rows = append (table .Rows , metav1beta1.TableRow {
706
- Cells : cells ,
707
- Object : runtime.RawExtension {Object : obj },
708
- })
709
- }
710
- return table , nil
711
- }
712
-
713
576
// TODO: this method assumes the meta/v1 server API, so should be refactored out of this package
714
577
func printUnstructured (unstructured runtime.Unstructured , w io.Writer , additionalFields []string , options PrintOptions ) error {
715
578
metadata , err := meta .Accessor (unstructured )
@@ -848,46 +711,3 @@ func AppendAllLabels(showLabels bool, itemLabels map[string]string) string {
848
711
849
712
return buffer .String ()
850
713
}
851
-
852
- // check if the object is unstructured. If so, attempt to convert it to a type we can understand.
853
- func decodeUnknownObject (obj runtime.Object , decoder runtime.Decoder ) (runtime.Object , error ) {
854
- var err error
855
- switch t := obj .(type ) {
856
- case runtime.Unstructured :
857
- if objBytes , err := runtime .Encode (unstructured .UnstructuredJSONScheme , obj ); err == nil {
858
- if decodedObj , err := runtime .Decode (decoder , objBytes ); err == nil {
859
- obj = decodedObj
860
- }
861
- }
862
- case * runtime.Unknown :
863
- if decodedObj , err := runtime .Decode (decoder , t .Raw ); err == nil {
864
- obj = decodedObj
865
- }
866
- }
867
-
868
- return obj , err
869
- }
870
-
871
- func tabbedLineToCells (data []byte , expected int ) ([]interface {}, []byte ) {
872
- var remainder []byte
873
- max := bytes .Index (data , []byte ("\n " ))
874
- if max != - 1 {
875
- remainder = data [max + 1 :]
876
- data = data [:max ]
877
- }
878
- cells := make ([]interface {}, expected )
879
- for i := 0 ; i < expected ; i ++ {
880
- next := bytes .Index (data , []byte ("\t " ))
881
- if next == - 1 {
882
- cells [i ] = string (data )
883
- // fill the remainder with empty strings, this indicates a printer bug
884
- for j := i + 1 ; j < expected ; j ++ {
885
- cells [j ] = ""
886
- }
887
- break
888
- }
889
- cells [i ] = string (data [:next ])
890
- data = data [next + 1 :]
891
- }
892
- return cells , remainder
893
- }
0 commit comments