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
b5e99be8
Commit
b5e99be8
authored
Feb 09, 2016
by
mqliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement kubectl get/describe for ReplicaSet
parent
3281e752
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
134 additions
and
38 deletions
+134
-38
kubectl
contrib/completions/bash/kubectl
+4
-0
helpers.go
pkg/api/unversioned/helpers.go
+14
-0
describe.go
pkg/kubectl/describe.go
+56
-38
resource_printer.go
pkg/kubectl/resource_printer.go
+60
-0
No files found.
contrib/completions/bash/kubectl
View file @
b5e99be8
...
@@ -333,6 +333,7 @@ _kubectl_get()
...
@@ -333,6 +333,7 @@ _kubectl_get()
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"podsecuritypolicy"
)
must_have_one_noun+
=(
"podsecuritypolicy"
)
must_have_one_noun+
=(
"podtemplate"
)
must_have_one_noun+
=(
"podtemplate"
)
must_have_one_noun+
=(
"replicaset"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"secret"
)
must_have_one_noun+
=(
"secret"
)
...
@@ -399,6 +400,7 @@ _kubectl_describe()
...
@@ -399,6 +400,7 @@ _kubectl_describe()
must_have_one_noun+
=(
"persistentvolume"
)
must_have_one_noun+
=(
"persistentvolume"
)
must_have_one_noun+
=(
"persistentvolumeclaim"
)
must_have_one_noun+
=(
"persistentvolumeclaim"
)
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"replicaset"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"secret"
)
must_have_one_noun+
=(
"secret"
)
...
@@ -831,6 +833,7 @@ _kubectl_delete()
...
@@ -831,6 +833,7 @@ _kubectl_delete()
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"podsecuritypolicy"
)
must_have_one_noun+
=(
"podsecuritypolicy"
)
must_have_one_noun+
=(
"podtemplate"
)
must_have_one_noun+
=(
"podtemplate"
)
must_have_one_noun+
=(
"replicaset"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"secret"
)
must_have_one_noun+
=(
"secret"
)
...
@@ -1975,6 +1978,7 @@ _kubectl_label()
...
@@ -1975,6 +1978,7 @@ _kubectl_label()
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"pod"
)
must_have_one_noun+
=(
"podsecuritypolicy"
)
must_have_one_noun+
=(
"podsecuritypolicy"
)
must_have_one_noun+
=(
"podtemplate"
)
must_have_one_noun+
=(
"podtemplate"
)
must_have_one_noun+
=(
"replicaset"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"replicationcontroller"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"resourcequota"
)
must_have_one_noun+
=(
"secret"
)
must_have_one_noun+
=(
"secret"
)
...
...
pkg/api/unversioned/helpers.go
View file @
b5e99be8
...
@@ -78,3 +78,17 @@ func SetAsLabelSelector(ls labels.Set) *LabelSelector {
...
@@ -78,3 +78,17 @@ func SetAsLabelSelector(ls labels.Set) *LabelSelector {
return
selector
return
selector
}
}
// FormatLabelSelector convert labelSelector into plain string
func
FormatLabelSelector
(
labelSelector
*
LabelSelector
)
string
{
var
l
string
if
selector
,
err
:=
LabelSelectorAsSelector
(
labelSelector
);
err
!=
nil
{
l
=
selector
.
String
()
}
else
{
l
=
"<error>"
}
if
l
==
""
{
l
=
"<none>"
}
return
l
}
pkg/kubectl/describe.go
View file @
b5e99be8
...
@@ -87,6 +87,7 @@ func describerMap(c *client.Client) map[unversioned.GroupKind]Describer {
...
@@ -87,6 +87,7 @@ func describerMap(c *client.Client) map[unversioned.GroupKind]Describer {
api
.
Kind
(
"Endpoints"
)
:
&
EndpointsDescriber
{
c
},
api
.
Kind
(
"Endpoints"
)
:
&
EndpointsDescriber
{
c
},
api
.
Kind
(
"ConfigMap"
)
:
&
ConfigMapDescriber
{
c
},
api
.
Kind
(
"ConfigMap"
)
:
&
ConfigMapDescriber
{
c
},
extensions
.
Kind
(
"ReplicaSet"
)
:
&
ReplicaSetDescriber
{
c
},
extensions
.
Kind
(
"HorizontalPodAutoscaler"
)
:
&
HorizontalPodAutoscalerDescriber
{
c
},
extensions
.
Kind
(
"HorizontalPodAutoscaler"
)
:
&
HorizontalPodAutoscalerDescriber
{
c
},
extensions
.
Kind
(
"DaemonSet"
)
:
&
DaemonSetDescriber
{
c
},
extensions
.
Kind
(
"DaemonSet"
)
:
&
DaemonSetDescriber
{
c
},
extensions
.
Kind
(
"Deployment"
)
:
&
DeploymentDescriber
{
clientset
.
FromUnversionedClient
(
c
)},
extensions
.
Kind
(
"Deployment"
)
:
&
DeploymentDescriber
{
clientset
.
FromUnversionedClient
(
c
)},
...
@@ -421,10 +422,7 @@ type PodDescriber struct {
...
@@ -421,10 +422,7 @@ type PodDescriber struct {
}
}
func
(
d
*
PodDescriber
)
Describe
(
namespace
,
name
string
)
(
string
,
error
)
{
func
(
d
*
PodDescriber
)
Describe
(
namespace
,
name
string
)
(
string
,
error
)
{
rc
:=
d
.
ReplicationControllers
(
namespace
)
pod
,
err
:=
d
.
Pods
(
namespace
)
.
Get
(
name
)
pc
:=
d
.
Pods
(
namespace
)
pod
,
err
:=
pc
.
Get
(
name
)
if
err
!=
nil
{
if
err
!=
nil
{
eventsInterface
:=
d
.
Events
(
namespace
)
eventsInterface
:=
d
.
Events
(
namespace
)
selector
:=
eventsInterface
.
GetFieldSelector
(
&
name
,
&
namespace
,
nil
,
nil
)
selector
:=
eventsInterface
.
GetFieldSelector
(
&
name
,
&
namespace
,
nil
,
nil
)
...
@@ -448,15 +446,10 @@ func (d *PodDescriber) Describe(namespace, name string) (string, error) {
...
@@ -448,15 +446,10 @@ func (d *PodDescriber) Describe(namespace, name string) (string, error) {
events
,
_
=
d
.
Events
(
namespace
)
.
Search
(
ref
)
events
,
_
=
d
.
Events
(
namespace
)
.
Search
(
ref
)
}
}
rcs
,
err
:=
getReplicationControllersForLabels
(
rc
,
labels
.
Set
(
pod
.
Labels
))
return
describePod
(
pod
,
events
)
if
err
!=
nil
{
return
""
,
err
}
return
describePod
(
pod
,
rcs
,
events
)
}
}
func
describePod
(
pod
*
api
.
Pod
,
rcs
[]
api
.
ReplicationController
,
events
*
api
.
EventList
)
(
string
,
error
)
{
func
describePod
(
pod
*
api
.
Pod
,
events
*
api
.
EventList
)
(
string
,
error
)
{
return
tabbedString
(
func
(
out
io
.
Writer
)
error
{
return
tabbedString
(
func
(
out
io
.
Writer
)
error
{
fmt
.
Fprintf
(
out
,
"Name:
\t
%s
\n
"
,
pod
.
Name
)
fmt
.
Fprintf
(
out
,
"Name:
\t
%s
\n
"
,
pod
.
Name
)
fmt
.
Fprintf
(
out
,
"Namespace:
\t
%s
\n
"
,
pod
.
Namespace
)
fmt
.
Fprintf
(
out
,
"Namespace:
\t
%s
\n
"
,
pod
.
Namespace
)
...
@@ -475,10 +468,6 @@ func describePod(pod *api.Pod, rcs []api.ReplicationController, events *api.Even
...
@@ -475,10 +468,6 @@ func describePod(pod *api.Pod, rcs []api.ReplicationController, events *api.Even
fmt
.
Fprintf
(
out
,
"Reason:
\t
%s
\n
"
,
pod
.
Status
.
Reason
)
fmt
.
Fprintf
(
out
,
"Reason:
\t
%s
\n
"
,
pod
.
Status
.
Reason
)
fmt
.
Fprintf
(
out
,
"Message:
\t
%s
\n
"
,
pod
.
Status
.
Message
)
fmt
.
Fprintf
(
out
,
"Message:
\t
%s
\n
"
,
pod
.
Status
.
Message
)
fmt
.
Fprintf
(
out
,
"IP:
\t
%s
\n
"
,
pod
.
Status
.
PodIP
)
fmt
.
Fprintf
(
out
,
"IP:
\t
%s
\n
"
,
pod
.
Status
.
PodIP
)
var
matchingRCs
[]
*
api
.
ReplicationController
for
_
,
rc
:=
range
rcs
{
matchingRCs
=
append
(
matchingRCs
,
&
rc
)
}
fmt
.
Fprintf
(
out
,
"Controllers:
\t
%s
\n
"
,
printControllers
(
pod
.
Annotations
))
fmt
.
Fprintf
(
out
,
"Controllers:
\t
%s
\n
"
,
printControllers
(
pod
.
Annotations
))
fmt
.
Fprintf
(
out
,
"Containers:
\n
"
)
fmt
.
Fprintf
(
out
,
"Containers:
\n
"
)
describeContainers
(
pod
,
out
)
describeContainers
(
pod
,
out
)
...
@@ -899,6 +888,58 @@ func DescribePodTemplate(template *api.PodTemplateSpec) (string, error) {
...
@@ -899,6 +888,58 @@ func DescribePodTemplate(template *api.PodTemplateSpec) (string, error) {
})
})
}
}
// ReplicaSetDescriber generates information about a ReplicaSet and the pods it has created.
type
ReplicaSetDescriber
struct
{
client
.
Interface
}
func
(
d
*
ReplicaSetDescriber
)
Describe
(
namespace
,
name
string
)
(
string
,
error
)
{
rsc
:=
d
.
Extensions
()
.
ReplicaSets
(
namespace
)
pc
:=
d
.
Pods
(
namespace
)
rs
,
err
:=
rsc
.
Get
(
name
)
if
err
!=
nil
{
return
""
,
err
}
selector
,
err
:=
unversioned
.
LabelSelectorAsSelector
(
rs
.
Spec
.
Selector
)
if
err
!=
nil
{
return
""
,
err
}
running
,
waiting
,
succeeded
,
failed
,
err
:=
getPodStatusForController
(
pc
,
selector
)
if
err
!=
nil
{
return
""
,
err
}
events
,
_
:=
d
.
Events
(
namespace
)
.
Search
(
rs
)
return
describeReplicaSet
(
rs
,
events
,
running
,
waiting
,
succeeded
,
failed
)
}
func
describeReplicaSet
(
rs
*
extensions
.
ReplicaSet
,
events
*
api
.
EventList
,
running
,
waiting
,
succeeded
,
failed
int
)
(
string
,
error
)
{
return
tabbedString
(
func
(
out
io
.
Writer
)
error
{
fmt
.
Fprintf
(
out
,
"Name:
\t
%s
\n
"
,
rs
.
Name
)
fmt
.
Fprintf
(
out
,
"Namespace:
\t
%s
\n
"
,
rs
.
Namespace
)
if
rs
.
Spec
.
Template
!=
nil
{
fmt
.
Fprintf
(
out
,
"Image(s):
\t
%s
\n
"
,
makeImageList
(
&
rs
.
Spec
.
Template
.
Spec
))
}
else
{
fmt
.
Fprintf
(
out
,
"Image(s):
\t
%s
\n
"
,
"<no template>"
)
}
fmt
.
Fprintf
(
out
,
"Selector:
\t
%s
\n
"
,
unversioned
.
FormatLabelSelector
(
rs
.
Spec
.
Selector
))
fmt
.
Fprintf
(
out
,
"Labels:
\t
%s
\n
"
,
labels
.
FormatLabels
(
rs
.
Labels
))
fmt
.
Fprintf
(
out
,
"Replicas:
\t
%d current / %d desired
\n
"
,
rs
.
Status
.
Replicas
,
rs
.
Spec
.
Replicas
)
fmt
.
Fprintf
(
out
,
"Pods Status:
\t
%d Running / %d Waiting / %d Succeeded / %d Failed
\n
"
,
running
,
waiting
,
succeeded
,
failed
)
if
rs
.
Spec
.
Template
!=
nil
{
describeVolumes
(
rs
.
Spec
.
Template
.
Spec
.
Volumes
,
out
)
}
if
events
!=
nil
{
DescribeEvents
(
events
,
out
)
}
return
nil
})
}
// JobDescriber generates information about a job and the pods it has created.
// JobDescriber generates information about a job and the pods it has created.
type
JobDescriber
struct
{
type
JobDescriber
struct
{
client
*
client
.
Client
client
*
client
.
Client
...
@@ -1668,29 +1709,6 @@ func getDaemonSetsForLabels(c client.DaemonSetInterface, labelsToMatch labels.La
...
@@ -1668,29 +1709,6 @@ func getDaemonSetsForLabels(c client.DaemonSetInterface, labelsToMatch labels.La
return
matchingDaemonSets
,
nil
return
matchingDaemonSets
,
nil
}
}
// Get all replication controllers whose selectors would match a given set of
// labels.
// TODO Move this to pkg/client and ideally implement it server-side (instead
// of getting all RC's and searching through them manually).
func
getReplicationControllersForLabels
(
c
client
.
ReplicationControllerInterface
,
labelsToMatch
labels
.
Labels
)
([]
api
.
ReplicationController
,
error
)
{
// Get all replication controllers.
// TODO this needs a namespace scope as argument
rcs
,
err
:=
c
.
List
(
api
.
ListOptions
{})
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error getting replication controllers: %v"
,
err
)
}
// Find the ones that match labelsToMatch.
var
matchingRCs
[]
api
.
ReplicationController
for
_
,
controller
:=
range
rcs
.
Items
{
selector
:=
labels
.
SelectorFromSet
(
controller
.
Spec
.
Selector
)
if
selector
.
Matches
(
labelsToMatch
)
{
matchingRCs
=
append
(
matchingRCs
,
controller
)
}
}
return
matchingRCs
,
nil
}
func
printReplicationControllersByLabels
(
matchingRCs
[]
*
api
.
ReplicationController
)
string
{
func
printReplicationControllersByLabels
(
matchingRCs
[]
*
api
.
ReplicationController
)
string
{
// Format the matching RC's into strings.
// Format the matching RC's into strings.
var
rcStrings
[]
string
var
rcStrings
[]
string
...
...
pkg/kubectl/resource_printer.go
View file @
b5e99be8
...
@@ -397,6 +397,7 @@ func (h *HumanReadablePrinter) HandledResources() []string {
...
@@ -397,6 +397,7 @@ func (h *HumanReadablePrinter) HandledResources() []string {
var
podColumns
=
[]
string
{
"NAME"
,
"READY"
,
"STATUS"
,
"RESTARTS"
,
"AGE"
}
var
podColumns
=
[]
string
{
"NAME"
,
"READY"
,
"STATUS"
,
"RESTARTS"
,
"AGE"
}
var
podTemplateColumns
=
[]
string
{
"TEMPLATE"
,
"CONTAINER(S)"
,
"IMAGE(S)"
,
"PODLABELS"
}
var
podTemplateColumns
=
[]
string
{
"TEMPLATE"
,
"CONTAINER(S)"
,
"IMAGE(S)"
,
"PODLABELS"
}
var
replicationControllerColumns
=
[]
string
{
"CONTROLLER"
,
"CONTAINER(S)"
,
"IMAGE(S)"
,
"SELECTOR"
,
"REPLICAS"
,
"AGE"
}
var
replicationControllerColumns
=
[]
string
{
"CONTROLLER"
,
"CONTAINER(S)"
,
"IMAGE(S)"
,
"SELECTOR"
,
"REPLICAS"
,
"AGE"
}
var
replicaSetColumns
=
[]
string
{
"CONTROLLER"
,
"CONTAINER(S)"
,
"IMAGE(S)"
,
"SELECTOR"
,
"REPLICAS"
,
"AGE"
}
var
jobColumns
=
[]
string
{
"JOB"
,
"CONTAINER(S)"
,
"IMAGE(S)"
,
"SELECTOR"
,
"SUCCESSFUL"
}
var
jobColumns
=
[]
string
{
"JOB"
,
"CONTAINER(S)"
,
"IMAGE(S)"
,
"SELECTOR"
,
"SUCCESSFUL"
}
var
serviceColumns
=
[]
string
{
"NAME"
,
"CLUSTER-IP"
,
"EXTERNAL-IP"
,
"PORT(S)"
,
"SELECTOR"
,
"AGE"
}
var
serviceColumns
=
[]
string
{
"NAME"
,
"CLUSTER-IP"
,
"EXTERNAL-IP"
,
"PORT(S)"
,
"SELECTOR"
,
"AGE"
}
var
ingressColumns
=
[]
string
{
"NAME"
,
"RULE"
,
"BACKEND"
,
"ADDRESS"
}
var
ingressColumns
=
[]
string
{
"NAME"
,
"RULE"
,
"BACKEND"
,
"ADDRESS"
}
...
@@ -427,6 +428,8 @@ func (h *HumanReadablePrinter) addDefaultHandlers() {
...
@@ -427,6 +428,8 @@ func (h *HumanReadablePrinter) addDefaultHandlers() {
h
.
Handler
(
podTemplateColumns
,
printPodTemplateList
)
h
.
Handler
(
podTemplateColumns
,
printPodTemplateList
)
h
.
Handler
(
replicationControllerColumns
,
printReplicationController
)
h
.
Handler
(
replicationControllerColumns
,
printReplicationController
)
h
.
Handler
(
replicationControllerColumns
,
printReplicationControllerList
)
h
.
Handler
(
replicationControllerColumns
,
printReplicationControllerList
)
h
.
Handler
(
replicaSetColumns
,
printReplicaSet
)
h
.
Handler
(
replicaSetColumns
,
printReplicaSetList
)
h
.
Handler
(
daemonSetColumns
,
printDaemonSet
)
h
.
Handler
(
daemonSetColumns
,
printDaemonSet
)
h
.
Handler
(
daemonSetColumns
,
printDaemonSetList
)
h
.
Handler
(
daemonSetColumns
,
printDaemonSetList
)
h
.
Handler
(
jobColumns
,
printJob
)
h
.
Handler
(
jobColumns
,
printJob
)
...
@@ -743,6 +746,63 @@ func printReplicationControllerList(list *api.ReplicationControllerList, w io.Wr
...
@@ -743,6 +746,63 @@ func printReplicationControllerList(list *api.ReplicationControllerList, w io.Wr
return
nil
return
nil
}
}
func
printReplicaSet
(
rs
*
extensions
.
ReplicaSet
,
w
io
.
Writer
,
options
PrintOptions
)
error
{
name
:=
rs
.
Name
namespace
:=
rs
.
Namespace
containers
:=
rs
.
Spec
.
Template
.
Spec
.
Containers
var
firstContainer
api
.
Container
if
len
(
containers
)
>
0
{
firstContainer
,
containers
=
containers
[
0
],
containers
[
1
:
]
}
if
options
.
WithNamespace
{
if
_
,
err
:=
fmt
.
Fprintf
(
w
,
"%s
\t
"
,
namespace
);
err
!=
nil
{
return
err
}
}
if
_
,
err
:=
fmt
.
Fprintf
(
w
,
"%s
\t
%s
\t
%s
\t
%s
\t
%d
\t
%s"
,
name
,
firstContainer
.
Name
,
firstContainer
.
Image
,
unversioned
.
FormatLabelSelector
(
rs
.
Spec
.
Selector
),
rs
.
Spec
.
Replicas
,
translateTimestamp
(
rs
.
CreationTimestamp
),
);
err
!=
nil
{
return
err
}
if
_
,
err
:=
fmt
.
Fprint
(
w
,
appendLabels
(
rs
.
Labels
,
options
.
ColumnLabels
));
err
!=
nil
{
return
err
}
if
_
,
err
:=
fmt
.
Fprint
(
w
,
appendAllLabels
(
options
.
ShowLabels
,
rs
.
Labels
));
err
!=
nil
{
return
err
}
// Lay out all the other containers on separate lines.
extraLinePrefix
:=
"
\t
"
if
options
.
WithNamespace
{
extraLinePrefix
=
"
\t\t
"
}
for
_
,
container
:=
range
containers
{
_
,
err
:=
fmt
.
Fprintf
(
w
,
"%s%s
\t
%s
\t
%s
\t
%s"
,
extraLinePrefix
,
container
.
Name
,
container
.
Image
,
""
,
""
)
if
err
!=
nil
{
return
err
}
if
_
,
err
:=
fmt
.
Fprint
(
w
,
appendLabelTabs
(
options
.
ColumnLabels
));
err
!=
nil
{
return
err
}
}
return
nil
}
func
printReplicaSetList
(
list
*
extensions
.
ReplicaSetList
,
w
io
.
Writer
,
options
PrintOptions
)
error
{
for
_
,
rs
:=
range
list
.
Items
{
if
err
:=
printReplicaSet
(
&
rs
,
w
,
options
);
err
!=
nil
{
return
err
}
}
return
nil
}
func
printJob
(
job
*
extensions
.
Job
,
w
io
.
Writer
,
options
PrintOptions
)
error
{
func
printJob
(
job
*
extensions
.
Job
,
w
io
.
Writer
,
options
PrintOptions
)
error
{
name
:=
job
.
Name
name
:=
job
.
Name
namespace
:=
job
.
Namespace
namespace
:=
job
.
Namespace
...
...
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