Skip to content

Commit

Permalink
ensure node cordoned
Browse files Browse the repository at this point in the history
  • Loading branch information
wjiec committed Nov 9, 2023
1 parent 0264228 commit 578137c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions internal/dao/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
var (
_ Accessor = (*Node)(nil)
_ NodeMaintainer = (*Node)(nil)

ErrNodeAlreadyCordoned = errors.New("node is already cordoned")
)

// NodeMetricsFunc retrieves node metrics.
Expand All @@ -51,7 +49,7 @@ func (n *Node) ToggleCordon(path string, cordon bool) error {

if !h.UpdateIfRequired(cordon) {
if cordon {
return ErrNodeAlreadyCordoned
return fmt.Errorf("node is already cordoned")
}
return fmt.Errorf("node is already uncordoned")
}
Expand Down Expand Up @@ -86,10 +84,17 @@ func (o DrainOptions) toDrainHelper(k kubernetes.Interface, w io.Writer) drain.H

// Drain drains a node.
func (n *Node) Drain(path string, opts DrainOptions, w io.Writer) error {
if err := n.ToggleCordon(path, true); ignoreNodeAlreadyCordoned(err) != nil {
cordoned, err := n.ensureCordoned(path)
if err != nil {
return err
}

if !cordoned {
if err = n.ToggleCordon(path, true); err != nil {
return err
}
}

dial, err := n.GetFactory().Client().Dial()
if err != nil {
return err
Expand Down Expand Up @@ -219,6 +224,16 @@ func (n *Node) GetPods(nodeName string) ([]*v1.Pod, error) {
return pp, nil
}

// ensureCordoned returns whether the given node has been cordoned
func (n *Node) ensureCordoned(path string) (bool, error) {
o, err := FetchNode(context.Background(), n.Factory, path)
if err != nil {
return false, err
}

return o.Spec.Unschedulable, nil
}

// ----------------------------------------------------------------------------
// Helpers...

Expand Down

0 comments on commit 578137c

Please sign in to comment.