-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathinstance_placement.go
367 lines (301 loc) · 10.5 KB
/
instance_placement.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
package scriptlet
import (
"context"
"fmt"
"strconv"
"strings"
"go.starlark.net/starlark"
"github.com/canonical/lxd/lxd/cluster"
"github.com/canonical/lxd/lxd/db"
instanceDrivers "github.com/canonical/lxd/lxd/instance/drivers"
"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/lxd/resources"
scriptletLoad "github.com/canonical/lxd/lxd/scriptlet/load"
"github.com/canonical/lxd/lxd/state"
"github.com/canonical/lxd/lxd/storage"
"github.com/canonical/lxd/shared/api"
apiScriptlet "github.com/canonical/lxd/shared/api/scriptlet"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/units"
)
// InstancePlacementRun runs the instance placement scriptlet and returns the chosen cluster member target.
func InstancePlacementRun(ctx context.Context, l logger.Logger, s *state.State, req *apiScriptlet.InstancePlacement, candidateMembers []db.NodeInfo, leaderAddress string) (*db.NodeInfo, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
logFunc := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var sb strings.Builder
for _, arg := range args {
s, err := strconv.Unquote(arg.String())
if err != nil {
s = arg.String()
}
sb.WriteString(s)
}
switch b.Name() {
case "log_info":
l.Info(fmt.Sprintf("Instance placement scriptlet: %s", sb.String()))
case "log_warn":
l.Warn(fmt.Sprintf("Instance placement scriptlet: %s", sb.String()))
default:
l.Error(fmt.Sprintf("Instance placement scriptlet: %s", sb.String()))
}
return starlark.None, nil
}
var targetMember *db.NodeInfo
setTargetFunc := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var memberName string
err := starlark.UnpackArgs(b.Name(), args, kwargs, "member_name", &memberName)
if err != nil {
return nil, err
}
for i := range candidateMembers {
if candidateMembers[i].Name == memberName {
targetMember = &candidateMembers[i]
break
}
}
if targetMember == nil {
l.Warn("Instance placement scriptlet set invalid member target", logger.Ctx{"member": memberName})
return starlark.String("Invalid member name"), nil
}
l.Info("Instance placement scriptlet set member target", logger.Ctx{"member": targetMember.Name})
return starlark.None, nil
}
getClusterMemberResourcesFunc := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var memberName string
err := starlark.UnpackArgs(b.Name(), args, kwargs, "member_name", &memberName)
if err != nil {
return nil, err
}
var res *api.Resources
// Get the local resource usage.
if memberName == s.ServerName {
res, err = resources.GetResources()
if err != nil {
return nil, err
}
} else {
// Get remote member resource usage.
var targetMember *db.NodeInfo
for i := range candidateMembers {
if candidateMembers[i].Name == memberName {
targetMember = &candidateMembers[i]
break
}
}
if targetMember == nil {
return starlark.String("Invalid member name"), nil
}
client, err := cluster.Connect(targetMember.Address, s.Endpoints.NetworkCert(), s.ServerCert(), nil, true)
if err != nil {
return nil, err
}
res, err = client.GetServerResources()
if err != nil {
return nil, err
}
}
rv, err := StarlarkMarshal(res)
if err != nil {
return nil, fmt.Errorf("Marshalling member resources for %q failed: %w", memberName, err)
}
return rv, nil
}
getClusterMemberStateFunc := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var memberName string
err := starlark.UnpackArgs(b.Name(), args, kwargs, "member_name", &memberName)
if err != nil {
return nil, err
}
var memberState *api.ClusterMemberState
// Get the local resource usage.
if memberName == s.ServerName {
memberState, err = cluster.MemberState(ctx, s)
if err != nil {
return nil, err
}
} else {
// Get remote member resource usage.
var targetMember *db.NodeInfo
for i := range candidateMembers {
if candidateMembers[i].Name == memberName {
targetMember = &candidateMembers[i]
break
}
}
if targetMember == nil {
return starlark.String("Invalid member name"), nil
}
client, err := cluster.Connect(targetMember.Address, s.Endpoints.NetworkCert(), s.ServerCert(), nil, true)
if err != nil {
return nil, err
}
memberState, _, err = client.GetClusterMemberState(memberName)
if err != nil {
return nil, err
}
}
rv, err := StarlarkMarshal(memberState)
if err != nil {
return nil, fmt.Errorf("Marshalling member state for %q failed: %w", memberName, err)
}
return rv, nil
}
getInstanceResourcesFunc := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var err error
var res apiScriptlet.InstanceResources
// Parse limits.cpu.
if req.Config["limits.cpu"] != "" {
// Check if using shared CPU limits.
res.CPUCores, err = strconv.ParseUint(req.Config["limits.cpu"], 10, 64)
if err != nil {
// Or get count of pinned CPUs.
pinnedCPUs, err := resources.ParseCpuset(req.Config["limits.cpu"])
if err != nil {
return nil, fmt.Errorf("Failed parsing instance resources limits.cpu: %w", err)
}
res.CPUCores = uint64(len(pinnedCPUs))
}
} else if req.Type == api.InstanceTypeVM {
// Apply VM CPU cores defaults if not specified.
res.CPUCores = instanceDrivers.QEMUDefaultCPUCores
}
// Parse limits.memory.
memoryLimitStr := req.Config["limits.memory"]
// Apply VM memory limit defaults if not specified.
if req.Type == api.InstanceTypeVM && memoryLimitStr == "" {
memoryLimitStr = instanceDrivers.QEMUDefaultMemSize
}
if memoryLimitStr != "" {
memoryLimit, err := units.ParseByteSizeString(memoryLimitStr)
if err != nil {
return nil, fmt.Errorf("Failed parsing instance resources limits.memory: %w", err)
}
res.MemorySize = uint64(memoryLimit)
}
// Parse root disk size.
_, rootDiskConfig, err := instancetype.GetRootDiskDevice(req.Devices)
if err == nil {
rootDiskSizeStr := rootDiskConfig["size"]
// Apply VM root disk size defaults if not specified.
if req.Type == api.InstanceTypeVM && rootDiskSizeStr == "" {
rootDiskSizeStr, err = storage.GetPoolDefaultBlockSize(s, rootDiskConfig["pool"])
if err != nil {
return nil, fmt.Errorf("Failed loading pool default size: %w", err)
}
}
if rootDiskSizeStr != "" {
rootDiskSize, err := units.ParseByteSizeString(rootDiskSizeStr)
if err != nil {
return nil, fmt.Errorf("Failed parsing instance resources root disk size: %w", err)
}
res.RootDiskSize = uint64(rootDiskSize)
}
}
rv, err := StarlarkMarshal(res)
if err != nil {
return nil, fmt.Errorf("Marshalling instance resources failed: %w", err)
}
return rv, nil
}
var err error
var raftNodes []db.RaftNode
err = s.DB.Node.Transaction(ctx, func(ctx context.Context, tx *db.NodeTx) error {
raftNodes, err = tx.GetRaftNodes(ctx)
if err != nil {
return fmt.Errorf("Failed loading RAFT nodes: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
candidateMembersInfo := make([]*api.ClusterMember, 0, len(candidateMembers))
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
failureDomains, err := tx.GetFailureDomainsNames(ctx)
if err != nil {
return fmt.Errorf("Failed loading failure domains names: %w", err)
}
memberFailureDomains, err := tx.GetNodesFailureDomains(ctx)
if err != nil {
return fmt.Errorf("Failed loading member failure domains: %w", err)
}
maxVersion, err := tx.GetNodeMaxVersion(ctx)
if err != nil {
return fmt.Errorf("Failed getting max member version: %w", err)
}
args := db.NodeInfoArgs{
LeaderAddress: leaderAddress,
FailureDomains: failureDomains,
MemberFailureDomains: memberFailureDomains,
OfflineThreshold: s.GlobalConfig.OfflineThreshold(),
MaxMemberVersion: maxVersion,
RaftNodes: raftNodes,
}
for i := range candidateMembers {
candidateMemberInfo, err := candidateMembers[i].ToAPI(ctx, tx, args)
if err != nil {
return err
}
candidateMembersInfo = append(candidateMembersInfo, candidateMemberInfo)
}
return nil
})
if err != nil {
return nil, err
}
// Remember to match the entries in scriptletLoad.InstancePlacementCompile() with this list so Starlark can
// perform compile time validation of functions used.
env := starlark.StringDict{
"log_info": starlark.NewBuiltin("log_info", logFunc),
"log_warn": starlark.NewBuiltin("log_warn", logFunc),
"log_error": starlark.NewBuiltin("log_error", logFunc),
"set_target": starlark.NewBuiltin("set_target", setTargetFunc),
"get_cluster_member_resources": starlark.NewBuiltin("get_cluster_member_resources", getClusterMemberResourcesFunc),
"get_cluster_member_state": starlark.NewBuiltin("get_cluster_member_state", getClusterMemberStateFunc),
"get_instance_resources": starlark.NewBuiltin("get_instance_resources", getInstanceResourcesFunc),
}
prog, thread, err := scriptletLoad.InstancePlacementProgram()
if err != nil {
return nil, err
}
go func() {
<-ctx.Done()
thread.Cancel("Request finished")
}()
globals, err := prog.Init(thread, env)
if err != nil {
return nil, fmt.Errorf("Failed initializing: %w", err)
}
globals.Freeze()
// Retrieve a global variable from starlark environment.
instancePlacement := globals["instance_placement"]
if instancePlacement == nil {
return nil, fmt.Errorf("Scriptlet missing instance_placement function")
}
rv, err := StarlarkMarshal(req)
if err != nil {
return nil, fmt.Errorf("Marshalling request failed: %w", err)
}
candidateMembersv, err := StarlarkMarshal(candidateMembersInfo)
if err != nil {
return nil, fmt.Errorf("Marshalling candidate members failed: %w", err)
}
// Call starlark function from Go.
v, err := starlark.Call(thread, instancePlacement, nil, []starlark.Tuple{
{
starlark.String("request"),
rv,
}, {
starlark.String("candidate_members"),
candidateMembersv,
},
})
if err != nil {
return nil, fmt.Errorf("Failed to run: %w", err)
}
if v.Type() != "NoneType" {
return nil, fmt.Errorf("Failed with unexpected return value: %v", v)
}
return targetMember, nil
}