github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/caasunitterminationworker/manifold.go (about) 1 // Copyright 2021 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasunitterminationworker 5 6 import ( 7 "github.com/juju/clock" 8 "github.com/juju/errors" 9 "github.com/juju/worker/v3" 10 "github.com/juju/worker/v3/dependency" 11 12 "github.com/juju/juju/agent" 13 "github.com/juju/juju/api" 14 "github.com/juju/juju/api/agent/caasapplication" 15 "github.com/juju/juju/worker/uniter" 16 ) 17 18 // Logger for logging messages. 19 type Logger interface { 20 Infof(string, ...interface{}) 21 Errorf(string, ...interface{}) 22 } 23 24 // ManifoldConfig defines the names of the manifolds on which a 25 // Manifold will depend. 26 type ManifoldConfig struct { 27 AgentName string 28 APICallerName string 29 UniterName string 30 Clock clock.Clock 31 Logger Logger 32 } 33 34 // Validate ensures all the required values for the config are set. 35 func (config *ManifoldConfig) Validate() error { 36 if config.Clock == nil { 37 return errors.NotValidf("missing Clock") 38 } 39 if config.Logger == nil { 40 return errors.NotValidf("missing Logger") 41 } 42 return nil 43 } 44 45 // Manifold returns a manifold whose worker returns ErrTerminateAgent 46 // if a termination signal is received by the process it's running in. 47 func Manifold(config ManifoldConfig) dependency.Manifold { 48 return dependency.Manifold{ 49 Inputs: []string{ 50 config.AgentName, 51 config.APICallerName, 52 config.UniterName, 53 }, 54 Start: func(context dependency.Context) (worker.Worker, error) { 55 if err := config.Validate(); err != nil { 56 return nil, errors.Trace(err) 57 } 58 var agent agent.Agent 59 if err := context.Get(config.AgentName, &agent); err != nil { 60 return nil, err 61 } 62 var apiConn api.Connection 63 if err := context.Get(config.APICallerName, &apiConn); err != nil { 64 return nil, err 65 } 66 var uniter *uniter.Uniter 67 if err := context.Get(config.UniterName, &uniter); err != nil { 68 return nil, err 69 } 70 state := caasapplication.NewClient(apiConn) 71 return NewWorker(Config{ 72 Agent: agent, 73 State: state, 74 UnitTerminator: uniter, 75 Logger: config.Logger, 76 Clock: config.Clock, 77 }), nil 78 }, 79 } 80 }