github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/certupdater/manifold.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package certupdater 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/worker/v3" 9 "github.com/juju/worker/v3/dependency" 10 11 jujuagent "github.com/juju/juju/agent" 12 "github.com/juju/juju/pki" 13 "github.com/juju/juju/state" 14 "github.com/juju/juju/worker/common" 15 workerstate "github.com/juju/juju/worker/state" 16 ) 17 18 // ManifoldConfig holds the information necessary to run a certupdater 19 // in a dependency.Engine. 20 type ManifoldConfig struct { 21 AgentName string 22 AuthorityName string 23 StateName string 24 NewWorker func(Config) worker.Worker 25 NewMachineAddressWatcher func(st *state.State, machineId string) (AddressWatcher, error) 26 } 27 28 // Validate validates the manifold configuration. 29 func (config ManifoldConfig) Validate() error { 30 if config.AgentName == "" { 31 return errors.NotValidf("empty AgentName") 32 } 33 if config.AuthorityName == "" { 34 return errors.NotValidf("empty AuthorityName") 35 } 36 if config.StateName == "" { 37 return errors.NotValidf("empty StateName") 38 } 39 if config.NewWorker == nil { 40 return errors.NotValidf("nil NewWorker") 41 } 42 if config.NewMachineAddressWatcher == nil { 43 return errors.NotValidf("nil NewMachineAddressWatcher") 44 } 45 return nil 46 } 47 48 // Manifold returns a dependency.Manifold that will run a pki Authority. 49 func Manifold(config ManifoldConfig) dependency.Manifold { 50 return dependency.Manifold{ 51 Inputs: []string{ 52 config.AgentName, 53 config.AuthorityName, 54 config.StateName, 55 }, 56 Start: config.start, 57 } 58 } 59 60 // start is a method on ManifoldConfig because it's more readable than a closure. 61 func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) { 62 if err := config.Validate(); err != nil { 63 return nil, errors.Trace(err) 64 } 65 66 var agent jujuagent.Agent 67 if err := context.Get(config.AgentName, &agent); err != nil { 68 return nil, errors.Trace(err) 69 } 70 71 var authority pki.Authority 72 if err := context.Get(config.AuthorityName, &authority); err != nil { 73 return nil, errors.Trace(err) 74 } 75 76 var stTracker workerstate.StateTracker 77 if err := context.Get(config.StateName, &stTracker); err != nil { 78 return nil, errors.Trace(err) 79 } 80 statePool, err := stTracker.Use() 81 if err != nil { 82 return nil, errors.Trace(err) 83 } 84 85 agentConfig := agent.CurrentConfig() 86 87 st, err := statePool.SystemState() 88 if err != nil { 89 return nil, errors.Trace(err) 90 } 91 addressWatcher, err := config.NewMachineAddressWatcher(st, agentConfig.Tag().Id()) 92 if err != nil { 93 _ = stTracker.Done() 94 return nil, errors.Trace(err) 95 } 96 97 w := config.NewWorker(Config{ 98 AddressWatcher: addressWatcher, 99 Authority: authority, 100 APIHostPortsGetter: st, 101 }) 102 return common.NewCleanupWorker(w, func() { _ = stTracker.Done() }), nil 103 } 104 105 // NewMachineAddressWatcher is the function that non-test code should 106 // pass into ManifoldConfig.NewMachineAddressWatcher. 107 func NewMachineAddressWatcher(st *state.State, machineId string) (AddressWatcher, error) { 108 return st.Machine(machineId) 109 }