check if the Node already cordoned when executing Drain (#2275)

* check if the Node already cordoned when executing Drain

* ensure node cordoned
mine
Jayson Wang 2023-11-10 08:15:20 +08:00 committed by GitHub
parent b3e85e9bbc
commit 893f15f802
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 1 deletions

View File

@ -84,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); 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
@ -217,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...