-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexec.go
375 lines (307 loc) · 10.9 KB
/
exec.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/go-vela/sdk-go/vela"
api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/queue/models"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime"
"github.com/go-vela/worker/version"
)
// exec is a helper function to poll the queue
// and execute Vela pipelines for the Worker.
//
//nolint:nilerr,funlen // ignore returning nil - don't want to crash worker
func (w *Worker) exec(index int, config *api.Worker) error {
var err error
// setup the version
v := version.New()
var (
execBuildClient *vela.Client
execBuildExecutable *library.BuildExecutable
p *pipeline.Build
item *models.Item
retries = 3
)
for i := 0; i < retries; i++ {
// check if we're on the first iteration of the loop
if i > 0 {
// incrementally sleep in between retries
time.Sleep(time.Duration(i*10) * time.Second)
}
logrus.Debugf("queue item prep - attempt %d", i+1)
// get worker from database
worker, _, err := w.VelaClient.Worker.Get(w.Config.API.Address.Hostname())
if err != nil {
logrus.Errorf("unable to retrieve worker from server: %s", err)
if i < retries-1 {
logrus.WithError(err).Warningf("retrying #%d", i+1)
// continue to the next iteration of the loop
continue
}
return err
}
// capture an item from the queue only on first loop iteration (failures here return nil)
if i == 0 {
item, err = w.Queue.Pop(context.Background(), worker.GetRoutes())
if err != nil {
logrus.Errorf("queue pop failed: %v", err)
// returning immediately on queue pop fail will attempt
// to pop in quick succession, so we honor the configured timeout
time.Sleep(w.Config.Queue.Timeout)
// returning nil to avoid unregistering the worker on pop failure;
// sometimes queue could be unavailable due to blip or maintenance
return nil
}
if item == nil {
return nil
}
}
// retrieve a build token from the server to setup the execBuildClient
bt, resp, err := w.VelaClient.Build.GetBuildToken(item.Build.GetRepo().GetOrg(), item.Build.GetRepo().GetName(), item.Build.GetNumber())
if err != nil {
logrus.Errorf("unable to retrieve build token: %s", err)
// build is not in pending state — user canceled build while it was in queue. Pop, discard, move on.
if resp != nil && resp.StatusCode == http.StatusConflict {
return nil
}
// check if the retry limit has been exceeded
if i < retries-1 {
logrus.WithError(err).Warningf("retrying #%d", i+1)
// continue to the next iteration of the loop
continue
}
return err
}
// set up build client with build token as auth
execBuildClient, err = setupClient(w.Config.Server, bt.GetToken())
if err != nil {
// check if the retry limit has been exceeded
if i < retries-1 {
logrus.WithError(err).Warningf("retrying #%d", i+1)
// continue to the next iteration of the loop
continue
}
return err
}
// request build executable containing pipeline.Build data using exec client
execBuildExecutable, _, err = execBuildClient.Build.GetBuildExecutable(item.Build.GetRepo().GetOrg(), item.Build.GetRepo().GetName(), item.Build.GetNumber())
if err != nil {
// check if the retry limit has been exceeded
if i < retries-1 {
logrus.WithError(err).Warningf("retrying #%d", i+1)
// continue to the next iteration of the loop
continue
}
return err
}
// get the build pipeline from the build executable
p = new(pipeline.Build)
err = json.Unmarshal(execBuildExecutable.GetData(), p)
if err != nil {
return err
}
break
}
// set the outputs container ID
w.Config.Executor.OutputCtn.ID = fmt.Sprintf("outputs_%s", p.ID)
// create logger with extra metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus#WithFields
logger := logrus.WithFields(logrus.Fields{
"build": item.Build.GetNumber(),
"executor": w.Config.Executor.Driver,
"host": w.Config.API.Address.Hostname(),
"repo": item.Build.GetRepo().GetFullName(),
"runtime": w.Config.Runtime.Driver,
"user": item.Build.GetRepo().GetOwner().GetName(),
"version": v.Semantic(),
})
// lock and append the build to the list
w.RunningBuildsMutex.Lock()
w.RunningBuilds = append(w.RunningBuilds, item.Build)
config.SetRunningBuilds(w.RunningBuilds)
w.RunningBuildsMutex.Unlock()
// set worker status
updateStatus := w.getWorkerStatusFromConfig(config)
config.SetStatus(updateStatus)
config.SetLastStatusUpdateAt(time.Now().Unix())
config.SetLastBuildStartedAt(time.Now().Unix())
// update worker in the database
_, _, err = w.VelaClient.Worker.Update(config.GetHostname(), config)
if err != nil {
logger.Errorf("unable to update worker: %v", err)
}
// handle stale item queued before a Vela upgrade or downgrade.
if item.ItemVersion != models.ItemVersion {
// If the ItemVersion is older or newer than what we expect, then it might
// not be safe to process the build. Fail the build and loop to the next item.
// TODO: Ask the server to re-compile and requeue the build instead of failing it.
logrus.Errorf("Failing stale queued build due to wrong item version: want %d, got %d", types.ItemVersion, item.ItemVersion)
build := item.Build
build.SetError("Unable to process stale build (queued before Vela upgrade/downgrade).")
build.SetStatus(constants.StatusError)
build.SetFinished(time.Now().UTC().Unix())
_, _, err := execBuildClient.Build.Update(build)
if err != nil {
logrus.Errorf("Unable to set build status to %s: %s", constants.StatusFailure, err)
return err
}
return nil
}
// setup the runtime
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime#New
w.Runtime, err = runtime.New(&runtime.Setup{
Logger: logger,
Mock: w.Config.Mock,
Driver: w.Config.Runtime.Driver,
ConfigFile: w.Config.Runtime.ConfigFile,
HostVolumes: w.Config.Runtime.HostVolumes,
Namespace: w.Config.Runtime.Namespace,
PodsTemplateName: w.Config.Runtime.PodsTemplateName,
PodsTemplateFile: w.Config.Runtime.PodsTemplateFile,
PrivilegedImages: w.Config.Runtime.PrivilegedImages,
DropCapabilities: w.Config.Runtime.DropCapabilities,
})
if err != nil {
return err
}
// setup the executor
//
// https://pkg.go.dev/github.com/go-vela/worker/executor#New
_executor, err := executor.New(&executor.Setup{
Logger: logger,
Mock: w.Config.Mock,
Driver: w.Config.Executor.Driver,
MaxLogSize: w.Config.Executor.MaxLogSize,
LogStreamingTimeout: w.Config.Executor.LogStreamingTimeout,
EnforceTrustedRepos: w.Config.Executor.EnforceTrustedRepos,
PrivilegedImages: w.Config.Runtime.PrivilegedImages,
Client: execBuildClient,
Hostname: w.Config.API.Address.Hostname(),
Runtime: w.Runtime,
Build: item.Build,
Pipeline: p.Sanitize(w.Config.Runtime.Driver),
Version: v.Semantic(),
OutputCtn: w.Config.Executor.OutputCtn,
})
// add the executor to the worker
w.Executors[index] = _executor
// This WaitGroup delays calling DestroyBuild until the StreamBuild goroutine finishes.
var wg sync.WaitGroup
// this gets deferred first so that DestroyBuild runs AFTER the
// new contexts (buildCtx and timeoutCtx) have been canceled
defer func() {
// if exec() exits before starting StreamBuild, this returns immediately.
wg.Wait()
logger.Info("destroying build")
// destroy the build with the executor (pass a background
// context to guarantee all build resources are destroyed).
err = _executor.DestroyBuild(context.Background())
if err != nil {
logger.Errorf("unable to destroy build: %v", err)
}
logger.Info("completed build")
// lock and remove the build from the list
w.RunningBuildsMutex.Lock()
for i, v := range w.RunningBuilds {
if v.GetID() == item.Build.GetID() {
w.RunningBuilds = append(w.RunningBuilds[:i], w.RunningBuilds[i+1:]...)
}
}
config.SetRunningBuilds(w.RunningBuilds)
w.RunningBuildsMutex.Unlock()
// set worker status
updateStatus := w.getWorkerStatusFromConfig(config)
config.SetStatus(updateStatus)
config.SetLastStatusUpdateAt(time.Now().Unix())
config.SetLastBuildFinishedAt(time.Now().Unix())
// update worker in the database
_, _, err := w.VelaClient.Worker.Update(config.GetHostname(), config)
if err != nil {
logger.Errorf("unable to update worker: %v", err)
}
}()
// capture the configured build timeout
t := w.Config.Build.Timeout
// check if the repository has a custom timeout
if item.Build.GetRepo().GetTimeout() > 0 {
// update timeout variable to repository custom timeout
t = time.Duration(item.Build.GetRepo().GetTimeout()) * time.Minute
}
// create a build context (from a background context
// so that other builds can't inadvertently cancel this build)
buildCtx, done := context.WithCancel(context.Background())
defer done()
// add to the background context with a timeout
// built in for ensuring a build doesn't run forever
timeoutCtx, timeout := context.WithTimeout(buildCtx, t)
defer timeout()
logger.Info("creating build")
// create the build with the executor
err = _executor.CreateBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to create build: %v", err)
return nil
}
logger.Info("planning build")
// plan the build with the executor
err = _executor.PlanBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to plan build: %v", err)
return nil
}
logger.Info("assembling build")
// assemble the build with the executor
err = _executor.AssembleBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to assemble build: %v", err)
return nil
}
// add StreamBuild goroutine to WaitGroup
wg.Add(1)
// log/event streaming uses buildCtx so that it is not subject to the timeout.
go func() {
defer wg.Done()
logger.Info("streaming build logs")
// execute the build with the executor
err = _executor.StreamBuild(buildCtx)
if err != nil {
logger.Errorf("unable to stream build logs: %v", err)
}
}()
logger.Info("executing build")
// execute the build with the executor
err = _executor.ExecBuild(timeoutCtx)
if err != nil {
logger.Errorf("unable to execute build: %v", err)
return nil
}
return nil
}
// getWorkerStatusFromConfig is a helper function
// to determine the appropriate worker status.
func (w *Worker) getWorkerStatusFromConfig(config *api.Worker) string {
switch rb := len(config.GetRunningBuilds()); {
case rb == 0:
return constants.WorkerStatusIdle
case rb < w.Config.Build.Limit:
return constants.WorkerStatusAvailable
case rb == w.Config.Build.Limit:
return constants.WorkerStatusBusy
default:
return constants.WorkerStatusError
}
}