forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancelbuild.go
291 lines (252 loc) · 8.93 KB
/
cancelbuild.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package cmd
import (
"errors"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/spf13/cobra"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildclient "github.com/openshift/origin/pkg/build/client"
buildinternal "github.com/openshift/origin/pkg/build/client/internalversion"
buildinternalclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
buildtypedclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion"
buildlister "github.com/openshift/origin/pkg/build/generated/listers/build/internalversion"
buildutil "github.com/openshift/origin/pkg/build/util"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
// CancelBuildRecommendedCommandName is the recommended command name.
const CancelBuildRecommendedCommandName = "cancel-build"
var (
cancelBuildLong = templates.LongDesc(`
Cancel running, pending, or new builds
This command requests a graceful shutdown of the build. There may be a delay between requesting
the build and the time the build is terminated.`)
cancelBuildExample = templates.Examples(`
# Cancel the build with the given name
%[1]s %[2]s ruby-build-2
# Cancel the named build and print the build logs
%[1]s %[2]s ruby-build-2 --dump-logs
# Cancel the named build and create a new one with the same parameters
%[1]s %[2]s ruby-build-2 --restart
# Cancel multiple builds
%[1]s %[2]s ruby-build-1 ruby-build-2 ruby-build-3
# Cancel all builds created from 'ruby-build' build configuration that are in 'new' state
%[1]s %[2]s bc/ruby-build --state=new`)
)
// CancelBuildOptions contains all the options for running the CancelBuild cli command.
type CancelBuildOptions struct {
In io.Reader
Out, ErrOut io.Writer
DumpLogs bool
Restart bool
States []string
Namespace string
BuildNames []string
HasError bool
ReportError func(error)
Mapper meta.RESTMapper
Client buildinternalclient.Interface
BuildClient buildtypedclient.BuildResourceInterface
BuildLister buildlister.BuildLister
// timeout is used by unit tests to shorten the polling period
timeout time.Duration
}
// NewCmdCancelBuild implements the OpenShift cli cancel-build command
func NewCmdCancelBuild(name, baseName string, f *clientcmd.Factory, in io.Reader, out, errout io.Writer) *cobra.Command {
o := &CancelBuildOptions{}
cmd := &cobra.Command{
Use: fmt.Sprintf("%s (BUILD | BUILDCONFIG)", name),
Short: "Cancel running, pending, or new builds",
Long: cancelBuildLong,
Example: fmt.Sprintf(cancelBuildExample, baseName, name),
SuggestFor: []string{"builds", "stop-build"},
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(f, cmd, args, in, out, errout))
kcmdutil.CheckErr(o.RunCancelBuild())
},
}
cmd.Flags().StringSliceVar(&o.States, "state", o.States, "Only cancel builds in this state")
cmd.Flags().BoolVar(&o.DumpLogs, "dump-logs", o.DumpLogs, "Specify if the build logs for the cancelled build should be shown.")
cmd.Flags().BoolVar(&o.Restart, "restart", o.Restart, "Specify if a new build should be created after the current build is cancelled.")
return cmd
}
// Complete completes all the required options.
func (o *CancelBuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string, in io.Reader, out, errout io.Writer) error {
o.In = in
o.Out = out
o.ErrOut = errout
o.ReportError = func(err error) {
o.HasError = true
fmt.Fprintf(o.ErrOut, "error: %s\n", err.Error())
}
if o.timeout.Seconds() == 0 {
o.timeout = 30 * time.Second
}
if len(args) == 0 {
return kcmdutil.UsageError(cmd, "Must pass a name of a build or a buildconfig to cancel")
}
namespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
if len(o.States) == 0 {
// If --state is not specified, set the default to "new", "pending" and
// "running".
o.States = []string{"new", "pending", "running"}
} else {
for _, state := range o.States {
if len(state) > 0 && !isStateCancellable(state) {
return kcmdutil.UsageError(cmd, "The '--state' flag has invalid value. Must be one of 'new', 'pending', or 'running'")
}
}
}
config, err := f.BareClientConfig()
if err != nil {
return err
}
client, err := buildinternalclient.NewForConfig(config)
if err != nil {
return err
}
o.Namespace = namespace
o.Client = client
o.BuildLister = buildclient.NewClientBuildLister(client)
o.BuildClient = client.Build().Builds(namespace)
o.Mapper, _ = f.Object()
for _, item := range args {
resource, name, err := cmdutil.ResolveResource(buildapi.Resource("builds"), item, o.Mapper)
if err != nil {
return err
}
switch {
case buildapi.IsResourceOrLegacy("buildconfigs", resource):
list, err := buildutil.BuildConfigBuilds(o.BuildLister, o.Namespace, name, nil)
if err != nil {
return err
}
for _, b := range list {
o.BuildNames = append(o.BuildNames, b.Name)
}
case buildapi.IsResourceOrLegacy("builds", resource):
o.BuildNames = append(o.BuildNames, strings.TrimSpace(name))
default:
return fmt.Errorf("invalid resource provided: %v", resource)
}
}
return nil
}
// RunCancelBuild implements all the necessary functionality for CancelBuild.
func (o *CancelBuildOptions) RunCancelBuild() error {
var builds []*buildapi.Build
for _, name := range o.BuildNames {
build, err := o.BuildClient.Get(name, metav1.GetOptions{})
if err != nil {
o.ReportError(fmt.Errorf("build %s/%s not found", o.Namespace, name))
continue
}
stateMatch := false
for _, state := range o.States {
if strings.ToLower(string(build.Status.Phase)) == state {
stateMatch = true
break
}
}
if stateMatch && !buildutil.IsBuildComplete(build) {
builds = append(builds, build)
}
}
if o.DumpLogs {
for _, b := range builds {
// Do not attempt to get logs from build that was not scheduled.
if b.Status.Phase == buildapi.BuildPhaseNew {
continue
}
logClient := buildinternal.NewBuildLogClient(o.Client.Build().RESTClient(), o.Namespace)
opts := buildapi.BuildLogOptions{NoWait: true}
response, err := logClient.Logs(b.Name, opts).Do().Raw()
if err != nil {
o.ReportError(fmt.Errorf("unable to fetch logs for %s/%s: %v", b.Namespace, b.Name, err))
continue
}
fmt.Fprintf(o.Out, "==== Build %s/%s logs ====\n", b.Namespace, b.Name)
fmt.Fprint(o.Out, string(response))
}
}
var wg sync.WaitGroup
for _, b := range builds {
wg.Add(1)
go func(build *buildapi.Build) {
defer wg.Done()
err := wait.Poll(500*time.Millisecond, o.timeout, func() (bool, error) {
build.Status.Cancelled = true
_, err := o.BuildClient.Update(build)
switch {
case err == nil:
return true, nil
case kapierrors.IsConflict(err):
build, err = o.BuildClient.Get(build.Name, metav1.GetOptions{})
return false, err
}
return true, err
})
if err != nil {
o.ReportError(fmt.Errorf("build %s/%s failed to update: %v", build.Namespace, build.Name, err))
return
}
// Make sure the build phase is really cancelled.
err = wait.Poll(500*time.Millisecond, o.timeout, func() (bool, error) {
updatedBuild, err := o.BuildClient.Get(build.Name, metav1.GetOptions{})
if err != nil {
return true, err
}
return updatedBuild.Status.Phase == buildapi.BuildPhaseCancelled, nil
})
if err != nil {
o.ReportError(fmt.Errorf("build %s/%s failed to cancel: %v", build.Namespace, build.Name, err))
return
}
resource, name, _ := cmdutil.ResolveResource(buildapi.Resource("builds"), build.Name, o.Mapper)
kcmdutil.PrintSuccess(o.Mapper, false, o.Out, resource.Resource, name, false, "cancelled")
}(b)
}
wg.Wait()
if o.Restart {
for _, b := range builds {
request := &buildapi.BuildRequest{ObjectMeta: metav1.ObjectMeta{Namespace: b.Namespace, Name: b.Name}}
build, err := o.BuildClient.Clone(request.Name, request)
if err != nil {
o.ReportError(fmt.Errorf("build %s/%s failed to restart: %v", b.Namespace, b.Name, err))
continue
}
resource, name, _ := cmdutil.ResolveResource(buildapi.Resource("builds"), build.Name, o.Mapper)
kcmdutil.PrintSuccess(o.Mapper, false, o.Out, resource.Resource, name, false, fmt.Sprintf("restarted build %q", b.Name))
}
}
if o.HasError {
return errors.New("failure during the build cancellation")
}
return nil
}
// isStateCancellable validates the state provided by the '--state' flag.
func isStateCancellable(state string) bool {
cancellablePhases := []string{
string(buildapi.BuildPhaseNew),
string(buildapi.BuildPhasePending),
string(buildapi.BuildPhaseRunning),
}
for _, p := range cancellablePhases {
if state == strings.ToLower(p) {
return true
}
}
return false
}