Skip to content

Commit

Permalink
Use upstream drain library
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Demichev committed Jan 16, 2020
1 parent d65b170 commit 8b7009a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1,428 deletions.
35 changes: 19 additions & 16 deletions pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import (
"fmt"
"time"

"github.com/go-log/log/info"
commonerrors "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
kubedrain "github.com/openshift/machine-api-operator/pkg/drain"
"github.com/openshift/machine-api-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -34,6 +32,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"k8s.io/kubectl/pkg/drain"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -323,20 +322,24 @@ func (r *ReconcileMachine) drainNode(machine *machinev1.Machine) error {
return fmt.Errorf("unable to get node %q: %v", machine.Status.NodeRef.Name, err)
}

if err := kubedrain.Drain(
kubeClient,
[]*corev1.Node{node},
&kubedrain.DrainOptions{
Force: true,
IgnoreDaemonsets: true,
DeleteLocalData: true,
GracePeriodSeconds: -1,
Logger: info.New(klog.V(0)),
// If a pod is not evicted in 20 second, retry the eviction next time the
// machine gets reconciled again (to allow other machines to be reconciled)
Timeout: 20 * time.Second,
},
); err != nil {
drainer := &drain.Helper{
Force: true,
IgnoreAllDaemonSets: true,
DeleteLocalData: true,
GracePeriodSeconds: -1,
// If a pod is not evicted in 20 second, retry the eviction next time the
// machine gets reconciled again (to allow other machines to be reconciled)
Timeout: 20 * time.Second,
Client: kubeClient,
}

if err := drain.RunCordonOrUncordon(drainer, node, true); err != nil {
// Can't cordon a node
klog.Warningf("cordon failed for node %q: %v", node.Name, err)
return &RequeueAfterError{RequeueAfter: 20 * time.Second}
}

if err := drain.RunNodeDrain(drainer, node.Name); err != nil {
// Machine still tries to terminate after drain failure
klog.Warningf("drain failed for machine %q: %v", machine.Name, err)
return &RequeueAfterError{RequeueAfter: 20 * time.Second}
Expand Down
Loading

0 comments on commit 8b7009a

Please sign in to comment.