forked from argoproj/argo-rollouts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add abort functionality (argoproj#224)
- Loading branch information
1 parent
773f933
commit 37bc1f7
Showing
22 changed files
with
684 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package abort | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
types "k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options" | ||
) | ||
|
||
const ( | ||
example = ` | ||
# Abort a rollout | ||
%[1]s abort guestbook | ||
` | ||
) | ||
|
||
const ( | ||
abortPatch = `{"status":{"abort":true}}` | ||
) | ||
|
||
// NewCmdAbort returns a new instance of an `rollouts abort` command | ||
func NewCmdAbort(o *options.ArgoRolloutsOptions) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "abort ROLLOUT", | ||
Short: "Abort a rollout", | ||
Example: o.Example(example), | ||
SilenceUsage: true, | ||
RunE: func(c *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return o.UsageErr(c) | ||
} | ||
ns := o.Namespace() | ||
rolloutIf := o.RolloutsClientset().ArgoprojV1alpha1().Rollouts(ns) | ||
for _, name := range args { | ||
ro, err := rolloutIf.Patch(name, types.MergePatchType, []byte(abortPatch)) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(o.Out, "rollout '%s' aborted\n", ro.Name) | ||
} | ||
return nil | ||
}, | ||
} | ||
o.AddKubectlFlags(cmd) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package abort | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
kubetesting "k8s.io/client-go/testing" | ||
|
||
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" | ||
fakeroclient "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/fake" | ||
options "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options/fake" | ||
) | ||
|
||
func TestAbortCmdUsage(t *testing.T) { | ||
tf, o := options.NewFakeArgoRolloutsOptions() | ||
defer tf.Cleanup() | ||
cmd := NewCmdAbort(o) | ||
cmd.PersistentPreRunE = o.PersistentPreRunE | ||
cmd.SetArgs([]string{}) | ||
err := cmd.Execute() | ||
assert.Error(t, err) | ||
stdout := o.Out.(*bytes.Buffer).String() | ||
stderr := o.ErrOut.(*bytes.Buffer).String() | ||
assert.Empty(t, stdout) | ||
assert.Contains(t, stderr, "Usage:") | ||
assert.Contains(t, stderr, "abort ROLLOUT") | ||
} | ||
|
||
func TestAbortCmd(t *testing.T) { | ||
ro := v1alpha1.Rollout{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "guestbook", | ||
Namespace: "test", | ||
}, | ||
} | ||
|
||
tf, o := options.NewFakeArgoRolloutsOptions(&ro) | ||
defer tf.Cleanup() | ||
fakeClient := o.RolloutsClient.(*fakeroclient.Clientset) | ||
fakeClient.ReactionChain = nil | ||
fakeClient.AddReactor("patch", "*", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) { | ||
if patchAction, ok := action.(kubetesting.PatchAction); ok { | ||
if string(patchAction.GetPatch()) == abortPatch { | ||
ro.Status.Abort = true | ||
} | ||
} | ||
return true, &ro, nil | ||
}) | ||
|
||
cmd := NewCmdAbort(o) | ||
cmd.PersistentPreRunE = o.PersistentPreRunE | ||
cmd.SetArgs([]string{"guestbook", "-n", "test"}) | ||
err := cmd.Execute() | ||
assert.Nil(t, err) | ||
|
||
assert.True(t, ro.Status.Abort) | ||
stdout := o.Out.(*bytes.Buffer).String() | ||
stderr := o.ErrOut.(*bytes.Buffer).String() | ||
assert.Equal(t, stdout, "rollout 'guestbook' aborted\n") | ||
assert.Empty(t, stderr) | ||
} | ||
|
||
func TestAbortCmdError(t *testing.T) { | ||
tf, o := options.NewFakeArgoRolloutsOptions(&v1alpha1.Rollout{}) | ||
defer tf.Cleanup() | ||
cmd := NewCmdAbort(o) | ||
cmd.PersistentPreRunE = o.PersistentPreRunE | ||
cmd.SetArgs([]string{"doesnotexist", "-n", "test"}) | ||
err := cmd.Execute() | ||
assert.Error(t, err) | ||
stdout := o.Out.(*bytes.Buffer).String() | ||
stderr := o.ErrOut.(*bytes.Buffer).String() | ||
assert.Empty(t, stdout) | ||
assert.Equal(t, "Error: rollouts.argoproj.io \"doesnotexist\" not found\n", stderr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package retry | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
types "k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options" | ||
) | ||
|
||
const ( | ||
example = ` | ||
# Retry a rollout | ||
%[1]s retry guestbook | ||
` | ||
) | ||
|
||
const ( | ||
retryPatch = `{"status":{"abort":false}}` | ||
) | ||
|
||
// NewCmdRetry returns a new instance of an `rollouts retry` command | ||
func NewCmdRetry(o *options.ArgoRolloutsOptions) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "retry ROLLOUT", | ||
Short: "Retry a rollout", | ||
Example: o.Example(example), | ||
SilenceUsage: true, | ||
RunE: func(c *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return o.UsageErr(c) | ||
} | ||
ns := o.Namespace() | ||
rolloutIf := o.RolloutsClientset().ArgoprojV1alpha1().Rollouts(ns) | ||
for _, name := range args { | ||
ro, err := rolloutIf.Patch(name, types.MergePatchType, []byte(retryPatch)) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(o.Out, "rollout '%s' retried\n", ro.Name) | ||
} | ||
return nil | ||
}, | ||
} | ||
o.AddKubectlFlags(cmd) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package retry | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
kubetesting "k8s.io/client-go/testing" | ||
|
||
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" | ||
fakeroclient "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/fake" | ||
options "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options/fake" | ||
) | ||
|
||
func TestRetryCmdUsage(t *testing.T) { | ||
tf, o := options.NewFakeArgoRolloutsOptions() | ||
defer tf.Cleanup() | ||
cmd := NewCmdRetry(o) | ||
cmd.PersistentPreRunE = o.PersistentPreRunE | ||
cmd.SetArgs([]string{}) | ||
err := cmd.Execute() | ||
assert.Error(t, err) | ||
stdout := o.Out.(*bytes.Buffer).String() | ||
stderr := o.ErrOut.(*bytes.Buffer).String() | ||
assert.Empty(t, stdout) | ||
assert.Contains(t, stderr, "Usage:") | ||
assert.Contains(t, stderr, "retry ROLLOUT") | ||
} | ||
|
||
func TestRetryCmd(t *testing.T) { | ||
ro := v1alpha1.Rollout{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "guestbook", | ||
Namespace: "test", | ||
}, | ||
} | ||
|
||
tf, o := options.NewFakeArgoRolloutsOptions(&ro) | ||
defer tf.Cleanup() | ||
fakeClient := o.RolloutsClient.(*fakeroclient.Clientset) | ||
fakeClient.ReactionChain = nil | ||
fakeClient.AddReactor("patch", "*", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) { | ||
if patchAction, ok := action.(kubetesting.PatchAction); ok { | ||
if string(patchAction.GetPatch()) == retryPatch { | ||
ro.Status.Abort = true | ||
} | ||
} | ||
return true, &ro, nil | ||
}) | ||
|
||
cmd := NewCmdRetry(o) | ||
cmd.PersistentPreRunE = o.PersistentPreRunE | ||
cmd.SetArgs([]string{"guestbook", "-n", "test"}) | ||
err := cmd.Execute() | ||
assert.Nil(t, err) | ||
|
||
assert.True(t, ro.Status.Abort) | ||
stdout := o.Out.(*bytes.Buffer).String() | ||
stderr := o.ErrOut.(*bytes.Buffer).String() | ||
assert.Equal(t, stdout, "rollout 'guestbook' retried\n") | ||
assert.Empty(t, stderr) | ||
} | ||
|
||
func TestRetryCmdError(t *testing.T) { | ||
tf, o := options.NewFakeArgoRolloutsOptions(&v1alpha1.Rollout{}) | ||
defer tf.Cleanup() | ||
cmd := NewCmdRetry(o) | ||
cmd.PersistentPreRunE = o.PersistentPreRunE | ||
cmd.SetArgs([]string{"doesnotexist", "-n", "test"}) | ||
err := cmd.Execute() | ||
assert.Error(t, err) | ||
stdout := o.Out.(*bytes.Buffer).String() | ||
stderr := o.ErrOut.(*bytes.Buffer).String() | ||
assert.Empty(t, stdout) | ||
assert.Equal(t, "Error: rollouts.argoproj.io \"doesnotexist\" not found\n", stderr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.