...

Source file src/k8s.io/kubectl/pkg/drain/default.go

Documentation: k8s.io/kubectl/pkg/drain

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package drain
    18  
    19  import (
    20  	"fmt"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	utilerrors "k8s.io/apimachinery/pkg/util/errors"
    24  )
    25  
    26  // This file contains default implementations of how to
    27  // drain/cordon/uncordon nodes.  These functions may be called
    28  // directly, or their functionality copied into your own code, for
    29  // example if you want different output behaviour.
    30  
    31  // RunNodeDrain shows the canonical way to drain a node.
    32  // You should first cordon the node, e.g. using RunCordonOrUncordon
    33  func RunNodeDrain(drainer *Helper, nodeName string) error {
    34  	// TODO(justinsb): Ensure we have adequate e2e coverage of this function in library consumers
    35  	list, errs := drainer.GetPodsForDeletion(nodeName)
    36  	if errs != nil {
    37  		return utilerrors.NewAggregate(errs)
    38  	}
    39  	if warnings := list.Warnings(); warnings != "" {
    40  		fmt.Fprintf(drainer.ErrOut, "WARNING: %s\n", warnings)
    41  	}
    42  
    43  	if err := drainer.DeleteOrEvictPods(list.Pods()); err != nil {
    44  		// Maybe warn about non-deleted pods here
    45  		return err
    46  	}
    47  	return nil
    48  }
    49  
    50  // RunCordonOrUncordon demonstrates the canonical way to cordon or uncordon a Node
    51  func RunCordonOrUncordon(drainer *Helper, node *corev1.Node, desired bool) error {
    52  	if drainer.Ctx == nil {
    53  		return fmt.Errorf("RunCordonOrUncordon error: drainer.Ctx can't be nil")
    54  	}
    55  	if drainer.Client == nil {
    56  		return fmt.Errorf("RunCordonOrUncordon error: drainer.Client can't be nil")
    57  	}
    58  	// TODO(justinsb): Ensure we have adequate e2e coverage of this function in library consumers
    59  	c := NewCordonHelper(node)
    60  
    61  	if updateRequired := c.UpdateIfRequired(desired); !updateRequired {
    62  		// Already done
    63  		return nil
    64  	}
    65  
    66  	err, patchErr := c.PatchOrReplaceWithContext(drainer.Ctx, drainer.Client, false)
    67  	if err != nil {
    68  		if patchErr != nil {
    69  			return fmt.Errorf("cordon error: %s; merge patch error: %w", err.Error(), patchErr)
    70  		}
    71  		return fmt.Errorf("cordon error: %w", err)
    72  	}
    73  
    74  	return nil
    75  }
    76  

View as plain text