Skip to content

Commit

Permalink
Merge pull request #18 from grosser/grosser/retry
Browse files Browse the repository at this point in the history
add retry option to keep evicting until all initial pods are gone
  • Loading branch information
rajatjindal authored Aug 12, 2024
2 parents a7ca0cc + d18b011 commit cbdb44a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This plugin evicts pods:

- evict single pod: `kubectl evict-pod <pod-name> -n <namespace>`
- evict multiple pods: `kubectl evict-pod -n <namespace> -l app=foo`
- evict multiple pods until every one is gone: `kubectl evict-pod -n <namespace> -l app=foo --retry`
- show options: `kubectl evict-pod -h`

## Install
Expand Down
41 changes: 35 additions & 6 deletions pkg/cmd/evict_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"time"
)

// Version is set during build time
Expand All @@ -26,6 +27,7 @@ type EvictPodOptions struct {
printVersion bool
labelSelector string
fieldSelector string
withRetry bool
}

// NewEvictPodOptions provides an instance of EvictPodOptions with default values
Expand Down Expand Up @@ -65,8 +67,9 @@ func NewEvictCmd(streams genericclioptions.IOStreams) *cobra.Command {
}

cmd.Flags().BoolVar(&o.printVersion, "version", false, "prints version of plugin")
cmd.Flags().StringVarP(&o.labelSelector, "labelSelector", "l", "", "labelSelector to evict pods with")
cmd.Flags().StringVar(&o.labelSelector, "fieldSelector", "", "fieldSelector to evict pods with")
cmd.Flags().StringVarP(&o.labelSelector, "label-selector", "l", "", "label selector to evict pods with")
cmd.Flags().StringVar(&o.labelSelector, "field-selector", "", "field selector to evict pods with")
cmd.Flags().BoolVar(&o.withRetry, "retry", false, "retry eviction until it succeeds")
o.configFlags.AddFlags(cmd.Flags())

return cmd
Expand Down Expand Up @@ -115,15 +118,41 @@ func (o *EvictPodOptions) Run() error {
}
}

for _, podName := range o.podNames {
return o.retryEachElement(o.podNames, func(podName string) error {
err := k8s.Evict(o.kubeclient, podName, o.namespace)
if err != nil {
return err
logrus.Warnf("pod %s in namespace %s evicting failed", podName, o.namespace)
} else {
logrus.Infof("pod %s in namespace %s evicted successfully", podName, o.namespace)
}
return err
})
}

logrus.Infof("pod %s in namespace %s evicted successfully", podName, o.namespace)
func (o *EvictPodOptions) retryEachElement(elements []string, fn func(string) error) error {
if o.withRetry { // retry until every element passes
var failed []string
for {
for _, podName := range elements {
if err := fn(podName); err != nil {
failed = append(failed, podName)
}
}
if len(failed) == 0 {
return nil
} else {
elements = failed
failed = nil
time.Sleep(5 * time.Second)
}
}
} else { // just iterate once
for _, element := range elements {
if err := fn(element); err != nil {
return err
}
}
}

return nil
}

Expand Down

0 comments on commit cbdb44a

Please sign in to comment.