/
remoteapplicationworker.go
795 lines (726 loc) · 28.3 KB
/
remoteapplicationworker.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package remoterelations
import (
"fmt"
"sync"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/catacomb"
"gopkg.in/macaroon.v2"
"github.com/juju/juju/api"
"github.com/juju/juju/core/crossmodel"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/status"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/rpc/params"
)
// remoteApplicationWorker listens for localChanges to relations
// involving a remote application, and publishes change to
// local relation units to the remote model. It also watches for
// changes originating from the offering model and consumes those
// in the local model.
type remoteApplicationWorker struct {
catacomb catacomb.Catacomb
mu sync.Mutex
// These attributes are relevant to dealing with a specific
// remote application proxy.
offerUUID string
applicationName string // name of the remote application proxy in the local model
localModelUUID string // uuid of the model hosting the local application
remoteModelUUID string // uuid of the model hosting the remote offer
isConsumerProxy bool
consumeVersion int
localRelationUnitChanges chan RelationUnitChangeEvent
remoteRelationUnitChanges chan RelationUnitChangeEvent
secretChangesWatcher watcher.SecretsRevisionWatcher
secretChanges watcher.SecretRevisionChannel
// relations is stored here for the engine report.
relations map[string]*relation
// offerMacaroon is used to confirm that permission has been granted to consume
// the remote application to which this worker pertains.
offerMacaroon *macaroon.Macaroon
// localModelFacade interacts with the local (consuming) model.
localModelFacade RemoteRelationsFacade
// remoteModelFacade interacts with the remote (offering) model.
remoteModelFacade RemoteModelRelationsFacadeCloser
newRemoteModelRelationsFacadeFunc newRemoteRelationsFacadeFunc
logger Logger
}
// relation holds attributes relevant to a particular
// relation between a local app and a remote offer.
type relation struct {
relationId int
localDead bool
suspended bool
localUnitCount int
localRuw *relationUnitsWorker
remoteRuw *relationUnitsWorker
remoteRrw *remoteRelationsWorker
applicationToken string // token for app in local model
relationToken string // token for relation in local model
localEndpoint params.RemoteEndpoint
remoteEndpointName string
macaroon *macaroon.Macaroon
}
// Kill is defined on worker.Worker
func (w *remoteApplicationWorker) Kill() {
w.catacomb.Kill(nil)
}
// Wait is defined on worker.Worker
func (w *remoteApplicationWorker) Wait() error {
err := w.catacomb.Wait()
if err != nil {
w.logger.Errorf("error in remote application worker for %v: %v", w.applicationName, err)
}
return err
}
func (w *remoteApplicationWorker) checkOfferPermissionDenied(err error, appToken, relationToken string) {
// If consume permission has been revoked for the offer, set the
// status of the local remote application entity.
if params.ErrCode(err) == params.CodeDischargeRequired {
if err := w.localModelFacade.SetRemoteApplicationStatus(w.applicationName, status.Error, err.Error()); err != nil {
w.logger.Errorf(
"updating remote application %v status from remote model %v: %v",
w.applicationName, w.remoteModelUUID, err)
}
w.logger.Debugf("discharge required error: app token: %v rel token: %v", appToken, relationToken)
// If we know a specific relation, update that too.
if relationToken != "" {
suspended := true
event := params.RemoteRelationChangeEvent{
RelationToken: relationToken,
ApplicationToken: appToken,
Suspended: &suspended,
SuspendedReason: "offer permission revoked",
}
if err := w.localModelFacade.ConsumeRemoteRelationChange(event); err != nil {
w.logger.Errorf("updating relation status: %v", err)
}
}
}
}
func (w *remoteApplicationWorker) remoteOfferRemoved() error {
w.logger.Debugf("remote offer for %s has been removed", w.applicationName)
if err := w.localModelFacade.SetRemoteApplicationStatus(w.applicationName, status.Terminated, "offer has been removed"); err != nil {
return errors.Annotatef(err, "updating remote application %v status from remote model %v", w.applicationName, w.remoteModelUUID)
}
return nil
}
// isNotFound allows either type of not found error
// to be correctly handled.
// TODO(wallyworld) - remove when all api facades are fixed
func isNotFound(err error) bool {
if err == nil {
return false
}
return errors.IsNotFound(err) || params.IsCodeNotFound(err)
}
func (w *remoteApplicationWorker) loop() (err error) {
// Watch for changes to any local relations to the remote application.
relationsWatcher, err := w.localModelFacade.WatchRemoteApplicationRelations(w.applicationName)
if err != nil && isNotFound(err) {
return nil
} else if err != nil {
return errors.Annotatef(err, "watching relations for remote application %q", w.applicationName)
}
if err := w.catacomb.Add(relationsWatcher); err != nil {
return errors.Trace(err)
}
// On the consuming side, watch for status changes to the offer.
var (
offerStatusWatcher watcher.OfferStatusWatcher
offerStatusChanges watcher.OfferStatusChannel
)
if !w.isConsumerProxy {
if err := w.newRemoteRelationsFacadeWithRedirect(); err != nil {
msg := fmt.Sprintf("cannot connect to external controller: %v", err.Error())
if err := w.localModelFacade.SetRemoteApplicationStatus(w.applicationName, status.Error, msg); err != nil {
return errors.Annotatef(err, "updating remote application %v status from remote model %v", w.applicationName, w.remoteModelUUID)
}
return errors.Annotate(err, "cannot connect to external controller")
}
defer func() {
if err := w.remoteModelFacade.Close(); err != nil {
w.logger.Errorf("error closing remote-relations facade: %s", err)
}
}()
arg := params.OfferArg{
OfferUUID: w.offerUUID,
}
if w.offerMacaroon != nil {
arg.Macaroons = macaroon.Slice{w.offerMacaroon}
arg.BakeryVersion = bakery.LatestVersion
}
offerStatusWatcher, err = w.remoteModelFacade.WatchOfferStatus(arg)
if err != nil {
w.checkOfferPermissionDenied(err, "", "")
if isNotFound(err) {
return w.remoteOfferRemoved()
}
return errors.Annotate(err, "watching status for offer")
}
if err := w.catacomb.Add(offerStatusWatcher); err != nil {
return errors.Trace(err)
}
offerStatusChanges = offerStatusWatcher.Changes()
}
w.mu.Lock()
w.relations = make(map[string]*relation)
w.mu.Unlock()
for {
select {
case <-w.catacomb.Dying():
return w.catacomb.ErrDying()
case change, ok := <-relationsWatcher.Changes():
w.logger.Debugf("relations changed: %#v, %v", &change, ok)
if !ok {
// We are dying.
return w.catacomb.ErrDying()
}
results, err := w.localModelFacade.Relations(change)
if err != nil {
return errors.Annotate(err, "querying relations")
}
for i, result := range results {
key := change[i]
if err := w.relationChanged(key, result); err != nil {
if isNotFound(err) {
// Relation has been deleted, so ensure relevant workers are stopped.
w.logger.Debugf("relation %q changed but has been removed", key)
err2 := w.localRelationChanged(key, nil)
if err2 != nil {
return errors.Annotatef(err2, "cleaning up removed local relation %q", key)
}
continue
}
return errors.Annotatef(err, "handling change for relation %q", key)
}
}
case change := <-w.localRelationUnitChanges:
w.logger.Debugf("local relation units changed -> publishing: %#v", &change)
// TODO(babbageclunk): add macaroons to event here instead
// of in the relation units worker.
if err := w.remoteModelFacade.PublishRelationChange(change.RemoteRelationChangeEvent); err != nil {
w.checkOfferPermissionDenied(err, change.ApplicationToken, change.RelationToken)
if isNotFound(err) || params.IsCodeCannotEnterScope(err) {
w.logger.Debugf("relation %v changed but remote side already removed", change.Tag.Id())
continue
}
return errors.Annotatef(err, "publishing relation change %#v to remote model %v", &change, w.remoteModelUUID)
}
// TODO(juju4) - remove
// UnitCount has had omitempty removed, but we need to account for older controllers.
zero := 0
unitCount := change.UnitCount
if unitCount == nil {
unitCount = &zero
}
if err := w.localRelationChanged(change.Tag.Id(), unitCount); err != nil {
return errors.Annotatef(err, "processing local relation change for %v", change.Tag.Id())
}
case change := <-w.remoteRelationUnitChanges:
w.logger.Debugf("remote relation units changed -> consuming: %#v", &change)
if err := w.localModelFacade.ConsumeRemoteRelationChange(change.RemoteRelationChangeEvent); err != nil {
if isNotFound(err) || params.IsCodeCannotEnterScope(err) {
w.logger.Debugf("relation %v changed but local side already removed", change.Tag.Id())
continue
}
return errors.Annotatef(err, "consuming relation change %#v from remote model %v", &change, w.remoteModelUUID)
}
case changes := <-offerStatusChanges:
w.logger.Debugf("offer status changed: %#v", changes)
for _, change := range changes {
if err := w.localModelFacade.SetRemoteApplicationStatus(w.applicationName, change.Status.Status, change.Status.Message); err != nil {
return errors.Annotatef(err, "updating remote application %v status from remote model %v", w.applicationName, w.remoteModelUUID)
}
// If the offer is terminated the status watcher can be stopped immediately.
if change.Status.Status == status.Terminated {
offerStatusWatcher.Kill()
if err := offerStatusWatcher.Wait(); err != nil {
w.logger.Warningf("error stopping status watcher for saas application %s: %v", w.applicationName, err)
}
offerStatusChanges = nil
break
}
}
case changes := <-w.secretChanges:
err := w.localModelFacade.ConsumeRemoteSecretChanges(changes)
if err != nil {
if isNotFound(err) {
w.logger.Debugf("secrets %v changed but local side already removed", changes)
continue
}
return errors.Annotatef(err, "consuming secrets change %#v from remote model %v", changes, w.remoteModelUUID)
}
}
}
}
// newRemoteRelationsFacadeWithRedirect attempts to open an API connection to
// the remote model for the watcher's application.
// If a redirect error is returned, we attempt to open a connection to the new
// controller and update our local controller info for the model so that future
// API connections are to the new location.
func (w *remoteApplicationWorker) newRemoteRelationsFacadeWithRedirect() error {
apiInfo, err := w.localModelFacade.ControllerAPIInfoForModel(w.remoteModelUUID)
if err != nil {
return errors.Annotate(err, "cannot get controller api info for remote model")
}
w.logger.Debugf("remote controller API addresses: %v", apiInfo.Addrs)
w.remoteModelFacade, err = w.newRemoteModelRelationsFacadeFunc(apiInfo)
if redirectErr, ok := errors.Cause(err).(*api.RedirectError); ok {
apiInfo.Addrs = network.CollapseToHostPorts(redirectErr.Servers).Strings()
apiInfo.CACert = redirectErr.CACert
w.logger.Debugf("received redirect; new API addresses: %v", apiInfo.Addrs)
if w.remoteModelFacade, err = w.newRemoteModelRelationsFacadeFunc(apiInfo); err == nil {
// We successfully followed the redirect,
// so update the controller information for this model.
controllerInfo := crossmodel.ControllerInfo{
ControllerTag: redirectErr.ControllerTag,
Alias: redirectErr.ControllerAlias,
Addrs: apiInfo.Addrs,
CACert: apiInfo.CACert,
}
if err = w.localModelFacade.UpdateControllerForModel(controllerInfo, w.remoteModelUUID); err != nil {
_ = w.remoteModelFacade.Close()
err = errors.Annotate(err, "updating external controller info")
}
}
}
return errors.Annotate(err, "opening facade to remote model")
}
func (w *remoteApplicationWorker) processRelationDying(key string, r *relation, forceCleanup bool) error {
w.logger.Debugf("relation %v dying (%v)", key, forceCleanup)
// On the consuming side, inform the remote side the relation is dying
// (but only if we are killing the relation due to it dying, not because
// it is suspended).
if !w.isConsumerProxy {
change := params.RemoteRelationChangeEvent{
RelationToken: r.relationToken,
Life: life.Dying,
ApplicationToken: r.applicationToken,
Macaroons: macaroon.Slice{r.macaroon},
BakeryVersion: bakery.LatestVersion,
}
// forceCleanup will be true if the worker has restarted and because the relation had
// already been removed, we won't get any more unit departed events.
if forceCleanup {
change.ForceCleanup = &forceCleanup
}
if err := w.remoteModelFacade.PublishRelationChange(change); err != nil {
w.checkOfferPermissionDenied(err, r.applicationToken, r.relationToken)
if isNotFound(err) {
w.logger.Debugf("relation %v dying but remote side already removed", key)
return nil
}
return errors.Annotatef(err, "publishing relation dying %#v to remote model %v", &change, w.remoteModelUUID)
}
}
return nil
}
func (w *remoteApplicationWorker) processRelationSuspended(key string, relLife life.Value, relations map[string]*relation) error {
w.logger.Debugf("(%v) relation %v suspended", relLife, key)
relation, ok := relations[key]
if !ok {
return nil
}
// Only stop the watchers for relation unit changes if relation is alive,
// as we need to always deal with units leaving scope etc if the relation is dying.
if relLife != life.Alive {
return nil
}
// On the offering side, if the relation is resumed,
// it will be treated like the relation has been joined
// for the first time; all workers will be restarted.
// The offering side has isConsumerProxy = true.
if w.isConsumerProxy {
delete(relations, key)
}
if relation.localRuw != nil {
if err := worker.Stop(relation.localRuw); err != nil {
w.logger.Warningf("stopping local relation unit worker for %v: %v", key, err)
}
relation.localRuw = nil
}
if relation.remoteRuw != nil {
if err := worker.Stop(relation.remoteRuw); err != nil {
w.logger.Warningf("stopping remote relation unit worker for %v: %v", key, err)
}
relation.remoteRuw = nil
}
return nil
}
// processLocalRelationRemoved is called when a change event arrives from the remote model
// but the relation in the local model has been removed.
func (w *remoteApplicationWorker) processLocalRelationRemoved(key string, relations map[string]*relation) error {
w.logger.Debugf("local relation %v removed", key)
relation, ok := relations[key]
if !ok {
return nil
}
// Stop the worker which watches remote status/life.
if relation.remoteRrw != nil {
if err := worker.Stop(relation.remoteRrw); err != nil {
w.logger.Warningf("stopping remote relations worker for %v: %v", key, err)
}
relation.remoteRrw = nil
relations[key] = relation
}
w.logger.Debugf("remote relation %v removed from local model", key)
return nil
}
// localRelationChanged processes changes to the relation
// as recorded in the local model; the primary function
// is to shut down workers when the relation is dead.
func (w *remoteApplicationWorker) localRelationChanged(key string, unitCountPtr *int) error {
unitCountMsg := " (removed)"
if unitCountPtr != nil {
unitCountMsg = fmt.Sprintf(", still has %d unit(s) in scope", *unitCountPtr)
}
w.logger.Debugf("local relation %v changed%s", key, unitCountMsg)
w.mu.Lock()
defer w.mu.Unlock()
relation, ok := w.relations[key]
if !ok {
w.logger.Debugf("local relation %v already gone", key)
return nil
}
w.logger.Debugf("relation %v in mem unit count is %d", key, relation.localUnitCount)
if unitCountPtr != nil {
relation.localUnitCount = *unitCountPtr
}
if !relation.localDead {
w.logger.Debugf("local relation %v not dead yet", key)
return nil
}
if relation.localUnitCount > 0 {
w.logger.Debugf("relation dead but still has %d units in scope", relation.localUnitCount)
return nil
}
return w.terminateLocalRelation(key)
}
func (w *remoteApplicationWorker) terminateLocalRelation(key string) error {
relation, ok := w.relations[key]
if !ok {
return nil
}
delete(w.relations, key)
w.logger.Debugf("local relation %v is terminated", key)
// For the unit watchers, check to see if these are nil before stopping.
// They will be nil if the relation was suspended and then we kill it for real.
if relation.localRuw != nil {
if err := worker.Stop(relation.localRuw); err != nil {
w.logger.Warningf("stopping local relation unit worker for %v: %v", key, err)
}
relation.localRuw = nil
}
if relation.remoteRuw != nil {
if err := worker.Stop(relation.remoteRuw); err != nil {
w.logger.Warningf("stopping remote relation unit worker for %v: %v", key, err)
}
relation.remoteRuw = nil
}
w.logger.Debugf("local relation %v removed from local model", key)
return nil
}
// relationChanged processes changes to the relation as recorded in the
// local model when a change event arrives from the remote model.
func (w *remoteApplicationWorker) relationChanged(key string, localRelation params.RemoteRelationResult) (err error) {
w.logger.Debugf("relation %q changed in local model: %#v", key, localRelation)
w.mu.Lock()
defer w.mu.Unlock()
defer func() {
if err == nil || !isNotFound(err) {
return
}
if err2 := w.processLocalRelationRemoved(key, w.relations); err2 != nil {
err = errors.Annotate(err2, "processing local relation removed")
}
if r := w.relations[key]; r != nil {
r.localDead = true
w.relations[key] = r
}
}()
if localRelation.Error != nil {
return localRelation.Error
}
localRelationInfo := localRelation.Result
// If we have previously started the watcher and the
// relation is now suspended, stop the watcher.
if r := w.relations[key]; r != nil {
wasSuspended := r.suspended
r.suspended = localRelationInfo.Suspended
w.relations[key] = r
if localRelationInfo.Suspended {
return w.processRelationSuspended(key, localRelationInfo.Life, w.relations)
}
if localRelationInfo.Life == life.Alive {
if r.localDead {
// A previous relation with the same name was removed but
// not cleaned up properly so do it now before starting up
// workers again.
w.logger.Debugf("still have zombie local relation %v", key)
if err := w.terminateLocalRelation(key); err != nil {
return errors.Annotatef(err, "terminating zombie local relation %v", key)
}
} else if !wasSuspended {
// Nothing to do, we have previously started the watcher.
return nil
}
}
}
if w.isConsumerProxy {
// Nothing else to do on the offering side.
return nil
}
return w.processConsumingRelation(key, localRelationInfo)
}
// startUnitsWorkers starts 2 workers to watch for unit settings or departed changes;
// one worker is for the local model, the other for the remote model.
func (w *remoteApplicationWorker) startUnitsWorkers(
relationTag names.RelationTag,
relationToken, remoteAppToken string,
applicationName string,
mac *macaroon.Macaroon,
) (*relationUnitsWorker, *relationUnitsWorker, error) {
// Start a watcher to track changes to the units in the relation in the local model.
localRelationUnitsWatcher, err := w.localModelFacade.WatchLocalRelationChanges(relationTag.Id())
if err != nil {
return nil, nil, errors.Annotatef(err, "watching local side of relation %v", relationTag.Id())
}
localUnitsWorker, err := newRelationUnitsWorker(
relationTag,
mac,
localRelationUnitsWatcher,
w.localRelationUnitChanges,
w.logger,
"local",
)
if err != nil {
return nil, nil, errors.Trace(err)
}
if err := w.catacomb.Add(localUnitsWorker); err != nil {
return nil, nil, errors.Trace(err)
}
// Start a watcher to track changes to the units in the relation in the remote model.
remoteRelationUnitsWatcher, err := w.remoteModelFacade.WatchRelationChanges(
relationToken, remoteAppToken, macaroon.Slice{mac},
)
if err != nil {
w.checkOfferPermissionDenied(err, remoteAppToken, relationToken)
return nil, nil, errors.Annotatef(
err, "watching remote side of application %v and relation %v",
applicationName, relationTag.Id())
}
remoteUnitsWorker, err := newRelationUnitsWorker(
relationTag,
mac,
remoteRelationUnitsWatcher,
w.remoteRelationUnitChanges,
w.logger,
"remote",
)
if err != nil {
return nil, nil, errors.Trace(err)
}
if err := w.catacomb.Add(remoteUnitsWorker); err != nil {
return nil, nil, errors.Trace(err)
}
return localUnitsWorker, remoteUnitsWorker, nil
}
// processConsumingRelation starts the sub-workers necessary to listen and publish
// local unit settings changes, and watch and consume remote unit settings changes.
// Ths will be called when a new relation is created or when a relation resumes
// after being suspended.
func (w *remoteApplicationWorker) processConsumingRelation(
key string,
remoteRelation *params.RemoteRelation,
) error {
// We have not seen the relation before, make
// sure it is registered on the offering side.
// Or relation was suspended and is now resumed so re-register.
applicationTag := names.NewApplicationTag(remoteRelation.ApplicationName)
relationTag := names.NewRelationTag(key)
applicationToken, remoteAppToken, relationToken, mac, err := w.registerRemoteRelation(
applicationTag, relationTag, w.offerUUID, w.consumeVersion,
remoteRelation.Endpoint, remoteRelation.RemoteEndpointName)
if err != nil {
w.checkOfferPermissionDenied(err, "", "")
return errors.Annotatef(err, "registering application %v and relation %v", remoteRelation.ApplicationName, relationTag.Id())
}
w.logger.Debugf("remote relation registered for %q: app token=%q, rel token=%q, remote app token=%q", key, applicationToken, relationToken, remoteAppToken)
// Have we seen the relation before.
r, relationKnown := w.relations[key]
if !relationKnown {
// Totally new so start the lifecycle watcher.
remoteRelationsWatcher, err := w.remoteModelFacade.WatchRelationSuspendedStatus(params.RemoteEntityArg{
Token: relationToken,
Macaroons: macaroon.Slice{mac},
BakeryVersion: bakery.LatestVersion,
})
if err != nil {
w.checkOfferPermissionDenied(err, remoteAppToken, relationToken)
return errors.Annotatef(err, "watching remote side of relation %v", remoteRelation.Key)
}
remoteRelationsWorker, err := newRemoteRelationsWorker(
relationTag,
remoteAppToken,
relationToken,
remoteRelationsWatcher,
w.remoteRelationUnitChanges,
w.logger,
)
if err != nil {
return errors.Trace(err)
}
if err := w.catacomb.Add(remoteRelationsWorker); err != nil {
return errors.Trace(err)
}
r = &relation{
relationId: remoteRelation.Id,
suspended: remoteRelation.Suspended,
localUnitCount: remoteRelation.UnitCount,
remoteRrw: remoteRelationsWorker,
macaroon: mac,
localEndpoint: remoteRelation.Endpoint,
remoteEndpointName: remoteRelation.RemoteEndpointName,
applicationToken: applicationToken,
relationToken: relationToken,
}
w.relations[key] = r
}
if r.localRuw == nil && !remoteRelation.Suspended {
// Also start the units watchers (local and remote).
localUnitsWorker, remoteUnitsWorker, err := w.startUnitsWorkers(
relationTag, relationToken, remoteAppToken, remoteRelation.ApplicationName, mac)
if err != nil {
return errors.Annotate(err, "starting relation units workers")
}
r.localRuw = localUnitsWorker
r.remoteRuw = remoteUnitsWorker
}
if w.secretChangesWatcher == nil {
w.secretChangesWatcher, err = w.remoteModelFacade.WatchConsumedSecretsChanges(applicationToken, relationToken, w.offerMacaroon)
if err != nil && !errors.Is(err, errors.NotFound) && !errors.Is(err, errors.NotImplemented) {
w.checkOfferPermissionDenied(err, "", "")
return errors.Annotate(err, "watching consumed secret changes")
}
if err == nil {
if err := w.catacomb.Add(w.secretChangesWatcher); err != nil {
return errors.Trace(err)
}
w.secretChanges = w.secretChangesWatcher.Changes()
}
}
// If the relation is dying, stop the watcher.
if remoteRelation.Life != life.Alive {
return w.processRelationDying(key, r, !relationKnown)
}
return nil
}
func (w *remoteApplicationWorker) registerRemoteRelation(
applicationTag, relationTag names.Tag, offerUUID string, consumeVersion int,
localEndpointInfo params.RemoteEndpoint, remoteEndpointName string,
) (applicationToken, offeringAppToken, relationToken string, _ *macaroon.Macaroon, _ error) {
w.logger.Debugf("register remote relation %v to local application %v", relationTag.Id(), applicationTag.Id())
fail := func(err error) (string, string, string, *macaroon.Macaroon, error) {
return "", "", "", nil, err
}
// Ensure the relation is exported first up.
results, err := w.localModelFacade.ExportEntities([]names.Tag{applicationTag, relationTag})
if err != nil {
return fail(errors.Annotatef(err, "exporting relation %v and application %v", relationTag, applicationTag))
}
if results[0].Error != nil && !params.IsCodeAlreadyExists(results[0].Error) {
return fail(errors.Annotatef(err, "exporting application %v", applicationTag))
}
applicationToken = results[0].Token
if results[1].Error != nil && !params.IsCodeAlreadyExists(results[1].Error) {
return fail(errors.Annotatef(err, "exporting relation %v", relationTag))
}
relationToken = results[1].Token
// This data goes to the remote model so we map local info
// from this model to the remote arg values and visa versa.
arg := params.RegisterRemoteRelationArg{
ApplicationToken: applicationToken,
SourceModelTag: names.NewModelTag(w.localModelUUID).String(),
RelationToken: relationToken,
OfferUUID: offerUUID,
RemoteEndpoint: localEndpointInfo,
LocalEndpointName: remoteEndpointName,
ConsumeVersion: consumeVersion,
}
if w.offerMacaroon != nil {
arg.Macaroons = macaroon.Slice{w.offerMacaroon}
arg.BakeryVersion = bakery.LatestVersion
}
remoteRelation, err := w.remoteModelFacade.RegisterRemoteRelations(arg)
if err != nil {
return fail(errors.Trace(err))
}
// remoteAppIds is a slice but there's only one item
// as we currently only register one remote application
if err := remoteRelation[0].Error; err != nil {
return fail(errors.Annotatef(err, "registering relation %v", relationTag))
}
// Import the application id from the offering model.
registerResult := *remoteRelation[0].Result
offeringAppToken = registerResult.Token
// We have a new macaroon attenuated to the relation.
// Save for the firewaller.
if err := w.localModelFacade.SaveMacaroon(relationTag, registerResult.Macaroon); err != nil {
return fail(errors.Annotatef(
err, "saving macaroon for %v", relationTag))
}
appTag := names.NewApplicationTag(w.applicationName)
w.logger.Debugf("import remote application token %v for %v", offeringAppToken, w.applicationName)
err = w.localModelFacade.ImportRemoteEntity(appTag, offeringAppToken)
if err != nil && !params.IsCodeAlreadyExists(err) {
return fail(errors.Annotatef(
err, "importing remote application %v to local model", w.applicationName))
}
return applicationToken, offeringAppToken, relationToken, registerResult.Macaroon, nil
}
// Report provides information for the engine report.
func (w *remoteApplicationWorker) Report() map[string]interface{} {
result := make(map[string]interface{})
w.mu.Lock()
defer w.mu.Unlock()
relationsInfo := make(map[string]interface{})
for rel, info := range w.relations {
report := map[string]interface{}{
"relation-id": info.relationId,
"local-dead": info.localDead,
"suspended": info.suspended,
"application-token": info.applicationToken,
"relation-token": info.relationToken,
"local-endpoint": info.localEndpoint.Name,
"remote-endpoint": info.remoteEndpointName,
}
if info.remoteRrw != nil {
report["last-status-event"] = info.remoteRrw.Report()
}
if info.localRuw != nil {
report["last-local-change"] = info.localRuw.Report()
}
if info.remoteRuw != nil {
report["last-remote-change"] = info.remoteRuw.Report()
}
relationsInfo[rel] = report
}
if len(relationsInfo) > 0 {
result["relations"] = relationsInfo
}
result["remote-model-uuid"] = w.remoteModelUUID
if w.isConsumerProxy {
result["consumer-proxy"] = true
result["consume-version"] = w.consumeVersion
} else {
result["saas-application"] = true
result["offer-uuid"] = w.offerUUID
}
return result
}