forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cancel.go
202 lines (174 loc) · 6.63 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
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"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
deployapi "github.com/openshift/origin/pkg/deploy/apis/apps"
deployutil "github.com/openshift/origin/pkg/deploy/util"
"github.com/spf13/cobra"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
kapi "k8s.io/kubernetes/pkg/api"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/kubectl/cmd/set"
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.UsageError(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(true).
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.(*deployapi.DeploymentConfig)
if !ok {
allErrs = append(allErrs, kcmdutil.AddSourceToErr("cancelling", info.Source, fmt.Errorf("expected deployment configuration, got %T", info.Object)))
}
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 deployutil.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(*resource.Info) ([]byte, error) {
rc.Annotations[deployapi.DeploymentCancelledAnnotation] = deployapi.DeploymentCancelledAnnotationValue
rc.Annotations[deployapi.DeploymentStatusReasonAnnotation] = deployapi.DeploymentCancelledByUser
return runtime.Encode(o.Encoder, rc)
})
if len(patches) == 0 {
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 deployutil.IsDeploymentCancelled(latest) && !deployutil.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",
deployutil.DeploymentVersionFor(latest),
strings.ToLower(string(deployutil.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: deployutil.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(deployutil.ByLatestVersionDesc(deployments))
allErrs := []error{}
cancelled := false
for _, deployment := range deployments {
status := deployutil.DeploymentStatusFor(deployment)
switch status {
case deployapi.DeploymentStatusNew,
deployapi.DeploymentStatusPending,
deployapi.DeploymentStatusRunning:
cancelled = mutateFunc(deployment)
}
}
return deployments, cancelled, utilerrors.NewAggregate(allErrs)
}