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
22b5e8bb
Unverified
Commit
22b5e8bb
authored
Mar 31, 2018
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Restore show-kind function when printing multiple kinds
parent
a22f9fd3
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
50 additions
and
93 deletions
+50
-93
get.go
pkg/kubectl/cmd/resource/get.go
+5
-27
get_test.go
pkg/kubectl/cmd/resource/get_test.go
+16
-16
humanreadable.go
pkg/printers/humanreadable.go
+7
-20
interface.go
pkg/printers/interface.go
+2
-1
printers.go
pkg/printers/internalversion/printers.go
+7
-1
printers_test.go
pkg/printers/internalversion/printers_test.go
+13
-28
No files found.
pkg/kubectl/cmd/resource/get.go
View file @
22b5e8bb
...
...
@@ -382,6 +382,11 @@ func (options *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []str
updatePrintOptionsForOpenAPI
(
f
,
mapping
,
printOpts
)
}
if
showKind
&&
mapping
!=
nil
{
printOpts
.
WithKind
=
true
printOpts
.
Kind
=
mapping
.
GroupVersionKind
.
GroupKind
()
}
printer
,
err
=
cmdutil
.
PrinterForOptions
(
printOpts
)
if
err
!=
nil
{
if
!
errs
.
Has
(
err
.
Error
())
{
...
...
@@ -415,33 +420,6 @@ func (options *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []str
}
}
if
resourcePrinter
,
found
:=
printer
.
(
*
printers
.
HumanReadablePrinter
);
found
{
resourceName
:=
resourcePrinter
.
GetResourceKind
()
if
mapping
!=
nil
{
if
resourceName
==
""
{
resourceName
=
mapping
.
Resource
}
if
alias
,
ok
:=
kubectl
.
ResourceShortFormFor
(
mapping
.
Resource
);
ok
{
resourceName
=
alias
}
else
if
resourceName
==
""
{
resourceName
=
"none"
}
}
else
{
resourceName
=
"none"
}
if
showKind
{
resourcePrinter
.
EnsurePrintWithKind
(
resourceName
)
}
if
err
:=
printer
.
PrintObj
(
typedObj
,
w
);
err
!=
nil
{
if
!
errs
.
Has
(
err
.
Error
())
{
errs
.
Insert
(
err
.
Error
())
allErrs
=
append
(
allErrs
,
err
)
}
}
continue
}
objToPrint
:=
typedObj
if
printer
.
IsGeneric
()
{
// use raw object as received from the builder when using generic
...
...
pkg/kubectl/cmd/resource/get_test.go
View file @
22b5e8bb
...
...
@@ -756,10 +756,10 @@ func TestGetMultipleTypeObjects(t *testing.T) {
cmd
.
Run
(
cmd
,
[]
string
{
"pods,services"
})
expected
:=
`NAME READY STATUS RESTARTS AGE
foo
0/0 0 <unknown>
bar
0/0 0 <unknown>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz
ClusterIP <none> <none> <none> <unknown>
pod/foo
0/0 0 <unknown>
pod/bar
0/0 0 <unknown>
NAME
TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/baz
ClusterIP <none> <none> <none> <unknown>
`
if
e
,
a
:=
expected
,
buf
.
String
();
e
!=
a
{
t
.
Errorf
(
"expected %v, got %v"
,
e
,
a
)
...
...
@@ -904,10 +904,10 @@ func TestGetMultipleTypeObjectsWithLabelSelector(t *testing.T) {
cmd
.
Run
(
cmd
,
[]
string
{
"pods,services"
})
expected
:=
`NAME READY STATUS RESTARTS AGE
foo
0/0 0 <unknown>
bar
0/0 0 <unknown>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz
ClusterIP <none> <none> <none> <unknown>
pod/foo
0/0 0 <unknown>
pod/bar
0/0 0 <unknown>
NAME
TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/baz
ClusterIP <none> <none> <none> <unknown>
`
if
e
,
a
:=
expected
,
buf
.
String
();
e
!=
a
{
t
.
Errorf
(
"expected %v, got %v"
,
e
,
a
)
...
...
@@ -949,10 +949,10 @@ func TestGetMultipleTypeObjectsWithFieldSelector(t *testing.T) {
cmd
.
Run
(
cmd
,
[]
string
{
"pods,services"
})
expected
:=
`NAME READY STATUS RESTARTS AGE
foo
0/0 0 <unknown>
bar
0/0 0 <unknown>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz
ClusterIP <none> <none> <none> <unknown>
pod/foo
0/0 0 <unknown>
pod/bar
0/0 0 <unknown>
NAME
TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/baz
ClusterIP <none> <none> <none> <unknown>
`
if
e
,
a
:=
expected
,
buf
.
String
();
e
!=
a
{
t
.
Errorf
(
"expected %v, got %v"
,
e
,
a
)
...
...
@@ -997,10 +997,10 @@ func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) {
cmd
.
Run
(
cmd
,
[]
string
{
"services/bar"
,
"node/foo"
})
expected
:=
`NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz
ClusterIP <none> <none> <none> <unknown>
NAME STATUS ROLES AGE VERSION
foo
Unknown <none> <unknown>
expected
:=
`NAME
TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/baz
ClusterIP <none> <none> <none> <unknown>
NAME
STATUS ROLES AGE VERSION
node/foo
Unknown <none> <unknown>
`
if
e
,
a
:=
expected
,
buf
.
String
();
e
!=
a
{
t
.
Errorf
(
"expected %v, got %v"
,
e
,
a
)
...
...
pkg/printers/humanreadable.go
View file @
22b5e8bb
...
...
@@ -101,19 +101,6 @@ func (a *HumanReadablePrinter) With(fns ...func(PrintHandler)) *HumanReadablePri
return
a
}
// GetResourceKind returns the type currently set for a resource
func
(
h
*
HumanReadablePrinter
)
GetResourceKind
()
string
{
return
h
.
options
.
Kind
}
// EnsurePrintWithKind sets HumanReadablePrinter options "WithKind" to true
// and "Kind" to the string arg it receives, pre-pending this string
// to the "NAME" column in an output of resources.
func
(
h
*
HumanReadablePrinter
)
EnsurePrintWithKind
(
kind
string
)
{
h
.
options
.
WithKind
=
true
h
.
options
.
Kind
=
kind
}
// EnsurePrintHeaders sets the HumanReadablePrinter option "NoHeaders" to false
// and removes the .lastType that was printed, which forces headers to be
// printed in cases where multiple lists of the same resource are printed
...
...
@@ -414,7 +401,7 @@ func DecorateTable(table *metav1beta1.Table, options PrintOptions) error {
columns
:=
table
.
ColumnDefinitions
nameColumn
:=
-
1
if
options
.
WithKind
&&
len
(
options
.
Kind
)
>
0
{
if
options
.
WithKind
&&
!
options
.
Kind
.
Empty
()
{
for
i
:=
range
columns
{
if
columns
[
i
]
.
Format
==
"name"
&&
columns
[
i
]
.
Type
==
"string"
{
nameColumn
=
i
...
...
@@ -454,7 +441,7 @@ func DecorateTable(table *metav1beta1.Table, options PrintOptions) error {
row
:=
rows
[
i
]
if
nameColumn
!=
-
1
{
row
.
Cells
[
nameColumn
]
=
fmt
.
Sprintf
(
"%s/%s"
,
options
.
Kind
,
row
.
Cells
[
nameColumn
])
row
.
Cells
[
nameColumn
]
=
fmt
.
Sprintf
(
"%s/%s"
,
strings
.
ToLower
(
options
.
Kind
.
String
())
,
row
.
Cells
[
nameColumn
])
}
var
m
metav1
.
Object
...
...
@@ -610,8 +597,8 @@ func printRows(output io.Writer, rows []metav1beta1.TableRow, options PrintOptio
fmt
.
Fprint
(
output
,
"
\t
"
)
}
else
{
// TODO: remove this once we drop the legacy printers
if
options
.
WithKind
&&
len
(
options
.
Kind
)
>
0
{
fmt
.
Fprintf
(
output
,
"%s/%s"
,
options
.
Kind
,
cell
)
if
options
.
WithKind
&&
!
options
.
Kind
.
Empty
()
{
fmt
.
Fprintf
(
output
,
"%s/%s"
,
strings
.
ToLower
(
options
.
Kind
.
String
())
,
cell
)
continue
}
}
...
...
@@ -788,12 +775,12 @@ func appendLabelCells(values []interface{}, itemLabels map[string]string, opts P
// FormatResourceName receives a resource kind, name, and boolean specifying
// whether or not to update the current name to "kind/name"
func
FormatResourceName
(
kind
,
name
string
,
withKind
bool
)
string
{
if
!
withKind
||
kind
==
""
{
func
FormatResourceName
(
kind
schema
.
GroupKind
,
name
string
,
withKind
bool
)
string
{
if
!
withKind
||
kind
.
Empty
()
{
return
name
}
return
kind
+
"/"
+
name
return
strings
.
ToLower
(
kind
.
String
())
+
"/"
+
name
}
func
AppendLabels
(
itemLabels
map
[
string
]
string
,
columnLabels
[]
string
)
string
{
...
...
pkg/printers/interface.go
View file @
22b5e8bb
...
...
@@ -21,6 +21,7 @@ import (
"io"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// ResourcePrinter is an interface that knows how to print runtime objects.
...
...
@@ -68,7 +69,7 @@ type PrintOptions struct {
ShowAll
bool
ShowLabels
bool
AbsoluteTimestamps
bool
Kind
s
tring
Kind
s
chema
.
GroupKind
ColumnLabels
[]
string
SortBy
string
...
...
pkg/printers/internalversion/printers.go
View file @
22b5e8bb
...
...
@@ -40,6 +40,7 @@ import (
metav1beta1
"k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/api/events"
...
...
@@ -1747,7 +1748,12 @@ func printControllerRevision(obj *apps.ControllerRevision, options printers.Prin
controllerName
:=
"<none>"
if
controllerRef
!=
nil
{
withKind
:=
true
controllerName
=
printers
.
FormatResourceName
(
controllerRef
.
Kind
,
controllerRef
.
Name
,
withKind
)
gv
,
err
:=
schema
.
ParseGroupVersion
(
controllerRef
.
APIVersion
)
if
err
!=
nil
{
return
nil
,
err
}
gvk
:=
gv
.
WithKind
(
controllerRef
.
Kind
)
controllerName
=
printers
.
FormatResourceName
(
gvk
.
GroupKind
(),
controllerRef
.
Name
,
withKind
)
}
revision
:=
obj
.
Revision
age
:=
translateTimestamp
(
obj
.
CreationTimestamp
)
...
...
pkg/printers/internalversion/printers_test.go
View file @
22b5e8bb
...
...
@@ -375,13 +375,15 @@ func TestJSONPrinter(t *testing.T) {
func
TestFormatResourceName
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
kind
,
name
string
want
string
kind
schema
.
GroupKind
name
string
want
string
}{
{
""
,
""
,
""
},
{
""
,
"name"
,
"name"
},
{
"kind"
,
""
,
"kind/"
},
// should not happen in practice
{
"kind"
,
"name"
,
"kind/name"
},
{
schema
.
GroupKind
{},
""
,
""
},
{
schema
.
GroupKind
{},
"name"
,
"name"
},
{
schema
.
GroupKind
{
Kind
:
"Kind"
},
""
,
"kind/"
},
// should not happen in practice
{
schema
.
GroupKind
{
Kind
:
"Kind"
},
"name"
,
"kind/name"
},
{
schema
.
GroupKind
{
Group
:
"group"
,
Kind
:
"Kind"
},
"name"
,
"kind.group/name"
},
}
for
_
,
tt
:=
range
tests
{
if
got
:=
printers
.
FormatResourceName
(
tt
.
kind
,
tt
.
name
,
true
);
got
!=
tt
.
want
{
...
...
@@ -394,7 +396,7 @@ func PrintCustomType(obj *TestPrintType, w io.Writer, options printers.PrintOpti
data
:=
obj
.
Data
kind
:=
options
.
Kind
if
options
.
WithKind
{
data
=
kind
+
"/"
+
data
data
=
kind
.
String
()
+
"/"
+
data
}
_
,
err
:=
fmt
.
Fprintf
(
w
,
"%s"
,
data
)
return
err
...
...
@@ -421,24 +423,6 @@ func TestCustomTypePrinting(t *testing.T) {
}
}
func
TestCustomTypePrintingWithKind
(
t
*
testing
.
T
)
{
columns
:=
[]
string
{
"Data"
}
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
nil
,
printers
.
PrintOptions
{})
printer
.
Handler
(
columns
,
nil
,
PrintCustomType
)
printer
.
EnsurePrintWithKind
(
"test"
)
obj
:=
TestPrintType
{
"test object"
}
buffer
:=
&
bytes
.
Buffer
{}
err
:=
printer
.
PrintObj
(
&
obj
,
buffer
)
if
err
!=
nil
{
t
.
Fatalf
(
"An error occurred printing the custom type: %#v"
,
err
)
}
expectedOutput
:=
"DATA
\n
test/test object"
if
buffer
.
String
()
!=
expectedOutput
{
t
.
Errorf
(
"The data was not printed as expected. Expected:
\n
%s
\n
Got:
\n
%s"
,
expectedOutput
,
buffer
.
String
())
}
}
func
TestPrintHandlerError
(
t
*
testing
.
T
)
{
columns
:=
[]
string
{
"Data"
}
printer
:=
printers
.
NewHumanReadablePrinter
(
nil
,
nil
,
printers
.
PrintOptions
{})
...
...
@@ -1534,8 +1518,8 @@ func TestPrintPodTable(t *testing.T) {
expect
:
"NAME
\t
READY
\t
STATUS
\t
RESTARTS
\t
AGE
\n
test1
\t
1/2
\t
Running
\t
6
\t
<unknown>
\n
"
,
},
{
obj
:
runningPod
,
opts
:
printers
.
PrintOptions
{
WithKind
:
true
,
Kind
:
"pods"
},
expect
:
"NAME
\t
READY
\t
STATUS
\t
RESTARTS
\t
AGE
\n
pod
s
/test1
\t
1/2
\t
Running
\t
6
\t
<unknown>
\n
"
,
obj
:
runningPod
,
opts
:
printers
.
PrintOptions
{
WithKind
:
true
,
Kind
:
schema
.
GroupKind
{
Kind
:
"Pod"
}
},
expect
:
"NAME
\t
READY
\t
STATUS
\t
RESTARTS
\t
AGE
\n
pod/test1
\t
1/2
\t
Running
\t
6
\t
<unknown>
\n
"
,
},
{
obj
:
runningPod
,
opts
:
printers
.
PrintOptions
{
ShowLabels
:
true
},
...
...
@@ -3010,6 +2994,7 @@ func TestPrintControllerRevision(t *testing.T) {
OwnerReferences
:
[]
metav1
.
OwnerReference
{
{
Controller
:
boolP
(
true
),
APIVersion
:
"apps/v1"
,
Kind
:
"DaemonSet"
,
Name
:
"foo"
,
},
...
...
@@ -3017,7 +3002,7 @@ func TestPrintControllerRevision(t *testing.T) {
},
Revision
:
1
,
},
"test1
\t
DaemonSet
/foo
\t
1
\t
0s
\n
"
,
"test1
\t
daemonset.apps
/foo
\t
1
\t
0s
\n
"
,
},
{
apps
.
ControllerRevision
{
...
...
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