2015-10-31 00:16:57 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2015 The Kubernetes Authors .
2015-10-31 00:16:57 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package cmd
import (
2016-01-27 18:27:14 +00:00
"errors"
2015-10-31 00:16:57 +00:00
"fmt"
"io"
2016-10-20 16:43:48 +00:00
"math"
2015-10-31 00:16:57 +00:00
"strings"
2016-10-13 21:34:51 +00:00
"time"
2015-10-31 00:16:57 +00:00
2016-10-20 16:43:48 +00:00
"github.com/jonboulle/clockwork"
2015-10-31 00:16:57 +00:00
"github.com/spf13/cobra"
2017-07-06 03:23:30 +00:00
corev1 "k8s.io/api/core/v1"
2017-01-13 17:48:50 +00:00
apierrors "k8s.io/apimachinery/pkg/api/errors"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-01-19 14:50:16 +00:00
"k8s.io/apimachinery/pkg/fields"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/runtime"
2017-06-30 12:58:57 +00:00
"k8s.io/apimachinery/pkg/types"
2017-09-22 17:55:54 +00:00
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/sets"
2017-06-30 12:58:57 +00:00
"k8s.io/apimachinery/pkg/util/strategicpatch"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/util/wait"
2017-01-19 18:27:59 +00:00
restclient "k8s.io/client-go/rest"
2017-06-30 12:58:57 +00:00
2015-10-31 00:16:57 +00:00
"k8s.io/kubernetes/pkg/api"
2016-10-20 16:43:48 +00:00
"k8s.io/kubernetes/pkg/apis/policy"
2016-09-07 20:29:57 +00:00
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
2016-10-13 21:34:51 +00:00
"k8s.io/kubernetes/pkg/kubectl"
2016-10-18 23:00:54 +00:00
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2015-10-31 00:16:57 +00:00
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
2017-07-07 04:04:11 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2015-10-31 00:16:57 +00:00
)
type DrainOptions struct {
2017-02-16 11:46:15 +00:00
client internalclientset . Interface
2016-09-07 20:29:57 +00:00
restClient * restclient . RESTClient
2017-02-26 05:41:39 +00:00
Factory cmdutil . Factory
2015-10-31 00:16:57 +00:00
Force bool
2017-09-13 19:20:54 +00:00
DryRun bool
2015-10-31 00:16:57 +00:00
GracePeriodSeconds int
2016-01-27 18:27:14 +00:00
IgnoreDaemonsets bool
2016-10-13 21:34:51 +00:00
Timeout time . Duration
2016-10-20 16:43:48 +00:00
backOff clockwork . Clock
2016-06-01 21:50:13 +00:00
DeleteLocalData bool
2017-09-22 17:55:54 +00:00
Selector string
2015-10-31 00:16:57 +00:00
mapper meta . RESTMapper
2017-09-22 17:55:54 +00:00
nodeInfos [ ] * resource . Info
2017-02-26 05:41:39 +00:00
Out io . Writer
ErrOut io . Writer
2015-10-31 00:16:57 +00:00
typer runtime . ObjectTyper
}
2016-06-01 21:50:13 +00:00
// Takes a pod and returns a bool indicating whether or not to operate on the
// pod, an optional warning message, and an optional fatal error.
type podFilter func ( api . Pod ) ( include bool , w * warning , f * fatal )
type warning struct {
string
}
type fatal struct {
string
}
2015-10-31 00:16:57 +00:00
const (
2016-10-20 16:43:48 +00:00
EvictionKind = "Eviction"
EvictionSubresource = "pods/eviction"
2016-06-01 21:50:13 +00:00
kDaemonsetFatal = "DaemonSet-managed pods (use --ignore-daemonsets to ignore)"
kDaemonsetWarning = "Ignoring DaemonSet-managed pods"
kLocalStorageFatal = "pods with local storage (use --delete-local-data to override)"
kLocalStorageWarning = "Deleting pods with local storage"
2016-11-22 22:51:03 +00:00
kUnmanagedFatal = "pods not managed by ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet (use --force to override)"
kUnmanagedWarning = "Deleting pods not managed by ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet"
2016-05-20 17:49:56 +00:00
)
var (
2017-03-15 03:49:10 +00:00
cordon_long = templates . LongDesc ( i18n . T ( `
Mark node as unschedulable . ` ) )
2016-10-07 22:24:42 +00:00
2017-03-15 03:49:10 +00:00
cordon_example = templates . Examples ( i18n . T ( `
2016-05-20 17:49:56 +00:00
# Mark node "foo" as unschedulable .
2017-03-15 03:49:10 +00:00
kubectl cordon foo ` ) )
2015-10-31 00:16:57 +00:00
)
2016-10-13 00:18:39 +00:00
func NewCmdCordon ( f cmdutil . Factory , out io . Writer ) * cobra . Command {
2017-02-26 05:41:39 +00:00
options := & DrainOptions { Factory : f , Out : out }
2015-10-31 00:16:57 +00:00
2016-03-10 01:27:19 +00:00
cmd := & cobra . Command {
2015-10-31 00:16:57 +00:00
Use : "cordon NODE" ,
2017-01-25 01:00:32 +00:00
Short : i18n . T ( "Mark node as unschedulable" ) ,
2015-10-31 00:16:57 +00:00
Long : cordon_long ,
Example : cordon_example ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
cmdutil . CheckErr ( options . SetupDrain ( cmd , args ) )
cmdutil . CheckErr ( options . RunCordonOrUncordon ( true ) )
} ,
}
2017-09-22 17:55:54 +00:00
cmd . Flags ( ) . StringVarP ( & options . Selector , "selector" , "l" , options . Selector , "Selector (label query) to filter on" )
2017-09-13 19:20:54 +00:00
cmdutil . AddDryRunFlag ( cmd )
2016-03-10 01:27:19 +00:00
return cmd
2015-10-31 00:16:57 +00:00
}
2016-05-20 17:49:56 +00:00
var (
2017-03-15 03:49:10 +00:00
uncordon_long = templates . LongDesc ( i18n . T ( `
Mark node as schedulable . ` ) )
2016-10-07 22:24:42 +00:00
2017-03-15 03:49:10 +00:00
uncordon_example = templates . Examples ( i18n . T ( `
2016-05-20 17:49:56 +00:00
# Mark node "foo" as schedulable .
2017-03-15 03:49:10 +00:00
$ kubectl uncordon foo ` ) )
2015-10-31 00:16:57 +00:00
)
2016-10-13 00:18:39 +00:00
func NewCmdUncordon ( f cmdutil . Factory , out io . Writer ) * cobra . Command {
2017-02-26 05:41:39 +00:00
options := & DrainOptions { Factory : f , Out : out }
2015-10-31 00:16:57 +00:00
2016-03-10 01:27:19 +00:00
cmd := & cobra . Command {
2015-10-31 00:16:57 +00:00
Use : "uncordon NODE" ,
2017-01-25 01:00:32 +00:00
Short : i18n . T ( "Mark node as schedulable" ) ,
2015-10-31 00:16:57 +00:00
Long : uncordon_long ,
Example : uncordon_example ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
cmdutil . CheckErr ( options . SetupDrain ( cmd , args ) )
cmdutil . CheckErr ( options . RunCordonOrUncordon ( false ) )
} ,
}
2017-09-22 17:55:54 +00:00
cmd . Flags ( ) . StringVarP ( & options . Selector , "selector" , "l" , options . Selector , "Selector (label query) to filter on" )
2017-09-13 19:20:54 +00:00
cmdutil . AddDryRunFlag ( cmd )
2016-03-10 01:27:19 +00:00
return cmd
2015-10-31 00:16:57 +00:00
}
2016-05-20 17:49:56 +00:00
var (
2017-03-15 03:49:10 +00:00
drain_long = templates . LongDesc ( i18n . T ( `
2016-05-20 17:49:56 +00:00
Drain node in preparation for maintenance .
The given node will be marked unschedulable to prevent new pods from arriving .
2016-12-14 18:00:25 +00:00
' drain ' evicts the pods if the APIServer supports eviction
2016-11-22 22:51:03 +00:00
( http : //kubernetes.io/docs/admin/disruptions/). Otherwise, it will use normal DELETE
to delete the pods .
The ' drain ' evicts or deletes all pods except mirror pods ( which cannot be deleted through
2016-05-20 17:49:56 +00:00
the API server ) . If there are DaemonSet - managed pods , drain will not proceed
without -- ignore - daemonsets , and regardless it will not delete any
DaemonSet - managed pods , because those pods would be immediately replaced by the
DaemonSet controller , which ignores unschedulable markings . If there are any
2016-09-21 04:26:04 +00:00
pods that are neither mirror pods nor managed by ReplicationController ,
2016-11-22 22:51:03 +00:00
ReplicaSet , DaemonSet , StatefulSet or Job , then drain will not delete any pods unless you
2017-02-22 01:07:42 +00:00
use -- force . -- force will also allow deletion to proceed if the managing resource of one
or more pods is missing .
2016-05-20 17:49:56 +00:00
2016-11-22 22:51:03 +00:00
' drain ' waits for graceful termination . You should not operate on the machine until
the command completes .
2016-05-20 17:49:56 +00:00
When you are ready to put the node back into service , use kubectl uncordon , which
will make the node schedulable again .
2016-05-17 11:59:43 +00:00
2017-03-15 03:49:10 +00:00
! [ Workflow ] ( http : //kubernetes.io/images/docs/kubectl_drain.svg)`))
2016-05-20 17:49:56 +00:00
2017-03-15 03:49:10 +00:00
drain_example = templates . Examples ( i18n . T ( `
2016-11-22 22:51:03 +00:00
# Drain node "foo" , even if there are pods not managed by a ReplicationController , ReplicaSet , Job , DaemonSet or StatefulSet on it .
2016-05-20 17:49:56 +00:00
$ kubectl drain foo -- force
2016-11-22 22:51:03 +00:00
# As above , but abort if there are pods not managed by a ReplicationController , ReplicaSet , Job , DaemonSet or StatefulSet , and use a grace period of 15 minutes .
2017-03-15 03:49:10 +00:00
$ kubectl drain foo -- grace - period = 900 ` ) )
2015-10-31 00:16:57 +00:00
)
2016-10-20 16:43:48 +00:00
func NewCmdDrain ( f cmdutil . Factory , out , errOut io . Writer ) * cobra . Command {
2017-02-26 05:41:39 +00:00
options := & DrainOptions { Factory : f , Out : out , ErrOut : errOut , backOff : clockwork . NewRealClock ( ) }
2015-10-31 00:16:57 +00:00
cmd := & cobra . Command {
Use : "drain NODE" ,
2017-01-25 01:00:32 +00:00
Short : i18n . T ( "Drain node in preparation for maintenance" ) ,
2015-10-31 00:16:57 +00:00
Long : drain_long ,
Example : drain_example ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
cmdutil . CheckErr ( options . SetupDrain ( cmd , args ) )
cmdutil . CheckErr ( options . RunDrain ( ) )
} ,
}
2016-11-22 22:51:03 +00:00
cmd . Flags ( ) . BoolVar ( & options . Force , "force" , false , "Continue even if there are pods not managed by a ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet." )
2016-01-27 18:27:14 +00:00
cmd . Flags ( ) . BoolVar ( & options . IgnoreDaemonsets , "ignore-daemonsets" , false , "Ignore DaemonSet-managed pods." )
2016-06-01 21:50:13 +00:00
cmd . Flags ( ) . BoolVar ( & options . DeleteLocalData , "delete-local-data" , false , "Continue even if there are pods using emptyDir (local data that will be deleted when the node is drained)." )
2015-10-31 00:16:57 +00:00
cmd . Flags ( ) . IntVar ( & options . GracePeriodSeconds , "grace-period" , - 1 , "Period of time in seconds given to each pod to terminate gracefully. If negative, the default value specified in the pod will be used." )
2016-11-29 03:18:01 +00:00
cmd . Flags ( ) . DurationVar ( & options . Timeout , "timeout" , 0 , "The length of time to wait before giving up, zero means infinite" )
2017-09-22 17:55:54 +00:00
cmd . Flags ( ) . StringVarP ( & options . Selector , "selector" , "l" , options . Selector , "Selector (label query) to filter on" )
2017-09-13 19:20:54 +00:00
cmdutil . AddDryRunFlag ( cmd )
2015-10-31 00:16:57 +00:00
return cmd
}
// SetupDrain populates some fields from the factory, grabs command line
// arguments and looks up the node using Builder
func ( o * DrainOptions ) SetupDrain ( cmd * cobra . Command , args [ ] string ) error {
var err error
2017-09-22 17:55:54 +00:00
o . Selector = cmdutil . GetFlagString ( cmd , "selector" )
if len ( args ) == 0 && ! cmd . Flags ( ) . Changed ( "selector" ) {
return cmdutil . UsageErrorf ( cmd , fmt . Sprintf ( "USAGE: %s [flags]" , cmd . Use ) )
}
if len ( args ) > 0 && len ( o . Selector ) > 0 {
return cmdutil . UsageErrorf ( cmd , "error: cannot specify both a node name and a --selector option" )
}
if len ( args ) > 0 && len ( args ) != 1 {
return cmdutil . UsageErrorf ( cmd , fmt . Sprintf ( "USAGE: %s [flags]" , cmd . Use ) )
2015-10-31 00:16:57 +00:00
}
2017-09-13 19:20:54 +00:00
o . DryRun = cmdutil . GetFlagBool ( cmd , "dry-run" )
2017-02-26 05:41:39 +00:00
if o . client , err = o . Factory . ClientSet ( ) ; err != nil {
2016-09-07 20:29:57 +00:00
return err
}
2017-02-26 05:41:39 +00:00
o . restClient , err = o . Factory . RESTClient ( )
2016-09-07 20:29:57 +00:00
if err != nil {
2015-10-31 00:16:57 +00:00
return err
}
2017-09-22 17:55:54 +00:00
o . nodeInfos = [ ] * resource . Info { }
2017-02-26 05:41:39 +00:00
o . mapper , o . typer = o . Factory . Object ( )
2015-10-31 00:16:57 +00:00
2017-02-26 05:41:39 +00:00
cmdNamespace , _ , err := o . Factory . DefaultNamespace ( )
2015-10-31 00:16:57 +00:00
if err != nil {
return err
}
2017-09-22 17:55:54 +00:00
nameArgs := [ ] string { "nodes" }
if len ( args ) > 0 {
nameArgs = append ( nameArgs , args [ 0 ] )
if strings . Contains ( args [ 0 ] , "/" ) {
nameArgs = [ ] string { args [ 0 ] }
}
}
2017-08-02 20:23:07 +00:00
r := o . Factory . NewBuilder ( ) .
2015-10-31 00:16:57 +00:00
NamespaceParam ( cmdNamespace ) . DefaultNamespace ( ) .
2017-09-22 17:55:54 +00:00
SelectorParam ( o . Selector ) .
ResourceTypeOrNameArgs ( true , nameArgs ... ) .
Flatten ( ) .
2015-10-31 00:16:57 +00:00
Do ( )
if err = r . Err ( ) ; err != nil {
return err
}
return r . Visit ( func ( info * resource . Info , err error ) error {
if err != nil {
return err
}
2017-09-22 17:55:54 +00:00
o . nodeInfos = append ( o . nodeInfos , info )
2015-10-31 00:16:57 +00:00
return nil
} )
}
// RunDrain runs the 'drain' command
func ( o * DrainOptions ) RunDrain ( ) error {
if err := o . RunCordonOrUncordon ( true ) ; err != nil {
return err
}
2017-09-22 17:55:54 +00:00
drainedNodes := sets . NewString ( )
var fatal error
for _ , info := range o . nodeInfos {
2017-09-13 19:20:54 +00:00
var err error
if ! o . DryRun {
err = o . deleteOrEvictPodsSimple ( info )
}
if err == nil || o . DryRun {
2017-09-22 17:55:54 +00:00
drainedNodes . Insert ( info . Name )
2017-09-13 19:20:54 +00:00
cmdutil . PrintSuccess ( o . mapper , false , o . Out , "node" , info . Name , o . DryRun , "drained" )
2017-09-22 17:55:54 +00:00
} else {
fmt . Fprintf ( o . ErrOut , "error: unable to drain node %q, aborting command...\n\n" , info . Name )
remainingNodes := [ ] string { }
fatal = err
for _ , remainingInfo := range o . nodeInfos {
if drainedNodes . Has ( remainingInfo . Name ) {
continue
}
remainingNodes = append ( remainingNodes , remainingInfo . Name )
}
if len ( remainingNodes ) > 0 {
fmt . Fprintf ( o . ErrOut , "There are pending nodes to be drained:\n" )
for _ , nodeName := range remainingNodes {
fmt . Fprintf ( o . ErrOut , " %s\n" , nodeName )
}
}
break
}
2016-10-20 16:43:48 +00:00
}
2017-09-22 17:55:54 +00:00
return fatal
2016-10-20 16:43:48 +00:00
}
2017-09-22 17:55:54 +00:00
func ( o * DrainOptions ) deleteOrEvictPodsSimple ( nodeInfo * resource . Info ) error {
pods , err := o . getPodsForDeletion ( nodeInfo )
2015-10-31 00:16:57 +00:00
if err != nil {
return err
}
2016-11-29 03:18:01 +00:00
2016-10-20 16:43:48 +00:00
err = o . deleteOrEvictPods ( pods )
if err != nil {
2017-09-22 17:55:54 +00:00
pendingPods , newErr := o . getPodsForDeletion ( nodeInfo )
2016-10-20 16:43:48 +00:00
if newErr != nil {
return newErr
}
2017-09-22 17:55:54 +00:00
fmt . Fprintf ( o . ErrOut , "There are pending pods in node %q when an error occurred: %v\n" , nodeInfo . Name , err )
2016-10-20 16:43:48 +00:00
for _ , pendingPod := range pendingPods {
2017-02-26 05:41:39 +00:00
fmt . Fprintf ( o . ErrOut , "%s/%s\n" , "pod" , pendingPod . Name )
2016-10-20 16:43:48 +00:00
}
2015-10-31 00:16:57 +00:00
}
2016-10-20 16:43:48 +00:00
return err
2015-10-31 00:16:57 +00:00
}
2017-06-13 23:46:34 +00:00
func ( o * DrainOptions ) getController ( namespace string , controllerRef * metav1 . OwnerReference ) ( interface { } , error ) {
switch controllerRef . Kind {
2016-06-01 21:50:13 +00:00
case "ReplicationController" :
2017-06-13 23:46:34 +00:00
return o . client . Core ( ) . ReplicationControllers ( namespace ) . Get ( controllerRef . Name , metav1 . GetOptions { } )
2016-06-01 21:50:13 +00:00
case "DaemonSet" :
2017-06-13 23:46:34 +00:00
return o . client . Extensions ( ) . DaemonSets ( namespace ) . Get ( controllerRef . Name , metav1 . GetOptions { } )
2016-06-01 21:50:13 +00:00
case "Job" :
2017-06-13 23:46:34 +00:00
return o . client . Batch ( ) . Jobs ( namespace ) . Get ( controllerRef . Name , metav1 . GetOptions { } )
2016-06-01 21:50:13 +00:00
case "ReplicaSet" :
2017-06-13 23:46:34 +00:00
return o . client . Extensions ( ) . ReplicaSets ( namespace ) . Get ( controllerRef . Name , metav1 . GetOptions { } )
2016-10-20 16:43:48 +00:00
case "StatefulSet" :
2017-06-13 23:46:34 +00:00
return o . client . Apps ( ) . StatefulSets ( namespace ) . Get ( controllerRef . Name , metav1 . GetOptions { } )
2016-06-01 21:50:13 +00:00
}
2017-06-13 23:46:34 +00:00
return nil , fmt . Errorf ( "Unknown controller kind %q" , controllerRef . Kind )
2016-06-01 21:50:13 +00:00
}
2017-06-13 23:46:34 +00:00
func ( o * DrainOptions ) getPodController ( pod api . Pod ) ( * metav1 . OwnerReference , error ) {
2017-08-02 09:41:33 +00:00
controllerRef := metav1 . GetControllerOf ( & pod )
2017-06-13 23:46:34 +00:00
if controllerRef == nil {
2016-06-01 21:50:13 +00:00
return nil , nil
}
2017-06-13 23:46:34 +00:00
2016-06-01 21:50:13 +00:00
// We assume the only reason for an error is because the controller is
2017-06-13 23:46:34 +00:00
// gone/missing, not for any other cause.
// TODO(mml): something more sophisticated than this
// TODO(juntee): determine if it's safe to remove getController(),
// so that drain can work for controller types that we don't know about
_ , err := o . getController ( pod . Namespace , controllerRef )
2015-10-31 00:16:57 +00:00
if err != nil {
2016-06-01 21:50:13 +00:00
return nil , err
2016-04-28 11:03:42 +00:00
}
2017-06-13 23:46:34 +00:00
return controllerRef , nil
2016-06-01 21:50:13 +00:00
}
2016-04-28 11:03:42 +00:00
2016-06-01 21:50:13 +00:00
func ( o * DrainOptions ) unreplicatedFilter ( pod api . Pod ) ( bool , * warning , * fatal ) {
2016-08-31 01:31:42 +00:00
// any finished pod can be removed
if pod . Status . Phase == api . PodSucceeded || pod . Status . Phase == api . PodFailed {
return true , nil , nil
}
2017-06-13 23:46:34 +00:00
controllerRef , err := o . getPodController ( pod )
2016-06-01 21:50:13 +00:00
if err != nil {
2017-02-22 01:07:42 +00:00
// if we're forcing, remove orphaned pods with a warning
if apierrors . IsNotFound ( err ) && o . Force {
return true , & warning { err . Error ( ) } , nil
}
2016-06-01 21:50:13 +00:00
return false , nil , & fatal { err . Error ( ) }
}
2017-06-13 23:46:34 +00:00
if controllerRef != nil {
2016-06-01 21:50:13 +00:00
return true , nil , nil
}
if ! o . Force {
return false , nil , & fatal { kUnmanagedFatal }
}
return true , & warning { kUnmanagedWarning } , nil
}
2016-04-28 11:03:42 +00:00
2016-06-01 21:50:13 +00:00
func ( o * DrainOptions ) daemonsetFilter ( pod api . Pod ) ( bool , * warning , * fatal ) {
2017-02-22 01:07:42 +00:00
// Note that we return false in cases where the pod is DaemonSet managed,
2016-06-01 21:50:13 +00:00
// regardless of flags. We never delete them, the only question is whether
// their presence constitutes an error.
2017-02-22 01:07:42 +00:00
//
// The exception is for pods that are orphaned (the referencing
// management resource - including DaemonSet - is not found).
// Such pods will be deleted if --force is used.
2017-06-13 23:46:34 +00:00
controllerRef , err := o . getPodController ( pod )
2016-06-01 21:50:13 +00:00
if err != nil {
2017-02-22 01:07:42 +00:00
// if we're forcing, remove orphaned pods with a warning
if apierrors . IsNotFound ( err ) && o . Force {
return true , & warning { err . Error ( ) } , nil
}
2016-06-01 21:50:13 +00:00
return false , nil , & fatal { err . Error ( ) }
}
2017-06-13 23:46:34 +00:00
if controllerRef == nil || controllerRef . Kind != "DaemonSet" {
2016-06-01 21:50:13 +00:00
return true , nil , nil
}
2017-06-13 23:46:34 +00:00
if _ , err := o . client . Extensions ( ) . DaemonSets ( pod . Namespace ) . Get ( controllerRef . Name , metav1 . GetOptions { } ) ; err != nil {
2016-06-01 21:50:13 +00:00
return false , nil , & fatal { err . Error ( ) }
2016-04-28 11:03:42 +00:00
}
2016-06-01 21:50:13 +00:00
if ! o . IgnoreDaemonsets {
return false , nil , & fatal { kDaemonsetFatal }
}
return false , & warning { kDaemonsetWarning } , nil
}
2016-04-28 11:03:42 +00:00
2016-06-01 21:50:13 +00:00
func mirrorPodFilter ( pod api . Pod ) ( bool , * warning , * fatal ) {
2017-07-06 03:23:30 +00:00
if _ , found := pod . ObjectMeta . Annotations [ corev1 . MirrorPodAnnotationKey ] ; found {
2016-06-01 21:50:13 +00:00
return false , nil , nil
2016-04-28 11:03:42 +00:00
}
2016-06-01 21:50:13 +00:00
return true , nil , nil
}
func hasLocalStorage ( pod api . Pod ) bool {
for _ , volume := range pod . Spec . Volumes {
if volume . EmptyDir != nil {
return true
}
2016-04-28 11:03:42 +00:00
}
2016-06-01 21:50:13 +00:00
return false
}
func ( o * DrainOptions ) localStorageFilter ( pod api . Pod ) ( bool , * warning , * fatal ) {
if ! hasLocalStorage ( pod ) {
return true , nil , nil
}
if ! o . DeleteLocalData {
return false , nil , & fatal { kLocalStorageFatal }
}
return true , & warning { kLocalStorageWarning } , nil
2016-04-28 11:03:42 +00:00
}
2016-06-01 21:50:13 +00:00
// Map of status message to a list of pod names having that status.
type podStatuses map [ string ] [ ] string
func ( ps podStatuses ) Message ( ) string {
msgs := [ ] string { }
for key , pods := range ps {
msgs = append ( msgs , fmt . Sprintf ( "%s: %s" , key , strings . Join ( pods , ", " ) ) )
}
return strings . Join ( msgs , "; " )
}
2016-04-28 11:03:42 +00:00
2017-09-22 17:55:54 +00:00
// getPodsForDeletion receives resource info for a node, and returns all the pods from the given node that we
// are planning on deleting. If there are any pods preventing us from deleting, we return that list in an error.
func ( o * DrainOptions ) getPodsForDeletion ( nodeInfo * resource . Info ) ( pods [ ] api . Pod , err error ) {
2017-01-22 03:36:02 +00:00
podList , err := o . client . Core ( ) . Pods ( metav1 . NamespaceAll ) . List ( metav1 . ListOptions {
2017-09-22 17:55:54 +00:00
FieldSelector : fields . SelectorFromSet ( fields . Set { "spec.nodeName" : nodeInfo . Name } ) . String ( ) } )
2016-04-28 11:03:42 +00:00
if err != nil {
2016-06-01 21:50:13 +00:00
return pods , err
2015-10-31 00:16:57 +00:00
}
2016-06-01 21:50:13 +00:00
ws := podStatuses { }
fs := podStatuses { }
2015-10-31 00:16:57 +00:00
for _ , pod := range podList . Items {
2016-06-01 21:50:13 +00:00
podOk := true
for _ , filt := range [ ] podFilter { mirrorPodFilter , o . localStorageFilter , o . unreplicatedFilter , o . daemonsetFilter } {
filterOk , w , f := filt ( pod )
podOk = podOk && filterOk
if w != nil {
ws [ w . string ] = append ( ws [ w . string ] , pod . Name )
2015-10-31 00:16:57 +00:00
}
2016-06-01 21:50:13 +00:00
if f != nil {
fs [ f . string ] = append ( fs [ f . string ] , pod . Name )
2015-10-31 00:16:57 +00:00
}
}
2016-06-01 21:50:13 +00:00
if podOk {
2016-01-27 18:27:14 +00:00
pods = append ( pods , pod )
2015-10-31 00:16:57 +00:00
}
}
2016-06-01 21:50:13 +00:00
if len ( fs ) > 0 {
return [ ] api . Pod { } , errors . New ( fs . Message ( ) )
2016-01-27 18:27:14 +00:00
}
2016-06-01 21:50:13 +00:00
if len ( ws ) > 0 {
2017-02-26 05:41:39 +00:00
fmt . Fprintf ( o . ErrOut , "WARNING: %s\n" , ws . Message ( ) )
2016-01-27 18:27:14 +00:00
}
2016-06-01 21:50:13 +00:00
return pods , nil
2016-01-27 18:27:14 +00:00
}
2016-10-20 16:43:48 +00:00
func ( o * DrainOptions ) deletePod ( pod api . Pod ) error {
2017-01-24 15:38:21 +00:00
deleteOptions := & metav1 . DeleteOptions { }
2015-10-31 00:16:57 +00:00
if o . GracePeriodSeconds >= 0 {
gracePeriodSeconds := int64 ( o . GracePeriodSeconds )
deleteOptions . GracePeriodSeconds = & gracePeriodSeconds
}
2016-10-20 16:43:48 +00:00
return o . client . Core ( ) . Pods ( pod . Namespace ) . Delete ( pod . Name , deleteOptions )
}
func ( o * DrainOptions ) evictPod ( pod api . Pod , policyGroupVersion string ) error {
2017-01-24 15:38:21 +00:00
deleteOptions := & metav1 . DeleteOptions { }
2016-10-20 16:43:48 +00:00
if o . GracePeriodSeconds >= 0 {
gracePeriodSeconds := int64 ( o . GracePeriodSeconds )
deleteOptions . GracePeriodSeconds = & gracePeriodSeconds
}
eviction := & policy . Eviction {
2016-12-03 18:57:26 +00:00
TypeMeta : metav1 . TypeMeta {
2016-10-20 16:43:48 +00:00
APIVersion : policyGroupVersion ,
Kind : EvictionKind ,
} ,
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-10-20 16:43:48 +00:00
Name : pod . Name ,
Namespace : pod . Namespace ,
} ,
DeleteOptions : deleteOptions ,
}
// Remember to change change the URL manipulation func when Evction's version change
return o . client . Policy ( ) . Evictions ( eviction . Namespace ) . Evict ( eviction )
}
// deleteOrEvictPods deletes or evicts the pods on the api server
func ( o * DrainOptions ) deleteOrEvictPods ( pods [ ] api . Pod ) error {
if len ( pods ) == 0 {
return nil
}
policyGroupVersion , err := SupportEviction ( o . client )
if err != nil {
return err
}
2015-10-31 00:16:57 +00:00
2016-11-29 03:18:01 +00:00
getPodFn := func ( namespace , name string ) ( * api . Pod , error ) {
2016-12-07 13:26:33 +00:00
return o . client . Core ( ) . Pods ( namespace ) . Get ( name , metav1 . GetOptions { } )
2016-11-29 03:18:01 +00:00
}
if len ( policyGroupVersion ) > 0 {
return o . evictPods ( pods , policyGroupVersion , getPodFn )
} else {
return o . deletePods ( pods , getPodFn )
}
}
func ( o * DrainOptions ) evictPods ( pods [ ] api . Pod , policyGroupVersion string , getPodFn func ( namespace , name string ) ( * api . Pod , error ) ) error {
doneCh := make ( chan bool , len ( pods ) )
errCh := make ( chan error , 1 )
2015-10-31 00:16:57 +00:00
for _ , pod := range pods {
2016-11-29 03:18:01 +00:00
go func ( pod api . Pod , doneCh chan bool , errCh chan error ) {
var err error
for {
err = o . evictPod ( pod , policyGroupVersion )
if err == nil {
break
2017-07-22 17:36:08 +00:00
} else if apierrors . IsNotFound ( err ) {
doneCh <- true
return
2016-11-29 03:18:01 +00:00
} else if apierrors . IsTooManyRequests ( err ) {
time . Sleep ( 5 * time . Second )
2017-07-22 17:36:08 +00:00
} else {
2016-11-29 03:18:01 +00:00
errCh <- fmt . Errorf ( "error when evicting pod %q: %v" , pod . Name , err )
return
}
}
podArray := [ ] api . Pod { pod }
_ , err = o . waitForDelete ( podArray , kubectl . Interval , time . Duration ( math . MaxInt64 ) , true , getPodFn )
if err == nil {
doneCh <- true
} else {
errCh <- fmt . Errorf ( "error when waiting for pod %q terminating: %v" , pod . Name , err )
}
} ( pod , doneCh , errCh )
}
doneCount := 0
// 0 timeout means infinite, we use MaxInt64 to represent it.
var globalTimeout time . Duration
if o . Timeout == 0 {
globalTimeout = time . Duration ( math . MaxInt64 )
} else {
globalTimeout = o . Timeout
}
for {
select {
case err := <- errCh :
2015-10-31 00:16:57 +00:00
return err
2016-11-29 03:18:01 +00:00
case <- doneCh :
doneCount ++
if doneCount == len ( pods ) {
return nil
}
case <- time . After ( globalTimeout ) :
return fmt . Errorf ( "Drain did not complete within %v" , globalTimeout )
2015-10-31 00:16:57 +00:00
}
}
2016-11-29 03:18:01 +00:00
}
2015-10-31 00:16:57 +00:00
2016-11-29 03:18:01 +00:00
func ( o * DrainOptions ) deletePods ( pods [ ] api . Pod , getPodFn func ( namespace , name string ) ( * api . Pod , error ) ) error {
// 0 timeout means infinite, we use MaxInt64 to represent it.
var globalTimeout time . Duration
if o . Timeout == 0 {
globalTimeout = time . Duration ( math . MaxInt64 )
} else {
globalTimeout = o . Timeout
}
for _ , pod := range pods {
err := o . deletePod ( pod )
2017-06-09 20:57:23 +00:00
if err != nil && ! apierrors . IsNotFound ( err ) {
2016-11-29 03:18:01 +00:00
return err
}
2016-10-18 23:00:54 +00:00
}
2016-11-29 03:18:01 +00:00
_ , err := o . waitForDelete ( pods , kubectl . Interval , globalTimeout , false , getPodFn )
2016-10-18 23:00:54 +00:00
return err
}
2016-11-29 03:18:01 +00:00
func ( o * DrainOptions ) waitForDelete ( pods [ ] api . Pod , interval , timeout time . Duration , usingEviction bool , getPodFn func ( string , string ) ( * api . Pod , error ) ) ( [ ] api . Pod , error ) {
var verbStr string
if usingEviction {
verbStr = "evicted"
} else {
verbStr = "deleted"
}
2016-10-18 23:00:54 +00:00
err := wait . PollImmediate ( interval , timeout , func ( ) ( bool , error ) {
pendingPods := [ ] api . Pod { }
2016-10-13 21:34:51 +00:00
for i , pod := range pods {
2016-10-18 23:00:54 +00:00
p , err := getPodFn ( pod . Namespace , pod . Name )
2016-10-13 21:34:51 +00:00
if apierrors . IsNotFound ( err ) || ( p != nil && p . ObjectMeta . UID != pod . ObjectMeta . UID ) {
2017-02-26 05:41:39 +00:00
cmdutil . PrintSuccess ( o . mapper , false , o . Out , "pod" , pod . Name , false , verbStr )
2016-10-13 21:34:51 +00:00
continue
} else if err != nil {
return false , err
} else {
2016-10-18 23:00:54 +00:00
pendingPods = append ( pendingPods , pods [ i ] )
2016-10-13 21:34:51 +00:00
}
}
2016-10-18 23:00:54 +00:00
pods = pendingPods
if len ( pendingPods ) > 0 {
2016-10-13 21:34:51 +00:00
return false , nil
}
return true , nil
} )
2016-10-18 23:00:54 +00:00
return pods , err
2015-10-31 00:16:57 +00:00
}
2016-10-20 16:43:48 +00:00
// SupportEviction uses Discovery API to find out if the server support eviction subresource
// If support, it will return its groupVersion; Otherwise, it will return ""
2017-02-16 11:46:15 +00:00
func SupportEviction ( clientset internalclientset . Interface ) ( string , error ) {
2016-10-20 16:43:48 +00:00
discoveryClient := clientset . Discovery ( )
groupList , err := discoveryClient . ServerGroups ( )
if err != nil {
return "" , err
}
foundPolicyGroup := false
var policyGroupVersion string
for _ , group := range groupList . Groups {
if group . Name == "policy" {
foundPolicyGroup = true
policyGroupVersion = group . PreferredVersion . GroupVersion
break
}
}
if ! foundPolicyGroup {
return "" , nil
}
resourceList , err := discoveryClient . ServerResourcesForGroupVersion ( "v1" )
if err != nil {
return "" , err
}
for _ , resource := range resourceList . APIResources {
if resource . Name == EvictionSubresource && resource . Kind == EvictionKind {
return policyGroupVersion , nil
}
}
return "" , nil
}
2015-10-31 00:16:57 +00:00
// RunCordonOrUncordon runs either Cordon or Uncordon. The desired value for
// "Unschedulable" is passed as the first arg.
func ( o * DrainOptions ) RunCordonOrUncordon ( desired bool ) error {
2017-02-26 05:41:39 +00:00
cmdNamespace , _ , err := o . Factory . DefaultNamespace ( )
2015-10-31 00:16:57 +00:00
if err != nil {
return err
}
2017-09-22 17:55:54 +00:00
cordonOrUncordon := "cordon"
if ! desired {
cordonOrUncordon = "un" + cordonOrUncordon
}
for _ , nodeInfo := range o . nodeInfos {
if nodeInfo . Mapping . GroupVersionKind . Kind == "Node" {
obj , err := nodeInfo . Mapping . ConvertToVersion ( nodeInfo . Object , nodeInfo . Mapping . GroupVersionKind . GroupVersion ( ) )
2017-08-08 05:38:31 +00:00
if err != nil {
2017-09-22 17:55:54 +00:00
fmt . Printf ( "error: unable to %s node %q: %v" , cordonOrUncordon , nodeInfo . Name , err )
continue
2017-08-08 05:38:31 +00:00
}
2017-09-22 17:55:54 +00:00
oldData , err := json . Marshal ( obj )
2017-06-30 12:58:57 +00:00
if err != nil {
2017-09-22 17:55:54 +00:00
fmt . Printf ( "error: unable to %s node %q: %v" , cordonOrUncordon , nodeInfo . Name , err )
continue
2016-11-04 06:16:22 +00:00
}
2017-09-22 17:55:54 +00:00
node , ok := obj . ( * corev1 . Node )
if ! ok {
fmt . Fprintf ( o . ErrOut , "error: unable to %s node %q: unexpected Type%T, expected Node" , cordonOrUncordon , nodeInfo . Name , obj )
continue
}
unsched := node . Spec . Unschedulable
if unsched == desired {
2017-09-13 19:20:54 +00:00
cmdutil . PrintSuccess ( o . mapper , false , o . Out , nodeInfo . Mapping . Resource , nodeInfo . Name , o . DryRun , already ( desired ) )
2017-09-22 17:55:54 +00:00
} else {
2017-09-13 19:20:54 +00:00
if ! o . DryRun {
helper := resource . NewHelper ( o . restClient , nodeInfo . Mapping )
node . Spec . Unschedulable = desired
newData , err := json . Marshal ( obj )
if err != nil {
fmt . Fprintf ( o . ErrOut , "error: unable to %s node %q: %v" , cordonOrUncordon , nodeInfo . Name , err )
continue
}
patchBytes , err := strategicpatch . CreateTwoWayMergePatch ( oldData , newData , obj )
if err != nil {
fmt . Printf ( "error: unable to %s node %q: %v" , cordonOrUncordon , nodeInfo . Name , err )
continue
}
_ , err = helper . Patch ( cmdNamespace , nodeInfo . Name , types . StrategicMergePatchType , patchBytes )
if err != nil {
fmt . Printf ( "error: unable to %s node %q: %v" , cordonOrUncordon , nodeInfo . Name , err )
continue
}
2017-09-22 17:55:54 +00:00
}
2017-09-13 19:20:54 +00:00
cmdutil . PrintSuccess ( o . mapper , false , o . Out , nodeInfo . Mapping . Resource , nodeInfo . Name , o . DryRun , changed ( desired ) )
2015-10-31 00:16:57 +00:00
}
2017-09-22 17:55:54 +00:00
} else {
2017-09-13 19:20:54 +00:00
cmdutil . PrintSuccess ( o . mapper , false , o . Out , nodeInfo . Mapping . Resource , nodeInfo . Name , o . DryRun , "skipped" )
2015-10-31 00:16:57 +00:00
}
}
return nil
}
// already() and changed() return suitable strings for {un,}cordoning
func already ( desired bool ) string {
if desired {
return "already cordoned"
}
return "already uncordoned"
}
func changed ( desired bool ) string {
if desired {
return "cordoned"
}
return "uncordoned"
}