forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifold.go
129 lines (113 loc) · 3.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package controllerport
import (
"github.com/juju/errors"
"github.com/juju/pubsub/v2"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/DavinZhang/juju/agent"
"github.com/DavinZhang/juju/controller"
"github.com/DavinZhang/juju/state"
workerstate "github.com/DavinZhang/juju/worker/state"
)
// Logger defines the methods needed for the worker to log messages.
type Logger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
Errorf(string, ...interface{})
}
// ManifoldConfig holds the information necessary to determine the controller
// api port and keep it up to date.
type ManifoldConfig struct {
AgentName string
HubName string
StateName string
Logger Logger
UpdateControllerAPIPort func(int) error
GetControllerConfig func(*state.State) (controller.Config, error)
NewWorker func(Config) (worker.Worker, error)
}
// Validate validates the manifold configuration.
func (config ManifoldConfig) Validate() error {
if config.AgentName == "" {
return errors.NotValidf("empty AgentName")
}
if config.HubName == "" {
return errors.NotValidf("empty HubName")
}
if config.StateName == "" {
return errors.NotValidf("empty StateName")
}
if config.Logger == nil {
return errors.NotValidf("nil Logger")
}
if config.UpdateControllerAPIPort == nil {
return errors.NotValidf("nil UpdateControllerAPIPort")
}
if config.GetControllerConfig == nil {
return errors.NotValidf("nil GetControllerConfig")
}
if config.NewWorker == nil {
return errors.NotValidf("nil NewWorker")
}
return nil
}
// Manifold returns a dependency.Manifold that will run an HTTP server
// worker. The manifold outputs an *apiserverhttp.Mux, for other workers
// to register handlers against.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.AgentName,
config.HubName,
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 agent.Agent
if err := context.Get(config.AgentName, &agent); err != nil {
return nil, errors.Trace(err)
}
var hub *pubsub.StructuredHub
if err := context.Get(config.HubName, &hub); 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)
}
defer func() { _ = stTracker.Done() }()
systemState := statePool.SystemState()
controllerConfig, err := config.GetControllerConfig(systemState)
if err != nil {
return nil, errors.Annotate(err, "unable to get controller config")
}
controllerAPIPort := controllerConfig.ControllerAPIPort()
w, err := config.NewWorker(Config{
AgentConfig: agent.CurrentConfig(),
Hub: hub,
Logger: config.Logger,
ControllerAPIPort: controllerAPIPort,
UpdateControllerAPIPort: config.UpdateControllerAPIPort,
})
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
// GetControllerConfig gets the controller config from the given state
// - it's a shim so we can test the manifold without a state suite.
func GetControllerConfig(st *state.State) (controller.Config, error) {
return st.ControllerConfig()
}