-
Notifications
You must be signed in to change notification settings - Fork 491
/
worker.go
405 lines (366 loc) · 12.8 KB
/
worker.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// This worker is responsible for watching the life cycle of CAAS pod-spec
// applications and creating their operator pods (or removing them). Unlike
// the caasapplicationprovisioner worker, this worker does not create a new
// child worker for every application being monitored.
//
// Note that the separate caasapplicationprovisioner worker handles CAAS
// sidecar applications.
package caasoperatorprovisioner
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/juju/charm/v12"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/retry"
"github.com/juju/utils/v3"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/catacomb"
"github.com/juju/juju/agent"
charmscommon "github.com/juju/juju/api/common/charms"
apicaasprovisioner "github.com/juju/juju/api/controller/caasoperatorprovisioner"
"github.com/juju/juju/caas"
k8sconstants "github.com/juju/juju/caas/kubernetes/provider/constants"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/storage"
)
// Logger is here to stop the desire of creating a package level Logger.
// Don't do this, instead use the one passed as manifold config.
type logger interface{}
var _ logger = struct{}{}
// CAASProvisionerFacade exposes CAAS provisioning functionality to a worker.
type CAASProvisionerFacade interface {
OperatorProvisioningInfo(string) (apicaasprovisioner.OperatorProvisioningInfo, error)
WatchApplications() (watcher.StringsWatcher, error)
SetPasswords([]apicaasprovisioner.ApplicationPassword) (params.ErrorResults, error)
Life(string) (life.Value, error)
IssueOperatorCertificate(string) (apicaasprovisioner.OperatorCertificate, error)
ApplicationCharmInfo(appName string) (*charmscommon.CharmInfo, error)
}
// Config defines the operation of a Worker.
type Config struct {
Facade CAASProvisionerFacade
OperatorManager caas.ApplicationOperatorManager
ModelTag names.ModelTag
AgentConfig agent.Config
Clock clock.Clock
Logger Logger
}
// NewProvisionerWorker starts and returns a new CAAS provisioner worker.
func NewProvisionerWorker(config Config) (worker.Worker, error) {
p := &provisioner{
provisionerFacade: config.Facade,
operatorManager: config.OperatorManager,
modelTag: config.ModelTag,
agentConfig: config.AgentConfig,
clock: config.Clock,
logger: config.Logger,
}
err := catacomb.Invoke(catacomb.Plan{
Site: &p.catacomb,
Work: p.loop,
})
return p, err
}
type provisioner struct {
catacomb catacomb.Catacomb
provisionerFacade CAASProvisionerFacade
operatorManager caas.ApplicationOperatorManager
clock clock.Clock
logger Logger
modelTag names.ModelTag
agentConfig agent.Config
}
// Kill is part of the worker.Worker interface.
func (p *provisioner) Kill() {
p.catacomb.Kill(nil)
}
// Wait is part of the worker.Worker interface.
func (p *provisioner) Wait() error {
return p.catacomb.Wait()
}
func (p *provisioner) loop() error {
// TODO(caas) - this loop should also keep an eye on kubernetes and ensure
// that the operator stays up, redeploying it if the pod goes
// away. For some runtimes we *could* rely on the the runtime's
// features to do this.
appWatcher, err := p.provisionerFacade.WatchApplications()
if err != nil {
return errors.Trace(err)
}
if err := p.catacomb.Add(appWatcher); err != nil {
return errors.Trace(err)
}
for {
select {
case <-p.catacomb.Dying():
return p.catacomb.ErrDying()
// CAAS applications changed so either create or remove pods as appropriate.
case apps, ok := <-appWatcher.Changes():
if !ok {
return errors.New("app watcher closed channel")
}
var newApps []string
for _, app := range apps {
// Ignore events for v2 charms.
format, err := p.charmFormat(app)
if errors.IsNotFound(err) {
p.logger.Debugf("application %q no longer exists", app)
continue
} else if err != nil {
return errors.Trace(err)
}
if format > charm.FormatV1 {
p.logger.Tracef("application %q is v2, ignoring event", app)
continue
}
// Process events for v1 charms.
appLife, err := p.provisionerFacade.Life(app)
if err != nil && !errors.IsNotFound(err) {
return errors.Trace(err)
}
if err != nil || appLife == life.Dead {
p.logger.Debugf("deleting operator for %q", app)
if err := p.operatorManager.DeleteOperator(app); err != nil {
return errors.Annotatef(err, "failed to stop operator for %q", app)
}
continue
}
if appLife != life.Alive {
continue
}
newApps = append(newApps, app)
}
if len(newApps) == 0 {
continue
}
if err := p.ensureOperators(newApps); err != nil {
return errors.Trace(err)
}
}
}
}
func (p *provisioner) charmFormat(appName string) (charm.Format, error) {
charmInfo, err := p.provisionerFacade.ApplicationCharmInfo(appName)
if err != nil {
return charm.FormatUnknown, errors.Annotatef(err, "failed to get charm info for application %q", appName)
}
return charm.MetaFormat(charmInfo.Charm()), nil
}
func (p *provisioner) waitForOperatorTerminated(app string) error {
tryAgain := errors.New("try again")
existsFunc := func() error {
opState, err := p.operatorManager.OperatorExists(app)
if err != nil {
return errors.Trace(err)
}
if !opState.Exists {
return nil
}
if opState.Exists && !opState.Terminating {
return errors.Errorf("operator %q should be terminating but is now running", app)
}
return tryAgain
}
retryCallArgs := retry.CallArgs{
Attempts: 60,
Delay: 3 * time.Second,
MaxDuration: 3 * time.Minute,
Clock: p.clock,
Func: existsFunc,
IsFatalError: func(err error) bool {
return err != tryAgain
},
}
return errors.Trace(retry.Call(retryCallArgs))
}
// ensureOperators creates operator pods for the specified app names -> api passwords.
func (p *provisioner) ensureOperators(apps []string) error {
var appPasswords []apicaasprovisioner.ApplicationPassword
operatorConfig := make([]*caas.OperatorConfig, len(apps))
for i, app := range apps {
opState, err := p.operatorManager.OperatorExists(app)
if err != nil {
return errors.Annotatef(err, "failed to find operator for %q", app)
}
if opState.Exists && opState.Terminating {
// We can't deploy an app while a previous version is terminating.
// TODO(caas) - the remove application process should block until app terminated
// TODO(caas) - consider making this async, but ok for now as it's a corner case
if err := p.waitForOperatorTerminated(app); err != nil {
return errors.Annotatef(err, "operator for %q was terminating and there was an error waiting for it to stop", app)
}
opState.Exists = false
}
op, err := p.operatorManager.Operator(app)
if err != nil && !errors.IsNotFound(err) {
return errors.Trace(err)
}
// If the operator does not exist already, we need to create an initial
// password for it.
var password string
if !opState.Exists {
if password, err = utils.RandomPassword(); err != nil {
return errors.Trace(err)
}
appPasswords = append(appPasswords, apicaasprovisioner.ApplicationPassword{Name: app, Password: password})
}
var prevCfg caas.OperatorConfig
if op != nil && op.Config != nil {
prevCfg = *op.Config
}
config, err := p.updateOperatorConfig(app, password, prevCfg)
if err != nil {
return errors.Annotatef(err, "failed to generate operator config for %q", app)
}
operatorConfig[i] = config
}
// If we did create any passwords for new operators, first they need
// to be saved so the agent can login when it starts up.
if len(appPasswords) > 0 {
errorResults, err := p.provisionerFacade.SetPasswords(appPasswords)
if err != nil {
return errors.Annotate(err, "failed to set application api passwords")
}
if err := errorResults.Combine(); err != nil {
return errors.Annotate(err, "failed to set application api passwords")
}
}
// Now that any new config/passwords are done, create or update
// the operators themselves.
var errorStrings []string
for i, app := range apps {
if err := p.ensureOperator(app, operatorConfig[i]); err != nil {
errorStrings = append(errorStrings, err.Error())
continue
}
}
if errorStrings != nil {
err := errors.New(strings.Join(errorStrings, "\n"))
return errors.Annotate(err, "failed to provision all operators")
}
return nil
}
func (p *provisioner) ensureOperator(app string, config *caas.OperatorConfig) error {
if err := p.operatorManager.EnsureOperator(app, p.agentConfig.DataDir(), config); err != nil {
return errors.Annotatef(err, "failed to start operator for %q", app)
}
p.logger.Infof("started operator for application %q", app)
return nil
}
func (p *provisioner) updateOperatorConfig(appName, password string, prevCfg caas.OperatorConfig) (*caas.OperatorConfig, error) {
info, err := p.provisionerFacade.OperatorProvisioningInfo(appName)
if err != nil {
return nil, errors.Annotatef(err, "fetching operator provisioning info")
}
// Operators may have storage configured because charms
// have persistent state which must be preserved between any
// operator restarts. Newer charms though store state in the controller.
if info.CharmStorage != nil && info.CharmStorage.Provider != k8sconstants.StorageProviderType {
if spType := info.CharmStorage.Provider; spType == "" {
return nil, errors.NotValidf("missing operator storage provider")
} else {
return nil, errors.NotSupportedf("operator storage provider %q", spType)
}
}
p.logger.Tracef("using operator info %+v", info)
cfg := &caas.OperatorConfig{
ImageDetails: info.ImageDetails,
BaseImageDetails: info.BaseImageDetails,
Version: info.Version,
ResourceTags: info.Tags,
CharmStorage: charmStorageParams(info.CharmStorage),
ConfigMapGeneration: prevCfg.ConfigMapGeneration,
}
cfg.AgentConf, err = p.updateAgentConf(appName, password, info, prevCfg.AgentConf)
if err != nil {
return nil, errors.Annotatef(err, "updating agent config")
}
cfg.OperatorInfo, err = p.updateOperatorInfo(appName, prevCfg.OperatorInfo)
if err != nil {
return nil, errors.Annotatef(err, "updating operator info")
}
return cfg, nil
}
func (p *provisioner) updateAgentConf(appName, password string,
info apicaasprovisioner.OperatorProvisioningInfo,
prevAgentConfData []byte) ([]byte, error) {
if len(prevAgentConfData) == 0 && password == "" {
return nil, errors.NewNotValid(nil, fmt.Sprintf("no existing agent conf found and no new password generated for %q operator", appName))
}
if password == "" {
// Read password from previous agent config for the existing operator.
prevAgentConf, err := agent.ParseConfigData(prevAgentConfData)
if err != nil {
return nil, errors.Trace(err)
}
password = prevAgentConf.OldPassword()
}
appTag := names.NewApplicationTag(appName)
conf, err := agent.NewAgentConfig(
agent.AgentConfigParams{
Paths: agent.Paths{
DataDir: p.agentConfig.DataDir(),
LogDir: p.agentConfig.LogDir(),
},
Tag: appTag,
Controller: p.agentConfig.Controller(),
Model: p.modelTag,
APIAddresses: info.APIAddresses,
CACert: p.agentConfig.CACert(),
Password: password,
// UpgradedToVersion is mandatory but not used by
// caas operator agents as they are not upgraded insitu.
UpgradedToVersion: info.Version,
},
)
if err != nil {
return nil, errors.Annotatef(err, "creating new agent config")
}
newAgentConfData, err := conf.Render()
if err != nil {
return nil, errors.Annotatef(err, "rendering new agent config")
}
p.logger.Debugf("agentConfData for %q changed %v", appName, !bytes.Equal(prevAgentConfData, newAgentConfData))
return newAgentConfData, nil
}
func (p *provisioner) updateOperatorInfo(appName string, prevOperatorInfoData []byte) ([]byte, error) {
var operatorInfo caas.OperatorInfo
if prevOperatorInfoData != nil {
prevOperatorInfo, err := caas.UnmarshalOperatorInfo(prevOperatorInfoData)
if err != nil {
return nil, errors.Annotatef(err, "unmarshalling operator info")
}
operatorInfo = *prevOperatorInfo
}
if operatorInfo.Cert == "" ||
operatorInfo.PrivateKey == "" ||
operatorInfo.CACert == "" {
cert, err := p.provisionerFacade.IssueOperatorCertificate(appName)
if err != nil {
return nil, errors.Annotatef(err, "issuing certificate")
}
operatorInfo.Cert = cert.Cert
operatorInfo.PrivateKey = cert.PrivateKey
operatorInfo.CACert = cert.CACert
}
return operatorInfo.Marshal()
}
func charmStorageParams(in *storage.KubernetesFilesystemParams) *caas.CharmStorageParams {
if in == nil {
return nil
}
return &caas.CharmStorageParams{
Provider: in.Provider,
Size: in.Size,
Attributes: in.Attributes,
ResourceTags: in.ResourceTags,
}
}