/
externalcontroller.go
342 lines (301 loc) · 11.2 KB
/
externalcontroller.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/mgo/v3"
"github.com/juju/mgo/v3/bson"
"github.com/juju/mgo/v3/txn"
"github.com/juju/names/v5"
"github.com/juju/juju/core/crossmodel"
)
// ExternalController represents the state of a controller hosting
// other models.
type ExternalController interface {
// Id returns the external controller UUID, also used as
// the mongo id.
Id() string
// ControllerInfo returns the details required to connect to the
// external controller.
ControllerInfo() crossmodel.ControllerInfo
}
// externalController is an implementation of ExternalController.
type externalController struct {
doc externalControllerDoc
}
type externalControllerDoc struct {
// Id holds external controller document key.
// It is the controller UUID.
Id string `bson:"_id"`
// Alias holds an alias (human friendly) name for the controller.
Alias string `bson:"alias"`
// Addrs holds the host:port values for the external
// controller's API server.
Addrs []string `bson:"addresses"`
// CACert holds the certificate to validate the external
// controller's target API server's TLS certificate.
CACert string `bson:"cacert"`
// Models holds model UUIDs hosted on this controller.
Models []string `bson:"models"`
}
// newExternalControllerDoc returns a new external controller document
// reference representing the input controller info.
func newExternalControllerDoc(controller crossmodel.ControllerInfo) *externalControllerDoc {
return &externalControllerDoc{
Id: controller.ControllerTag.Id(),
Alias: controller.Alias,
Addrs: controller.Addrs,
CACert: controller.CACert,
}
}
// Id implements ExternalController.
func (rc *externalController) Id() string {
return rc.doc.Id
}
// ControllerInfo implements ExternalController.
func (rc *externalController) ControllerInfo() crossmodel.ControllerInfo {
return crossmodel.ControllerInfo{
ControllerTag: names.NewControllerTag(rc.doc.Id),
Alias: rc.doc.Alias,
Addrs: rc.doc.Addrs,
CACert: rc.doc.CACert,
}
}
// ExternalControllers instances provide access to external controllers in state.
type ExternalControllers interface {
Save(_ crossmodel.ControllerInfo, modelUUIDs ...string) (ExternalController, error)
SaveAndMoveModels(_ crossmodel.ControllerInfo, modelUUIDs ...string) error
Controller(controllerUUID string) (ExternalController, error)
ControllerForModel(modelUUID string) (ExternalController, error)
Remove(controllerUUID string) error
Watch() StringsWatcher
WatchController(controllerUUID string) NotifyWatcher
}
type externalControllers struct {
st *State
}
// NewExternalControllers creates an external controllers instance backed by a state.
func (s *State) NewExternalControllers() ExternalControllers {
return NewExternalControllers(s)
}
// NewExternalControllers creates an external controllers instance backed by a state.
func NewExternalControllers(st *State) *externalControllers {
return &externalControllers{st: st}
}
// Save creates or updates an external controller record.
func (ec *externalControllers) Save(
controller crossmodel.ControllerInfo, modelUUIDs ...string,
) (ExternalController, error) {
if err := controller.Validate(); err != nil {
return nil, errors.Trace(err)
}
doc := newExternalControllerDoc(controller)
buildTxn := func(int) ([]txn.Op, error) {
ops, err := ec.upsertExternalControllerOps(doc, modelUUIDs)
return ops, errors.Trace(err)
}
if err := ec.st.db().Run(buildTxn); err != nil {
return nil, errors.Annotate(err, "failed to save external controller")
}
return &externalController{
doc: *doc,
}, nil
}
// SaveAndMoveModels is the same as `Save`, but if any of the input model UUIDs
// are in other external external controllers, those records will be updated
// to disassociate them.
func (ec *externalControllers) SaveAndMoveModels(controller crossmodel.ControllerInfo, modelUUIDs ...string) error {
if err := controller.Validate(); err != nil {
return errors.Trace(err)
}
doc := newExternalControllerDoc(controller)
buildTxn := func(int) ([]txn.Op, error) {
// Find any controllers that already have one of
// the input model UUIDs associated with it.
controllers, err := ec.st.externalControllerDocsForModels(modelUUIDs...)
if err != nil {
return nil, errors.Trace(err)
}
var ops []txn.Op
changingModels := set.NewStrings(modelUUIDs...)
for _, controller := range controllers {
// If the controller to be saved already has one
// of the input model UUIDs, do not change it.
if controller.Id == doc.Id {
continue
}
// If the models for the controller need changing,
// generate a transaction operation.
currentModels := set.NewStrings(controller.Models...)
modelDiff := currentModels.Difference(changingModels)
if modelDiff.Size() != currentModels.Size() {
// TODO (manadart 2019-12-13): There is a case for deleting
// records with no more associated model UUIDs.
// We are being conservative here and keeping them.
ops = append(ops, txn.Op{
C: externalControllersC,
Id: controller.Id,
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{{"models", modelDiff.SortedValues()}}}},
})
}
}
newControllerOps, err := ec.upsertExternalControllerOps(doc, modelUUIDs)
if err != nil {
return nil, errors.Trace(err)
}
return append(ops, newControllerOps...), nil
}
return errors.Annotate(ec.st.db().Run(buildTxn), "saving location of external models")
}
// upsertExternalControllerOps returns the transaction operations for saving
// the input controller document as the location of the input models.
func (ec *externalControllers) upsertExternalControllerOps(
doc *externalControllerDoc, modelUUIDs []string,
) ([]txn.Op, error) {
model, err := ec.st.Model()
if err != nil {
return nil, errors.Annotate(err, "failed to load model")
}
if err := checkModelActive(ec.st); err != nil {
return nil, errors.Trace(err)
}
existing, err := ec.controller(doc.Id)
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
upsertOp := upsertExternalControllerOp(doc, existing, modelUUIDs)
ops := []txn.Op{
upsertOp,
model.assertActiveOp(),
}
return ops, nil
}
// Remove removes an external controller record with the given controller UUID.
func (ec *externalControllers) Remove(controllerUUID string) error {
ops := []txn.Op{{
C: externalControllersC,
Id: controllerUUID,
Remove: true,
}}
err := ec.st.db().RunTransaction(ops)
return errors.Annotate(err, "failed to remove external controller")
}
// Controller retrieves an ExternalController with a given controller UUID.
func (ec *externalControllers) Controller(controllerUUID string) (ExternalController, error) {
doc, err := ec.controller(controllerUUID)
if err != nil {
return nil, errors.Trace(err)
}
return &externalController{*doc}, nil
}
func (ec *externalControllers) controller(controllerUUID string) (*externalControllerDoc, error) {
coll, closer := ec.st.db().GetCollection(externalControllersC)
defer closer()
var doc externalControllerDoc
err := coll.FindId(controllerUUID).One(&doc)
if err == mgo.ErrNotFound {
return nil, errors.NotFoundf("external controller with UUID %v", controllerUUID)
}
if err != nil {
return nil, errors.Trace(err)
}
return &doc, nil
}
// ControllerForModel retrieves an ExternalController with a given model UUID.
func (ec *externalControllers) ControllerForModel(modelUUID string) (ExternalController, error) {
return ec.st.ExternalControllerForModel(modelUUID)
}
// Watch returns a strings watcher that watches for addition and removal of
// external controller documents. The strings returned will be the controller
// UUIDs.
func (ec *externalControllers) Watch() StringsWatcher {
return newExternalControllersWatcher(ec.st)
}
// WatchController returns a notify watcher that watches for changes to the
// external controller with the specified controller UUID.
func (ec *externalControllers) WatchController(controllerUUID string) NotifyWatcher {
return newEntityWatcher(ec.st, externalControllersC, controllerUUID)
}
// ExternalControllerForModel retrieves an ExternalController with a given
// model UUID.
// This is very similar to externalControllers.ControllerForModel, except the
// return type is a lot less strict, one that we can access the ModelUUIDs from
// the controller.
func (st *State) ExternalControllerForModel(modelUUID string) (*externalController, error) {
docs, err := st.externalControllerDocsForModels(modelUUID)
if err != nil {
return nil, errors.Trace(err)
}
switch len(docs) {
case 0:
return nil, errors.NotFoundf("external controller with model %v", modelUUID)
case 1:
return &externalController{doc: docs[0]}, nil
}
return nil, errors.Errorf("expected 1 controller with model %v, got %d", modelUUID, len(docs))
}
func (st *State) externalControllerDocsForModels(modelUUIDs ...string) ([]externalControllerDoc, error) {
coll, closer := st.db().GetCollection(externalControllersC)
defer closer()
var docs []externalControllerDoc
err := coll.Find(bson.M{"models": bson.M{"$in": modelUUIDs}}).All(&docs)
return docs, errors.Trace(err)
}
func upsertExternalControllerOp(doc, existing *externalControllerDoc, modelUUIDs []string) txn.Op {
if existing != nil {
models := set.NewStrings(existing.Models...)
models = models.Union(set.NewStrings(modelUUIDs...))
return txn.Op{
C: externalControllersC,
Id: existing.Id,
Assert: txn.DocExists,
Update: bson.D{
{"$set",
bson.D{
{"addresses", doc.Addrs},
{"alias", doc.Alias},
{"cacert", doc.CACert},
{"models", models.SortedValues()},
},
},
},
}
}
doc.Models = modelUUIDs
return txn.Op{
C: externalControllersC,
Id: doc.Id,
Assert: txn.DocMissing,
Insert: *doc,
}
}
// externalControllerRefCountKey returns a key for refcounting consumed
// apps for the specified controller. Each time a consumed app is created,
// the refcount is incremented, and the opposite happens on removal.
func externalControllerRefCountKey(controllerUUID string) string {
return fmt.Sprintf("controller#%s", controllerUUID)
}
// incExternalControllersRefOp returns a txn.Op that increments the reference
// count for an external controller. These ref counts are controller wide.
func incExternalControllersRefOp(mb modelBackend, controllerUUID string) (txn.Op, error) {
refcounts, closer := mb.db().GetCollection(globalRefcountsC)
defer closer()
refCountKey := externalControllerRefCountKey(controllerUUID)
incRefOp, err := nsRefcounts.CreateOrIncRefOp(refcounts, refCountKey, 1)
return incRefOp, errors.Trace(err)
}
// decExternalControllersRefOp returns a txn.Op that decrements the reference
// count for an external controller. These ref counts are controller wide.
func decExternalControllersRefOp(mb modelBackend, controllerUUID string) (txn.Op, bool, error) {
refcounts, closer := mb.db().GetCollection(globalRefcountsC)
defer closer()
refCountKey := externalControllerRefCountKey(controllerUUID)
decRefOp, isFinal, err := nsRefcounts.DyingDecRefOp(refcounts, refCountKey)
if err != nil {
return txn.Op{}, false, errors.Trace(err)
}
return decRefOp, isFinal, nil
}