Commit c10d12b7 authored by k8s-merge-robot's avatar k8s-merge-robot

Merge pull request #24923 from mwielgus/drain-refactor

Automatic merge from submit-queue Small refactoring around drain - move drain logic to separate function. cc: @piosz @fgrzadkowski
parents 60715407 60f13391
...@@ -183,7 +183,7 @@ func (o *DrainOptions) RunDrain() error { ...@@ -183,7 +183,7 @@ func (o *DrainOptions) RunDrain() error {
return err return err
} }
pods, err := o.GetPodsForDeletion() pods, err := o.getPodsForDeletion()
if err != nil { if err != nil {
return err return err
} }
...@@ -195,17 +195,55 @@ func (o *DrainOptions) RunDrain() error { ...@@ -195,17 +195,55 @@ func (o *DrainOptions) RunDrain() error {
return nil return nil
} }
// GetPodsForDeletion returns all the pods we're going to delete. If there are // getPodsForDeletion returns all the pods we're going to delete. If there are
// any unmanaged pods and the user didn't pass --force, we return that list in // any unmanaged pods and the user didn't pass --force, we return that list in
// an error. // an error.
func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) { func (o *DrainOptions) getPodsForDeletion() ([]api.Pod, error) {
pods := []api.Pod{} pods, unreplicatedPodNames, daemonSetPodNames, err := GetPodsForDeletionOnNodeDrain(
podList, err := o.client.Pods(api.NamespaceAll).List(api.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": o.nodeInfo.Name})}) o.client,
o.nodeInfo.Name,
o.factory.Decoder(true),
o.Force,
o.IgnoreDaemonsets,
)
if err != nil { if err != nil {
return pods, err return []api.Pod{}, err
}
daemonSetErrors := !o.IgnoreDaemonsets && len(daemonSetPodNames) > 0
unreplicatedErrors := !o.Force && len(unreplicatedPodNames) > 0
switch {
case daemonSetErrors && unreplicatedErrors:
return []api.Pod{}, errors.New(unmanagedMsg(unreplicatedPodNames, daemonSetPodNames, true))
case daemonSetErrors && !unreplicatedErrors:
return []api.Pod{}, errors.New(unmanagedMsg([]string{}, daemonSetPodNames, true))
case unreplicatedErrors && !daemonSetErrors:
return []api.Pod{}, errors.New(unmanagedMsg(unreplicatedPodNames, []string{}, true))
}
if len(unreplicatedPodNames) > 0 {
fmt.Fprintf(o.out, "WARNING: About to delete these %s\n", unmanagedMsg(unreplicatedPodNames, []string{}, false))
}
if len(daemonSetPodNames) > 0 {
fmt.Fprintf(o.out, "WARNING: Skipping %s\n", unmanagedMsg([]string{}, daemonSetPodNames, false))
}
return pods, nil
}
// GetPodsForDeletionOnNodeDrain returns pods that should be deleted on node drain as well as some extra information
// about possibly problematic pods (unreplicated and deamon sets).
func GetPodsForDeletionOnNodeDrain(client *client.Client, nodename string, decoder runtime.Decoder, force bool,
ignoreDeamonSet bool) (pods []api.Pod, unreplicatedPodNames []string, daemonSetPodNames []string, finalError error) {
pods = []api.Pod{}
unreplicatedPodNames = []string{}
daemonSetPodNames = []string{}
podList, err := client.Pods(api.NamespaceAll).List(api.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": nodename})})
if err != nil {
return []api.Pod{}, []string{}, []string{}, err
} }
unreplicatedPodNames := []string{}
daemonSetPodNames := []string{}
for _, pod := range podList.Items { for _, pod := range podList.Items {
_, found := pod.ObjectMeta.Annotations[types.ConfigMirrorAnnotationKey] _, found := pod.ObjectMeta.Annotations[types.ConfigMirrorAnnotationKey]
...@@ -220,11 +258,11 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) { ...@@ -220,11 +258,11 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
if found { if found {
// Now verify that the specified creator actually exists. // Now verify that the specified creator actually exists.
var sr api.SerializedReference var sr api.SerializedReference
if err := runtime.DecodeInto(o.factory.Decoder(true), []byte(creatorRef), &sr); err != nil { if err := runtime.DecodeInto(decoder, []byte(creatorRef), &sr); err != nil {
return pods, err return []api.Pod{}, []string{}, []string{}, err
} }
if sr.Reference.Kind == "ReplicationController" { if sr.Reference.Kind == "ReplicationController" {
rc, err := o.client.ReplicationControllers(sr.Reference.Namespace).Get(sr.Reference.Name) rc, err := client.ReplicationControllers(sr.Reference.Namespace).Get(sr.Reference.Name)
// Assume the only reason for an error is because the RC is // Assume the only reason for an error is because the RC is
// gone/missing, not for any other cause. TODO(mml): something more // gone/missing, not for any other cause. TODO(mml): something more
// sophisticated than this // sophisticated than this
...@@ -232,7 +270,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) { ...@@ -232,7 +270,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
replicated = true replicated = true
} }
} else if sr.Reference.Kind == "DaemonSet" { } else if sr.Reference.Kind == "DaemonSet" {
ds, err := o.client.DaemonSets(sr.Reference.Namespace).Get(sr.Reference.Name) ds, err := client.DaemonSets(sr.Reference.Namespace).Get(sr.Reference.Name)
// Assume the only reason for an error is because the DaemonSet is // Assume the only reason for an error is because the DaemonSet is
// gone/missing, not for any other cause. TODO(mml): something more // gone/missing, not for any other cause. TODO(mml): something more
...@@ -245,7 +283,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) { ...@@ -245,7 +283,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
daemonset_pod = true daemonset_pod = true
} }
} else if sr.Reference.Kind == "Job" { } else if sr.Reference.Kind == "Job" {
job, err := o.client.ExtensionsClient.Jobs(sr.Reference.Namespace).Get(sr.Reference.Name) job, err := client.ExtensionsClient.Jobs(sr.Reference.Namespace).Get(sr.Reference.Name)
// Assume the only reason for an error is because the Job is // Assume the only reason for an error is because the Job is
// gone/missing, not for any other cause. TODO(mml): something more // gone/missing, not for any other cause. TODO(mml): something more
...@@ -254,7 +292,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) { ...@@ -254,7 +292,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
replicated = true replicated = true
} }
} else if sr.Reference.Kind == "ReplicaSet" { } else if sr.Reference.Kind == "ReplicaSet" {
rs, err := o.client.ExtensionsClient.ReplicaSets(sr.Reference.Namespace).Get(sr.Reference.Name) rs, err := client.ExtensionsClient.ReplicaSets(sr.Reference.Namespace).Get(sr.Reference.Name)
// Assume the only reason for an error is because the RS is // Assume the only reason for an error is because the RS is
// gone/missing, not for any other cause. TODO(mml): something more // gone/missing, not for any other cause. TODO(mml): something more
...@@ -270,34 +308,14 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) { ...@@ -270,34 +308,14 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
daemonSetPodNames = append(daemonSetPodNames, pod.Name) daemonSetPodNames = append(daemonSetPodNames, pod.Name)
case !replicated: case !replicated:
unreplicatedPodNames = append(unreplicatedPodNames, pod.Name) unreplicatedPodNames = append(unreplicatedPodNames, pod.Name)
if o.Force { if force {
pods = append(pods, pod) pods = append(pods, pod)
} }
default: default:
pods = append(pods, pod) pods = append(pods, pod)
} }
} }
return pods, unreplicatedPodNames, daemonSetPodNames, nil
daemonSetErrors := !o.IgnoreDaemonsets && len(daemonSetPodNames) > 0
unreplicatedErrors := !o.Force && len(unreplicatedPodNames) > 0
switch {
case daemonSetErrors && unreplicatedErrors:
return []api.Pod{}, errors.New(unmanagedMsg(unreplicatedPodNames, daemonSetPodNames, true))
case daemonSetErrors && !unreplicatedErrors:
return []api.Pod{}, errors.New(unmanagedMsg([]string{}, daemonSetPodNames, true))
case unreplicatedErrors && !daemonSetErrors:
return []api.Pod{}, errors.New(unmanagedMsg(unreplicatedPodNames, []string{}, true))
}
if len(unreplicatedPodNames) > 0 {
fmt.Fprintf(o.out, "WARNING: About to delete these %s\n", unmanagedMsg(unreplicatedPodNames, []string{}, false))
}
if len(daemonSetPodNames) > 0 {
fmt.Fprintf(o.out, "WARNING: Skipping %s\n", unmanagedMsg([]string{}, daemonSetPodNames, false))
}
return pods, nil
} }
// Helper for generating errors or warnings about unmanaged pods. // Helper for generating errors or warnings about unmanaged pods.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment