Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
c758f0af
Commit
c758f0af
authored
Apr 08, 2019
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove legacy table printing and decoding
parent
c082ace1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
35 additions
and
225 deletions
+35
-225
humanreadable_flags.go
pkg/kubectl/cmd/get/humanreadable_flags.go
+1
-4
BUILD
pkg/printers/BUILD
+0
-1
humanreadable.go
pkg/printers/humanreadable.go
+6
-186
humanreadable_test.go
pkg/printers/humanreadable_test.go
+0
-4
printers_test.go
pkg/printers/internalversion/printers_test.go
+28
-30
No files found.
pkg/kubectl/cmd/get/humanreadable_flags.go
View file @
c758f0af
...
@@ -21,7 +21,6 @@ import (
...
@@ -21,7 +21,6 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
"k8s.io/kubernetes/pkg/printers"
printersinternal
"k8s.io/kubernetes/pkg/printers/internalversion"
printersinternal
"k8s.io/kubernetes/pkg/printers/internalversion"
)
)
...
@@ -73,8 +72,6 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
...
@@ -73,8 +72,6 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
return
nil
,
genericclioptions
.
NoCompatiblePrinterError
{
Options
:
f
,
AllowedFormats
:
f
.
AllowedFormats
()}
return
nil
,
genericclioptions
.
NoCompatiblePrinterError
{
Options
:
f
,
AllowedFormats
:
f
.
AllowedFormats
()}
}
}
decoder
:=
scheme
.
Codecs
.
UniversalDecoder
()
showKind
:=
false
showKind
:=
false
if
f
.
ShowKind
!=
nil
{
if
f
.
ShowKind
!=
nil
{
showKind
=
*
f
.
ShowKind
showKind
=
*
f
.
ShowKind
...
@@ -90,7 +87,7 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
...
@@ -90,7 +87,7 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
columnLabels
=
*
f
.
ColumnLabels
columnLabels
=
*
f
.
ColumnLabels
}
}
p
:=
printers
.
NewHumanReadablePrinter
(
decoder
,
printers
.
PrintOptions
{
p
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
Kind
:
f
.
Kind
,
Kind
:
f
.
Kind
,
WithKind
:
showKind
,
WithKind
:
showKind
,
NoHeaders
:
f
.
NoHeaders
,
NoHeaders
:
f
.
NoHeaders
,
...
...
pkg/printers/BUILD
View file @
c758f0af
...
@@ -17,7 +17,6 @@ go_library(
...
@@ -17,7 +17,6 @@ go_library(
deps = [
deps = [
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
...
...
pkg/printers/humanreadable.go
View file @
c758f0af
...
@@ -27,7 +27,6 @@ import (
...
@@ -27,7 +27,6 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/meta"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
metav1beta1
"k8s.io/apimachinery/pkg/apis/meta/v1beta1"
metav1beta1
"k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
...
@@ -40,7 +39,6 @@ type TablePrinter interface {
...
@@ -40,7 +39,6 @@ type TablePrinter interface {
}
}
type
PrintHandler
interface
{
type
PrintHandler
interface
{
Handler
(
columns
,
columnsWithWide
[]
string
,
printFunc
interface
{})
error
TableHandler
(
columns
[]
metav1beta1
.
TableColumnDefinition
,
printFunc
interface
{})
error
TableHandler
(
columns
[]
metav1beta1
.
TableColumnDefinition
,
printFunc
interface
{})
error
DefaultTableHandler
(
columns
[]
metav1beta1
.
TableColumnDefinition
,
printFunc
interface
{})
error
DefaultTableHandler
(
columns
[]
metav1beta1
.
TableColumnDefinition
,
printFunc
interface
{})
error
}
}
...
@@ -49,7 +47,6 @@ var withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print
...
@@ -49,7 +47,6 @@ var withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print
type
handlerEntry
struct
{
type
handlerEntry
struct
{
columnDefinitions
[]
metav1beta1
.
TableColumnDefinition
columnDefinitions
[]
metav1beta1
.
TableColumnDefinition
printRows
bool
printFunc
reflect
.
Value
printFunc
reflect
.
Value
args
[]
reflect
.
Value
args
[]
reflect
.
Value
}
}
...
@@ -65,19 +62,15 @@ type HumanReadablePrinter struct {
...
@@ -65,19 +62,15 @@ type HumanReadablePrinter struct {
lastType
interface
{}
lastType
interface
{}
lastColumns
[]
metav1beta1
.
TableColumnDefinition
lastColumns
[]
metav1beta1
.
TableColumnDefinition
skipTabWriter
bool
skipTabWriter
bool
encoder
runtime
.
Encoder
decoder
runtime
.
Decoder
}
}
var
_
PrintHandler
=
&
HumanReadablePrinter
{}
var
_
PrintHandler
=
&
HumanReadablePrinter
{}
// NewHumanReadablePrinter creates a HumanReadablePrinter.
// NewHumanReadablePrinter creates a HumanReadablePrinter.
// If encoder and decoder are provided, an attempt to convert unstructured types to internal types is made.
func
NewHumanReadablePrinter
(
options
PrintOptions
)
*
HumanReadablePrinter
{
func
NewHumanReadablePrinter
(
decoder
runtime
.
Decoder
,
options
PrintOptions
)
*
HumanReadablePrinter
{
printer
:=
&
HumanReadablePrinter
{
printer
:=
&
HumanReadablePrinter
{
handlerMap
:
make
(
map
[
reflect
.
Type
]
*
handlerEntry
),
handlerMap
:
make
(
map
[
reflect
.
Type
]
*
handlerEntry
),
options
:
options
,
options
:
options
,
decoder
:
decoder
,
}
}
return
printer
return
printer
}
}
...
@@ -112,53 +105,6 @@ func (h *HumanReadablePrinter) EnsurePrintHeaders() {
...
@@ -112,53 +105,6 @@ func (h *HumanReadablePrinter) EnsurePrintHeaders() {
h
.
lastType
=
nil
h
.
lastType
=
nil
}
}
// Handler adds a print handler with a given set of columns to HumanReadablePrinter instance.
// See ValidatePrintHandlerFunc for required method signature.
func
(
h
*
HumanReadablePrinter
)
Handler
(
columns
,
columnsWithWide
[]
string
,
printFunc
interface
{})
error
{
var
columnDefinitions
[]
metav1beta1
.
TableColumnDefinition
for
i
,
column
:=
range
columns
{
format
:=
""
if
i
==
0
&&
strings
.
EqualFold
(
column
,
"name"
)
{
format
=
"name"
}
columnDefinitions
=
append
(
columnDefinitions
,
metav1beta1
.
TableColumnDefinition
{
Name
:
column
,
Description
:
column
,
Type
:
"string"
,
Format
:
format
,
})
}
for
_
,
column
:=
range
columnsWithWide
{
columnDefinitions
=
append
(
columnDefinitions
,
metav1beta1
.
TableColumnDefinition
{
Name
:
column
,
Description
:
column
,
Type
:
"string"
,
Priority
:
1
,
})
}
printFuncValue
:=
reflect
.
ValueOf
(
printFunc
)
if
err
:=
ValidatePrintHandlerFunc
(
printFuncValue
);
err
!=
nil
{
utilruntime
.
HandleError
(
fmt
.
Errorf
(
"unable to register print function: %v"
,
err
))
return
err
}
entry
:=
&
handlerEntry
{
columnDefinitions
:
columnDefinitions
,
printFunc
:
printFuncValue
,
}
objType
:=
printFuncValue
.
Type
()
.
In
(
0
)
if
_
,
ok
:=
h
.
handlerMap
[
objType
];
ok
{
err
:=
fmt
.
Errorf
(
"registered duplicate printer for %v"
,
objType
)
utilruntime
.
HandleError
(
err
)
return
err
}
h
.
handlerMap
[
objType
]
=
entry
return
nil
}
// TableHandler adds a print handler with a given set of columns to HumanReadablePrinter instance.
// TableHandler adds a print handler with a given set of columns to HumanReadablePrinter instance.
// See ValidateRowPrintHandlerFunc for required method signature.
// See ValidateRowPrintHandlerFunc for required method signature.
func
(
h
*
HumanReadablePrinter
)
TableHandler
(
columnDefinitions
[]
metav1beta1
.
TableColumnDefinition
,
printFunc
interface
{})
error
{
func
(
h
*
HumanReadablePrinter
)
TableHandler
(
columnDefinitions
[]
metav1beta1
.
TableColumnDefinition
,
printFunc
interface
{})
error
{
...
@@ -169,7 +115,6 @@ func (h *HumanReadablePrinter) TableHandler(columnDefinitions []metav1beta1.Tabl
...
@@ -169,7 +115,6 @@ func (h *HumanReadablePrinter) TableHandler(columnDefinitions []metav1beta1.Tabl
}
}
entry
:=
&
handlerEntry
{
entry
:=
&
handlerEntry
{
columnDefinitions
:
columnDefinitions
,
columnDefinitions
:
columnDefinitions
,
printRows
:
true
,
printFunc
:
printFuncValue
,
printFunc
:
printFuncValue
,
}
}
...
@@ -194,7 +139,6 @@ func (h *HumanReadablePrinter) DefaultTableHandler(columnDefinitions []metav1bet
...
@@ -194,7 +139,6 @@ func (h *HumanReadablePrinter) DefaultTableHandler(columnDefinitions []metav1bet
}
}
entry
:=
&
handlerEntry
{
entry
:=
&
handlerEntry
{
columnDefinitions
:
columnDefinitions
,
columnDefinitions
:
columnDefinitions
,
printRows
:
true
,
printFunc
:
printFuncValue
,
printFunc
:
printFuncValue
,
}
}
...
@@ -312,12 +256,6 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
...
@@ -312,12 +256,6 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
return
PrintTable
(
table
,
output
,
localOptions
)
return
PrintTable
(
table
,
output
,
localOptions
)
}
}
// check if the object is unstructured. If so, let's attempt to convert it to a type we can understand before
// trying to print, since the printers are keyed by type. This is extremely expensive.
if
h
.
decoder
!=
nil
{
obj
,
_
=
decodeUnknownObject
(
obj
,
h
.
decoder
)
}
// print with a registered handler
// print with a registered handler
t
:=
reflect
.
TypeOf
(
obj
)
t
:=
reflect
.
TypeOf
(
obj
)
if
handler
:=
h
.
handlerMap
[
t
];
handler
!=
nil
{
if
handler
:=
h
.
handlerMap
[
t
];
handler
!=
nil
{
...
@@ -516,9 +454,6 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
...
@@ -516,9 +454,6 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
if
!
ok
{
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"no table handler registered for this type %v"
,
t
)
return
nil
,
fmt
.
Errorf
(
"no table handler registered for this type %v"
,
t
)
}
}
if
!
handler
.
printRows
{
return
h
.
legacyPrinterToTable
(
obj
,
handler
)
}
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
options
)}
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
options
)}
results
:=
handler
.
printFunc
.
Call
(
args
)
results
:=
handler
.
printFunc
.
Call
(
args
)
...
@@ -567,12 +502,11 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
...
@@ -567,12 +502,11 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
// or an error, if any.
// or an error, if any.
func
printRowsForHandlerEntry
(
output
io
.
Writer
,
handler
*
handlerEntry
,
obj
runtime
.
Object
,
options
PrintOptions
,
includeHeaders
bool
)
error
{
func
printRowsForHandlerEntry
(
output
io
.
Writer
,
handler
*
handlerEntry
,
obj
runtime
.
Object
,
options
PrintOptions
,
includeHeaders
bool
)
error
{
var
results
[]
reflect
.
Value
var
results
[]
reflect
.
Value
if
handler
.
printRows
{
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
options
)}
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
options
)}
results
=
handler
.
printFunc
.
Call
(
args
)
results
=
handler
.
printFunc
.
Call
(
args
)
if
!
results
[
1
]
.
IsNil
()
{
if
!
results
[
1
]
.
IsNil
()
{
return
results
[
1
]
.
Interface
()
.
(
error
)
return
results
[
1
]
.
Interface
()
.
(
error
)
}
}
}
if
includeHeaders
{
if
includeHeaders
{
...
@@ -592,16 +526,6 @@ func printRowsForHandlerEntry(output io.Writer, handler *handlerEntry, obj runti
...
@@ -592,16 +526,6 @@ func printRowsForHandlerEntry(output io.Writer, handler *handlerEntry, obj runti
printHeader
(
headers
,
output
)
printHeader
(
headers
,
output
)
}
}
if
!
handler
.
printRows
{
// TODO: this code path is deprecated and will be removed when all handlers are row printers
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
output
),
reflect
.
ValueOf
(
options
)}
resultValue
:=
handler
.
printFunc
.
Call
(
args
)[
0
]
if
resultValue
.
IsNil
()
{
return
nil
}
return
resultValue
.
Interface
()
.
(
error
)
}
if
results
[
1
]
.
IsNil
()
{
if
results
[
1
]
.
IsNil
()
{
rows
:=
results
[
0
]
.
Interface
()
.
([]
metav1beta1
.
TableRow
)
rows
:=
results
[
0
]
.
Interface
()
.
([]
metav1beta1
.
TableRow
)
printRows
(
output
,
rows
,
options
)
printRows
(
output
,
rows
,
options
)
...
@@ -649,67 +573,6 @@ func printRows(output io.Writer, rows []metav1beta1.TableRow, options PrintOptio
...
@@ -649,67 +573,6 @@ func printRows(output io.Writer, rows []metav1beta1.TableRow, options PrintOptio
}
}
}
}
// legacyPrinterToTable uses the old printFunc with tabbed writer to generate a table.
// TODO: remove when all legacy printers are removed.
func
(
h
*
HumanReadablePrinter
)
legacyPrinterToTable
(
obj
runtime
.
Object
,
handler
*
handlerEntry
)
(
*
metav1beta1
.
Table
,
error
)
{
printFunc
:=
handler
.
printFunc
table
:=
&
metav1beta1
.
Table
{
ColumnDefinitions
:
handler
.
columnDefinitions
,
}
options
:=
PrintOptions
{
NoHeaders
:
true
,
Wide
:
true
,
}
buf
:=
&
bytes
.
Buffer
{}
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
buf
),
reflect
.
ValueOf
(
options
)}
if
meta
.
IsListType
(
obj
)
{
listInterface
,
ok
:=
obj
.
(
metav1
.
ListInterface
)
if
ok
{
table
.
ListMeta
.
SelfLink
=
listInterface
.
GetSelfLink
()
table
.
ListMeta
.
ResourceVersion
=
listInterface
.
GetResourceVersion
()
table
.
ListMeta
.
Continue
=
listInterface
.
GetContinue
()
}
// TODO: this uses more memory than it has to, as we refactor printers we should remove the need
// for this.
args
[
0
]
=
reflect
.
ValueOf
(
obj
)
resultValue
:=
printFunc
.
Call
(
args
)[
0
]
if
!
resultValue
.
IsNil
()
{
return
nil
,
resultValue
.
Interface
()
.
(
error
)
}
data
:=
buf
.
Bytes
()
i
:=
0
items
,
err
:=
meta
.
ExtractList
(
obj
)
if
err
!=
nil
{
return
nil
,
err
}
for
len
(
data
)
>
0
{
cells
,
remainder
:=
tabbedLineToCells
(
data
,
len
(
table
.
ColumnDefinitions
))
table
.
Rows
=
append
(
table
.
Rows
,
metav1beta1
.
TableRow
{
Cells
:
cells
,
Object
:
runtime
.
RawExtension
{
Object
:
items
[
i
]},
})
data
=
remainder
i
++
}
}
else
{
args
[
0
]
=
reflect
.
ValueOf
(
obj
)
resultValue
:=
printFunc
.
Call
(
args
)[
0
]
if
!
resultValue
.
IsNil
()
{
return
nil
,
resultValue
.
Interface
()
.
(
error
)
}
data
:=
buf
.
Bytes
()
cells
,
_
:=
tabbedLineToCells
(
data
,
len
(
table
.
ColumnDefinitions
))
table
.
Rows
=
append
(
table
.
Rows
,
metav1beta1
.
TableRow
{
Cells
:
cells
,
Object
:
runtime
.
RawExtension
{
Object
:
obj
},
})
}
return
table
,
nil
}
// TODO: this method assumes the meta/v1 server API, so should be refactored out of this package
// TODO: this method assumes the meta/v1 server API, so should be refactored out of this package
func
printUnstructured
(
unstructured
runtime
.
Unstructured
,
w
io
.
Writer
,
additionalFields
[]
string
,
options
PrintOptions
)
error
{
func
printUnstructured
(
unstructured
runtime
.
Unstructured
,
w
io
.
Writer
,
additionalFields
[]
string
,
options
PrintOptions
)
error
{
metadata
,
err
:=
meta
.
Accessor
(
unstructured
)
metadata
,
err
:=
meta
.
Accessor
(
unstructured
)
...
@@ -848,46 +711,3 @@ func AppendAllLabels(showLabels bool, itemLabels map[string]string) string {
...
@@ -848,46 +711,3 @@ func AppendAllLabels(showLabels bool, itemLabels map[string]string) string {
return
buffer
.
String
()
return
buffer
.
String
()
}
}
// check if the object is unstructured. If so, attempt to convert it to a type we can understand.
func
decodeUnknownObject
(
obj
runtime
.
Object
,
decoder
runtime
.
Decoder
)
(
runtime
.
Object
,
error
)
{
var
err
error
switch
t
:=
obj
.
(
type
)
{
case
runtime
.
Unstructured
:
if
objBytes
,
err
:=
runtime
.
Encode
(
unstructured
.
UnstructuredJSONScheme
,
obj
);
err
==
nil
{
if
decodedObj
,
err
:=
runtime
.
Decode
(
decoder
,
objBytes
);
err
==
nil
{
obj
=
decodedObj
}
}
case
*
runtime
.
Unknown
:
if
decodedObj
,
err
:=
runtime
.
Decode
(
decoder
,
t
.
Raw
);
err
==
nil
{
obj
=
decodedObj
}
}
return
obj
,
err
}
func
tabbedLineToCells
(
data
[]
byte
,
expected
int
)
([]
interface
{},
[]
byte
)
{
var
remainder
[]
byte
max
:=
bytes
.
Index
(
data
,
[]
byte
(
"
\n
"
))
if
max
!=
-
1
{
remainder
=
data
[
max
+
1
:
]
data
=
data
[
:
max
]
}
cells
:=
make
([]
interface
{},
expected
)
for
i
:=
0
;
i
<
expected
;
i
++
{
next
:=
bytes
.
Index
(
data
,
[]
byte
(
"
\t
"
))
if
next
==
-
1
{
cells
[
i
]
=
string
(
data
)
// fill the remainder with empty strings, this indicates a printer bug
for
j
:=
i
+
1
;
j
<
expected
;
j
++
{
cells
[
j
]
=
""
}
break
}
cells
[
i
]
=
string
(
data
[
:
next
])
data
=
data
[
next
+
1
:
]
}
return
cells
,
remainder
}
pkg/printers/humanreadable_test.go
View file @
c758f0af
...
@@ -61,7 +61,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
...
@@ -61,7 +61,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
name
:
"no tablecolumndefinition and includeheader flase"
,
name
:
"no tablecolumndefinition and includeheader flase"
,
h
:
&
handlerEntry
{
h
:
&
handlerEntry
{
columnDefinitions
:
[]
metav1beta1
.
TableColumnDefinition
{},
columnDefinitions
:
[]
metav1beta1
.
TableColumnDefinition
{},
printRows
:
true
,
printFunc
:
printFunc
,
printFunc
:
printFunc
,
},
},
opt
:
PrintOptions
{},
opt
:
PrintOptions
{},
...
@@ -75,7 +74,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
...
@@ -75,7 +74,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
name
:
"no tablecolumndefinition and includeheader true"
,
name
:
"no tablecolumndefinition and includeheader true"
,
h
:
&
handlerEntry
{
h
:
&
handlerEntry
{
columnDefinitions
:
[]
metav1beta1
.
TableColumnDefinition
{},
columnDefinitions
:
[]
metav1beta1
.
TableColumnDefinition
{},
printRows
:
true
,
printFunc
:
printFunc
,
printFunc
:
printFunc
,
},
},
opt
:
PrintOptions
{},
opt
:
PrintOptions
{},
...
@@ -89,7 +87,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
...
@@ -89,7 +87,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
name
:
"have tablecolumndefinition and includeheader true"
,
name
:
"have tablecolumndefinition and includeheader true"
,
h
:
&
handlerEntry
{
h
:
&
handlerEntry
{
columnDefinitions
:
testNamespaceColumnDefinitions
,
columnDefinitions
:
testNamespaceColumnDefinitions
,
printRows
:
true
,
printFunc
:
printFunc
,
printFunc
:
printFunc
,
},
},
opt
:
PrintOptions
{},
opt
:
PrintOptions
{},
...
@@ -103,7 +100,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
...
@@ -103,7 +100,6 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
name
:
"print namespace and withnamespace true, should not print header"
,
name
:
"print namespace and withnamespace true, should not print header"
,
h
:
&
handlerEntry
{
h
:
&
handlerEntry
{
columnDefinitions
:
testNamespaceColumnDefinitions
,
columnDefinitions
:
testNamespaceColumnDefinitions
,
printRows
:
true
,
printFunc
:
printFunc
,
printFunc
:
printFunc
,
},
},
opt
:
PrintOptions
{
opt
:
PrintOptions
{
...
...
pkg/printers/internalversion/printers_test.go
View file @
c758f0af
...
@@ -20,7 +20,6 @@ import (
...
@@ -20,7 +20,6 @@ import (
"bytes"
"bytes"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"io"
"reflect"
"reflect"
"regexp"
"regexp"
"strconv"
"strconv"
...
@@ -30,7 +29,7 @@ import (
...
@@ -30,7 +29,7 @@ import (
"sigs.k8s.io/yaml"
"sigs.k8s.io/yaml"
"k8s.io/api/core/v1"
v1
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/api/resource"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
...
@@ -172,7 +171,7 @@ func TestPrintUnstructuredObject(t *testing.T) {
...
@@ -172,7 +171,7 @@ func TestPrintUnstructuredObject(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
out
.
Reset
()
out
.
Reset
()
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
test
.
options
)
.
With
(
AddDefaultHandlers
)
printer
:=
printers
.
NewHumanReadablePrinter
(
test
.
options
)
.
With
(
AddDefaultHandlers
)
printer
.
PrintObj
(
test
.
object
,
out
)
printer
.
PrintObj
(
test
.
object
,
out
)
matches
,
err
:=
regexp
.
MatchString
(
test
.
expected
,
out
.
String
())
matches
,
err
:=
regexp
.
MatchString
(
test
.
expected
,
out
.
String
())
...
@@ -285,24 +284,23 @@ func TestFormatResourceName(t *testing.T) {
...
@@ -285,24 +284,23 @@ func TestFormatResourceName(t *testing.T) {
}
}
}
}
func
PrintCustomType
(
obj
*
TestPrintType
,
w
io
.
Writer
,
options
printers
.
PrintOptions
)
error
{
func
PrintCustomType
(
obj
*
TestPrintType
,
options
printers
.
PrintOptions
)
([]
metav1beta1
.
TableRow
,
error
)
{
data
:=
obj
.
Data
data
:=
obj
.
Data
kind
:=
options
.
Kind
kind
:=
options
.
Kind
if
options
.
WithKind
{
if
options
.
WithKind
{
data
=
kind
.
String
()
+
"/"
+
data
data
=
kind
.
String
()
+
"/"
+
data
}
}
_
,
err
:=
fmt
.
Fprintf
(
w
,
"%s"
,
data
)
return
[]
metav1beta1
.
TableRow
{{
Cells
:
[]
interface
{}{
data
}}},
nil
return
err
}
}
func
ErrorPrintHandler
(
obj
*
TestPrintType
,
w
io
.
Writer
,
options
printers
.
PrintOptions
)
error
{
func
ErrorPrintHandler
(
obj
*
TestPrintType
,
options
printers
.
PrintOptions
)
([]
metav1beta1
.
TableRow
,
error
)
{
return
fmt
.
Errorf
(
"ErrorPrintHandler error"
)
return
nil
,
fmt
.
Errorf
(
"ErrorPrintHandler error"
)
}
}
func
TestCustomTypePrinting
(
t
*
testing
.
T
)
{
func
TestCustomTypePrinting
(
t
*
testing
.
T
)
{
columns
:=
[]
string
{
"Data"
}
columns
:=
[]
metav1beta1
.
TableColumnDefinition
{{
Name
:
"Data"
}
}
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{})
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{})
printer
.
Handler
(
columns
,
nil
,
PrintCustomType
)
printer
.
TableHandler
(
columns
,
PrintCustomType
)
obj
:=
TestPrintType
{
"test object"
}
obj
:=
TestPrintType
{
"test object"
}
buffer
:=
&
bytes
.
Buffer
{}
buffer
:=
&
bytes
.
Buffer
{}
...
@@ -310,16 +308,16 @@ func TestCustomTypePrinting(t *testing.T) {
...
@@ -310,16 +308,16 @@ func TestCustomTypePrinting(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"An error occurred printing the custom type: %#v"
,
err
)
t
.
Fatalf
(
"An error occurred printing the custom type: %#v"
,
err
)
}
}
expectedOutput
:=
"DATA
\n
test object"
expectedOutput
:=
"DATA
\n
test object
\n
"
if
buffer
.
String
()
!=
expectedOutput
{
if
buffer
.
String
()
!=
expectedOutput
{
t
.
Errorf
(
"The data was not printed as expected. Expected:
\n
%s
\n
Got:
\n
%s"
,
expectedOutput
,
buffer
.
String
())
t
.
Errorf
(
"The data was not printed as expected. Expected:
\n
%s
\n
Got:
\n
%s"
,
expectedOutput
,
buffer
.
String
())
}
}
}
}
func
TestPrintHandlerError
(
t
*
testing
.
T
)
{
func
TestPrintHandlerError
(
t
*
testing
.
T
)
{
columns
:=
[]
string
{
"Data"
}
columns
:=
[]
metav1beta1
.
TableColumnDefinition
{{
Name
:
"Data"
}
}
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{})
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{})
printer
.
Handler
(
columns
,
nil
,
ErrorPrintHandler
)
printer
.
TableHandler
(
columns
,
ErrorPrintHandler
)
obj
:=
TestPrintType
{
"test object"
}
obj
:=
TestPrintType
{
"test object"
}
buffer
:=
&
bytes
.
Buffer
{}
buffer
:=
&
bytes
.
Buffer
{}
err
:=
printer
.
PrintObj
(
&
obj
,
buffer
)
err
:=
printer
.
PrintObj
(
&
obj
,
buffer
)
...
@@ -329,7 +327,7 @@ func TestPrintHandlerError(t *testing.T) {
...
@@ -329,7 +327,7 @@ func TestPrintHandlerError(t *testing.T) {
}
}
func
TestUnknownTypePrinting
(
t
*
testing
.
T
)
{
func
TestUnknownTypePrinting
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{})
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{})
buffer
:=
&
bytes
.
Buffer
{}
buffer
:=
&
bytes
.
Buffer
{}
err
:=
printer
.
PrintObj
(
&
TestUnknownType
{},
buffer
)
err
:=
printer
.
PrintObj
(
&
TestUnknownType
{},
buffer
)
if
err
==
nil
{
if
err
==
nil
{
...
@@ -590,10 +588,10 @@ func TestPrinters(t *testing.T) {
...
@@ -590,10 +588,10 @@ func TestPrinters(t *testing.T) {
// a humanreadable printer deals with internal-versioned objects
// a humanreadable printer deals with internal-versioned objects
humanReadablePrinter
:=
map
[
string
]
printers
.
ResourcePrinter
{
humanReadablePrinter
:=
map
[
string
]
printers
.
ResourcePrinter
{
"humanReadable"
:
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
"humanReadable"
:
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
NoHeaders
:
true
,
NoHeaders
:
true
,
}),
}),
"humanReadableHeaders"
:
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{}),
"humanReadableHeaders"
:
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{}),
}
}
AddHandlers
((
humanReadablePrinter
[
"humanReadable"
])
.
(
*
printers
.
HumanReadablePrinter
))
AddHandlers
((
humanReadablePrinter
[
"humanReadable"
])
.
(
*
printers
.
HumanReadablePrinter
))
AddHandlers
((
humanReadablePrinter
[
"humanReadableHeaders"
])
.
(
*
printers
.
HumanReadablePrinter
))
AddHandlers
((
humanReadablePrinter
[
"humanReadableHeaders"
])
.
(
*
printers
.
HumanReadablePrinter
))
...
@@ -613,7 +611,7 @@ func TestPrinters(t *testing.T) {
...
@@ -613,7 +611,7 @@ func TestPrinters(t *testing.T) {
func
TestPrintEventsResultSorted
(
t
*
testing
.
T
)
{
func
TestPrintEventsResultSorted
(
t
*
testing
.
T
)
{
// Arrange
// Arrange
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{})
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{})
AddHandlers
(
printer
)
AddHandlers
(
printer
)
obj
:=
api
.
EventList
{
obj
:=
api
.
EventList
{
...
@@ -658,7 +656,7 @@ func TestPrintEventsResultSorted(t *testing.T) {
...
@@ -658,7 +656,7 @@ func TestPrintEventsResultSorted(t *testing.T) {
}
}
func
TestPrintNodeStatus
(
t
*
testing
.
T
)
{
func
TestPrintNodeStatus
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{})
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{})
AddHandlers
(
printer
)
AddHandlers
(
printer
)
table
:=
[]
struct
{
table
:=
[]
struct
{
node
api
.
Node
node
api
.
Node
...
@@ -748,7 +746,7 @@ func TestPrintNodeStatus(t *testing.T) {
...
@@ -748,7 +746,7 @@ func TestPrintNodeStatus(t *testing.T) {
}
}
func
TestPrintNodeRole
(
t
*
testing
.
T
)
{
func
TestPrintNodeRole
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{})
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{})
AddHandlers
(
printer
)
AddHandlers
(
printer
)
table
:=
[]
struct
{
table
:=
[]
struct
{
node
api
.
Node
node
api
.
Node
...
@@ -793,7 +791,7 @@ func TestPrintNodeRole(t *testing.T) {
...
@@ -793,7 +791,7 @@ func TestPrintNodeRole(t *testing.T) {
}
}
func
TestPrintNodeOSImage
(
t
*
testing
.
T
)
{
func
TestPrintNodeOSImage
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
ColumnLabels
:
[]
string
{},
ColumnLabels
:
[]
string
{},
Wide
:
true
,
Wide
:
true
,
})
})
...
@@ -838,7 +836,7 @@ func TestPrintNodeOSImage(t *testing.T) {
...
@@ -838,7 +836,7 @@ func TestPrintNodeOSImage(t *testing.T) {
}
}
func
TestPrintNodeKernelVersion
(
t
*
testing
.
T
)
{
func
TestPrintNodeKernelVersion
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
ColumnLabels
:
[]
string
{},
ColumnLabels
:
[]
string
{},
Wide
:
true
,
Wide
:
true
,
})
})
...
@@ -883,7 +881,7 @@ func TestPrintNodeKernelVersion(t *testing.T) {
...
@@ -883,7 +881,7 @@ func TestPrintNodeKernelVersion(t *testing.T) {
}
}
func
TestPrintNodeContainerRuntimeVersion
(
t
*
testing
.
T
)
{
func
TestPrintNodeContainerRuntimeVersion
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
ColumnLabels
:
[]
string
{},
ColumnLabels
:
[]
string
{},
Wide
:
true
,
Wide
:
true
,
})
})
...
@@ -928,7 +926,7 @@ func TestPrintNodeContainerRuntimeVersion(t *testing.T) {
...
@@ -928,7 +926,7 @@ func TestPrintNodeContainerRuntimeVersion(t *testing.T) {
}
}
func
TestPrintNodeName
(
t
*
testing
.
T
)
{
func
TestPrintNodeName
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
Wide
:
true
,
Wide
:
true
,
})
})
AddHandlers
(
printer
)
AddHandlers
(
printer
)
...
@@ -965,7 +963,7 @@ func TestPrintNodeName(t *testing.T) {
...
@@ -965,7 +963,7 @@ func TestPrintNodeName(t *testing.T) {
}
}
func
TestPrintNodeExternalIP
(
t
*
testing
.
T
)
{
func
TestPrintNodeExternalIP
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
Wide
:
true
,
Wide
:
true
,
})
})
AddHandlers
(
printer
)
AddHandlers
(
printer
)
...
@@ -1013,7 +1011,7 @@ func TestPrintNodeExternalIP(t *testing.T) {
...
@@ -1013,7 +1011,7 @@ func TestPrintNodeExternalIP(t *testing.T) {
}
}
func
TestPrintNodeInternalIP
(
t
*
testing
.
T
)
{
func
TestPrintNodeInternalIP
(
t
*
testing
.
T
)
{
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
Wide
:
true
,
Wide
:
true
,
})
})
AddHandlers
(
printer
)
AddHandlers
(
printer
)
...
@@ -1421,7 +1419,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
...
@@ -1421,7 +1419,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
for
i
,
test
:=
range
table
{
for
i
,
test
:=
range
table
{
if
test
.
isNamespaced
{
if
test
.
isNamespaced
{
// Expect output to include namespace when requested.
// Expect output to include namespace when requested.
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
WithNamespace
:
true
,
WithNamespace
:
true
,
})
})
AddHandlers
(
printer
)
AddHandlers
(
printer
)
...
@@ -1436,7 +1434,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
...
@@ -1436,7 +1434,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
}
}
}
else
{
}
else
{
// Expect error when trying to get all namespaces for un-namespaced object.
// Expect error when trying to get all namespaces for un-namespaced object.
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
printers
.
PrintOptions
{
printer
:=
printers
.
NewHumanReadablePrinter
(
printers
.
PrintOptions
{
WithNamespace
:
true
,
WithNamespace
:
true
,
})
})
buffer
:=
&
bytes
.
Buffer
{}
buffer
:=
&
bytes
.
Buffer
{}
...
@@ -1515,7 +1513,7 @@ func TestPrintPodTable(t *testing.T) {
...
@@ -1515,7 +1513,7 @@ func TestPrintPodTable(t *testing.T) {
}
}
verifyTable
(
t
,
table
)
verifyTable
(
t
,
table
)
buf
:=
&
bytes
.
Buffer
{}
buf
:=
&
bytes
.
Buffer
{}
p
:=
printers
.
NewHumanReadablePrinter
(
nil
,
test
.
opts
)
.
With
(
AddHandlers
)
.
AddTabWriter
(
false
)
p
:=
printers
.
NewHumanReadablePrinter
(
test
.
opts
)
.
With
(
AddHandlers
)
.
AddTabWriter
(
false
)
if
err
:=
p
.
PrintObj
(
table
,
buf
);
err
!=
nil
{
if
err
:=
p
.
PrintObj
(
table
,
buf
);
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment