This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cancel.go
211 lines (183 loc) · 6.77 KB
/
cancel.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package rollout
import (
"fmt"
"io"
"sort"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/kubectl/resource"
units "github.com/docker/go-units"
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"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
kapi "k8s.io/kubernetes/pkg/apis/core"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
type CancelOptions struct {
Mapper meta.RESTMapper
Typer runtime.ObjectTyper
Encoder runtime.Encoder
Infos []*resource.Info
Out io.Writer
FilenameOptions resource.FilenameOptions
Clientset kclientset.Interface
}
var (
rolloutCancelLong = templates.LongDesc(`
Cancel the in-progress deployment
Running this command will cause the current in-progress deployment to be
cancelled, but keep in mind that this is a best-effort operation and may take
some time to complete. It’s possible the deployment will partially or totally
complete before the cancellation is effective. In such a case an appropriate
event will be emitted.`)
rolloutCancelExample = templates.Examples(`
# Cancel the in-progress deployment based on 'nginx'
%[1]s rollout cancel dc/nginx`)
)
func NewCmdRolloutCancel(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
opts := &CancelOptions{}
cmd := &cobra.Command{
Use: "cancel (TYPE NAME | TYPE/NAME) [flags]",
Long: rolloutCancelLong,
Example: fmt.Sprintf(rolloutCancelExample, fullName),
Short: "cancel the in-progress deployment",
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 *CancelOptions) 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 = f.JSONEncoder()
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 CancelOptions) Run() error {
allErrs := []error{}
for _, info := range o.Infos {
config, ok := info.Object.(*appsapi.DeploymentConfig)
if !ok {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("cancelling", info.Source, fmt.Errorf("expected deployment configuration, got %s", info.Mapping.Resource)))
continue
}
if config.Spec.Paused {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("cancelling", info.Source, fmt.Errorf("unable to cancel paused deployment %s/%s", config.Namespace, config.Name)))
}
mapping, err := o.Mapper.RESTMapping(kapi.Kind("ReplicationController"))
if err != nil {
return err
}
mutateFn := func(rc *kapi.ReplicationController) bool {
if appsutil.IsDeploymentCancelled(rc) {
kcmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, false, "already cancelled")
return false
}
patches := set.CalculatePatches([]*resource.Info{{Object: rc, Mapping: mapping}}, o.Encoder, func(info *resource.Info) (bool, error) {
rc.Annotations[appsapi.DeploymentCancelledAnnotation] = appsapi.DeploymentCancelledAnnotationValue
rc.Annotations[appsapi.DeploymentStatusReasonAnnotation] = appsapi.DeploymentCancelledByUser
return true, nil
})
allPatchesEmpty := true
for _, patch := range patches {
if len(patch.Patch) > 0 {
allPatchesEmpty = false
break
}
}
if allPatchesEmpty {
kcmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, false, "already cancelled")
return false
}
_, err := o.Clientset.Core().ReplicationControllers(rc.Namespace).Patch(rc.Name, types.StrategicMergePatchType, patches[0].Patch)
if err != nil {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("cancelling", info.Source, err))
return false
}
kcmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, false, "cancelling")
return true
}
deployments, cancelled, err := o.forEachControllerInConfig(config.Namespace, config.Name, mutateFn)
if err != nil {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("cancelling", info.Source, err))
continue
}
if !cancelled {
latest := deployments[0]
maybeCancelling := ""
if appsutil.IsDeploymentCancelled(latest) && !appsutil.IsTerminatedDeployment(latest) {
maybeCancelling = " (cancelling)"
}
timeAt := strings.ToLower(units.HumanDuration(time.Now().Sub(latest.CreationTimestamp.Time)))
fmt.Fprintf(o.Out, "No rollout is in progress (latest rollout #%d %s%s %s ago)\n",
appsutil.DeploymentVersionFor(latest),
strings.ToLower(string(appsutil.DeploymentStatusFor(latest))),
maybeCancelling,
timeAt)
}
}
return utilerrors.NewAggregate(allErrs)
}
func (o CancelOptions) forEachControllerInConfig(namespace, name string, mutateFunc func(*kapi.ReplicationController) bool) ([]*kapi.ReplicationController, bool, error) {
deploymentList, err := o.Clientset.Core().ReplicationControllers(namespace).List(metav1.ListOptions{LabelSelector: appsutil.ConfigSelector(name).String()})
if err != nil {
return nil, false, err
}
if len(deploymentList.Items) == 0 {
return nil, false, fmt.Errorf("there have been no replication controllers for %s/%s\n", namespace, name)
}
deployments := make([]*kapi.ReplicationController, 0, len(deploymentList.Items))
for i := range deploymentList.Items {
deployments = append(deployments, &deploymentList.Items[i])
}
sort.Sort(appsutil.ByLatestVersionDesc(deployments))
allErrs := []error{}
cancelled := false
for _, deployment := range deployments {
status := appsutil.DeploymentStatusFor(deployment)
switch status {
case appsapi.DeploymentStatusNew,
appsapi.DeploymentStatusPending,
appsapi.DeploymentStatusRunning:
cancelled = mutateFunc(deployment)
}
}
return deployments, cancelled, utilerrors.NewAggregate(allErrs)
}