Skip to content

Commit

Permalink
Merge pull request #11284 from mfojtik/evacuate
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot authored Oct 14, 2016
2 parents 6d047d3 + 010bf86 commit fc9374d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
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.
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

0 comments on commit fc9374d

Please sign in to comment.