Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if the Node already cordoned when executing Drain #2275

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/dao/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
var (
_ Accessor = (*Node)(nil)
_ NodeMaintainer = (*Node)(nil)

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

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

if !h.UpdateIfRequired(cordon) {
if cordon {
return fmt.Errorf("node is already cordoned")
return ErrNodeAlreadyCordoned
}
return fmt.Errorf("node is already uncordoned")
}
Expand Down Expand Up @@ -84,7 +86,7 @@ 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); err != nil {
if err := n.ToggleCordon(path, true); ignoreNodeAlreadyCordoned(err) != nil {
wjiec marked this conversation as resolved.
Show resolved Hide resolved
return err
}

Expand Down Expand Up @@ -270,3 +272,12 @@ func FetchNodes(ctx context.Context, f Factory, labelsSel string) (*v1.NodeList,

return &v1.NodeList{Items: nn}, nil
}

// ignoreNodeAlreadyCordoned returns nil on ErrNodeAlreadyCordoned errors.
// All other values that are not ErrNodeAlreadyCordoned errors or nil are returned unmodified.
func ignoreNodeAlreadyCordoned(err error) error {
if err == nil || errors.Is(err, ErrNodeAlreadyCordoned) {
return nil
}
return err
}