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
d9dbfc78
Commit
d9dbfc78
authored
Oct 20, 2017
by
zhengjiajin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix issue(51245)kubectl printObj should not print header when occur error
parent
fe6258fb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
160 additions
and
3 deletions
+160
-3
BUILD
pkg/printers/BUILD
+13
-0
humanreadable.go
pkg/printers/humanreadable.go
+9
-3
humanreadable_test.go
pkg/printers/humanreadable_test.go
+138
-0
No files found.
pkg/printers/BUILD
View file @
d9dbfc78
...
@@ -66,3 +66,16 @@ filegroup(
...
@@ -66,3 +66,16 @@ filegroup(
],
],
tags = ["automanaged"],
tags = ["automanaged"],
)
)
go_test(
name = "go_default_test",
srcs = ["humanreadable_test.go"],
importpath = "k8s.io/kubernetes/pkg/printers",
library = ":go_default_library",
deps = [
"//pkg/api:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1alpha1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
],
)
pkg/printers/humanreadable.go
View file @
d9dbfc78
...
@@ -535,6 +535,15 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
...
@@ -535,6 +535,15 @@ func (h *HumanReadablePrinter) PrintTable(obj runtime.Object, options PrintOptio
// different from lastType) including all the rows in the object. It returns the current type
// different from lastType) including all the rows in the object. It returns the current type
// 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
if
handler
.
printRows
{
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
options
)}
results
=
handler
.
printFunc
.
Call
(
args
)
if
!
results
[
1
]
.
IsNil
()
{
return
results
[
1
]
.
Interface
()
.
(
error
)
}
}
if
includeHeaders
{
if
includeHeaders
{
var
headers
[]
string
var
headers
[]
string
for
_
,
column
:=
range
handler
.
columnDefinitions
{
for
_
,
column
:=
range
handler
.
columnDefinitions
{
...
@@ -562,15 +571,12 @@ func printRowsForHandlerEntry(output io.Writer, handler *handlerEntry, obj runti
...
@@ -562,15 +571,12 @@ func printRowsForHandlerEntry(output io.Writer, handler *handlerEntry, obj runti
return
resultValue
.
Interface
()
.
(
error
)
return
resultValue
.
Interface
()
.
(
error
)
}
}
args
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
obj
),
reflect
.
ValueOf
(
options
)}
results
:=
handler
.
printFunc
.
Call
(
args
)
if
results
[
1
]
.
IsNil
()
{
if
results
[
1
]
.
IsNil
()
{
rows
:=
results
[
0
]
.
Interface
()
.
([]
metav1alpha1
.
TableRow
)
rows
:=
results
[
0
]
.
Interface
()
.
([]
metav1alpha1
.
TableRow
)
printRows
(
output
,
rows
,
options
)
printRows
(
output
,
rows
,
options
)
return
nil
return
nil
}
}
return
results
[
1
]
.
Interface
()
.
(
error
)
return
results
[
1
]
.
Interface
()
.
(
error
)
}
}
// printRows writes the provided rows to output.
// printRows writes the provided rows to output.
...
...
pkg/printers/humanreadable_test.go
0 → 100644
View file @
d9dbfc78
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
printers
import
(
"bytes"
"fmt"
"reflect"
"testing"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1alpha1
"k8s.io/apimachinery/pkg/apis/meta/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/api"
)
var
testNamespaceColumnDefinitions
=
[]
metav1alpha1
.
TableColumnDefinition
{
{
Name
:
"Name"
,
Type
:
"string"
,
Format
:
"name"
,
Description
:
metav1
.
ObjectMeta
{}
.
SwaggerDoc
()[
"name"
]},
{
Name
:
"Status"
,
Type
:
"string"
,
Description
:
"The status of the namespace"
},
{
Name
:
"Age"
,
Type
:
"string"
,
Description
:
metav1
.
ObjectMeta
{}
.
SwaggerDoc
()[
"creationTimestamp"
]},
}
func
testPrintNamespace
(
obj
*
api
.
Namespace
,
options
PrintOptions
)
([]
metav1alpha1
.
TableRow
,
error
)
{
if
options
.
WithNamespace
{
return
nil
,
fmt
.
Errorf
(
"namespace is not namespaced"
)
}
row
:=
metav1alpha1
.
TableRow
{
Object
:
runtime
.
RawExtension
{
Object
:
obj
},
}
row
.
Cells
=
append
(
row
.
Cells
,
obj
.
Name
,
obj
.
Status
.
Phase
,
"<unknow>"
)
return
[]
metav1alpha1
.
TableRow
{
row
},
nil
}
func
testPrintNamespaceList
(
list
*
api
.
NamespaceList
,
options
PrintOptions
)
([]
metav1alpha1
.
TableRow
,
error
)
{
rows
:=
make
([]
metav1alpha1
.
TableRow
,
0
,
len
(
list
.
Items
))
for
i
:=
range
list
.
Items
{
r
,
err
:=
testPrintNamespace
(
&
list
.
Items
[
i
],
options
)
if
err
!=
nil
{
return
nil
,
err
}
rows
=
append
(
rows
,
r
...
)
}
return
rows
,
nil
}
func
TestPrintRowsForHandlerEntry
(
t
*
testing
.
T
)
{
printFunc
:=
reflect
.
ValueOf
(
testPrintNamespace
)
testCase
:=
map
[
string
]
struct
{
h
*
handlerEntry
opt
PrintOptions
obj
runtime
.
Object
includeHeader
bool
expectOut
string
expectErr
string
}{
"no tablecolumndefinition and includeheader flase"
:
{
h
:
&
handlerEntry
{
columnDefinitions
:
[]
metav1alpha1
.
TableColumnDefinition
{},
printRows
:
true
,
printFunc
:
printFunc
,
},
opt
:
PrintOptions
{},
obj
:
&
api
.
Namespace
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"test"
},
},
includeHeader
:
false
,
expectOut
:
"test
\t\t
<unknow>
\n
"
,
},
"no tablecolumndefinition and includeheader true"
:
{
h
:
&
handlerEntry
{
columnDefinitions
:
[]
metav1alpha1
.
TableColumnDefinition
{},
printRows
:
true
,
printFunc
:
printFunc
,
},
opt
:
PrintOptions
{},
obj
:
&
api
.
Namespace
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"test"
},
},
includeHeader
:
true
,
expectOut
:
"
\n
test
\t\t
<unknow>
\n
"
,
},
"have tablecolumndefinition and includeheader true"
:
{
h
:
&
handlerEntry
{
columnDefinitions
:
testNamespaceColumnDefinitions
,
printRows
:
true
,
printFunc
:
printFunc
,
},
opt
:
PrintOptions
{},
obj
:
&
api
.
Namespace
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"test"
},
},
includeHeader
:
true
,
expectOut
:
"NAME
\t
STATUS
\t
AGE
\n
test
\t\t
<unknow>
\n
"
,
},
"print namespace and withnamespace true, should not print header"
:
{
h
:
&
handlerEntry
{
columnDefinitions
:
testNamespaceColumnDefinitions
,
printRows
:
true
,
printFunc
:
printFunc
,
},
opt
:
PrintOptions
{
WithNamespace
:
true
,
},
obj
:
&
api
.
Namespace
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"test"
},
},
includeHeader
:
true
,
expectOut
:
""
,
expectErr
:
"namespace is not namespaced"
,
},
}
for
name
,
test
:=
range
testCase
{
buffer
:=
&
bytes
.
Buffer
{}
err
:=
printRowsForHandlerEntry
(
buffer
,
test
.
h
,
test
.
obj
,
test
.
opt
,
test
.
includeHeader
)
if
err
!=
nil
{
if
err
.
Error
()
!=
test
.
expectErr
{
t
.
Errorf
(
"[%s]expect:
\n
%v
\n
but got:
\n
%v
\n
"
,
name
,
test
.
expectErr
,
err
)
}
}
if
test
.
expectOut
!=
buffer
.
String
()
{
t
.
Errorf
(
"[%s]expect:
\n
%v
\n
but got:
\n
%v
\n
"
,
name
,
test
.
expectOut
,
buffer
.
String
())
}
}
}
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