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
1453cd62
Commit
1453cd62
authored
Dec 05, 2016
by
tianshapjq
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reduce decoding times by changing kubectl.Filter(runtime.Object, *PrintOptions)'s return value
parent
3dcbafa8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
20 deletions
+35
-20
get.go
pkg/kubectl/cmd/get.go
+6
-3
get_test.go
pkg/kubectl/cmd/get_test.go
+13
-3
resource_filter.go
pkg/kubectl/resource_filter.go
+15
-6
resource_printer.go
pkg/kubectl/resource_printer.go
+1
-8
No files found.
pkg/kubectl/cmd/get.go
View file @
1453cd62
...
@@ -458,8 +458,11 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
...
@@ -458,8 +458,11 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
lastMapping
=
mapping
lastMapping
=
mapping
}
}
// try to convert before apply filter func
decodedObj
,
_
:=
kubectl
.
DecodeUnknownObject
(
original
)
// filter objects if filter has been defined for current object
// filter objects if filter has been defined for current object
if
isFiltered
,
err
:=
filterFuncs
.
Filter
(
original
,
filterOpts
);
isFiltered
{
if
isFiltered
,
err
:=
filterFuncs
.
Filter
(
decodedObj
,
filterOpts
);
isFiltered
{
if
err
==
nil
{
if
err
==
nil
{
filteredResourceCount
++
filteredResourceCount
++
continue
continue
...
@@ -489,7 +492,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
...
@@ -489,7 +492,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
resourcePrinter
.
EnsurePrintWithKind
(
resourceName
)
resourcePrinter
.
EnsurePrintWithKind
(
resourceName
)
}
}
if
err
:=
printer
.
PrintObj
(
original
,
w
);
err
!=
nil
{
if
err
:=
printer
.
PrintObj
(
decodedObj
,
w
);
err
!=
nil
{
if
!
errs
.
Has
(
err
.
Error
())
{
if
!
errs
.
Has
(
err
.
Error
())
{
errs
.
Insert
(
err
.
Error
())
errs
.
Insert
(
err
.
Error
())
allErrs
=
append
(
allErrs
,
err
)
allErrs
=
append
(
allErrs
,
err
)
...
@@ -497,7 +500,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
...
@@ -497,7 +500,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
}
}
continue
continue
}
}
if
err
:=
printer
.
PrintObj
(
original
,
w
);
err
!=
nil
{
if
err
:=
printer
.
PrintObj
(
decodedObj
,
w
);
err
!=
nil
{
if
!
errs
.
Has
(
err
.
Error
())
{
if
!
errs
.
Has
(
err
.
Error
())
{
errs
.
Insert
(
err
.
Error
())
errs
.
Insert
(
err
.
Error
())
allErrs
=
append
(
allErrs
,
err
)
allErrs
=
append
(
allErrs
,
err
)
...
...
pkg/kubectl/cmd/get_test.go
View file @
1453cd62
...
@@ -257,13 +257,23 @@ func TestGetSortedObjects(t *testing.T) {
...
@@ -257,13 +257,23 @@ func TestGetSortedObjects(t *testing.T) {
}
}
func
verifyObjects
(
t
*
testing
.
T
,
expected
,
actual
[]
runtime
.
Object
)
{
func
verifyObjects
(
t
*
testing
.
T
,
expected
,
actual
[]
runtime
.
Object
)
{
var
actualObj
runtime
.
Object
var
err
error
if
len
(
actual
)
!=
len
(
expected
)
{
if
len
(
actual
)
!=
len
(
expected
)
{
t
.
Fatal
(
actual
)
t
.
Fatal
(
actual
)
}
}
for
i
,
obj
:=
range
actual
{
for
i
,
obj
:=
range
actual
{
actualObj
,
err
:=
runtime
.
Decode
(
switch
obj
.
(
type
)
{
api
.
Codecs
.
UniversalDecoder
(),
case
runtime
.
Unstructured
,
*
runtime
.
Unknown
:
[]
byte
(
runtime
.
EncodeOrDie
(
api
.
Codecs
.
LegacyCodec
(),
obj
)))
actualObj
,
err
=
runtime
.
Decode
(
api
.
Codecs
.
UniversalDecoder
(),
[]
byte
(
runtime
.
EncodeOrDie
(
api
.
Codecs
.
LegacyCodec
(),
obj
)))
default
:
actualObj
=
obj
err
=
nil
}
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
...
...
pkg/kubectl/resource_filter.go
View file @
1453cd62
...
@@ -60,6 +60,20 @@ func filterPods(obj runtime.Object, options PrintOptions) bool {
...
@@ -60,6 +60,20 @@ func filterPods(obj runtime.Object, options PrintOptions) bool {
func
(
f
Filters
)
Filter
(
obj
runtime
.
Object
,
opts
*
PrintOptions
)
(
bool
,
error
)
{
func
(
f
Filters
)
Filter
(
obj
runtime
.
Object
,
opts
*
PrintOptions
)
(
bool
,
error
)
{
// check if the object is unstructured. If so, let's attempt to convert it to a type we can understand
// check if the object is unstructured. If so, let's attempt to convert it to a type we can understand
// before apply filter func.
// before apply filter func.
obj
,
_
=
DecodeUnknownObject
(
obj
)
for
_
,
filter
:=
range
f
{
if
ok
:=
filter
(
obj
,
*
opts
);
ok
{
return
true
,
nil
}
}
return
false
,
nil
}
// check if the object is unstructured. If so, let's attempt to convert it to a type we can understand.
func
DecodeUnknownObject
(
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
var
err
error
switch
obj
.
(
type
)
{
switch
obj
.
(
type
)
{
case
runtime
.
Unstructured
,
*
runtime
.
Unknown
:
case
runtime
.
Unstructured
,
*
runtime
.
Unknown
:
if
objBytes
,
err
:=
runtime
.
Encode
(
api
.
Codecs
.
LegacyCodec
(),
obj
);
err
==
nil
{
if
objBytes
,
err
:=
runtime
.
Encode
(
api
.
Codecs
.
LegacyCodec
(),
obj
);
err
==
nil
{
...
@@ -69,10 +83,5 @@ func (f Filters) Filter(obj runtime.Object, opts *PrintOptions) (bool, error) {
...
@@ -69,10 +83,5 @@ func (f Filters) Filter(obj runtime.Object, opts *PrintOptions) (bool, error) {
}
}
}
}
for
_
,
filter
:=
range
f
{
return
obj
,
err
if
ok
:=
filter
(
obj
,
*
opts
);
ok
{
return
true
,
nil
}
}
return
false
,
nil
}
}
pkg/kubectl/resource_printer.go
View file @
1453cd62
...
@@ -2394,14 +2394,7 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
...
@@ -2394,14 +2394,7 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
// check if the object is unstructured. If so, let's attempt to convert it to a type we can understand before
// 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.
// trying to print, since the printers are keyed by type. This is extremely expensive.
switch
obj
.
(
type
)
{
obj
,
_
=
DecodeUnknownObject
(
obj
)
case
runtime
.
Unstructured
,
*
runtime
.
Unknown
:
if
objBytes
,
err
:=
runtime
.
Encode
(
api
.
Codecs
.
LegacyCodec
(),
obj
);
err
==
nil
{
if
decodedObj
,
err
:=
runtime
.
Decode
(
api
.
Codecs
.
UniversalDecoder
(),
objBytes
);
err
==
nil
{
obj
=
decodedObj
}
}
}
t
:=
reflect
.
TypeOf
(
obj
)
t
:=
reflect
.
TypeOf
(
obj
)
if
handler
:=
h
.
handlerMap
[
t
];
handler
!=
nil
{
if
handler
:=
h
.
handlerMap
[
t
];
handler
!=
nil
{
...
...
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