Merge pull request #9030 from brendandburns/kubectl
Add a custom timeout flag for stop/delete.
This commit is contained in:
@@ -19,6 +19,7 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -76,6 +77,7 @@ func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful delete.")
|
||||
cmd.Flags().Bool("cascade", true, "If true, cascade the delete resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.")
|
||||
cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.")
|
||||
cmd.Flags().Duration("timeout", 0, "The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -102,12 +104,12 @@ func RunDelete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
||||
ignoreNotFound := cmdutil.GetFlagBool(cmd, "ignore-not-found")
|
||||
// By default use a reaper to delete all related resources.
|
||||
if cmdutil.GetFlagBool(cmd, "cascade") {
|
||||
return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, cmdutil.GetFlagInt(cmd, "grace-period"))
|
||||
return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, cmdutil.GetFlagDuration(cmd, "timeout"), cmdutil.GetFlagInt(cmd, "grace-period"))
|
||||
}
|
||||
return DeleteResult(r, out, ignoreNotFound)
|
||||
}
|
||||
|
||||
func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, gracePeriod int) error {
|
||||
func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, timeout time.Duration, gracePeriod int) error {
|
||||
found := 0
|
||||
if ignoreNotFound {
|
||||
r = r.IgnoreErrors(errors.IsNotFound)
|
||||
@@ -126,7 +128,7 @@ func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefault
|
||||
if gracePeriod >= 0 {
|
||||
options = api.NewDeleteOptions(int64(gracePeriod))
|
||||
}
|
||||
if _, err := reaper.Stop(info.Namespace, info.Name, options); err != nil {
|
||||
if _, err := reaper.Stop(info.Namespace, info.Name, timeout, options); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s/%s\n", info.Mapping.Resource, info.Name)
|
||||
|
@@ -63,6 +63,7 @@ func NewCmdStop(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources.")
|
||||
cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful stop.")
|
||||
cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.")
|
||||
cmd.Flags().Duration("timeout", 0, "The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -84,5 +85,5 @@ func RunStop(f *cmdutil.Factory, cmd *cobra.Command, args []string, filenames ut
|
||||
if r.Err() != nil {
|
||||
return r.Err()
|
||||
}
|
||||
return ReapResult(r, f, out, false, cmdutil.GetFlagBool(cmd, "ignore-not-found"), cmdutil.GetFlagInt(cmd, "grace-period"))
|
||||
return ReapResult(r, f, out, false, cmdutil.GetFlagBool(cmd, "ignore-not-found"), cmdutil.GetFlagDuration(cmd, "timeout"), cmdutil.GetFlagInt(cmd, "grace-period"))
|
||||
}
|
||||
|
@@ -26,13 +26,15 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Interval = time.Millisecond * 100
|
||||
Interval = time.Second * 1
|
||||
Timeout = time.Minute * 5
|
||||
)
|
||||
|
||||
// A Reaper handles terminating an object as gracefully as possible.
|
||||
// timeout is how long we'll wait for the termination to be successful
|
||||
// gracePeriod is time given to an API object for it to delete itself cleanly (e.g. pod shutdown)
|
||||
type Reaper interface {
|
||||
Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error)
|
||||
Stop(namespace, name string, timeout time.Duration, gracePeriod *api.DeleteOptions) (string, error)
|
||||
}
|
||||
|
||||
type NoSuchReaperError struct {
|
||||
@@ -80,14 +82,21 @@ type objInterface interface {
|
||||
Get(name string) (meta.Interface, error)
|
||||
}
|
||||
|
||||
func (reaper *ReplicationControllerReaper) Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error) {
|
||||
func (reaper *ReplicationControllerReaper) Stop(namespace, name string, timeout time.Duration, gracePeriod *api.DeleteOptions) (string, error) {
|
||||
rc := reaper.ReplicationControllers(namespace)
|
||||
scaler, err := ScalerFor("ReplicationController", NewScalerClient(*reaper))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if timeout == 0 {
|
||||
rc, err := rc.Get(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
timeout = Timeout + time.Duration(10*rc.Spec.Replicas)*time.Second
|
||||
}
|
||||
retry := NewRetryParams(reaper.pollInterval, reaper.timeout)
|
||||
waitForReplicas := NewRetryParams(reaper.pollInterval, reaper.timeout)
|
||||
waitForReplicas := NewRetryParams(reaper.pollInterval, timeout)
|
||||
if err = scaler.Scale(namespace, name, 0, nil, retry, waitForReplicas); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -97,7 +106,7 @@ func (reaper *ReplicationControllerReaper) Stop(namespace, name string, gracePer
|
||||
return fmt.Sprintf("%s stopped", name), nil
|
||||
}
|
||||
|
||||
func (reaper *PodReaper) Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error) {
|
||||
func (reaper *PodReaper) Stop(namespace, name string, timeout time.Duration, gracePeriod *api.DeleteOptions) (string, error) {
|
||||
pods := reaper.Pods(namespace)
|
||||
_, err := pods.Get(name)
|
||||
if err != nil {
|
||||
@@ -110,7 +119,7 @@ func (reaper *PodReaper) Stop(namespace, name string, gracePeriod *api.DeleteOpt
|
||||
return fmt.Sprintf("%s stopped", name), nil
|
||||
}
|
||||
|
||||
func (reaper *ServiceReaper) Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error) {
|
||||
func (reaper *ServiceReaper) Stop(namespace, name string, timeout time.Duration, gracePeriod *api.DeleteOptions) (string, error) {
|
||||
services := reaper.Services(namespace)
|
||||
_, err := services.Get(name)
|
||||
if err != nil {
|
||||
|
@@ -34,7 +34,7 @@ func TestReplicationControllerStop(t *testing.T) {
|
||||
})
|
||||
reaper := ReplicationControllerReaper{fake, time.Millisecond, time.Millisecond}
|
||||
name := "foo"
|
||||
s, err := reaper.Stop("default", name, nil)
|
||||
s, err := reaper.Stop("default", name, 0, nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -42,10 +42,10 @@ func TestReplicationControllerStop(t *testing.T) {
|
||||
if s != expected {
|
||||
t.Errorf("expected %s, got %s", expected, s)
|
||||
}
|
||||
if len(fake.Actions) != 4 {
|
||||
t.Errorf("unexpected actions: %v, expected 4 actions (get, update, get, delete)", fake.Actions)
|
||||
if len(fake.Actions) != 5 {
|
||||
t.Errorf("unexpected actions: %v, expected 4 actions (get, get, update, get, delete)", fake.Actions)
|
||||
}
|
||||
for i, action := range []string{"get", "update", "get", "delete"} {
|
||||
for i, action := range []string{"get", "get", "update", "get", "delete"} {
|
||||
if fake.Actions[i].Action != action+"-replicationController" {
|
||||
t.Errorf("unexpected action: %v, expected %s-replicationController", fake.Actions[i], action)
|
||||
}
|
||||
@@ -142,7 +142,7 @@ func TestSimpleStop(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v (%s)", err, test.test)
|
||||
}
|
||||
s, err := reaper.Stop("default", "foo", nil)
|
||||
s, err := reaper.Stop("default", "foo", 0, nil)
|
||||
if err != nil && !test.expectError {
|
||||
t.Errorf("unexpected error: %v (%s)", err, test.test)
|
||||
}
|
||||
|
Reference in New Issue
Block a user