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
29e9ff36
Unverified
Commit
29e9ff36
authored
Jan 31, 2019
by
Kubernetes Prow Robot
Committed by
GitHub
Jan 31, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #73063 from WanLinghao/kubectl_get_custom_clomuns_fix
Improve custom-columns option of `kubectl get ` command
parents
94b5aeba
97ec74df
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
196 additions
and
1 deletion
+196
-1
customcolumn.go
pkg/kubectl/cmd/get/customcolumn.go
+1
-1
customcolumn_test.go
pkg/kubectl/cmd/get/customcolumn_test.go
+195
-0
No files found.
pkg/kubectl/cmd/get/customcolumn.go
View file @
29e9ff36
...
@@ -75,7 +75,7 @@ func NewCustomColumnsPrinterFromSpec(spec string, decoder runtime.Decoder, noHea
...
@@ -75,7 +75,7 @@ func NewCustomColumnsPrinterFromSpec(spec string, decoder runtime.Decoder, noHea
parts
:=
strings
.
Split
(
spec
,
","
)
parts
:=
strings
.
Split
(
spec
,
","
)
columns
:=
make
([]
Column
,
len
(
parts
))
columns
:=
make
([]
Column
,
len
(
parts
))
for
ix
:=
range
parts
{
for
ix
:=
range
parts
{
colSpec
:=
strings
.
Split
(
parts
[
ix
],
":"
)
colSpec
:=
strings
.
Split
N
(
parts
[
ix
],
":"
,
2
)
if
len
(
colSpec
)
!=
2
{
if
len
(
colSpec
)
!=
2
{
return
nil
,
fmt
.
Errorf
(
"unexpected custom-columns spec: %s, expected <header>:<json-path-expr>"
,
parts
[
ix
])
return
nil
,
fmt
.
Errorf
(
"unexpected custom-columns spec: %s, expected <header>:<json-path-expr>"
,
parts
[
ix
])
}
}
...
...
pkg/kubectl/cmd/get/customcolumn_test.go
View file @
29e9ff36
...
@@ -373,3 +373,198 @@ bar bar bar
...
@@ -373,3 +373,198 @@ bar bar bar
t
.
Errorf
(
"
\n
expected:
\n
'%s'
\n
saw
\n
'%s'
\n
"
,
expectedOutput
,
buffer
.
String
())
t
.
Errorf
(
"
\n
expected:
\n
'%s'
\n
saw
\n
'%s'
\n
"
,
expectedOutput
,
buffer
.
String
())
}
}
}
}
func
TestSliceColumnPrint
(
t
*
testing
.
T
)
{
pod
:=
&
corev1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"fake-name"
,
Namespace
:
"fake-namespace"
,
},
Spec
:
corev1
.
PodSpec
{
Containers
:
[]
corev1
.
Container
{
{
Name
:
"fake0"
,
},
{
Name
:
"fake1"
,
},
{
Name
:
"fake2"
,
},
{
Name
:
"fake3"
,
},
},
},
}
tests
:=
[]
struct
{
name
string
spec
string
expectedOutput
string
expectErr
bool
}{
{
name
:
"containers[0]"
,
spec
:
"CONTAINER:.spec.containers[0].name"
,
expectedOutput
:
`CONTAINER
fake0
`
,
expectErr
:
false
,
},
{
name
:
"containers[3]"
,
spec
:
"CONTAINER:.spec.containers[3].name"
,
expectedOutput
:
`CONTAINER
fake3
`
,
expectErr
:
false
,
},
{
name
:
"containers[5], illegal expression because it is out of bounds"
,
spec
:
"CONTAINER:.spec.containers[5].name"
,
expectedOutput
:
""
,
expectErr
:
true
,
},
{
name
:
"containers[-1], it equals containers[3]"
,
spec
:
"CONTAINER:.spec.containers[-1].name"
,
expectedOutput
:
`CONTAINER
fake3
`
,
expectErr
:
false
,
},
{
name
:
"containers[-2], it equals containers[2]"
,
spec
:
"CONTAINER:.spec.containers[-2].name"
,
expectedOutput
:
`CONTAINER
fake2
`
,
expectErr
:
false
,
},
{
name
:
"containers[-4], it equals containers[0]"
,
spec
:
"CONTAINER:.spec.containers[-4].name"
,
expectedOutput
:
`CONTAINER
fake0
`
,
expectErr
:
false
,
},
{
name
:
"containers[-5], illegal expression because it is out of bounds"
,
spec
:
"CONTAINER:.spec.containers[-5].name"
,
expectedOutput
:
""
,
expectErr
:
true
,
},
{
name
:
"containers[0:0], it equals empty set"
,
spec
:
"CONTAINER:.spec.containers[0:0].name"
,
expectedOutput
:
`CONTAINER
<none>
`
,
expectErr
:
false
,
},
{
name
:
"containers[0:3]"
,
spec
:
"CONTAINER:.spec.containers[0:3].name"
,
expectedOutput
:
`CONTAINER
fake0,fake1,fake2
`
,
expectErr
:
false
,
},
{
name
:
"containers[1:]"
,
spec
:
"CONTAINER:.spec.containers[1:].name"
,
expectedOutput
:
`CONTAINER
fake1,fake2,fake3
`
,
expectErr
:
false
,
},
{
name
:
"containers[3:1], illegal expression because start index is greater than end index"
,
spec
:
"CONTAINER:.spec.containers[3:1].name"
,
expectedOutput
:
""
,
expectErr
:
true
,
},
{
name
:
"containers[0:-1], it equals containers[0:3]"
,
spec
:
"CONTAINER:.spec.containers[0:-1].name"
,
expectedOutput
:
`CONTAINER
fake0,fake1,fake2
`
,
expectErr
:
false
,
},
{
name
:
"containers[-1:], it equals containers[3:]"
,
spec
:
"CONTAINER:.spec.containers[-1:].name"
,
expectedOutput
:
`CONTAINER
fake3
`
,
expectErr
:
false
,
},
{
name
:
"containers[-4:], it equals containers[0:]"
,
spec
:
"CONTAINER:.spec.containers[-4:].name"
,
expectedOutput
:
`CONTAINER
fake0,fake1,fake2,fake3
`
,
expectErr
:
false
,
},
{
name
:
"containers[-3:-1], it equasl containers[1:3]"
,
spec
:
"CONTAINER:.spec.containers[-3:-1].name"
,
expectedOutput
:
`CONTAINER
fake1,fake2
`
,
expectErr
:
false
,
},
{
name
:
"containers[-2:-3], it equals containers[2:1], illegal expression because start index is greater than end index"
,
spec
:
"CONTAINER:.spec.containers[-2:-3].name"
,
expectedOutput
:
""
,
expectErr
:
true
,
},
{
name
:
"containers[4:4], it equals empty set"
,
spec
:
"CONTAINER:.spec.containers[4:4].name"
,
expectedOutput
:
`CONTAINER
<none>
`
,
expectErr
:
false
,
},
{
name
:
"containers[-5:-5], it equals empty set"
,
spec
:
"CONTAINER:.spec.containers[-5:-5].name"
,
expectedOutput
:
`CONTAINER
<none>
`
,
expectErr
:
false
,
},
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
printer
,
err
:=
NewCustomColumnsPrinterFromSpec
(
test
.
spec
,
decoder
,
false
)
if
err
!=
nil
{
t
.
Errorf
(
"test %s has unexpected error: %v"
,
test
.
name
,
err
)
}
buffer
:=
&
bytes
.
Buffer
{}
err
=
printer
.
PrintObj
(
pod
,
buffer
)
if
test
.
expectErr
{
if
err
==
nil
{
t
.
Errorf
(
"test %s has unexpected error: %v"
,
test
.
name
,
err
)
}
}
else
{
if
err
!=
nil
{
t
.
Errorf
(
"test %s has unexpected error: %v"
,
test
.
name
,
err
)
}
else
if
buffer
.
String
()
!=
test
.
expectedOutput
{
t
.
Errorf
(
"test %s has unexpected output:
\n
expected: %s
\n
saw: %s"
,
test
.
name
,
test
.
expectedOutput
,
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