Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make evacuate aware of replica set and daemon set #11284

Merged
merged 2 commits into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions pkg/cmd/admin/node/evacuate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,30 @@ func (e *EvacuateOptions) RunEvacuate(node *kapi.Node) error {
fieldSelector := fields.Set{GetPodHostFieldLabel(node.TypeMeta.APIVersion): node.ObjectMeta.Name}.AsSelector()

// Filter all pods that satisfies pod label selector and belongs to the given node
pods, err := e.Options.Kclient.Pods(kapi.NamespaceAll).List(kapi.ListOptions{LabelSelector: labelSelector, FieldSelector: fieldSelector})
pods, err := e.Options.KubeClient.Pods(kapi.NamespaceAll).List(kapi.ListOptions{LabelSelector: labelSelector, FieldSelector: fieldSelector})
if err != nil {
return err
}
if len(pods.Items) == 0 {
fmt.Fprint(e.Options.ErrWriter, "\nNo pods found on node: ", node.ObjectMeta.Name, "\n\n")
return nil
}
rcs, err := e.Options.Kclient.ReplicationControllers(kapi.NamespaceAll).List(kapi.ListOptions{})
rcs, err := e.Options.KubeClient.ReplicationControllers(kapi.NamespaceAll).List(kapi.ListOptions{})
if err != nil {
return err
}

rss, err := e.Options.KubeClient.ReplicaSets(kapi.NamespaceAll).List(kapi.ListOptions{})
if err != nil {
return err
}

dss, err := e.Options.KubeClient.DaemonSets(kapi.NamespaceAll).List(kapi.ListOptions{})
if err != nil {
return err
}

jobs, err := e.Options.KubeClient.Batch().Jobs(kapi.NamespaceAll).List(kapi.ListOptions{})
if err != nil {
return err
}
Expand All @@ -105,19 +120,43 @@ func (e *EvacuateOptions) RunEvacuate(node *kapi.Node) error {

errList := []error{}
firstPod := true
numPodsWithNoRC := 0
numUnmanagedPods := 0

var deleteOptions *kapi.DeleteOptions
if e.GracePeriod >= 0 {
deleteOptions = e.makeDeleteOptions()
}

for _, pod := range pods.Items {
foundrc := false
isManaged := false
for _, rc := range rcs.Items {
selector := labels.SelectorFromSet(rc.Spec.Selector)
if selector.Matches(labels.Set(pod.Labels)) {
foundrc = true
isManaged = true
break
}
}

for _, rs := range rss.Items {
selector := labels.SelectorFromSet(rs.Spec.Selector.MatchLabels)
if selector.Matches(labels.Set(pod.Labels)) {
isManaged = true
break
}
}

for _, ds := range dss.Items {
selector := labels.SelectorFromSet(ds.Spec.Selector.MatchLabels)
if selector.Matches(labels.Set(pod.Labels)) {
isManaged = true
break
}
}

for _, job := range jobs.Items {
selector := labels.SelectorFromSet(job.Spec.Selector.MatchLabels)
if selector.Matches(labels.Set(pod.Labels)) {
isManaged = true
break
}
}
Expand All @@ -129,18 +168,18 @@ func (e *EvacuateOptions) RunEvacuate(node *kapi.Node) error {

printer.PrintObj(&pod, e.Options.Writer)

if foundrc || e.Force {
if err := e.Options.Kclient.Pods(pod.Namespace).Delete(pod.Name, deleteOptions); err != nil {
if isManaged || e.Force {
if err := e.Options.KubeClient.Pods(pod.Namespace).Delete(pod.Name, deleteOptions); err != nil {
glog.Errorf("Unable to delete a pod: %+v, error: %v", pod, err)
errList = append(errList, err)
continue
}
} else { // Pods without replication controller and no --force option
numPodsWithNoRC++
numUnmanagedPods++
}
}
if numPodsWithNoRC > 0 {
err := fmt.Errorf(`Unable to evacuate some pods because they are not backed by replication controller.
if numUnmanagedPods > 0 {
err := fmt.Errorf(`Unable to evacuate some pods because they are not managed by replication controller or replica set or deaemon set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"because they are not managed by any type of controller (replication controller, replica set, daemon set, pet set)" or something similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Suggested options:
- You can list bare pods in json/yaml format using '--list-pods -o json|yaml'
- Force deletion of bare pods with --force option to --evacuate
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/admin/node/listpods.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (l *ListPodsOptions) runListPods(node *kapi.Node, printer kubectl.ResourceP
fieldSelector := fields.Set{GetPodHostFieldLabel(node.TypeMeta.APIVersion): node.ObjectMeta.Name}.AsSelector()

// Filter all pods that satisfies pod label selector and belongs to the given node
pods, err := l.Options.Kclient.Pods(kapi.NamespaceAll).List(kapi.ListOptions{LabelSelector: labelSelector, FieldSelector: fieldSelector})
pods, err := l.Options.KubeClient.Pods(kapi.NamespaceAll).List(kapi.ListOptions{LabelSelector: labelSelector, FieldSelector: fieldSelector})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/admin/node/node_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

type NodeOptions struct {
DefaultNamespace string
Kclient *client.Client
KubeClient *client.Client
Writer io.Writer
ErrWriter io.Writer

Expand Down Expand Up @@ -61,7 +61,7 @@ func (n *NodeOptions) Complete(f *clientcmd.Factory, c *cobra.Command, args []st
mapper, typer := f.Object(false)

n.DefaultNamespace = defaultNamespace
n.Kclient = kc
n.KubeClient = kc
n.Writer = out
n.ErrWriter = errout
n.Mapper = mapper
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/admin/node/schedulable.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *SchedulableOptions) Run() error {
for _, node := range nodes {
if node.Spec.Unschedulable != unschedulable {
node.Spec.Unschedulable = unschedulable
node, err = s.Options.Kclient.Nodes().Update(node)
node, err = s.Options.KubeClient.Nodes().Update(node)
if err != nil {
errList = append(errList, err)
continue
Expand Down