/
application.go
301 lines (270 loc) · 7.29 KB
/
application.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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasapplication
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v4"
"github.com/juju/retry"
"github.com/juju/utils/v2"
"github.com/juju/juju/agent"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/caas"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/paths"
"github.com/juju/juju/state"
)
var logger = loggo.GetLogger("juju.apiserver.caasapplication")
type Facade struct {
auth facade.Authorizer
resources facade.Resources
ctrlSt ControllerState
state State
model Model
clock clock.Clock
}
// NewStateFacade provides the signature required for facade registration.
func NewStateFacade(ctx facade.Context) (*Facade, error) {
authorizer := ctx.Auth()
resources := ctx.Resources()
return NewFacade(resources, authorizer,
ctx.StatePool().SystemState(),
&stateShim{ctx.State()},
ctx.StatePool().Clock())
}
// NewFacade returns a new CAASOperator facade.
func NewFacade(
resources facade.Resources,
authorizer facade.Authorizer,
ctrlSt ControllerState,
st State,
clock clock.Clock,
) (*Facade, error) {
if !authorizer.AuthApplicationAgent() {
return nil, apiservererrors.ErrPerm
}
model, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
return &Facade{
auth: authorizer,
resources: resources,
ctrlSt: ctrlSt,
state: st,
model: model,
clock: clock,
}, nil
}
// UnitIntroduction sets the status of each given entity.
func (f *Facade) UnitIntroduction(args params.CAASUnitIntroductionArgs) (params.CAASUnitIntroductionResult, error) {
tag, ok := f.auth.GetAuthTag().(names.ApplicationTag)
if !ok {
return params.CAASUnitIntroductionResult{}, apiservererrors.ErrPerm
}
errResp := func(err error) (params.CAASUnitIntroductionResult, error) {
return params.CAASUnitIntroductionResult{Error: apiservererrors.ServerError(err)}, nil
}
if args.PodName == "" {
return errResp(errors.NotValidf("pod-name"))
}
if args.PodUUID == "" {
return errResp(errors.NotValidf("pod-uuid"))
}
logger.Debugf("introducing pod %q (%q)", args.PodName, args.PodUUID)
application, err := f.state.Application(tag.Name)
if err != nil {
return errResp(err)
}
if application.Life() != state.Alive {
return errResp(errors.NotProvisionedf("application"))
}
// TODO(embedded): handle deployment other than statefulset
// ch, _, err := application.Charm()
// if err != nil {
// return errResp(err)
// }
deploymentType := caas.DeploymentStateful
containerID := args.PodName
var unitName *string
switch deploymentType {
case caas.DeploymentStateful:
splitPodName := strings.Split(args.PodName, "-")
ord, err := strconv.Atoi(splitPodName[len(splitPodName)-1])
if err != nil {
return errResp(err)
}
n := fmt.Sprintf("%s/%d", application.Name(), ord)
unitName = &n
case caas.DeploymentStateless, caas.DeploymentDaemon:
// Both handled the same way.
default:
return errResp(errors.NotSupportedf("unknown deployment type"))
}
var unit Unit = nil
if unitName != nil {
unit, err = f.state.Unit(*unitName)
if err != nil && !errors.IsNotFound(err) {
return errResp(err)
}
} else {
containers, err := f.model.Containers(containerID)
if err != nil {
return errResp(err)
}
if len(containers) != 0 {
container := containers[0]
unit, err = f.state.Unit(container.Unit())
if err != nil {
return errResp(err)
}
logger.Debugf("pod %q matched unit %q", args.PodName, unit.Tag().String())
}
// Find an existing unit that isn't yet assigned.
if unit == nil {
units, err := application.AllUnits()
if err != nil {
return errResp(err)
}
for _, existingUnit := range units {
info, err := existingUnit.ContainerInfo()
if errors.IsNotFound(err) {
unit = existingUnit
break
} else if err != nil {
return errResp(err)
}
if info.ProviderId() == "" {
unit = existingUnit
break
}
}
if unit != nil {
logger.Debugf("pod %q matched unused unit %q", args.PodName, unit.Tag().String())
}
}
}
if unit != nil && unit.Life() != state.Alive {
retryErr := errors.New("retry")
call := retry.CallArgs{
Clock: f.clock,
Delay: 5 * time.Second,
Attempts: 12,
IsFatalError: func(err error) bool {
return err != retryErr
},
Func: func() error {
err := unit.Refresh()
if errors.IsNotFound(err) {
unit = nil
return nil
} else if err != nil {
return retryErr
}
switch unit.Life() {
case state.Alive:
return nil
case state.Dying, state.Dead:
logger.Debugf("still waiting for old unit %q to cleanup", unit.Tag().String())
return retryErr
default:
return errors.Errorf("unknown life state")
}
},
}
err := retry.Call(call)
if err != nil {
return errResp(errors.Annotatef(err,
"failed waiting for old unit %q to cleanup", unit.Tag().String()))
}
}
// Force update of provider-id.
if unit != nil {
update := unit.UpdateOperation(state.UnitUpdateProperties{
ProviderId: &containerID,
})
var unitUpdate state.UpdateUnitsOperation
unitUpdate.Updates = append(unitUpdate.Updates, update)
err = application.UpdateUnits(&unitUpdate)
if err != nil {
return errResp(err)
}
logger.Debugf("unit %q updated provider id to %q", unit.Tag().String(), containerID)
}
// Create a new unit if we never found one.
if unit == nil {
unit, err = application.AddUnit(state.AddUnitParams{
UnitName: unitName,
ProviderId: &containerID,
})
if err != nil {
return errResp(err)
}
logger.Debugf("created new unit %q for pod %q", unit.Tag().String(), args.PodName)
}
password, err := utils.RandomPassword()
if err != nil {
return errResp(err)
}
err = unit.SetPassword(password)
if err != nil {
return errResp(err)
}
controllerConfig, err := f.ctrlSt.ControllerConfig()
if err != nil {
return errResp(err)
}
apiHostPorts, err := f.ctrlSt.APIHostPortsForAgents()
if err != nil {
return errResp(err)
}
addrs := []string(nil)
for _, hostPorts := range apiHostPorts {
ordered := hostPorts.HostPorts().PrioritizedForScope(network.ScopeMatchCloudLocal)
for _, addr := range ordered {
if addr != "" {
addrs = append(addrs, addr)
}
}
}
caCert, _ := controllerConfig.CACert()
version, _ := f.model.AgentVersion()
dataDir := paths.DataDir(paths.OSUnixLike)
logDir := paths.LogDir(paths.OSUnixLike)
conf, err := agent.NewAgentConfig(
agent.AgentConfigParams{
Paths: agent.Paths{
DataDir: dataDir,
LogDir: logDir,
},
Tag: unit.Tag(),
Controller: f.model.ControllerTag(),
Model: f.model.Tag().(names.ModelTag),
APIAddresses: addrs,
CACert: caCert,
Password: password,
UpgradedToVersion: version,
},
)
if err != nil {
return errResp(errors.Annotatef(err, "creating new agent config"))
}
agentConfBytes, err := conf.Render()
if err != nil {
return errResp(err)
}
res := params.CAASUnitIntroductionResult{
Result: ¶ms.CAASUnitIntroduction{
UnitName: unit.Tag().Id(),
AgentConf: agentConfBytes,
},
}
return res, nil
}