/
manifold.go
109 lines (95 loc) · 3.01 KB
/
manifold.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package certupdater
import (
"github.com/juju/errors"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
jujuagent "github.com/juju/juju/agent"
"github.com/juju/juju/pki"
"github.com/juju/juju/state"
"github.com/juju/juju/worker/common"
workerstate "github.com/juju/juju/worker/state"
)
// ManifoldConfig holds the information necessary to run a certupdater
// in a dependency.Engine.
type ManifoldConfig struct {
AgentName string
AuthorityName string
StateName string
NewWorker func(Config) worker.Worker
NewMachineAddressWatcher func(st *state.State, machineId string) (AddressWatcher, error)
}
// Validate validates the manifold configuration.
func (config ManifoldConfig) Validate() error {
if config.AgentName == "" {
return errors.NotValidf("empty AgentName")
}
if config.AuthorityName == "" {
return errors.NotValidf("empty AuthorityName")
}
if config.StateName == "" {
return errors.NotValidf("empty StateName")
}
if config.NewWorker == nil {
return errors.NotValidf("nil NewWorker")
}
if config.NewMachineAddressWatcher == nil {
return errors.NotValidf("nil NewMachineAddressWatcher")
}
return nil
}
// Manifold returns a dependency.Manifold that will run a pki Authority.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.AgentName,
config.AuthorityName,
config.StateName,
},
Start: config.start,
}
}
// start is a method on ManifoldConfig because it's more readable than a closure.
func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
var agent jujuagent.Agent
if err := context.Get(config.AgentName, &agent); err != nil {
return nil, errors.Trace(err)
}
var authority pki.Authority
if err := context.Get(config.AuthorityName, &authority); err != nil {
return nil, errors.Trace(err)
}
var stTracker workerstate.StateTracker
if err := context.Get(config.StateName, &stTracker); err != nil {
return nil, errors.Trace(err)
}
statePool, err := stTracker.Use()
if err != nil {
return nil, errors.Trace(err)
}
agentConfig := agent.CurrentConfig()
st, err := statePool.SystemState()
if err != nil {
return nil, errors.Trace(err)
}
addressWatcher, err := config.NewMachineAddressWatcher(st, agentConfig.Tag().Id())
if err != nil {
_ = stTracker.Done()
return nil, errors.Trace(err)
}
w := config.NewWorker(Config{
AddressWatcher: addressWatcher,
Authority: authority,
APIHostPortsGetter: st,
})
return common.NewCleanupWorker(w, func() { _ = stTracker.Done() }), nil
}
// NewMachineAddressWatcher is the function that non-test code should
// pass into ManifoldConfig.NewMachineAddressWatcher.
func NewMachineAddressWatcher(st *state.State, machineId string) (AddressWatcher, error) {
return st.Machine(machineId)
}