forked from fluxcd/flux
/
daemon.go
700 lines (635 loc) · 20.1 KB
/
daemon.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
package daemon
import (
"bytes"
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/api"
"github.com/weaveworks/flux/api/v10"
"github.com/weaveworks/flux/api/v6"
"github.com/weaveworks/flux/api/v9"
"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/event"
"github.com/weaveworks/flux/git"
"github.com/weaveworks/flux/guid"
"github.com/weaveworks/flux/image"
"github.com/weaveworks/flux/job"
"github.com/weaveworks/flux/policy"
"github.com/weaveworks/flux/registry"
"github.com/weaveworks/flux/release"
"github.com/weaveworks/flux/resource"
"github.com/weaveworks/flux/update"
)
const (
// This is set to be in sympathy with the request / RPC timeout (i.e., empirically)
defaultHandlerTimeout = 10 * time.Second
// A job can take an arbitrary amount of time but we want to have
// a (generous) threshold for considering a job stuck and
// abandoning it
defaultJobTimeout = 60 * time.Second
)
// Daemon is the fully-functional state of a daemon (compare to
// `NotReadyDaemon`).
type Daemon struct {
V string
Cluster cluster.Cluster
Manifests cluster.Manifests
Registry registry.Registry
ImageRefresh chan image.Name
Repo *git.Repo
GitConfig git.Config
Jobs *job.Queue
JobStatusCache *job.StatusCache
EventWriter event.EventWriter
Logger log.Logger
// bookkeeping
*LoopVars
}
// Invariant.
var _ api.Server = &Daemon{}
func (d *Daemon) Version(ctx context.Context) (string, error) {
return d.V, nil
}
func (d *Daemon) Ping(ctx context.Context) error {
return d.Cluster.Ping()
}
func (d *Daemon) Export(ctx context.Context) ([]byte, error) {
return d.Cluster.Export()
}
func (d *Daemon) getResources(ctx context.Context) (map[string]resource.Resource, v6.ReadOnlyReason, error) {
var resources map[string]resource.Resource
var globalReadOnly v6.ReadOnlyReason
err := d.WithClone(ctx, func(checkout *git.Checkout) error {
var err error
resources, err = d.Manifests.LoadManifests(checkout.Dir(), checkout.ManifestDirs())
return err
})
// The reason something is missing from the map differs depending
// on the state of the git repo.
_, notReady := err.(git.NotReadyError)
switch {
case notReady:
globalReadOnly = v6.ReadOnlyNotReady
case err == git.ErrNoConfig:
globalReadOnly = v6.ReadOnlyNoRepo
case err != nil:
return nil, globalReadOnly, manifestLoadError(err)
default:
globalReadOnly = v6.ReadOnlyMissing
}
return resources, globalReadOnly, nil
}
func (d *Daemon) ListServices(ctx context.Context, namespace string) ([]v6.ControllerStatus, error) {
clusterServices, err := d.Cluster.AllControllers(namespace)
if err != nil {
return nil, errors.Wrap(err, "getting services from cluster")
}
resources, missingReason, err := d.getResources(ctx)
if err != nil {
return nil, err
}
var res []v6.ControllerStatus
for _, service := range clusterServices {
readOnly := v6.ReadOnlyOK
var policies policy.Set
if resource, ok := resources[service.ID.String()]; ok {
policies = resource.Policy()
}
switch {
case policies == nil:
readOnly = missingReason
case service.IsSystem:
readOnly = v6.ReadOnlySystem
}
res = append(res, v6.ControllerStatus{
ID: service.ID,
Containers: containers2containers(service.ContainersOrNil()),
ReadOnly: readOnly,
Status: service.Status,
Antecedent: service.Antecedent,
Labels: service.Labels,
Automated: policies.Has(policy.Automated),
Locked: policies.Has(policy.Locked),
Ignore: policies.Has(policy.Ignore),
Policies: policies.ToStringMap(),
})
}
return res, nil
}
type clusterContainers []cluster.Controller
func (cs clusterContainers) Len() int {
return len(cs)
}
func (cs clusterContainers) Containers(i int) []resource.Container {
return cs[i].ContainersOrNil()
}
// ListImages - deprecated from v10, lists the images available for set of services
func (d *Daemon) ListImages(ctx context.Context, spec update.ResourceSpec) ([]v6.ImageStatus, error) {
return d.ListImagesWithOptions(ctx, v10.ListImagesOptions{Spec: spec})
}
// ListImagesWithOptions lists the images available for set of services
func (d *Daemon) ListImagesWithOptions(ctx context.Context, opts v10.ListImagesOptions) ([]v6.ImageStatus, error) {
var services []cluster.Controller
var err error
if opts.Spec == update.ResourceSpecAll {
services, err = d.Cluster.AllControllers("")
} else {
id, err := opts.Spec.AsID()
if err != nil {
return nil, errors.Wrap(err, "treating service spec as ID")
}
services, err = d.Cluster.SomeControllers([]flux.ResourceID{id})
}
resources, _, err := d.getResources(ctx)
if err != nil {
return nil, err
}
imageRepos, err := update.FetchImageRepos(d.Registry, clusterContainers(services), d.Logger)
if err != nil {
return nil, errors.Wrap(err, "getting images for services")
}
var res []v6.ImageStatus
for _, service := range services {
serviceContainers, err := getServiceContainers(service, imageRepos, resources[service.ID.String()], opts.OverrideContainerFields)
if err != nil {
return nil, err
}
res = append(res, v6.ImageStatus{
ID: service.ID,
Containers: serviceContainers,
})
}
return res, nil
}
// jobFunc is a type for procedures that the daemon will execute in a job
type jobFunc func(ctx context.Context, jobID job.ID, logger log.Logger) (job.Result, error)
// updateFunc is a type for procedures that operate on a git checkout, to be run in a job
type updateFunc func(ctx context.Context, jobID job.ID, working *git.Checkout, logger log.Logger) (job.Result, error)
// makeJobFromUpdate turns an updateFunc into a jobFunc that will run
// the update with a fresh clone, and log the result as an event.
func (d *Daemon) makeJobFromUpdate(update updateFunc) jobFunc {
return func(ctx context.Context, jobID job.ID, logger log.Logger) (job.Result, error) {
var result job.Result
err := d.WithClone(ctx, func(working *git.Checkout) error {
var err error
result, err = update(ctx, jobID, working, logger)
if err != nil {
return err
}
return nil
})
if err != nil {
return result, err
}
return result, nil
}
}
// executeJob runs a job func and keeps track of its status, so the
// daemon can report it when asked.
func (d *Daemon) executeJob(id job.ID, do jobFunc, logger log.Logger) (job.Result, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultJobTimeout)
defer cancel()
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusRunning})
result, err := do(ctx, id, logger)
if err != nil {
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusFailed, Err: err.Error()})
return result, err
}
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusSucceeded, Result: result})
return result, nil
}
// makeLoggingFunc takes a jobFunc and returns a jobFunc that will log
// a commit event with the result.
func (d *Daemon) makeLoggingJobFunc(f jobFunc) jobFunc {
return func(ctx context.Context, id job.ID, logger log.Logger) (job.Result, error) {
started := time.Now().UTC()
result, err := f(ctx, id, logger)
if err != nil {
return result, err
}
logger.Log("revision", result.Revision)
if result.Revision != "" {
var serviceIDs []flux.ResourceID
for id, result := range result.Result {
if result.Status == update.ReleaseStatusSuccess {
serviceIDs = append(serviceIDs, id)
}
}
metadata := &event.CommitEventMetadata{
Revision: result.Revision,
Spec: result.Spec,
Result: result.Result,
}
return result, d.LogEvent(event.Event{
ServiceIDs: serviceIDs,
Type: event.EventCommit,
StartedAt: started,
EndedAt: started,
LogLevel: event.LogLevelInfo,
Metadata: metadata,
})
}
return result, nil
}
}
// queueJob queues a job func to be executed.
func (d *Daemon) queueJob(do jobFunc) job.ID {
id := job.ID(guid.New())
enqueuedAt := time.Now()
d.Jobs.Enqueue(&job.Job{
ID: id,
Do: func(logger log.Logger) error {
queueDuration.Observe(time.Since(enqueuedAt).Seconds())
_, err := d.executeJob(id, do, logger)
if err != nil {
return err
}
return nil
},
})
queueLength.Set(float64(d.Jobs.Len()))
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusQueued})
return id
}
// Apply the desired changes to the config files
func (d *Daemon) UpdateManifests(ctx context.Context, spec update.Spec) (job.ID, error) {
var id job.ID
if spec.Type == "" {
return id, errors.New("no type in update spec")
}
switch s := spec.Spec.(type) {
case release.Changes:
if s.ReleaseKind() == update.ReleaseKindPlan {
id := job.ID(guid.New())
_, err := d.executeJob(id, d.makeJobFromUpdate(d.release(spec, s)), d.Logger)
return id, err
}
return d.queueJob(d.makeLoggingJobFunc(d.makeJobFromUpdate(d.release(spec, s)))), nil
case policy.Updates:
return d.queueJob(d.makeLoggingJobFunc(d.makeJobFromUpdate(d.updatePolicy(spec, s)))), nil
case update.ManualSync:
return d.queueJob(d.sync()), nil
default:
return id, fmt.Errorf(`unknown update type "%s"`, spec.Type)
}
}
func (d *Daemon) sync() jobFunc {
return func(ctx context.Context, jobID job.ID, logger log.Logger) (job.Result, error) {
var result job.Result
ctx, cancel := context.WithTimeout(ctx, defaultJobTimeout)
defer cancel()
err := d.Repo.Refresh(ctx)
if err != nil {
return result, err
}
head, err := d.Repo.Revision(ctx, d.GitConfig.Branch)
if err != nil {
return result, err
}
result.Revision = head
return result, nil
}
}
func (d *Daemon) updatePolicy(spec update.Spec, updates policy.Updates) updateFunc {
return func(ctx context.Context, jobID job.ID, working *git.Checkout, logger log.Logger) (job.Result, error) {
// For each update
var serviceIDs []flux.ResourceID
result := job.Result{
Spec: &spec,
Result: update.Result{},
}
// A shortcut to make things more responsive: if anything
// was (probably) set to automated, we will ask for an
// automation run straight ASAP.
var anythingAutomated bool
for serviceID, u := range updates {
if policy.Set(u.Add).Has(policy.Automated) {
anythingAutomated = true
}
// find the service manifest
err := cluster.UpdateManifest(d.Manifests, working.Dir(), working.ManifestDirs(), serviceID, func(def []byte) ([]byte, error) {
newDef, err := d.Manifests.UpdatePolicies(def, serviceID, u)
if err != nil {
result.Result[serviceID] = update.ControllerResult{
Status: update.ReleaseStatusFailed,
Error: err.Error(),
}
return nil, err
}
if string(newDef) == string(def) {
result.Result[serviceID] = update.ControllerResult{
Status: update.ReleaseStatusSkipped,
}
} else {
serviceIDs = append(serviceIDs, serviceID)
result.Result[serviceID] = update.ControllerResult{
Status: update.ReleaseStatusSuccess,
}
}
return newDef, nil
})
if err != nil {
switch err := err.(type) {
case cluster.ManifestError:
result.Result[serviceID] = update.ControllerResult{
Status: update.ReleaseStatusFailed,
Error: err.Error(),
}
default:
return result, err
}
}
}
if len(serviceIDs) == 0 {
return result, nil
}
commitAuthor := ""
if d.GitConfig.SetAuthor {
commitAuthor = spec.Cause.User
}
commitAction := git.CommitAction{Author: commitAuthor, Message: policyCommitMessage(updates, spec.Cause)}
if err := working.CommitAndPush(ctx, commitAction, ¬e{JobID: jobID, Spec: spec}); err != nil {
// On the chance pushing failed because it was not
// possible to fast-forward, ask for a sync so the
// next attempt is more likely to succeed.
d.AskForSync()
return result, err
}
if anythingAutomated {
d.AskForImagePoll()
}
var err error
result.Revision, err = working.HeadRevision(ctx)
if err != nil {
return result, err
}
return result, nil
}
}
func (d *Daemon) release(spec update.Spec, c release.Changes) updateFunc {
return func(ctx context.Context, jobID job.ID, working *git.Checkout, logger log.Logger) (job.Result, error) {
rc := release.NewReleaseContext(d.Cluster, d.Manifests, d.Registry, working)
result, err := release.Release(rc, c, logger)
var zero job.Result
if err != nil {
return zero, err
}
var revision string
if c.ReleaseKind() == update.ReleaseKindExecute {
commitMsg := spec.Cause.Message
if commitMsg == "" {
commitMsg = c.CommitMessage(result)
}
commitAuthor := ""
if d.GitConfig.SetAuthor {
commitAuthor = spec.Cause.User
}
commitAction := git.CommitAction{Author: commitAuthor, Message: commitMsg}
if err := working.CommitAndPush(ctx, commitAction, ¬e{JobID: jobID, Spec: spec, Result: result}); err != nil {
// On the chance pushing failed because it was not
// possible to fast-forward, ask the repo to fetch
// from upstream ASAP, so the next attempt is more
// likely to succeed.
d.Repo.Notify()
return zero, err
}
revision, err = working.HeadRevision(ctx)
if err != nil {
return zero, err
}
}
return job.Result{
Revision: revision,
Spec: &spec,
Result: result,
}, nil
}
}
// Tell the daemon to synchronise the cluster with the manifests in
// the git repo. This has an error return value because upstream there
// may be comms difficulties or other sources of problems; here, we
// always succeed because it's just bookkeeping.
func (d *Daemon) NotifyChange(ctx context.Context, change v9.Change) error {
switch change.Kind {
case v9.GitChange:
gitUpdate := change.Source.(v9.GitUpdate)
if gitUpdate.URL != d.Repo.Origin().URL && gitUpdate.Branch != d.GitConfig.Branch {
// It isn't strictly an _error_ to be notified about a repo/branch pair
// that isn't ours, but it's worth logging anyway for debugging.
d.Logger.Log("msg", "notified about unrelated change",
"url", gitUpdate.URL,
"branch", gitUpdate.Branch)
break
}
d.Repo.Notify()
case v9.ImageChange:
imageUpdate := change.Source.(v9.ImageUpdate)
d.ImageRefresh <- imageUpdate.Name
}
return nil
}
// JobStatus - Ask the daemon how far it's got committing things; in particular, is the job
// queued? running? committed? If it is done, the commit ref is returned.
func (d *Daemon) JobStatus(ctx context.Context, jobID job.ID) (job.Status, error) {
// Is the job queued, running, or recently finished?
status, ok := d.JobStatusCache.Status(jobID)
if ok {
return status, nil
}
// Look through the commits for a note referencing this job. This
// means that even if fluxd restarts, we will at least remember
// jobs which have pushed a commit.
// FIXME(michael): consider looking at the repo for this, since read op
err := d.WithClone(ctx, func(working *git.Checkout) error {
notes, err := working.NoteRevList(ctx)
if err != nil {
return errors.Wrap(err, "enumerating commit notes")
}
commits, err := d.Repo.CommitsBefore(ctx, "HEAD", d.GitConfig.Paths...)
if err != nil {
return errors.Wrap(err, "checking revisions for status")
}
for _, commit := range commits {
if _, ok := notes[commit.Revision]; ok {
var n note
ok, err := working.GetNote(ctx, commit.Revision, &n)
if ok && err == nil && n.JobID == jobID {
status = job.Status{
StatusString: job.StatusSucceeded,
Result: job.Result{
Revision: commit.Revision,
Spec: &n.Spec,
Result: n.Result,
},
}
return nil
}
}
}
return unknownJobError(jobID)
})
return status, err
}
// Ask the daemon how far it's got applying things; in particular, is it
// past the given commit? Return the list of commits between where
// we have applied (the sync tag) and the ref given, inclusive. E.g., if you send HEAD,
// you'll get all the commits yet to be applied. If you send a hash
// and it's applied at or _past_ it, you'll get an empty list.
func (d *Daemon) SyncStatus(ctx context.Context, commitRef string) ([]string, error) {
commits, err := d.Repo.CommitsBetween(ctx, d.GitConfig.SyncTag, commitRef, d.GitConfig.Paths...)
if err != nil {
return nil, err
}
// NB we could use the messages too if we decide to change the
// signature of the API to include it.
revs := make([]string, len(commits))
for i, commit := range commits {
revs[i] = commit.Revision
}
return revs, nil
}
func (d *Daemon) GitRepoConfig(ctx context.Context, regenerate bool) (v6.GitConfig, error) {
publicSSHKey, err := d.Cluster.PublicSSHKey(regenerate)
if err != nil {
return v6.GitConfig{}, err
}
origin := d.Repo.Origin()
status, _ := d.Repo.Status()
path := ""
if len(d.GitConfig.Paths) > 0 {
path = strings.Join(d.GitConfig.Paths, ",")
}
return v6.GitConfig{
Remote: v6.GitRemoteConfig{
URL: origin.URL,
Branch: d.GitConfig.Branch,
Path: path,
},
PublicSSHKey: publicSSHKey,
Status: status,
}, nil
}
// Non-api.Server methods
func (d *Daemon) WithClone(ctx context.Context, fn func(*git.Checkout) error) error {
co, err := d.Repo.Clone(ctx, d.GitConfig)
if err != nil {
return err
}
defer co.Clean()
return fn(co)
}
func (d *Daemon) LogEvent(ev event.Event) error {
if d.EventWriter == nil {
d.Logger.Log("event", ev, "logupstream", "false")
return nil
}
d.Logger.Log("event", ev, "logupstream", "true")
return d.EventWriter.LogEvent(ev)
}
// vvv helpers vvv
func containers2containers(cs []resource.Container) []v6.Container {
res := make([]v6.Container, len(cs))
for i, c := range cs {
res[i] = v6.Container{
Name: c.Name,
Current: image.Info{
ID: c.Image,
},
}
}
return res
}
func getServiceContainers(service cluster.Controller, imageRepos update.ImageRepos, resource resource.Resource, fields []string) (res []v6.Container, err error) {
for _, c := range service.ContainersOrNil() {
imageRepo := c.Image.Name
var policies policy.Set
if resource != nil {
policies = resource.Policy()
}
tagPattern := policy.GetTagPattern(policies, c.Name)
images := imageRepos.GetRepoImages(imageRepo)
currentImage := images.FindWithRef(c.Image)
container, err := v6.NewContainer(c.Name, images, currentImage, tagPattern, fields)
if err != nil {
return res, err
}
res = append(res, container)
}
return res, nil
}
func policyCommitMessage(us policy.Updates, cause update.Cause) string {
// shortcut, since we want roughly the same information
events := policyEvents(us, time.Now())
commitMsg := &bytes.Buffer{}
prefix := "- "
switch {
case cause.Message != "":
fmt.Fprintf(commitMsg, "%s\n\n", cause.Message)
case len(events) > 1:
fmt.Fprintf(commitMsg, "Updated service policies\n\n")
default:
prefix = ""
}
for _, event := range events {
fmt.Fprintf(commitMsg, "%s%v\n", prefix, event)
}
return commitMsg.String()
}
// policyEvents builds a map of events (by type), for all the events in this set of
// updates. There will be one event per type, containing all service ids
// affected by that event. e.g. all automated services will share an event.
func policyEvents(us policy.Updates, now time.Time) map[string]event.Event {
eventsByType := map[string]event.Event{}
for serviceID, update := range us {
for _, eventType := range policyEventTypes(update) {
e, ok := eventsByType[eventType]
if !ok {
e = event.Event{
ServiceIDs: []flux.ResourceID{},
Type: eventType,
StartedAt: now,
EndedAt: now,
LogLevel: event.LogLevelInfo,
}
}
e.ServiceIDs = append(e.ServiceIDs, serviceID)
eventsByType[eventType] = e
}
}
return eventsByType
}
// policyEventTypes is a deduped list of all event types this update contains
func policyEventTypes(u policy.Update) []string {
types := map[string]struct{}{}
for p, _ := range u.Add {
switch {
case p == policy.Automated:
types[event.EventAutomate] = struct{}{}
case p == policy.Locked:
types[event.EventLock] = struct{}{}
default:
types[event.EventUpdatePolicy] = struct{}{}
}
}
for p, _ := range u.Remove {
switch {
case p == policy.Automated:
types[event.EventDeautomate] = struct{}{}
case p == policy.Locked:
types[event.EventUnlock] = struct{}{}
default:
types[event.EventUpdatePolicy] = struct{}{}
}
}
var result []string
for t := range types {
result = append(result, t)
}
sort.Strings(result)
return result
}