-
Notifications
You must be signed in to change notification settings - Fork 840
/
retry.go
129 lines (116 loc) · 4.19 KB
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package retry
import (
"context"
"fmt"
"github.com/spf13/cobra"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
clientset "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned/typed/rollouts/v1alpha1"
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options"
completionutil "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/util/completion"
)
const (
retryRolloutPatch = `{"status":{"abort":false}}`
retryExperimentPatch = `{"status":null}`
)
const (
retryExample = `
# Retry an aborted rollout
%[1]s retry rollout guestbook
# Retry a failed experiment
%[1]s retry experiment my-experiment`
retryRolloutExample = `
# Retry an aborted rollout
%[1]s retry rollout guestbook`
retryExperimentExample = `
# Retry an experiment
%[1]s retry experiment my-experiment`
)
// NewCmdRetry returns a new instance of an `argo rollouts retry` command
func NewCmdRetry(o *options.ArgoRolloutsOptions) *cobra.Command {
var cmd = &cobra.Command{
Use: "retry <rollout|experiment> RESOURCE_NAME",
Short: "Retry a rollout or experiment",
Long: "This command consists of multiple subcommands which can be used to restart an aborted rollout or a failed experiment.",
Example: o.Example(retryExample),
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
return o.UsageErr(c)
},
}
cmd.AddCommand(NewCmdRetryRollout(o))
cmd.AddCommand(NewCmdRetryExperiment(o))
return cmd
}
// NewCmdRetryRollout returns a new instance of an `argo rollouts retry rollout` command
func NewCmdRetryRollout(o *options.ArgoRolloutsOptions) *cobra.Command {
var cmd = &cobra.Command{
Use: "rollout ROLLOUT_NAME",
Aliases: []string{"ro", "rollouts"},
Short: "Retry an aborted rollout",
Example: o.Example(retryRolloutExample),
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 := RetryRollout(rolloutIf, name)
if err != nil {
return err
}
fmt.Fprintf(o.Out, "rollout '%s' retried\n", ro.Name)
}
return nil
},
ValidArgsFunction: completionutil.RolloutNameCompletionFunc(o),
}
return cmd
}
// RetryRollout retries a rollout after it's been aborted
func RetryRollout(rolloutIf clientset.RolloutInterface, name string) (*v1alpha1.Rollout, error) {
ctx := context.TODO()
// attempt using status subresource, first
ro, err := rolloutIf.Patch(ctx, name, types.MergePatchType, []byte(retryRolloutPatch), metav1.PatchOptions{}, "status")
if err != nil && k8serrors.IsNotFound(err) {
ro, err = rolloutIf.Patch(ctx, name, types.MergePatchType, []byte(retryRolloutPatch), metav1.PatchOptions{})
}
return ro, err
}
// NewCmdRetryExperiment returns a new instance of an `argo rollouts retry experiment` command
func NewCmdRetryExperiment(o *options.ArgoRolloutsOptions) *cobra.Command {
var cmd = &cobra.Command{
Use: "experiment EXPERIMENT_NAME",
Aliases: []string{"exp", "experiments"},
Short: "Retry an experiment",
Long: "Retry a failed experiment.",
Example: o.Example(retryExperimentExample),
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
if len(args) == 0 {
return o.UsageErr(c)
}
ns := o.Namespace()
experimentIf := o.RolloutsClientset().ArgoprojV1alpha1().Experiments(ns)
for _, name := range args {
ex, err := RetryExperiment(experimentIf, name)
if err != nil {
return err
}
fmt.Fprintf(o.Out, "experiment '%s' retried\n", ex.Name)
}
return nil
},
ValidArgsFunction: completionutil.ExperimentNameCompletionFunc(o),
}
return cmd
}
// RetryExperiment retries an experiment after it's been aborted
func RetryExperiment(experimentIf clientset.ExperimentInterface, name string) (*v1alpha1.Experiment, error) {
ctx := context.TODO()
return experimentIf.Patch(ctx, name, types.MergePatchType, []byte(retryExperimentPatch), metav1.PatchOptions{})
}