/
types.go
668 lines (609 loc) · 16.2 KB
/
types.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
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package multiwatcher
import (
"time"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/core/status"
)
// The kind constants are there to stop typos when switching on kinds.
const (
ActionKind = "action"
AnnotationKind = "annotation" // the annotations should really be parts of the other entities
ApplicationKind = "application"
ApplicationOfferKind = "applicationOffer"
BlockKind = "block"
BranchKind = "branch"
CharmKind = "charm"
MachineKind = "machine"
ModelKind = "model"
RelationKind = "relation"
RemoteApplicationKind = "remoteApplication"
UnitKind = "unit"
)
// Factory is used to create multiwatchers.
type Factory interface {
// TODO: WatchUsersModels to filter just the user's models
WatchModel(modelUUID string) Watcher
WatchController() Watcher
}
// Watcher is the way a caller can find out what changes have happened
// on one or more models.
type Watcher interface {
Stop() error
Next() ([]Delta, error)
}
// EntityInfo is implemented by all entity Info types.
type EntityInfo interface {
// EntityID returns an identifier that will uniquely
// identify the entity within its kind
EntityID() EntityID
// Clone returns a clone of the EntityInfo.
Clone() EntityInfo
}
// EntityID uniquely identifies an entity being tracked by the
// multiwatcherStore.
type EntityID struct {
Kind string
ModelUUID string
ID string
}
// Delta holds details of a change to the model.
type Delta struct {
// If Removed is true, the entity has been removed;
// otherwise it has been created or changed.
Removed bool
// Entity holds data about the entity that has changed.
Entity EntityInfo
}
// MachineInfo holds the information about a machine
// that is tracked by multiwatcherStore.
type MachineInfo struct {
ModelUUID string
ID string
InstanceID string
AgentStatus StatusInfo
InstanceStatus StatusInfo
Life life.Value
Annotations map[string]string
Config map[string]interface{}
Base string
ContainerType string
IsManual bool
SupportedContainers []instance.ContainerType
SupportedContainersKnown bool
HardwareCharacteristics *instance.HardwareCharacteristics
CharmProfiles []string
Jobs []model.MachineJob
Addresses []network.ProviderAddress
HasVote bool
WantsVote bool
// The preferred public and preferred private address are
// only stored on the machine info to populate the unit info.
PreferredPublicAddress network.SpaceAddress
PreferredPrivateAddress network.SpaceAddress
// The machine's hostname reported by the machine agent when it started.
Hostname string
}
// EntityID returns a unique identifier for a machine across
// models.
func (i *MachineInfo) EntityID() EntityID {
return EntityID{
Kind: MachineKind,
ModelUUID: i.ModelUUID,
ID: i.ID,
}
}
// Clone returns a clone of the EntityInfo.
func (i *MachineInfo) Clone() EntityInfo {
clone := *i
clone.HardwareCharacteristics = i.HardwareCharacteristics.Clone()
if i.Annotations != nil {
clone.Annotations = map[string]string{}
for k, v := range i.Annotations {
clone.Annotations[k] = v
}
}
if i.Config != nil {
clone.Config = map[string]interface{}{}
for k, v := range i.Config {
clone.Config[k] = v
}
}
if i.SupportedContainers != nil {
clone.SupportedContainers = make([]instance.ContainerType, len(i.SupportedContainers))
for i, c := range i.SupportedContainers {
clone.SupportedContainers[i] = c
}
}
if i.CharmProfiles != nil {
clone.CharmProfiles = make([]string, len(i.CharmProfiles))
for i, c := range i.CharmProfiles {
clone.CharmProfiles[i] = c
}
}
if i.Jobs != nil {
clone.Jobs = make([]model.MachineJob, len(i.Jobs))
for i, j := range i.Jobs {
clone.Jobs[i] = j
}
}
if i.Addresses != nil {
clone.Addresses = make([]network.ProviderAddress, len(i.Addresses))
for i, a := range i.Addresses {
clone.Addresses[i] = a
}
}
return &clone
}
// StatusInfo holds the unit and machine status information. It is
// used by ApplicationInfo and UnitInfo.
type StatusInfo struct {
Err error
Current status.Status
Message string
Since *time.Time
Version string
Data map[string]interface{}
}
// NewStatusInfo return a new multiwatcher StatusInfo from a
// status StatusInfo.
func NewStatusInfo(s status.StatusInfo, err error) StatusInfo {
return StatusInfo{
Err: err,
Current: s.Status,
Message: s.Message,
Since: s.Since,
Data: s.Data,
}
}
// PodSpec is used to determine whether or not a CAAS application
// has called the command to set the pod spec.
type PodSpec struct {
Spec string
Raw bool
Counter int
}
// ApplicationInfo holds the information about an application that is tracked
// by multiwatcherStore.
type ApplicationInfo struct {
ModelUUID string
Name string
Exposed bool
CharmURL string
OwnerTag string
Life life.Value
MinUnits int
Constraints constraints.Value
Annotations map[string]string
Config map[string]interface{}
Subordinate bool
Status StatusInfo
OperatorStatus StatusInfo // For CAAS models.
WorkloadVersion string
PodSpec *PodSpec // For CAAS models.
}
// EntityID returns a unique identifier for an application across
// models.
func (i *ApplicationInfo) EntityID() EntityID {
return EntityID{
Kind: ApplicationKind,
ModelUUID: i.ModelUUID,
ID: i.Name,
}
}
// Clone returns a clone of the EntityInfo.
func (i *ApplicationInfo) Clone() EntityInfo {
clone := *i
if i.Annotations != nil {
clone.Annotations = map[string]string{}
for k, v := range i.Annotations {
clone.Annotations[k] = v
}
}
if i.Config != nil {
clone.Config = map[string]interface{}{}
for k, v := range i.Config {
clone.Config[k] = v
}
}
return &clone
}
// Profile is a representation of charm.v6 LXDProfile
type Profile struct {
Config map[string]string
Description string
Devices map[string]map[string]string
}
func (p *Profile) Clone() *Profile {
if p == nil {
return nil
}
clone := *p
if len(p.Config) > 0 {
clone.Config = map[string]string{}
for k, v := range p.Config {
clone.Config[k] = v
}
}
if len(p.Devices) > 0 {
clone.Devices = map[string]map[string]string{}
for k, cfg := range p.Devices {
var cCfg map[string]string
if len(cfg) > 0 {
cCfg = map[string]string{}
for k, v := range cfg {
cCfg[k] = v
}
}
clone.Devices[k] = cCfg
}
}
return &clone
}
// CharmInfo holds the information about a charm that is tracked by the
// multiwatcher.
type CharmInfo struct {
ModelUUID string
CharmURL string
CharmVersion string
Life life.Value
LXDProfile *Profile
// DefaultConfig is derived from state-stored *charm.Config.
DefaultConfig map[string]interface{}
}
// EntityID returns a unique identifier for an charm across
// models.
func (i *CharmInfo) EntityID() EntityID {
return EntityID{
Kind: CharmKind,
ModelUUID: i.ModelUUID,
ID: i.CharmURL,
}
}
// Clone returns a clone of the EntityInfo.
func (i *CharmInfo) Clone() EntityInfo {
clone := *i
clone.LXDProfile = i.LXDProfile.Clone()
if i.DefaultConfig != nil {
clone.DefaultConfig = map[string]interface{}{}
for k, v := range i.DefaultConfig {
clone.DefaultConfig[k] = v
}
}
return &clone
}
// RemoteApplicationUpdate holds the information about a remote application that is
// tracked by multiwatcherStore.
type RemoteApplicationUpdate struct {
ModelUUID string
Name string
OfferURL string
Life life.Value
Status StatusInfo
}
// EntityID returns a unique identifier for a remote application across models.
func (i *RemoteApplicationUpdate) EntityID() EntityID {
return EntityID{
Kind: RemoteApplicationKind,
ModelUUID: i.ModelUUID,
ID: i.Name,
}
}
// Clone returns a clone of the EntityInfo.
func (i *RemoteApplicationUpdate) Clone() EntityInfo {
clone := *i
return &clone
}
// ApplicationOfferInfo holds the information about an application offer that is
// tracked by multiwatcherStore.
type ApplicationOfferInfo struct {
ModelUUID string
OfferName string
OfferUUID string
ApplicationName string
CharmName string
TotalConnectedCount int
ActiveConnectedCount int
}
// EntityID returns a unique identifier for an application offer across models.
func (i *ApplicationOfferInfo) EntityID() EntityID {
return EntityID{
Kind: ApplicationOfferKind,
ModelUUID: i.ModelUUID,
ID: i.OfferName,
}
}
// Clone returns a clone of the EntityInfo.
func (i *ApplicationOfferInfo) Clone() EntityInfo {
clone := *i
return &clone
}
// UnitInfo holds the information about a unit
// that is tracked by multiwatcherStore.
type UnitInfo struct {
ModelUUID string
Name string
Application string
Base string
CharmURL string
Life life.Value
Annotations map[string]string
PublicAddress string
PrivateAddress string
MachineID string
OpenPortRangesByEndpoint network.GroupedPortRanges
Principal string
Subordinate bool
// Workload and agent state are modelled separately.
WorkloadStatus StatusInfo
AgentStatus StatusInfo
ContainerStatus StatusInfo // For CAAS models.
}
// EntityID returns a unique identifier for a unit across
// models.
func (i *UnitInfo) EntityID() EntityID {
return EntityID{
Kind: UnitKind,
ModelUUID: i.ModelUUID,
ID: i.Name,
}
}
// Clone returns a clone of the EntityInfo.
func (i *UnitInfo) Clone() EntityInfo {
clone := *i
if i.Annotations != nil {
clone.Annotations = map[string]string{}
for k, v := range i.Annotations {
clone.Annotations[k] = v
}
}
clone.OpenPortRangesByEndpoint = i.OpenPortRangesByEndpoint.Clone()
return &clone
}
// ActionInfo holds the information about an action that is tracked by
// multiwatcherStore.
type ActionInfo struct {
ModelUUID string
ID string
Receiver string
Name string
Parameters map[string]interface{}
Parallel bool
ExecutionGroup string
Status string
Message string
Results map[string]interface{}
Enqueued time.Time
Started time.Time
Completed time.Time
}
// EntityID returns a unique identifier for an action across
// models.
func (i *ActionInfo) EntityID() EntityID {
return EntityID{
Kind: ActionKind,
ModelUUID: i.ModelUUID,
ID: i.ID,
}
}
// Clone returns a clone of the EntityInfo.
func (i *ActionInfo) Clone() EntityInfo {
clone := *i
if i.Parameters != nil {
clone.Parameters = map[string]interface{}{}
for k, v := range i.Parameters {
clone.Parameters[k] = v
}
}
if i.Results != nil {
clone.Results = map[string]interface{}{}
for k, v := range i.Results {
clone.Results[k] = v
}
}
return &clone
}
// RelationInfo holds the information about a relation that is tracked
// by multiwatcherStore.
type RelationInfo struct {
ModelUUID string
Key string
ID int
Endpoints []Endpoint
}
// Endpoint holds an application-relation pair.
type Endpoint struct {
ApplicationName string
Relation CharmRelation
}
// CharmRelation mirrors charm.Relation.
type CharmRelation struct {
Name string
Role string
Interface string
Optional bool
Limit int
Scope string
}
// EntityID returns a unique identifier for a relation across
// models.
func (i *RelationInfo) EntityID() EntityID {
return EntityID{
Kind: RelationKind,
ModelUUID: i.ModelUUID,
ID: i.Key,
}
}
// Clone returns a clone of the EntityInfo.
func (i *RelationInfo) Clone() EntityInfo {
clone := *i
if i.Endpoints != nil {
clone.Endpoints = make([]Endpoint, len(i.Endpoints))
for i, ep := range i.Endpoints {
clone.Endpoints[i] = ep
}
}
return &clone
}
// AnnotationInfo holds the information about an annotation that is
// tracked by multiwatcherStore.
type AnnotationInfo struct {
ModelUUID string
Tag string
Annotations map[string]string
}
// EntityID returns a unique identifier for an annotation across
// models.
func (i *AnnotationInfo) EntityID() EntityID {
return EntityID{
Kind: AnnotationKind,
ModelUUID: i.ModelUUID,
ID: i.Tag,
}
}
// Clone returns a clone of the EntityInfo.
func (i *AnnotationInfo) Clone() EntityInfo {
clone := *i
if i.Annotations != nil {
clone.Annotations = map[string]string{}
for k, v := range i.Annotations {
clone.Annotations[k] = v
}
}
return &clone
}
// BlockInfo holds the information about a block that is tracked by
// multiwatcherStore.
type BlockInfo struct {
ModelUUID string
ID string
Type model.BlockType
Message string
Tag string
}
// EntityID returns a unique identifier for a block across
// models.
func (i *BlockInfo) EntityID() EntityID {
return EntityID{
Kind: BlockKind,
ModelUUID: i.ModelUUID,
ID: i.ID,
}
}
// Clone returns a clone of the EntityInfo.
func (i *BlockInfo) Clone() EntityInfo {
clone := *i
return &clone
}
// ModelInfo holds the information about a model that is
// tracked by multiwatcherStore.
type ModelInfo struct {
ModelUUID string
Type model.ModelType
Name string
Life life.Value
Owner string
ControllerUUID string
IsController bool
Cloud string
CloudRegion string
CloudCredential string
Annotations map[string]string
Config map[string]interface{}
Status StatusInfo
Constraints constraints.Value
SLA ModelSLAInfo
UserPermissions map[string]permission.Access
}
// ModelSLAInfo describes the SLA info for a model.
type ModelSLAInfo struct {
Level string
Owner string
}
// EntityID returns a unique identifier for a model.
func (i *ModelInfo) EntityID() EntityID {
return EntityID{
Kind: ModelKind,
ModelUUID: i.ModelUUID,
ID: i.ModelUUID,
}
}
// Clone returns a clone of the EntityInfo.
func (i *ModelInfo) Clone() EntityInfo {
clone := *i
if i.Annotations != nil {
clone.Annotations = map[string]string{}
for k, v := range i.Annotations {
clone.Annotations[k] = v
}
}
if i.Config != nil {
clone.Config = map[string]interface{}{}
for k, v := range i.Config {
clone.Config[k] = v
}
}
if i.UserPermissions != nil {
clone.UserPermissions = map[string]permission.Access{}
for k, v := range i.UserPermissions {
clone.UserPermissions[k] = v
}
}
return &clone
}
// ItemChange is the multiwatcher representation of a core settings ItemChange.
type ItemChange struct {
Type int
Key string
OldValue interface{}
NewValue interface{}
}
// BranchInfo holds data about a model branch
// that is tracked by multiwatcherStore.
type BranchInfo struct {
ModelUUID string
ID string
Name string
AssignedUnits map[string][]string
Config map[string][]ItemChange
Created int64
CreatedBy string
Completed int64
CompletedBy string
GenerationID int
}
// EntityID returns a unique identifier for a generation.
func (i *BranchInfo) EntityID() EntityID {
return EntityID{
Kind: BranchKind,
ModelUUID: i.ModelUUID,
ID: i.ID,
}
}
// Clone returns a clone of the EntityInfo.
func (i *BranchInfo) Clone() EntityInfo {
clone := *i
if i.AssignedUnits != nil {
clone.AssignedUnits = map[string][]string{}
for k, units := range i.AssignedUnits {
cUnits := make([]string, len(units))
copy(cUnits, units)
clone.AssignedUnits[k] = cUnits
}
}
if i.Config != nil {
clone.Config = map[string][]ItemChange{}
for k, itemChanges := range i.Config {
cItems := make([]ItemChange, len(itemChanges))
copy(cItems, itemChanges)
clone.Config[k] = cItems
}
}
return &clone
}