/
caasoperator.go
320 lines (292 loc) · 10.2 KB
/
caasoperator.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package agent
import (
"io"
"os"
"path/filepath"
"runtime"
"time"
"github.com/juju/clock"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/featureflag"
"github.com/juju/gnuflag"
"github.com/juju/loggo"
"github.com/juju/names/v5"
"github.com/juju/pubsub/v2"
"github.com/juju/utils/v3/voyeur"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/prometheus/client_golang/prometheus"
"github.com/juju/juju/agent"
"github.com/juju/juju/agent/addons"
apicaasoperator "github.com/juju/juju/api/agent/caasoperator"
"github.com/juju/juju/api/base"
caasconstants "github.com/juju/juju/caas/kubernetes/provider/constants"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/jujud/agent/agentconf"
"github.com/juju/juju/cmd/jujud/agent/caasoperator"
"github.com/juju/juju/cmd/jujud/agent/engine"
agenterrors "github.com/juju/juju/cmd/jujud/agent/errors"
cmdutil "github.com/juju/juju/cmd/jujud/util"
"github.com/juju/juju/core/machinelock"
"github.com/juju/juju/upgrades"
jujuversion "github.com/juju/juju/version"
jworker "github.com/juju/juju/worker"
"github.com/juju/juju/worker/gate"
"github.com/juju/juju/worker/introspection"
"github.com/juju/juju/worker/logsender"
"github.com/juju/juju/worker/upgradesteps"
)
var (
// Should be an explicit dependency, can't do it cleanly yet.
// Exported for testing.
CaasOperatorManifolds = caasoperator.Manifolds
)
// CaasOperatorAgent is a cmd.Command responsible for running a CAAS operator agent.
type CaasOperatorAgent struct {
cmd.CommandBase
agentconf.AgentConf
configChangedVal *voyeur.Value
ApplicationName string
runner *worker.Runner
bufferedLogger *logsender.BufferedLogWriter
ctx *cmd.Context
dead chan struct{}
errReason error
machineLock machinelock.Lock
preUpgradeSteps upgrades.PreUpgradeStepsFunc
upgradeComplete gate.Lock
prometheusRegistry *prometheus.Registry
configure func(*caasoperator.ManifoldsConfig) error
}
// NewCaasOperatorAgent creates a new CAASOperatorAgent instance properly initialized.
func NewCaasOperatorAgent(
ctx *cmd.Context,
bufferedLogger *logsender.BufferedLogWriter,
configure func(*caasoperator.ManifoldsConfig) error,
) (*CaasOperatorAgent, error) {
prometheusRegistry, err := addons.NewPrometheusRegistry()
if err != nil {
return nil, errors.Trace(err)
}
return &CaasOperatorAgent{
AgentConf: agentconf.NewAgentConf(""),
configChangedVal: voyeur.NewValue(true),
ctx: ctx,
dead: make(chan struct{}),
bufferedLogger: bufferedLogger,
prometheusRegistry: prometheusRegistry,
preUpgradeSteps: upgrades.PreUpgradeSteps,
configure: configure,
}, nil
}
// Info implements Command.
func (op *CaasOperatorAgent) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "caasoperator",
Purpose: "run a juju CAAS Operator",
})
}
// SetFlags implements Command.
func (op *CaasOperatorAgent) SetFlags(f *gnuflag.FlagSet) {
op.AgentConf.AddFlags(f)
f.StringVar(&op.ApplicationName, "application-name", "", "name of the application")
}
// Init initializes the command for running.
func (op *CaasOperatorAgent) Init(args []string) error {
if op.ApplicationName == "" {
return agenterrors.RequiredError("application-name")
}
if !names.IsValidApplication(op.ApplicationName) {
return errors.Errorf(`--application-name option expects "<application>" argument`)
}
if err := op.AgentConf.CheckArgs(args); err != nil {
return err
}
op.runner = worker.NewRunner(worker.RunnerParams{
IsFatal: agenterrors.IsFatal,
MoreImportant: agenterrors.MoreImportant,
RestartDelay: jworker.RestartDelay,
Logger: logger,
})
return nil
}
// Wait waits for the CaasOperator agent to finish.
func (op *CaasOperatorAgent) Wait() error {
<-op.dead
return op.errReason
}
// Stop implements Worker.
func (op *CaasOperatorAgent) Stop() error {
op.runner.Kill()
return op.Wait()
}
// Done signals the machine agent is finished
func (op *CaasOperatorAgent) Done(err error) {
op.errReason = err
close(op.dead)
}
// maybeCopyAgentConfig copies the read-only agent config template
// to the writeable agent config file if the file doesn't yet exist.
func (op *CaasOperatorAgent) maybeCopyAgentConfig() error {
err := op.ReadConfig(op.Tag().String())
if err == nil {
return nil
}
if !os.IsNotExist(errors.Cause(err)) {
logger.Errorf("reading initial agent config file: %v", err)
return errors.Trace(err)
}
templateFile := filepath.Join(agent.Dir(op.DataDir(), op.Tag()), caasconstants.TemplateFileNameAgentConf)
if err := copyFile(agent.ConfigPath(op.DataDir(), op.Tag()), templateFile); err != nil {
logger.Errorf("copying agent config file template: %v", err)
return errors.Trace(err)
}
return op.ReadConfig(op.Tag().String())
}
func copyFile(dest, source string) error {
df, err := os.OpenFile(dest, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
if err != nil {
return errors.Trace(err)
}
defer df.Close()
f, err := os.Open(source)
if err != nil {
return errors.Trace(err)
}
defer f.Close()
_, err = io.Copy(df, f)
return errors.Trace(err)
}
// Run implements Command.
func (op *CaasOperatorAgent) Run(ctx *cmd.Context) (err error) {
defer op.Done(err)
if err := op.maybeCopyAgentConfig(); err != nil {
return errors.Annotate(err, "creating agent config from template")
}
agentConfig := op.CurrentConfig()
machineLock, err := machinelock.New(machinelock.Config{
AgentName: op.Tag().String(),
Clock: clock.WallClock,
Logger: loggo.GetLogger("juju.machinelock"),
LogFilename: agent.MachineLockLogFilename(agentConfig),
})
// There will only be an error if the required configuration
// values are not passed in.
if err != nil {
return errors.Trace(err)
}
op.machineLock = machineLock
op.upgradeComplete = upgradesteps.NewLock(agentConfig)
logger.Infof("caas operator %v start (%s [%s])", op.Tag().String(), jujuversion.Current, runtime.Compiler)
if flags := featureflag.String(); flags != "" {
logger.Warningf("developer feature flags enabled: %s", flags)
}
_ = op.runner.StartWorker("api", op.Workers)
return cmdutil.AgentDone(logger, op.runner.Wait())
}
// Workers returns a dependency.Engine running the operator's responsibilities.
func (op *CaasOperatorAgent) Workers() (worker.Worker, error) {
updateAgentConfLogging := func(loggingConfig string) error {
return op.AgentConf.ChangeConfig(func(setter agent.ConfigSetter) error {
setter.SetLoggingConfig(loggingConfig)
return nil
})
}
localHub := pubsub.NewSimpleHub(&pubsub.SimpleHubConfig{
Logger: loggo.GetLogger("juju.localhub"),
})
agentConfig := op.AgentConf.CurrentConfig()
manifoldConfig := caasoperator.ManifoldsConfig{
Agent: agent.APIHostPortsSetter{Agent: op},
AgentConfigChanged: op.configChangedVal,
Clock: clock.WallClock,
LogSource: op.bufferedLogger.Logs(),
UpdateLoggerConfig: updateAgentConfLogging,
PrometheusRegisterer: op.prometheusRegistry,
LeadershipGuarantee: 15 * time.Second,
PreUpgradeSteps: op.preUpgradeSteps,
UpgradeStepsLock: op.upgradeComplete,
ValidateMigration: op.validateMigration,
MachineLock: op.machineLock,
PreviousAgentVersion: agentConfig.UpgradedToVersion(),
LocalHub: localHub,
}
if op.configure != nil {
if err := op.configure(&manifoldConfig); err != nil {
return nil, errors.Trace(err)
}
}
manifolds := CaasOperatorManifolds(manifoldConfig)
metrics := engine.NewMetrics()
workerMetricsSink := metrics.ForModel(agentConfig.Model())
engine, err := dependency.NewEngine(engine.DependencyEngineConfig(workerMetricsSink))
if err != nil {
return nil, err
}
if err := dependency.Install(engine, manifolds); err != nil {
if err := worker.Stop(engine); err != nil {
logger.Errorf("while stopping engine with bad manifolds: %v", err)
}
return nil, err
}
if err := addons.StartIntrospection(addons.IntrospectionConfig{
AgentTag: op.CurrentConfig().Tag(),
Engine: engine,
MachineLock: op.machineLock,
NewSocketName: addons.DefaultIntrospectionSocketName,
PrometheusGatherer: op.prometheusRegistry,
WorkerFunc: introspection.NewWorker,
Clock: clock.WallClock,
LocalHub: localHub,
// If the caas operator gains the ability to interact with the
// introspection worker, the introspection worker should be configured
// with a clock and hub. See the machine agent.
}); err != nil {
// If the introspection worker failed to start, we just log error
// but continue. It is very unlikely to happen in the real world
// as the only issue is connecting to the abstract domain socket
// and the agent is controlled by by the OS to only have one.
logger.Errorf("failed to start introspection worker: %v", err)
}
if err := addons.RegisterEngineMetrics(op.prometheusRegistry, metrics, engine, workerMetricsSink); err != nil {
// If the dependency engine metrics fail, continue on. This is unlikely
// to happen in the real world, but should't stop or bring down an
// agent.
logger.Errorf("failed to start the dependency engine metrics %v", err)
}
return engine, nil
}
// Tag implements Agent.
func (op *CaasOperatorAgent) Tag() names.Tag {
return names.NewApplicationTag(op.ApplicationName)
}
// ChangeConfig implements Agent.
func (op *CaasOperatorAgent) ChangeConfig(mutate agent.ConfigMutator) error {
err := op.AgentConf.ChangeConfig(mutate)
op.configChangedVal.Set(true)
return errors.Trace(err)
}
// validateMigration is called by the migrationminion to help check
// that the agent will be ok when connected to a new controller.
func (op *CaasOperatorAgent) validateMigration(apiCaller base.APICaller) error {
// TODO(wallyworld) - more extensive checks to come.
facade := apicaasoperator.NewClient(apiCaller)
_, err := facade.Life(op.ApplicationName)
if err != nil {
return errors.Trace(err)
}
model, err := facade.Model()
if err != nil {
return errors.Trace(err)
}
curModelUUID := op.CurrentConfig().Model().Id()
newModelUUID := model.UUID
if newModelUUID != curModelUUID {
return errors.Errorf("model mismatch when validating: got %q, expected %q",
newModelUUID, curModelUUID)
}
return nil
}