-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprecheck.go
540 lines (482 loc) · 16.8 KB
/
precheck.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package migration
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/juju/replicaset/v3"
"github.com/juju/version/v2"
"github.com/juju/juju/apiserver/common"
coremigration "github.com/juju/juju/core/migration"
"github.com/juju/juju/core/presence"
"github.com/juju/juju/core/status"
environscloudspec "github.com/juju/juju/environs/cloudspec"
"github.com/juju/juju/state"
"github.com/juju/juju/tools"
"github.com/juju/juju/upgrades/upgradevalidation"
)
// PrecheckBackend defines the interface to query Juju's state
// for migration prechecks.
type PrecheckBackend interface {
AgentVersion() (version.Number, error)
NeedsCleanup() (bool, error)
Model() (PrecheckModel, error)
AllModelUUIDs() ([]string, error)
IsUpgrading() (bool, error)
IsMigrationActive(string) (bool, error)
AllMachines() ([]PrecheckMachine, error)
AllApplications() ([]PrecheckApplication, error)
AllRelations() ([]PrecheckRelation, error)
AllCharmURLs() ([]*string, error)
ControllerBackend() (PrecheckBackend, error)
CloudCredential(tag names.CloudCredentialTag) (state.Credential, error)
HasUpgradeSeriesLocks() (bool, error)
MachineCountForBase(base ...state.Base) (map[string]int, error)
MongoCurrentStatus() (*replicaset.Status, error)
}
// Pool defines the interface to a StatePool used by the migration
// prechecks.
type Pool interface {
GetModel(string) (PrecheckModel, func(), error)
}
// PrecheckModel describes the state interface a model as needed by
// the migration prechecks.
type PrecheckModel interface {
UUID() string
Name() string
Type() state.ModelType
Owner() names.UserTag
Life() state.Life
MigrationMode() state.MigrationMode
AgentVersion() (version.Number, error)
CloudCredentialTag() (names.CloudCredentialTag, bool)
}
// PrecheckMachine describes the state interface for a machine needed
// by migration prechecks.
type PrecheckMachine interface {
Id() string
AgentTools() (*tools.Tools, error)
Life() state.Life
Status() (status.StatusInfo, error)
InstanceStatus() (status.StatusInfo, error)
ShouldRebootOrShutdown() (state.RebootAction, error)
}
// PrecheckApplication describes the state interface for an
// application needed by migration prechecks.
type PrecheckApplication interface {
Name() string
Life() state.Life
CharmURL() (*string, bool)
AllUnits() ([]PrecheckUnit, error)
MinUnits() int
}
// PrecheckUnit describes state interface for a unit needed by
// migration prechecks.
type PrecheckUnit interface {
Name() string
AgentTools() (*tools.Tools, error)
Life() state.Life
CharmURL() *string
AgentStatus() (status.StatusInfo, error)
Status() (status.StatusInfo, error)
ShouldBeAssigned() bool
IsSidecar() (bool, error)
}
// PrecheckRelation describes the state interface for relations needed
// for prechecks.
type PrecheckRelation interface {
String() string
Endpoints() []state.Endpoint
Unit(PrecheckUnit) (PrecheckRelationUnit, error)
AllRemoteUnits(appName string) ([]PrecheckRelationUnit, error)
RemoteApplication() (string, bool, error)
}
// PrecheckRelationUnit describes the interface for relation units
// needed for migration prechecks.
type PrecheckRelationUnit interface {
Valid() (bool, error)
InScope() (bool, error)
UnitName() string
}
// ModelPresence represents the API server connections for a model.
type ModelPresence interface {
// For a given non controller agent, return the Status for that agent.
AgentStatus(agent string) (presence.Status, error)
}
// SourcePrecheck checks the state of the source controller to make
// sure that the preconditions for model migration are met. The
// backend provided must be for the model to be migrated.
func SourcePrecheck(
backend PrecheckBackend,
targetControllerVersion version.Number,
modelPresence ModelPresence, controllerPresence ModelPresence,
environscloudspecGetter func(names.ModelTag) (environscloudspec.CloudSpec, error),
) error {
ctx := precheckContext{
backend: backend,
presence: modelPresence,
targetControllerVersion: targetControllerVersion,
environscloudspecGetter: environscloudspecGetter,
}
if err := ctx.checkModel(); err != nil {
return errors.Trace(err)
}
if err := ctx.checkMachines(); err != nil {
return errors.Trace(err)
}
appUnits, err := ctx.checkApplications()
if err != nil {
return errors.Trace(err)
}
if err := ctx.checkRelations(appUnits); err != nil {
return errors.Trace(err)
}
if cleanupNeeded, err := backend.NeedsCleanup(); err != nil {
return errors.Annotate(err, "checking cleanups")
} else if cleanupNeeded {
return errors.New("cleanup needed")
}
// Check the source controller.
controllerBackend, err := backend.ControllerBackend()
if err != nil {
return errors.Trace(err)
}
controllerCtx := precheckContext{
backend: controllerBackend,
presence: controllerPresence,
targetControllerVersion: targetControllerVersion,
environscloudspecGetter: environscloudspecGetter,
}
if err := controllerCtx.checkController(); err != nil {
return errors.Annotate(err, "controller")
}
return nil
}
type precheckContext struct {
backend PrecheckBackend
presence ModelPresence
targetControllerVersion version.Number
environscloudspecGetter func(names.ModelTag) (environscloudspec.CloudSpec, error)
}
func (ctx *precheckContext) checkModel() error {
model, err := ctx.backend.Model()
if err != nil {
return errors.Annotate(err, "retrieving model")
}
if model.Life() != state.Alive {
return errors.Errorf("model is %s", model.Life())
}
if model.MigrationMode() == state.MigrationModeImporting {
return errors.New("model is being imported as part of another migration")
}
if credTag, found := model.CloudCredentialTag(); found {
creds, err := ctx.backend.CloudCredential(credTag)
if err != nil {
return errors.Trace(err)
}
if creds.Revoked {
return errors.New("model has revoked credentials")
}
}
cloudspec, err := ctx.environscloudspecGetter(names.NewModelTag(model.UUID()))
if err != nil {
return errors.Trace(err)
}
validators := upgradevalidation.ValidatorsForModelMigrationSource(ctx.targetControllerVersion, cloudspec)
checker := upgradevalidation.NewModelUpgradeCheck(model.UUID(), nil, ctx.backend, model, validators...)
blockers, err := checker.Validate()
if err != nil {
return errors.Trace(err)
}
if blockers == nil {
return nil
}
return errors.NewNotSupported(nil, fmt.Sprintf("cannot migrate to controller (%q) due to issues:\n%s", ctx.targetControllerVersion, blockers))
}
// TargetPrecheck checks the state of the target controller to make
// sure that the preconditions for model migration are met. The
// backend provided must be for the target controller.
func TargetPrecheck(backend PrecheckBackend, pool Pool, modelInfo coremigration.ModelInfo, presence ModelPresence) error {
if err := modelInfo.Validate(); err != nil {
return errors.Trace(err)
}
// This check is necessary because there is a window between the
// REAP phase and then end of the DONE phase where a model's
// documents have been deleted but the migration isn't quite done
// yet. Migrating a model back into the controller during this
// window can upset the migrationmaster worker.
//
// See also https://lpad.tv/1611391
if migrating, err := backend.IsMigrationActive(modelInfo.UUID); err != nil {
return errors.Annotate(err, "checking for active migration")
} else if migrating {
return errors.New("model is being migrated out of target controller")
}
controllerVersion, err := backend.AgentVersion()
if err != nil {
return errors.Annotate(err, "retrieving model version")
}
if controllerVersion.Compare(modelInfo.AgentVersion) < 0 {
return errors.Errorf("model has higher version than target controller (%s > %s)",
modelInfo.AgentVersion, controllerVersion)
}
if !controllerVersionCompatible(modelInfo.ControllerAgentVersion, controllerVersion) {
return errors.Errorf("source controller has higher version than target controller (%s > %s)",
modelInfo.ControllerAgentVersion, controllerVersion)
}
// The MigrateToAllowed check is the same as validating if a model can be migrated
// to a controller running a newer Juju version.
allowed, minVer, err := upgradevalidation.MigrateToAllowed(modelInfo.AgentVersion, controllerVersion)
if err != nil {
return errors.Maskf(err, "unknown target controller version %v", controllerVersion)
}
if !allowed {
return errors.Errorf("model must be upgraded to at least version %s before being migrated to a controller with version %s", minVer, controllerVersion)
}
controllerCtx := precheckContext{
backend: backend,
presence: presence,
targetControllerVersion: controllerVersion,
}
if err := controllerCtx.checkController(); err != nil {
return errors.Trace(err)
}
// Check for conflicts with existing models
modelUUIDs, err := backend.AllModelUUIDs()
if err != nil {
return errors.Annotate(err, "retrieving models")
}
for _, modelUUID := range modelUUIDs {
model, release, err := pool.GetModel(modelUUID)
if err != nil {
return errors.Trace(err)
}
defer release()
// If the model is importing then it's probably left behind
// from a previous migration attempt. It will be removed
// before the next import.
if model.UUID() == modelInfo.UUID && model.MigrationMode() != state.MigrationModeImporting {
return errors.Errorf("model with same UUID already exists (%s)", modelInfo.UUID)
}
if model.Name() == modelInfo.Name && model.Owner() == modelInfo.Owner {
return errors.Errorf("model named %q already exists", model.Name())
}
}
return nil
}
func controllerVersionCompatible(sourceVersion, targetVersion version.Number) bool {
// Compare source controller version to target controller version, only
// considering major and minor version numbers. Downgrades between
// patch, build releases for a given major.minor release are
// ok. Tag differences are ok too.
sourceVersion = versionToMajMin(sourceVersion)
targetVersion = versionToMajMin(targetVersion)
return sourceVersion.Compare(targetVersion) <= 0
}
func versionToMajMin(ver version.Number) version.Number {
ver.Patch = 0
ver.Build = 0
ver.Tag = ""
return ver
}
func (ctx *precheckContext) checkController() error {
model, err := ctx.backend.Model()
if err != nil {
return errors.Annotate(err, "retrieving model")
}
if model.Life() != state.Alive {
return errors.Errorf("model is %s", model.Life())
}
if upgrading, err := ctx.backend.IsUpgrading(); err != nil {
return errors.Annotate(err, "checking for upgrades")
} else if upgrading {
return errors.New("upgrade in progress")
}
return errors.Trace(ctx.checkMachines())
}
func (ctx *precheckContext) checkMachines() error {
modelVersion, err := ctx.backend.AgentVersion()
if err != nil {
return errors.Annotate(err, "retrieving model version")
}
machines, err := ctx.backend.AllMachines()
if err != nil {
return errors.Annotate(err, "retrieving machines")
}
modelPresenceContext := common.ModelPresenceContext{Presence: ctx.presence}
for _, machine := range machines {
if machine.Life() != state.Alive {
return errors.Errorf("machine %s is %s", machine.Id(), machine.Life())
}
if statusInfo, err := machine.InstanceStatus(); err != nil {
return errors.Annotatef(err, "retrieving machine %s instance status", machine.Id())
} else if statusInfo.Status != status.Running {
return newStatusError("machine %s not running", machine.Id(), statusInfo.Status)
}
if statusInfo, err := modelPresenceContext.MachineStatus(machine); err != nil {
return errors.Annotatef(err, "retrieving machine %s status", machine.Id())
} else if statusInfo.Status != status.Started {
return newStatusError("machine %s agent not functioning at this time",
machine.Id(), statusInfo.Status)
}
if rebootAction, err := machine.ShouldRebootOrShutdown(); err != nil {
return errors.Annotatef(err, "retrieving machine %s reboot status", machine.Id())
} else if rebootAction != state.ShouldDoNothing {
return errors.Errorf("machine %s is scheduled to %s", machine.Id(), rebootAction)
}
if err := checkAgentTools(modelVersion, machine, "machine "+machine.Id()); err != nil {
return errors.Trace(err)
}
}
return nil
}
func (ctx *precheckContext) checkApplications() (map[string][]PrecheckUnit, error) {
modelVersion, err := ctx.backend.AgentVersion()
if err != nil {
return nil, errors.Annotate(err, "retrieving model version")
}
apps, err := ctx.backend.AllApplications()
if err != nil {
return nil, errors.Annotate(err, "retrieving applications")
}
model, err := ctx.backend.Model()
if err != nil {
return nil, errors.Annotate(err, "retrieving model")
}
appUnits := make(map[string][]PrecheckUnit, len(apps))
for _, app := range apps {
if app.Life() != state.Alive {
return nil, errors.Errorf("application %s is %s", app.Name(), app.Life())
}
units, err := app.AllUnits()
if err != nil {
return nil, errors.Annotatef(err, "retrieving units for %s", app.Name())
}
err = ctx.checkUnits(app, units, modelVersion, model.Type())
if err != nil {
return nil, errors.Trace(err)
}
appUnits[app.Name()] = units
}
return appUnits, nil
}
func (ctx *precheckContext) checkUnits(app PrecheckApplication, units []PrecheckUnit, modelVersion version.Number, modelType state.ModelType) error {
if len(units) < app.MinUnits() {
return errors.Errorf("application %s is below its minimum units threshold", app.Name())
}
appCharmURL, _ := app.CharmURL()
if appCharmURL == nil {
return errors.Errorf("application charm url is nil")
}
for _, unit := range units {
if unit.Life() != state.Alive {
return errors.Errorf("unit %s is %s", unit.Name(), unit.Life())
}
if err := ctx.checkUnitAgentStatus(unit); err != nil {
return errors.Trace(err)
}
if modelType == state.ModelTypeIAAS {
if err := checkAgentTools(modelVersion, unit, "unit "+unit.Name()); err != nil {
return errors.Trace(err)
}
}
unitCharmURL := unit.CharmURL()
if unitCharmURL == nil || *appCharmURL != *unitCharmURL {
return errors.Errorf("unit %s is upgrading", unit.Name())
}
}
return nil
}
func (ctx *precheckContext) checkUnitAgentStatus(unit PrecheckUnit) error {
modelPresenceContext := common.ModelPresenceContext{ctx.presence}
statusData, _ := modelPresenceContext.UnitStatus(unit)
if statusData.Err != nil {
return errors.Annotatef(statusData.Err, "retrieving unit %s status", unit.Name())
}
agentStatus := statusData.Status.Status
switch agentStatus {
case status.Idle, status.Executing:
// These two are fine.
default:
return newStatusError("unit %s not idle or executing", unit.Name(), agentStatus)
}
return nil
}
func checkAgentTools(modelVersion version.Number, agent agentToolsGetter, agentLabel string) error {
tools, err := agent.AgentTools()
if err != nil {
return errors.Annotatef(err, "retrieving agent binaries for %s", agentLabel)
}
agentVersion := tools.Version.Number
if agentVersion != modelVersion {
return errors.Errorf("%s agent binaries don't match model (%s != %s)",
agentLabel, agentVersion, modelVersion)
}
return nil
}
type agentToolsGetter interface {
AgentTools() (*tools.Tools, error)
}
func newStatusError(format, id string, s status.Status) error {
msg := fmt.Sprintf(format, id)
if s != status.Empty {
msg += fmt.Sprintf(" (%s)", s)
}
return errors.New(msg)
}
func (ctx *precheckContext) checkRelations(appUnits map[string][]PrecheckUnit) error {
relations, err := ctx.backend.AllRelations()
if err != nil {
return errors.Annotate(err, "retrieving model relations")
}
for _, rel := range relations {
remoteAppName, crossModel, err := rel.RemoteApplication()
if err != nil && !errors.IsNotFound(err) {
return errors.Annotatef(err, "checking whether relation %s is cross-model", rel)
}
checkRelationUnit := func(ru PrecheckRelationUnit) error {
valid, err := ru.Valid()
if err != nil {
return errors.Trace(err)
}
if !valid {
return nil
}
inScope, err := ru.InScope()
if err != nil {
return errors.Trace(err)
}
if !inScope {
return errors.Errorf("unit %s hasn't joined relation %q yet", ru.UnitName(), rel)
}
return nil
}
for _, ep := range rel.Endpoints() {
// The endpoint app is either local or cross model.
// Handle each one as appropriate.
if crossModel && ep.ApplicationName == remoteAppName {
remoteUnits, err := rel.AllRemoteUnits(remoteAppName)
if err != nil {
return errors.Trace(err)
}
for _, ru := range remoteUnits {
if err := checkRelationUnit(ru); err != nil {
return errors.Trace(err)
}
}
} else {
for _, unit := range appUnits[ep.ApplicationName] {
ru, err := rel.Unit(unit)
if err != nil {
return errors.Trace(err)
}
if err := checkRelationUnit(ru); err != nil {
return errors.Trace(err)
}
}
}
}
}
return nil
}