forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
retry.go
192 lines (169 loc) · 7.04 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package rollout
import (
"errors"
"fmt"
"io"
"strings"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/kubectl/resource"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
appsutil "github.com/openshift/origin/pkg/apps/util"
"github.com/openshift/origin/pkg/oc/cli/cmd/set"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/api/errors"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
type RetryOptions struct {
Mapper meta.RESTMapper
Typer runtime.ObjectTyper
Encoder runtime.Encoder
Infos []*resource.Info
Out io.Writer
FilenameOptions resource.FilenameOptions
Clientset kclientset.Interface
}
var (
rolloutRetryLong = templates.LongDesc(`
If a rollout fails, you may opt to retry it (if the error was transient). Some rollouts may
never successfully complete - in which case you can use the rollout latest to force a redeployment.
If a deployment config has completed rolling out successfully at least once in the past, it would be
automatically rolled back in the event of a new failed rollout. Note that you would still need
to update the erroneous deployment config in order to have its template persisted across your
application.
`)
rolloutRetryExample = templates.Examples(`
# Retry the latest failed deployment based on 'frontend'
# The deployer pod and any hook pods are deleted for the latest failed deployment
%[1]s rollout retry dc/frontend
`)
)
func NewCmdRolloutRetry(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
opts := &RetryOptions{}
cmd := &cobra.Command{
Use: "retry (TYPE NAME | TYPE/NAME) [flags]",
Long: rolloutRetryLong,
Example: fmt.Sprintf(rolloutRetryExample, fullName),
Short: "Retry the latest failed rollout",
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(opts.Complete(f, cmd, out, args))
kcmdutil.CheckErr(opts.Run())
},
}
usage := "Filename, directory, or URL to a file identifying the resource to get from a server."
kcmdutil.AddFilenameOptionFlags(cmd, &opts.FilenameOptions, usage)
return cmd
}
func (o *RetryOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, out io.Writer, args []string) error {
if len(args) == 0 && len(o.FilenameOptions.Filenames) == 0 {
return kcmdutil.UsageErrorf(cmd, cmd.Use)
}
o.Mapper, o.Typer = f.Object()
o.Encoder = kcmdutil.InternalVersionJSONEncoder()
o.Out = out
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
o.Clientset, err = f.ClientSet()
if err != nil {
return err
}
r := f.NewBuilder().
Internal().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, &o.FilenameOptions).
ResourceTypeOrNameArgs(true, args...).
ContinueOnError().
Latest().
Flatten().
Do()
err = r.Err()
if err != nil {
return err
}
o.Infos, err = r.Infos()
return err
}
func (o RetryOptions) Run() error {
allErrs := []error{}
mapping, err := o.Mapper.RESTMapping(kapi.Kind("ReplicationController"))
if err != nil {
return err
}
for _, info := range o.Infos {
config, ok := info.Object.(*appsapi.DeploymentConfig)
if !ok {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, fmt.Errorf("expected deployment configuration, got %T", info.Object)))
continue
}
if config.Spec.Paused {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, fmt.Errorf("unable to retry paused deployment config %q", config.Name)))
continue
}
if config.Status.LatestVersion == 0 {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, fmt.Errorf("no rollouts found for %q", config.Name)))
continue
}
latestDeploymentName := appsutil.LatestDeploymentNameForConfig(config)
rc, err := o.Clientset.Core().ReplicationControllers(config.Namespace).Get(latestDeploymentName, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, fmt.Errorf("unable to find the latest rollout (#%d).\nYou can start a new rollout with 'oc rollout latest dc/%s'.", config.Status.LatestVersion, config.Name)))
continue
}
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, fmt.Errorf("unable to fetch replication controller %q", config.Name)))
continue
}
if !appsutil.IsFailedDeployment(rc) {
message := fmt.Sprintf("rollout #%d is %s; only failed deployments can be retried.\n", config.Status.LatestVersion, strings.ToLower(string(appsutil.DeploymentStatusFor(rc))))
if appsutil.IsCompleteDeployment(rc) {
message += fmt.Sprintf("You can start a new deployment with 'oc rollout latest dc/%s'.", config.Name)
} else {
message += fmt.Sprintf("Optionally, you can cancel this deployment with 'oc rollout cancel dc/%s'.", config.Name)
}
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, errors.New(message)))
continue
}
// Delete the deployer pod as well as the deployment hooks pods, if any
pods, err := o.Clientset.Core().Pods(config.Namespace).List(metav1.ListOptions{LabelSelector: appsutil.DeployerPodSelector(latestDeploymentName).String()})
if err != nil {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, fmt.Errorf("failed to list deployer/hook pods for deployment #%d: %v", config.Status.LatestVersion, err)))
continue
}
hasError := false
for _, pod := range pods.Items {
err := o.Clientset.Core().Pods(pod.Namespace).Delete(pod.Name, metav1.NewDeleteOptions(0))
if err != nil {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, fmt.Errorf("failed to delete deployer/hook pod %s for deployment #%d: %v", pod.Name, config.Status.LatestVersion, err)))
hasError = true
}
}
if hasError {
continue
}
patches := set.CalculatePatches([]*resource.Info{{Object: rc, Mapping: mapping}}, o.Encoder, func(info *resource.Info) (bool, error) {
rc.Annotations[appsapi.DeploymentStatusAnnotation] = string(appsapi.DeploymentStatusNew)
delete(rc.Annotations, appsapi.DeploymentStatusReasonAnnotation)
delete(rc.Annotations, appsapi.DeploymentCancelledAnnotation)
return true, nil
})
if len(patches) == 0 {
kcmdutil.PrintSuccess(false, o.Out, info.Object, false, "already retried")
continue
}
if _, err := o.Clientset.Core().ReplicationControllers(rc.Namespace).Patch(rc.Name, types.StrategicMergePatchType, patches[0].Patch); err != nil {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("retrying", info.Source, err))
continue
}
kcmdutil.PrintSuccess(false, o.Out, info.Object, false, fmt.Sprintf("retried rollout #%d", config.Status.LatestVersion))
}
return utilerrors.NewAggregate(allErrs)
}