/
cancelbuild.go
337 lines (291 loc) · 10.1 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package cancelbuild
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"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/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericclioptions/printers"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/util/templates"
"github.com/openshift/api/build"
buildv1 "github.com/openshift/api/build/v1"
buildclientset "github.com/openshift/client-go/build/clientset/versioned"
buildtv1client "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
buildlisterv1 "github.com/openshift/client-go/build/listers/build/v1"
buildclientinternal "github.com/openshift/origin/pkg/build/client"
buildclientv1 "github.com/openshift/origin/pkg/build/client/v1"
buildutil "github.com/openshift/origin/pkg/build/util"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
)
// 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 {
DumpLogs bool
Restart bool
States []string
Namespace string
BuildNames []string
HasError bool
ReportError func(error)
PrinterCancel printers.ResourcePrinter
PrinterCancelInProgress printers.ResourcePrinter
PrinterRestart printers.ResourcePrinter
Mapper meta.RESTMapper
Client buildtv1client.BuildV1Interface
BuildClient buildtv1client.BuildInterface
BuildLister buildlisterv1.BuildLister
// timeout is used by unit tests to shorten the polling period
timeout time.Duration
genericclioptions.IOStreams
}
func NewCancelBuildOptions(streams genericclioptions.IOStreams) *CancelBuildOptions {
return &CancelBuildOptions{
IOStreams: streams,
States: []string{"new", "pending", "running"},
}
}
// NewCmdCancelBuild implements the OpenShift cli cancel-build command
func NewCmdCancelBuild(name, baseName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := NewCancelBuildOptions(streams)
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))
kcmdutil.CheckErr(o.Validate())
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 kcmdutil.Factory, cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("build or a buildconfig name is required")
}
o.ReportError = func(err error) {
o.HasError = true
fmt.Fprintf(o.ErrOut, "error: %s\n", err.Error())
}
var err error
o.PrinterCancel, err = printers.NewTypeSetter(scheme.Scheme).
WrapToPrinter(&printers.NamePrinter{Operation: "cancelled"}, nil)
if err != nil {
return err
}
o.PrinterRestart, err = printers.NewTypeSetter(scheme.Scheme).
WrapToPrinter(&printers.NamePrinter{Operation: "restarted"}, nil)
if err != nil {
return err
}
o.PrinterCancelInProgress, err = printers.NewTypeSetter(scheme.Scheme).
WrapToPrinter(&printers.NamePrinter{Operation: "marked for cancellation, waiting to be cancelled"}, nil)
if err != nil {
return err
}
if o.timeout.Seconds() == 0 {
o.timeout = 30 * time.Second
}
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
config, err := f.ToRESTConfig()
if err != nil {
return err
}
o.Client, err = buildtv1client.NewForConfig(config)
if err != nil {
return err
}
internalclient, err := buildclientset.NewForConfig(config)
if err != nil {
return err
}
o.BuildLister = buildclientinternal.NewClientBuildLister(internalclient.BuildV1())
o.BuildClient = o.Client.Builds(o.Namespace)
o.Mapper, err = f.ToRESTMapper()
if err != nil {
return err
}
for _, item := range args {
resource, name, err := cmdutil.ResolveResource(build.Resource("builds"), item, o.Mapper)
if err != nil {
return err
}
switch resource {
case build.Resource("buildconfigs"):
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 build.Resource("builds"):
o.BuildNames = append(o.BuildNames, strings.TrimSpace(name))
default:
return fmt.Errorf("invalid resource provided: %v", resource)
}
}
return nil
}
func (o *CancelBuildOptions) Validate() error {
for _, state := range o.States {
if len(state) > 0 && !isStateCancellable(state) {
return fmt.Errorf("invalid --state flag value, must be one of 'new', 'pending', or 'running'")
}
}
return nil
}
// RunCancelBuild implements all the necessary functionality for CancelBuild.
func (o *CancelBuildOptions) RunCancelBuild() error {
var builds []*buildv1.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.IsTerminalPhase(build.Status.Phase) {
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 == buildv1.BuildPhaseNew {
continue
}
logClient := buildclientv1.NewBuildLogClient(o.Client.RESTClient(), o.Namespace)
opts := buildv1.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 *buildv1.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
}
// ignore exit if error here; the phase verfication below is more important
o.PrinterCancelInProgress.PrintObj(build, o.Out)
// Make sure the build phase is really cancelled.
timeout := o.timeout
if build.Spec.Strategy.JenkinsPipelineStrategy != nil {
//bump the timeout in case we have to wait for Jenkins
//to come up so that the sync plugin can actually change
//the phase
timeout = timeout + (3 * time.Minute)
}
err = wait.Poll(500*time.Millisecond, timeout, func() (bool, error) {
updatedBuild, err := o.BuildClient.Get(build.Name, metav1.GetOptions{})
if err != nil {
return true, err
}
return updatedBuild.Status.Phase == buildv1.BuildPhaseCancelled, nil
})
if err != nil {
o.ReportError(fmt.Errorf("build %s/%s failed to cancel: %v", build.Namespace, build.Name, err))
return
}
if err := o.PrinterCancel.PrintObj(build, o.Out); err != nil {
o.ReportError(fmt.Errorf("build %s/%s failed to print: %v", build.Namespace, build.Name, err))
return
}
}(b)
}
wg.Wait()
if o.Restart {
for _, b := range builds {
request := &buildv1.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
}
if err := o.PrinterRestart.PrintObj(b, o.Out); err != nil {
o.ReportError(fmt.Errorf("build %s/%s failed to print: %v", build.Namespace, build.Name, err))
continue
}
}
}
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(buildv1.BuildPhaseNew),
string(buildv1.BuildPhasePending),
string(buildv1.BuildPhaseRunning),
}
for _, p := range cancellablePhases {
if state == strings.ToLower(p) {
return true
}
}
return false
}