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
00c75410
Commit
00c75410
authored
Dec 18, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #18165 from fabianofranz/fixes_get_show_all
Auto commit by PR queue bot
parents
9e8233fc
717896ea
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
126 additions
and
21 deletions
+126
-21
get.go
pkg/kubectl/cmd/get.go
+9
-0
get_test.go
pkg/kubectl/cmd/get_test.go
+23
-0
builder.go
pkg/kubectl/resource/builder.go
+32
-17
builder_test.go
pkg/kubectl/resource/builder_test.go
+58
-0
resource_printer.go
pkg/kubectl/resource_printer.go
+4
-4
No files found.
pkg/kubectl/cmd/get.go
View file @
00c75410
...
@@ -123,6 +123,15 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
...
@@ -123,6 +123,15 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
return
cmdutil
.
UsageError
(
cmd
,
"Required resource not specified."
)
return
cmdutil
.
UsageError
(
cmd
,
"Required resource not specified."
)
}
}
// always show resources when getting by name or filename
argsHasNames
,
err
:=
resource
.
HasNames
(
args
)
if
err
!=
nil
{
return
err
}
if
len
(
options
.
Filenames
)
>
0
||
argsHasNames
{
cmd
.
Flag
(
"show-all"
)
.
Value
.
Set
(
"true"
)
}
// handle watch separately since we cannot watch multiple resource types
// handle watch separately since we cannot watch multiple resource types
isWatch
,
isWatchOnly
:=
cmdutil
.
GetFlagBool
(
cmd
,
"watch"
),
cmdutil
.
GetFlagBool
(
cmd
,
"watch-only"
)
isWatch
,
isWatchOnly
:=
cmdutil
.
GetFlagBool
(
cmd
,
"watch"
),
cmdutil
.
GetFlagBool
(
cmd
,
"watch-only"
)
if
isWatch
||
isWatchOnly
{
if
isWatch
||
isWatchOnly
{
...
...
pkg/kubectl/cmd/get_test.go
View file @
00c75410
...
@@ -589,6 +589,29 @@ func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) {
...
@@ -589,6 +589,29 @@ func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) {
t
.
Errorf
(
"unexpected empty output"
)
t
.
Errorf
(
"unexpected empty output"
)
}
}
}
}
func
TestGetByNameForcesFlag
(
t
*
testing
.
T
)
{
pods
,
_
,
_
:=
testData
()
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
fake
.
RESTClient
{
Codec
:
codec
,
Resp
:
&
http
.
Response
{
StatusCode
:
200
,
Body
:
objBody
(
codec
,
&
pods
.
Items
[
0
])},
}
tf
.
Namespace
=
"test"
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdGet
(
f
,
buf
)
cmd
.
SetOutput
(
buf
)
cmd
.
Run
(
cmd
,
[]
string
{
"pods"
,
"foo"
})
showAllFlag
,
_
:=
cmd
.
Flags
()
.
GetBool
(
"show-all"
)
if
!
showAllFlag
{
t
.
Errorf
(
"expected showAll to be true when getting resource by name"
)
}
}
func
watchTestData
()
([]
api
.
Pod
,
[]
watch
.
Event
)
{
func
watchTestData
()
([]
api
.
Pod
,
[]
watch
.
Event
)
{
pods
:=
[]
api
.
Pod
{
pods
:=
[]
api
.
Pod
{
{
{
...
...
pkg/kubectl/resource/builder.go
View file @
00c75410
...
@@ -281,23 +281,7 @@ func (b *Builder) SelectAllParam(selectAll bool) *Builder {
...
@@ -281,23 +281,7 @@ func (b *Builder) SelectAllParam(selectAll bool) *Builder {
// When two or more arguments are received, they must be a single type and resource name(s).
// When two or more arguments are received, they must be a single type and resource name(s).
// The allowEmptySelector permits to select all the resources (via Everything func).
// The allowEmptySelector permits to select all the resources (via Everything func).
func
(
b
*
Builder
)
ResourceTypeOrNameArgs
(
allowEmptySelector
bool
,
args
...
string
)
*
Builder
{
func
(
b
*
Builder
)
ResourceTypeOrNameArgs
(
allowEmptySelector
bool
,
args
...
string
)
*
Builder
{
// convert multiple resources to resource tuples, a,b,c d as a transform to a/d b/d c/d
args
=
normalizeMultipleResourcesArgs
(
args
)
if
len
(
args
)
>=
2
{
resources
:=
[]
string
{}
resources
=
append
(
resources
,
SplitResourceArgument
(
args
[
0
])
...
)
if
len
(
resources
)
>
1
{
names
:=
[]
string
{}
names
=
append
(
names
,
args
[
1
:
]
...
)
newArgs
:=
[]
string
{}
for
_
,
resource
:=
range
resources
{
for
_
,
name
:=
range
names
{
newArgs
=
append
(
newArgs
,
strings
.
Join
([]
string
{
resource
,
name
},
"/"
))
}
}
args
=
newArgs
}
}
if
ok
,
err
:=
hasCombinedTypeArgs
(
args
);
ok
{
if
ok
,
err
:=
hasCombinedTypeArgs
(
args
);
ok
{
if
err
!=
nil
{
if
err
!=
nil
{
b
.
errs
=
append
(
b
.
errs
,
err
)
b
.
errs
=
append
(
b
.
errs
,
err
)
...
@@ -368,6 +352,27 @@ func hasCombinedTypeArgs(args []string) (bool, error) {
...
@@ -368,6 +352,27 @@ func hasCombinedTypeArgs(args []string) (bool, error) {
}
}
}
}
// Normalize args convert multiple resources to resource tuples, a,b,c d
// as a transform to a/d b/d c/d
func
normalizeMultipleResourcesArgs
(
args
[]
string
)
[]
string
{
if
len
(
args
)
>=
2
{
resources
:=
[]
string
{}
resources
=
append
(
resources
,
SplitResourceArgument
(
args
[
0
])
...
)
if
len
(
resources
)
>
1
{
names
:=
[]
string
{}
names
=
append
(
names
,
args
[
1
:
]
...
)
newArgs
:=
[]
string
{}
for
_
,
resource
:=
range
resources
{
for
_
,
name
:=
range
names
{
newArgs
=
append
(
newArgs
,
strings
.
Join
([]
string
{
resource
,
name
},
"/"
))
}
}
return
newArgs
}
}
return
args
}
// splitResourceTypeName handles type/name resource formats and returns a resource tuple
// splitResourceTypeName handles type/name resource formats and returns a resource tuple
// (empty or not), whether it successfully found one, and an error
// (empty or not), whether it successfully found one, and an error
func
splitResourceTypeName
(
s
string
)
(
resourceTuple
,
bool
,
error
)
{
func
splitResourceTypeName
(
s
string
)
(
resourceTuple
,
bool
,
error
)
{
...
@@ -696,3 +701,13 @@ func SplitResourceArgument(arg string) []string {
...
@@ -696,3 +701,13 @@ func SplitResourceArgument(arg string) []string {
}
}
return
out
return
out
}
}
// HasNames returns true if the provided args contain resource names
func
HasNames
(
args
[]
string
)
(
bool
,
error
)
{
args
=
normalizeMultipleResourcesArgs
(
args
)
hasCombinedTypes
,
err
:=
hasCombinedTypeArgs
(
args
)
if
err
!=
nil
{
return
false
,
err
}
return
hasCombinedTypes
||
len
(
args
)
>
1
,
nil
}
pkg/kubectl/resource/builder_test.go
View file @
00c75410
...
@@ -1003,3 +1003,61 @@ func TestReplaceAliases(t *testing.T) {
...
@@ -1003,3 +1003,61 @@ func TestReplaceAliases(t *testing.T) {
}
}
}
}
}
}
func
TestHasNames
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
args
[]
string
expectedHasName
bool
expectedError
error
}{
{
args
:
[]
string
{
""
},
expectedHasName
:
false
,
expectedError
:
nil
,
},
{
args
:
[]
string
{
"rc"
},
expectedHasName
:
false
,
expectedError
:
nil
,
},
{
args
:
[]
string
{
"rc,pod,svc"
},
expectedHasName
:
false
,
expectedError
:
nil
,
},
{
args
:
[]
string
{
"rc/foo"
},
expectedHasName
:
true
,
expectedError
:
nil
,
},
{
args
:
[]
string
{
"rc"
,
"foo"
},
expectedHasName
:
true
,
expectedError
:
nil
,
},
{
args
:
[]
string
{
"rc,pod,svc"
,
"foo"
},
expectedHasName
:
true
,
expectedError
:
nil
,
},
{
args
:
[]
string
{
"rc/foo"
,
"rc/bar"
,
"rc/zee"
},
expectedHasName
:
true
,
expectedError
:
nil
,
},
{
args
:
[]
string
{
"rc/foo"
,
"bar"
},
expectedHasName
:
false
,
expectedError
:
fmt
.
Errorf
(
"when passing arguments in resource/name form, all arguments must include the resource"
),
},
}
for
_
,
test
:=
range
tests
{
hasNames
,
err
:=
HasNames
(
test
.
args
)
if
!
reflect
.
DeepEqual
(
test
.
expectedError
,
err
)
{
t
.
Errorf
(
"expected HasName to error %v, got %s"
,
test
.
expectedError
,
err
)
}
if
hasNames
!=
test
.
expectedHasName
{
t
.
Errorf
(
"expected HasName to return %v for %s"
,
test
.
expectedHasName
,
test
.
args
)
}
}
}
pkg/kubectl/resource_printer.go
View file @
00c75410
...
@@ -537,10 +537,10 @@ func translateTimestamp(timestamp unversioned.Time) string {
...
@@ -537,10 +537,10 @@ func translateTimestamp(timestamp unversioned.Time) string {
}
}
func
printPod
(
pod
*
api
.
Pod
,
w
io
.
Writer
,
options
printOptions
)
error
{
func
printPod
(
pod
*
api
.
Pod
,
w
io
.
Writer
,
options
printOptions
)
error
{
return
printPodBase
(
pod
,
w
,
true
,
options
)
return
printPodBase
(
pod
,
w
,
options
)
}
}
func
printPodBase
(
pod
*
api
.
Pod
,
w
io
.
Writer
,
showIfTerminating
bool
,
options
printOptions
)
error
{
func
printPodBase
(
pod
*
api
.
Pod
,
w
io
.
Writer
,
options
printOptions
)
error
{
name
:=
pod
.
Name
name
:=
pod
.
Name
namespace
:=
pod
.
Namespace
namespace
:=
pod
.
Namespace
...
@@ -550,7 +550,7 @@ func printPodBase(pod *api.Pod, w io.Writer, showIfTerminating bool, options pri
...
@@ -550,7 +550,7 @@ func printPodBase(pod *api.Pod, w io.Writer, showIfTerminating bool, options pri
reason
:=
string
(
pod
.
Status
.
Phase
)
reason
:=
string
(
pod
.
Status
.
Phase
)
// if not printing all pods, skip terminated pods (default)
// if not printing all pods, skip terminated pods (default)
if
!
showIfTerminating
&&
!
options
.
showAll
&&
(
reason
==
string
(
api
.
PodSucceeded
)
||
reason
==
string
(
api
.
PodFailed
))
{
if
!
options
.
showAll
&&
(
reason
==
string
(
api
.
PodSucceeded
)
||
reason
==
string
(
api
.
PodFailed
))
{
return
nil
return
nil
}
}
if
pod
.
Status
.
Reason
!=
""
{
if
pod
.
Status
.
Reason
!=
""
{
...
@@ -610,7 +610,7 @@ func printPodBase(pod *api.Pod, w io.Writer, showIfTerminating bool, options pri
...
@@ -610,7 +610,7 @@ func printPodBase(pod *api.Pod, w io.Writer, showIfTerminating bool, options pri
func
printPodList
(
podList
*
api
.
PodList
,
w
io
.
Writer
,
options
printOptions
)
error
{
func
printPodList
(
podList
*
api
.
PodList
,
w
io
.
Writer
,
options
printOptions
)
error
{
for
_
,
pod
:=
range
podList
.
Items
{
for
_
,
pod
:=
range
podList
.
Items
{
if
err
:=
printPodBase
(
&
pod
,
w
,
false
,
options
);
err
!=
nil
{
if
err
:=
printPodBase
(
&
pod
,
w
,
options
);
err
!=
nil
{
return
err
return
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