Skip to content

Commit 2f11084

Browse files
committed
Remove hard-coded pod-controller check
This allows pods with third-party, or unknown controllers to be drained successfully.
1 parent 151398e commit 2f11084

File tree

1 file changed

+15
-51
lines changed

1 file changed

+15
-51
lines changed

pkg/kubectl/cmd/drain.go

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -338,38 +338,8 @@ func (o *DrainOptions) deleteOrEvictPodsSimple(nodeInfo *resource.Info) error {
338338
return err
339339
}
340340

341-
func (o *DrainOptions) getController(namespace string, controllerRef *metav1.OwnerReference) (interface{}, error) {
342-
switch controllerRef.Kind {
343-
case "ReplicationController":
344-
return o.client.CoreV1().ReplicationControllers(namespace).Get(controllerRef.Name, metav1.GetOptions{})
345-
case "DaemonSet":
346-
return o.client.ExtensionsV1beta1().DaemonSets(namespace).Get(controllerRef.Name, metav1.GetOptions{})
347-
case "Job":
348-
return o.client.BatchV1().Jobs(namespace).Get(controllerRef.Name, metav1.GetOptions{})
349-
case "ReplicaSet":
350-
return o.client.ExtensionsV1beta1().ReplicaSets(namespace).Get(controllerRef.Name, metav1.GetOptions{})
351-
case "StatefulSet":
352-
return o.client.AppsV1beta1().StatefulSets(namespace).Get(controllerRef.Name, metav1.GetOptions{})
353-
}
354-
return nil, fmt.Errorf("Unknown controller kind %q", controllerRef.Kind)
355-
}
356-
357-
func (o *DrainOptions) getPodController(pod corev1.Pod) (*metav1.OwnerReference, error) {
358-
controllerRef := metav1.GetControllerOf(&pod)
359-
if controllerRef == nil {
360-
return nil, nil
361-
}
362-
363-
// We assume the only reason for an error is because the controller is
364-
// gone/missing, not for any other cause.
365-
// TODO(mml): something more sophisticated than this
366-
// TODO(juntee): determine if it's safe to remove getController(),
367-
// so that drain can work for controller types that we don't know about
368-
_, err := o.getController(pod.Namespace, controllerRef)
369-
if err != nil {
370-
return nil, err
371-
}
372-
return controllerRef, nil
341+
func (o *DrainOptions) getPodController(pod corev1.Pod) *metav1.OwnerReference {
342+
return metav1.GetControllerOf(&pod)
373343
}
374344

375345
func (o *DrainOptions) unreplicatedFilter(pod corev1.Pod) (bool, *warning, *fatal) {
@@ -378,21 +348,15 @@ func (o *DrainOptions) unreplicatedFilter(pod corev1.Pod) (bool, *warning, *fata
378348
return true, nil, nil
379349
}
380350

381-
controllerRef, err := o.getPodController(pod)
382-
if err != nil {
383-
// if we're forcing, remove orphaned pods with a warning
384-
if apierrors.IsNotFound(err) && o.Force {
385-
return true, &warning{err.Error()}, nil
386-
}
387-
return false, nil, &fatal{err.Error()}
388-
}
351+
controllerRef := o.getPodController(pod)
389352
if controllerRef != nil {
390353
return true, nil, nil
391354
}
392-
if !o.Force {
393-
return false, nil, &fatal{kUnmanagedFatal}
355+
if o.Force {
356+
return true, &warning{kUnmanagedWarning}, nil
394357
}
395-
return true, &warning{kUnmanagedWarning}, nil
358+
359+
return false, nil, &fatal{kUnmanagedFatal}
396360
}
397361

398362
func (o *DrainOptions) daemonsetFilter(pod corev1.Pod) (bool, *warning, *fatal) {
@@ -403,23 +367,23 @@ func (o *DrainOptions) daemonsetFilter(pod corev1.Pod) (bool, *warning, *fatal)
403367
// The exception is for pods that are orphaned (the referencing
404368
// management resource - including DaemonSet - is not found).
405369
// Such pods will be deleted if --force is used.
406-
controllerRef, err := o.getPodController(pod)
407-
if err != nil {
408-
// if we're forcing, remove orphaned pods with a warning
409-
if apierrors.IsNotFound(err) && o.Force {
410-
return true, &warning{err.Error()}, nil
411-
}
412-
return false, nil, &fatal{err.Error()}
413-
}
370+
controllerRef := o.getPodController(pod)
414371
if controllerRef == nil || controllerRef.Kind != "DaemonSet" {
415372
return true, nil, nil
416373
}
374+
417375
if _, err := o.client.ExtensionsV1beta1().DaemonSets(pod.Namespace).Get(controllerRef.Name, metav1.GetOptions{}); err != nil {
376+
// remove orphaned pods with a warning if --force is used
377+
if apierrors.IsNotFound(err) && o.Force {
378+
return true, &warning{err.Error()}, nil
379+
}
418380
return false, nil, &fatal{err.Error()}
419381
}
382+
420383
if !o.IgnoreDaemonsets {
421384
return false, nil, &fatal{kDaemonsetFatal}
422385
}
386+
423387
return false, &warning{kDaemonsetWarning}, nil
424388
}
425389

0 commit comments

Comments
 (0)