forked from juju/juju
/
manifolds.go
543 lines (481 loc) · 21.3 KB
/
manifolds.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package machine
import (
"runtime"
"time"
"github.com/juju/errors"
"github.com/juju/pubsub"
"github.com/juju/utils/clock"
"github.com/juju/utils/proxy"
"github.com/juju/utils/voyeur"
"github.com/juju/version"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/juju/worker.v1"
coreagent "github.com/juju/juju/agent"
"github.com/juju/juju/api"
"github.com/juju/juju/api/base"
apideployer "github.com/juju/juju/api/deployer"
"github.com/juju/juju/cmd/jujud/agent/engine"
"github.com/juju/juju/container/lxd"
"github.com/juju/juju/environs"
"github.com/juju/juju/state"
proxyconfig "github.com/juju/juju/utils/proxy"
jworker "github.com/juju/juju/worker"
"github.com/juju/juju/worker/agent"
"github.com/juju/juju/worker/apiaddressupdater"
"github.com/juju/juju/worker/apicaller"
"github.com/juju/juju/worker/apiconfigwatcher"
"github.com/juju/juju/worker/authenticationworker"
"github.com/juju/juju/worker/centralhub"
"github.com/juju/juju/worker/dependency"
"github.com/juju/juju/worker/deployer"
"github.com/juju/juju/worker/diskmanager"
"github.com/juju/juju/worker/fortress"
"github.com/juju/juju/worker/gate"
"github.com/juju/juju/worker/hostkeyreporter"
"github.com/juju/juju/worker/identityfilewriter"
"github.com/juju/juju/worker/logforwarder"
"github.com/juju/juju/worker/logforwarder/sinks"
"github.com/juju/juju/worker/logger"
"github.com/juju/juju/worker/logsender"
"github.com/juju/juju/worker/machineactions"
"github.com/juju/juju/worker/machiner"
"github.com/juju/juju/worker/migrationflag"
"github.com/juju/juju/worker/migrationminion"
"github.com/juju/juju/worker/proxyupdater"
"github.com/juju/juju/worker/reboot"
"github.com/juju/juju/worker/resumer"
workerstate "github.com/juju/juju/worker/state"
"github.com/juju/juju/worker/stateconfigwatcher"
"github.com/juju/juju/worker/storageprovisioner"
"github.com/juju/juju/worker/terminationworker"
"github.com/juju/juju/worker/toolsversionchecker"
"github.com/juju/juju/worker/upgrader"
"github.com/juju/juju/worker/upgradesteps"
)
// ManifoldsConfig allows specialisation of the result of Manifolds.
type ManifoldsConfig struct {
// Agent contains the agent that will be wrapped and made available to
// its dependencies via a dependency.Engine.
Agent coreagent.Agent
// AgentConfigChanged is set whenever the machine agent's config
// is updated.
AgentConfigChanged *voyeur.Value
// RootDir is the root directory that any worker that needs to
// access local filesystems should use as a base. In actual use it
// will be "" but it may be overriden in tests.
RootDir string
// PreviousAgentVersion passes through the version the machine
// agent was running before the current restart.
PreviousAgentVersion version.Number
// UpgradeStepsLock is passed to the upgrade steps gate to
// coordinate workers that shouldn't do anything until the
// upgrade-steps worker is done.
UpgradeStepsLock gate.Lock
// UpgradeCheckLock is passed to the upgrade check gate to
// coordinate workers that shouldn't do anything until the
// upgrader worker completes it's first check.
UpgradeCheckLock gate.Lock
// OpenState is function used by the state manifold to create a
// *state.State.
OpenState func(coreagent.Config) (*state.State, error)
// OpenStateForUpgrade is a function the upgradesteps worker can
// use to establish a connection to state.
OpenStateForUpgrade func() (*state.State, error)
// StartStateWorkers is function called by the stateworkers
// manifold to start workers which rely on a *state.State but
// which haven't been converted to run directly under the
// dependency engine yet. This will go once these workers have
// been converted.
StartStateWorkers func(*state.State) (worker.Worker, error)
// StartAPIWorkers is passed to the apiworkers manifold. It starts
// workers which rely on an API connection (which have not yet
// been converted to work directly with the dependency engine).
StartAPIWorkers func(api.Connection) (worker.Worker, error)
// PreUpgradeSteps is a function that is used by the upgradesteps
// worker to ensure that conditions are OK for an upgrade to
// proceed.
PreUpgradeSteps func(*state.State, coreagent.Config, bool, bool) error
// LogSource defines the channel type used to send log message
// structs within the machine agent.
LogSource logsender.LogRecordCh
// newDeployContext gives the tests the opportunity to create a deployer.Context
// that can be used for testing so as to avoid (1) deploying units to the system
// running the tests and (2) get access to the *State used internally, so that
// tests can be run without waiting for the 5s watcher refresh time to which we would
// otherwise be restricted.
NewDeployContext func(st *apideployer.State, agentConfig coreagent.Config) deployer.Context
// NewEnvironFunc is a function opens a provider "environment"
// (typically environs.New).
NewEnvironFunc environs.NewEnvironFunc
// Clock supplies timekeeping services to various workers.
Clock clock.Clock
// ValidateMigration is called by the migrationminion during the
// migration process to check that the agent will be ok when
// connected to the new target controller.
ValidateMigration func(base.APICaller) error
// PrometheusRegisterer is a prometheus.Registerer that may be used
// by workers to register Prometheus metric collectors.
PrometheusRegisterer prometheus.Registerer
// CentralHub is the primary hub that exists in the apiserver.
CentralHub *pubsub.StructuredHub
// DepEngineReporter is a dependency engine reporter.
DepEngineReporter dependency.Reporter
}
// Manifolds returns a set of co-configured manifolds covering the
// various responsibilities of a machine agent.
//
// Thou Shalt Not Use String Literals In This Function. Or Else.
func Manifolds(config ManifoldsConfig) dependency.Manifolds {
// connectFilter exists:
// 1) to let us retry api connections immediately on password change,
// rather than causing the dependency engine to wait for a while;
// 2) to ensure that certain connection failures correctly trigger
// complete agent removal. (It's not safe to let any agent other
// than the machine mess around with SetCanUninstall).
connectFilter := func(err error) error {
cause := errors.Cause(err)
if cause == apicaller.ErrConnectImpossible {
err2 := coreagent.SetCanUninstall(config.Agent)
if err2 != nil {
return errors.Trace(err2)
}
return jworker.ErrTerminateAgent
} else if cause == apicaller.ErrChangedPassword {
return dependency.ErrBounce
}
return err
}
var externalUpdateProxyFunc func(proxy.Settings) error
if runtime.GOOS == "linux" {
externalUpdateProxyFunc = lxd.ConfigureLXDProxies
}
return dependency.Manifolds{
// The agent manifold references the enclosing agent, and is the
// foundation stone on which most other manifolds ultimately depend.
agentName: agent.Manifold(config.Agent),
// The termination worker returns ErrTerminateAgent if a
// termination signal is received by the process it's running
// in. It has no inputs and its only output is the error it
// returns. It depends on the uninstall file having been
// written *by the manual provider* at install time; it would
// be Very Wrong Indeed to use SetCanUninstall in conjunction
// with this code.
terminationName: terminationworker.Manifold(),
// The stateconfigwatcher manifold watches the machine agent's
// configuration and reports if state serving info is
// present. It will bounce itself if state serving info is
// added or removed. It is intended as a dependency just for
// the state manifold.
stateConfigWatcherName: stateconfigwatcher.Manifold(stateconfigwatcher.ManifoldConfig{
AgentName: agentName,
AgentConfigChanged: config.AgentConfigChanged,
}),
// The centralhub manifold watches the state config to make sure it
// only starts for machines that are api servers. Currently the hub is
// passed in as config, but when the apiserver and peergrouper are
// updated to use the dependency engine, the centralhub manifold
// should also take the agentName so the worker can get the machine ID
// for the creation of the hub.
centralHubName: centralhub.Manifold(centralhub.ManifoldConfig{
StateConfigWatcherName: stateConfigWatcherName,
Hub: config.CentralHub,
}),
// The state manifold creates a *state.State and makes it
// available to other manifolds. It pings the mongodb session
// regularly and will die if pings fail.
stateName: workerstate.Manifold(workerstate.ManifoldConfig{
AgentName: agentName,
StateConfigWatcherName: stateConfigWatcherName,
OpenState: config.OpenState,
}),
// The stateworkers manifold starts workers which rely on a
// *state.State but which haven't been converted to run
// directly under the dependency engine yet. This manifold
// will be removed once all such workers have been converted;
// until then, the workers are expected to handle their own
// checks for upgrades etc, rather than blocking this whole
// worker on upgrade completion.
stateWorkersName: StateWorkersManifold(StateWorkersConfig{
StateName: stateName,
StartStateWorkers: config.StartStateWorkers,
}),
// The api-config-watcher manifold monitors the API server
// addresses in the agent config and bounces when they
// change. It's required as part of model migrations.
apiConfigWatcherName: apiconfigwatcher.Manifold(apiconfigwatcher.ManifoldConfig{
AgentName: agentName,
AgentConfigChanged: config.AgentConfigChanged,
}),
// The api caller is a thin concurrent wrapper around a connection
// to some API server. It's used by many other manifolds, which all
// select their own desired facades. It will be interesting to see
// how this works when we consolidate the agents; might be best to
// handle the auth changes server-side..?
apiCallerName: apicaller.Manifold(apicaller.ManifoldConfig{
AgentName: agentName,
APIConfigWatcherName: apiConfigWatcherName,
APIOpen: api.Open,
NewConnection: apicaller.ScaryConnect,
Filter: connectFilter,
}),
// The upgrade steps gate is used to coordinate workers which
// shouldn't do anything until the upgrade-steps worker has
// finished running any required upgrade steps. The flag of
// similar name is used to implement the isFullyUpgraded func
// that keeps upgrade concerns out of unrelated manifolds.
upgradeStepsGateName: gate.ManifoldEx(config.UpgradeStepsLock),
upgradeStepsFlagName: gate.FlagManifold(gate.FlagManifoldConfig{
GateName: upgradeStepsGateName,
NewWorker: gate.NewFlagWorker,
}),
// The upgrade check gate is used to coordinate workers which
// shouldn't do anything until the upgrader worker has
// completed its first check for a new tools version to
// upgrade to. The flag of similar name is used to implement
// the isFullyUpgraded func that keeps upgrade concerns out of
// unrelated manifolds.
upgradeCheckGateName: gate.ManifoldEx(config.UpgradeCheckLock),
upgradeCheckFlagName: gate.FlagManifold(gate.FlagManifoldConfig{
GateName: upgradeCheckGateName,
NewWorker: gate.NewFlagWorker,
}),
// The upgrader is a leaf worker that returns a specific error
// type recognised by the machine agent, causing other workers
// to be stopped and the agent to be restarted running the new
// tools. We should only need one of these in a consolidated
// agent, but we'll need to be careful about behavioural
// differences, and interactions with the upgrade-steps
// worker.
upgraderName: upgrader.Manifold(upgrader.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
UpgradeStepsGateName: upgradeStepsGateName,
UpgradeCheckGateName: upgradeCheckGateName,
PreviousAgentVersion: config.PreviousAgentVersion,
}),
// The upgradesteps worker runs soon after the machine agent
// starts and runs any steps required to upgrade to the
// running jujud version. Once upgrade steps have run, the
// upgradesteps gate is unlocked and the worker exits.
upgradeStepsName: upgradesteps.Manifold(upgradesteps.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
UpgradeStepsGateName: upgradeStepsGateName,
OpenStateForUpgrade: config.OpenStateForUpgrade,
PreUpgradeSteps: config.PreUpgradeSteps,
NewEnvironFunc: config.NewEnvironFunc,
}),
// The migration workers collaborate to run migrations;
// and to create a mechanism for running other workers
// so they can't accidentally interfere with a migration
// in progress. Such a manifold should (1) depend on the
// migration-inactive flag, to know when to start or die;
// and (2) occupy the migration-fortress, so as to avoid
// possible interference with the minion (which will not
// take action until it's gained sole control of the
// fortress).
//
// Note that the fortress itself will not be created
// until the upgrade process is complete; this frees all
// its dependencies from upgrade concerns.
migrationFortressName: ifFullyUpgraded(fortress.Manifold()),
migrationInactiveFlagName: migrationflag.Manifold(migrationflag.ManifoldConfig{
APICallerName: apiCallerName,
Check: migrationflag.IsTerminal,
NewFacade: migrationflag.NewFacade,
NewWorker: migrationflag.NewWorker,
}),
migrationMinionName: migrationminion.Manifold(migrationminion.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
FortressName: migrationFortressName,
APIOpen: api.Open,
ValidateMigration: config.ValidateMigration,
NewFacade: migrationminion.NewFacade,
NewWorker: migrationminion.NewWorker,
}),
// The serving-info-setter manifold sets grabs the state
// serving info from the API connection and writes it to the
// agent config.
servingInfoSetterName: ifNotMigrating(ServingInfoSetterManifold(ServingInfoSetterConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
// The apiworkers manifold starts workers which rely on the
// machine agent's API connection but have not been converted
// to work directly under the dependency engine. It waits for
// upgrades to be finished before starting these workers.
apiWorkersName: ifNotMigrating(APIWorkersManifold(APIWorkersConfig{
APICallerName: apiCallerName,
StartAPIWorkers: config.StartAPIWorkers,
})),
// The reboot manifold manages a worker which will reboot the
// machine when requested. It needs an API connection and
// waits for upgrades to be complete.
rebootName: ifNotMigrating(reboot.Manifold(reboot.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
MachineLockName: coreagent.MachineLockName,
Clock: config.Clock,
})),
// The logging config updater is a leaf worker that indirectly
// controls the messages sent via the log sender or rsyslog,
// according to changes in environment config. We should only need
// one of these in a consolidated agent.
loggingConfigUpdaterName: ifNotMigrating(logger.Manifold(logger.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
// The diskmanager worker periodically lists block devices on the
// machine it runs on. This worker will be run on all Juju-managed
// machines (one per machine agent).
diskManagerName: ifNotMigrating(diskmanager.Manifold(diskmanager.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
// The proxy config updater is a leaf worker that sets http/https/apt/etc
// proxy settings.
proxyConfigUpdater: ifNotMigrating(proxyupdater.Manifold(proxyupdater.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
WorkerFunc: proxyupdater.NewWorker,
ExternalUpdate: externalUpdateProxyFunc,
InProcessUpdate: proxyconfig.DefaultConfig.Set,
})),
// The api address updater is a leaf worker that rewrites agent config
// as the state server addresses change. We should only need one of
// these in a consolidated agent.
apiAddressUpdaterName: ifNotMigrating(apiaddressupdater.Manifold(apiaddressupdater.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
// The machiner Worker will wait for the identified machine to become
// Dying and make it Dead; or until the machine becomes Dead by other
// means.
machinerName: ifNotMigrating(machiner.Manifold(machiner.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
// The log sender is a leaf worker that sends log messages to some
// API server, when configured so to do. We should only need one of
// these in a consolidated agent.
//
// NOTE: the LogSource will buffer a large number of messages as an upgrade
// runs; it currently seems better to fill the buffer and send when stable,
// optimising for stable controller upgrades rather than up-to-the-moment
// observable normal-machine upgrades.
logSenderName: ifNotMigrating(logsender.Manifold(logsender.ManifoldConfig{
APICallerName: apiCallerName,
LogSource: config.LogSource,
})),
// The deployer worker is responsible for deploying and recalling unit
// agents, according to changes in a set of state units; and for the
// final removal of its agents' units from state when they are no
// longer needed.
deployerName: ifNotMigrating(deployer.Manifold(deployer.ManifoldConfig{
NewDeployContext: config.NewDeployContext,
AgentName: agentName,
APICallerName: apiCallerName,
})),
authenticationWorkerName: ifNotMigrating(authenticationworker.Manifold(authenticationworker.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
// The storageProvisioner worker manages provisioning
// (deprovisioning), and attachment (detachment) of first-class
// volumes and filesystems.
storageProvisionerName: ifNotMigrating(storageprovisioner.MachineManifold(storageprovisioner.MachineManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
Clock: config.Clock,
})),
resumerName: ifNotMigrating(resumer.Manifold(resumer.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
Clock: config.Clock,
Interval: time.Minute,
NewFacade: resumer.NewFacade,
NewWorker: resumer.NewWorker,
})),
identityFileWriterName: ifNotMigrating(identityfilewriter.Manifold(identityfilewriter.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
toolsVersionCheckerName: ifNotMigrating(toolsversionchecker.Manifold(toolsversionchecker.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
})),
machineActionName: ifNotMigrating(machineactions.Manifold(machineactions.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
NewFacade: machineactions.NewFacade,
NewWorker: machineactions.NewMachineActionsWorker,
})),
hostKeyReporterName: ifNotMigrating(hostkeyreporter.Manifold(hostkeyreporter.ManifoldConfig{
AgentName: agentName,
APICallerName: apiCallerName,
RootDir: config.RootDir,
NewFacade: hostkeyreporter.NewFacade,
NewWorker: hostkeyreporter.NewWorker,
})),
logForwarderName: ifFullyUpgraded(logforwarder.Manifold(logforwarder.ManifoldConfig{
StateName: stateName,
APICallerName: apiCallerName,
Sinks: []logforwarder.LogSinkSpec{{
Name: "juju-log-forward",
OpenFn: sinks.OpenSyslog,
}},
})),
}
}
var ifFullyUpgraded = engine.Housing{
Flags: []string{
upgradeStepsFlagName,
upgradeCheckFlagName,
},
}.Decorate
var ifNotMigrating = engine.Housing{
Flags: []string{
migrationInactiveFlagName,
},
Occupy: migrationFortressName,
}.Decorate
const (
agentName = "agent"
terminationName = "termination-signal-handler"
stateConfigWatcherName = "state-config-watcher"
stateName = "state"
stateWorkersName = "unconverted-state-workers"
apiCallerName = "api-caller"
apiConfigWatcherName = "api-config-watcher"
centralHubName = "central-hub"
upgraderName = "upgrader"
upgradeStepsName = "upgrade-steps-runner"
upgradeStepsGateName = "upgrade-steps-gate"
upgradeStepsFlagName = "upgrade-steps-flag"
upgradeCheckGateName = "upgrade-check-gate"
upgradeCheckFlagName = "upgrade-check-flag"
migrationFortressName = "migration-fortress"
migrationInactiveFlagName = "migration-inactive-flag"
migrationMinionName = "migration-minion"
servingInfoSetterName = "serving-info-setter"
apiWorkersName = "unconverted-api-workers"
rebootName = "reboot-executor"
loggingConfigUpdaterName = "logging-config-updater"
diskManagerName = "disk-manager"
proxyConfigUpdater = "proxy-config-updater"
apiAddressUpdaterName = "api-address-updater"
machinerName = "machiner"
logSenderName = "log-sender"
deployerName = "unit-agent-deployer"
authenticationWorkerName = "ssh-authkeys-updater"
storageProvisionerName = "storage-provisioner"
resumerName = "mgo-txn-resumer"
identityFileWriterName = "ssh-identity-writer"
toolsVersionCheckerName = "tools-version-checker"
machineActionName = "machine-action-runner"
hostKeyReporterName = "host-key-reporter"
logForwarderName = "log-forwarder"
)