-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmessages.go
More file actions
360 lines (324 loc) · 13.4 KB
/
messages.go
File metadata and controls
360 lines (324 loc) · 13.4 KB
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
// Copyright SAP SE
// SPDX-License-Identifier: Apache-2.0
package api
import (
"errors"
"fmt"
"log/slog"
"strings"
"github.com/cobaltcore-dev/cortex/api/v1alpha1"
"github.com/cobaltcore-dev/cortex/internal/scheduling/lib"
)
// Host object from the Nova scheduler pipeline.
// See: https://github.com/sapcc/nova/blob/stable/xena-m3/nova/scheduler/host_manager.py class HostState
type ExternalSchedulerHost struct {
// Name of the Nova compute host, e.g. nova-compute-bb123.
ComputeHost string `json:"host"`
// Name of the hypervisor hostname, e.g. domain-c123.<uuid>
HypervisorHostname string `json:"hypervisor_hostname"`
}
// Request generated by the Nova scheduler when calling cortex.
// The request contains a spec of the VM to be scheduled, a list of hosts and
// their status, and a map of weights that were calculated by the Nova weigher
// pipeline. Some additional flags are also included.
type ExternalSchedulerRequest struct {
Spec NovaObject[NovaSpec] `json:"spec"`
// Request context from Nova that contains additional meta information.
Context NovaRequestContext `json:"context"`
Hosts []ExternalSchedulerHost `json:"hosts"`
Weights map[string]float64 `json:"weights"`
// The name of the pipeline to execute.
Pipeline string `json:"pipeline"`
}
func (r ExternalSchedulerRequest) GetHosts() []string {
hosts := make([]string, len(r.Hosts))
for i, host := range r.Hosts {
hosts[i] = host.ComputeHost
}
return hosts
}
func (r ExternalSchedulerRequest) GetWeights() map[string]float64 {
return r.Weights
}
func (r ExternalSchedulerRequest) GetTraceLogArgs() []slog.Attr {
greq := ""
if r.Context.GlobalRequestID != nil {
greq = *r.Context.GlobalRequestID
}
return []slog.Attr{
slog.String("greq", greq),
slog.String("req", r.Context.RequestID),
slog.String("user", r.Context.UserID),
slog.String("project", r.Context.ProjectID),
}
}
func (r ExternalSchedulerRequest) FilterHosts(includedHosts map[string]float64) lib.FilterWeigherPipelineRequest {
filteredHosts := make([]ExternalSchedulerHost, 0, len(includedHosts))
for _, host := range r.Hosts {
if _, exists := includedHosts[host.ComputeHost]; exists {
filteredHosts = append(filteredHosts, host)
}
}
r.Hosts = filteredHosts
return r
}
type FlavorType string
const (
// FlavorTypeGeneralPurpose represents general purpose workloads.
FlavorTypeGeneralPurpose FlavorType = "general-purpose"
// FlavorTypeHANA represents HANA workloads.
FlavorTypeHANA FlavorType = "hana"
)
// GetFlavorType determines the flavor type based on the requested flavor's extra specs.
func (r ExternalSchedulerRequest) GetFlavorType() (FlavorType, error) {
extraSpecs := r.Spec.Data.Flavor.Data.ExtraSpecs
val, ok := extraSpecs["trait:CUSTOM_HANA_EXCLUSIVE_HOST"]
if !ok {
return FlavorTypeGeneralPurpose, nil
}
// If the key is provided, it must be either "required" or "forbidden".
switch strings.ToLower(val) {
case "forbidden":
return FlavorTypeGeneralPurpose, nil
case "required":
return FlavorTypeHANA, nil
default:
return "", fmt.Errorf("unsupported value for trait:CUSTOM_HANA_EXCLUSIVE_HOST in flavor extra specs: %s", val)
}
}
type HypervisorType string
const (
// HypervisorTypeQEMU represents QEMU/KVM hypervisors.
HypervisorTypeQEMU HypervisorType = "qemu"
// HypervisorTypeCH represents Cloud-Hypervisor/KVM hypervisors.
HypervisorTypeCH HypervisorType = "ch"
// HypervisorTypeVMware represents VMware hypervisors.
HypervisorTypeVMware HypervisorType = "vmware"
)
// GetHypervisorType determines the hypervisor type based on the requested flavor.
func (req ExternalSchedulerRequest) GetHypervisorType() (HypervisorType, error) {
extraSpecs := req.Spec.Data.Flavor.Data.ExtraSpecs
if val, ok := extraSpecs["capabilities:hypervisor_type"]; ok {
switch strings.ToLower(val) {
case "qemu":
return HypervisorTypeQEMU, nil
case "ch":
return HypervisorTypeCH, nil
case "vmware vcenter server":
return HypervisorTypeVMware, nil
}
return "", fmt.Errorf("unsupported hypervisor_type: %s", val)
}
return "", errors.New("hypervisor type not specified in flavor extra specs")
}
const (
// LiveMigrationIntent indicates that the request is intended for live migration.
LiveMigrationIntent v1alpha1.SchedulingIntent = "live_migration"
// RebuildIntent indicates that the request is intended for rebuilding a VM.
RebuildIntent v1alpha1.SchedulingIntent = "rebuild"
// ResizeIntent indicates that the request is intended for resizing a VM.
ResizeIntent v1alpha1.SchedulingIntent = "resize"
// EvacuateIntent indicates that the request is intended for evacuating a VM.
EvacuateIntent v1alpha1.SchedulingIntent = "evacuate"
// CreateIntent indicates that the request is intended for creating a new VM.
CreateIntent v1alpha1.SchedulingIntent = "create"
// ReserveForFailoverIntent indicates that the request is for failover reservation scheduling.
ReserveForFailoverIntent v1alpha1.SchedulingIntent = "reserve_for_failover"
// ReserveForCommittedResourceIntent indicates that the request is for CR reservation scheduling.
ReserveForCommittedResourceIntent v1alpha1.SchedulingIntent = "reserve_for_committed_resource"
)
// GetIntent analyzes the request spec and determines the intent of the scheduling request.
func (req ExternalSchedulerRequest) GetIntent() (v1alpha1.SchedulingIntent, error) {
str, err := req.Spec.Data.GetSchedulerHintStr("_nova_check_type")
if err != nil {
return "", err
}
switch str {
// See: https://github.com/sapcc/nova/blob/ba4813/nova/scheduler/utils.py#L1251
case "rebuild":
return RebuildIntent, nil
// See: https://github.com/sapcc/nova/blob/ba4813/nova/scheduler/utils.py#L1264
case "resize":
return ResizeIntent, nil
// See: https://github.com/sapcc/nova/blob/ba4813/nova/scheduler/utils.py#L1277C47-L1277C63
case "live_migrate":
return LiveMigrationIntent, nil
// Added in https://github.com/sapcc/nova/pull/594
// See: https://github.com/sapcc/nova/blob/c88393/nova/compute/api.py#L5770
case "evacuate":
return EvacuateIntent, nil
// Used by cortex failover reservation controller
case "reserve_for_failover":
return ReserveForFailoverIntent, nil
// Used by cortex committed resource reservation controller
case "reserve_for_committed_resource":
return ReserveForCommittedResourceIntent, nil
default:
return CreateIntent, nil
}
}
// Response generated by cortex for the Nova scheduler.
// Cortex returns an ordered list of hosts that the VM should be scheduled on.
type ExternalSchedulerResponse struct {
Hosts []string `json:"hosts"`
}
// Wrapped Nova object. Nova returns objects in this format.
type NovaObject[V any] struct {
Name string `json:"nova_object.name"`
Namespace string `json:"nova_object.namespace"`
Version string `json:"nova_object.version"`
Data V `json:"nova_object.data"`
Changes []string `json:"nova_object.changes"`
}
type NovaObjectList[V any] struct {
Objects []NovaObject[V] `json:"objects"`
}
// Spec object from the Nova scheduler pipeline. Fields unused by Nova are omitted.
// See: https://github.com/sapcc/nova/blob/stable/xena-m3/nova/objects/request_spec.py
type NovaSpec struct {
ProjectID string `json:"project_id"`
UserID string `json:"user_id"`
InstanceUUID string `json:"instance_uuid"`
AvailabilityZone string `json:"availability_zone"`
NumInstances uint64 `json:"num_instances"`
IsBfv bool `json:"is_bfv"`
// Consider using GetSchedulerHintStr.
SchedulerHints map[string]any `json:"scheduler_hints"`
IgnoreHosts *[]string `json:"ignore_hosts"`
ForceHosts *[]string `json:"force_hosts"`
ForceNodes *[]string `json:"force_nodes"`
Image NovaObject[NovaImageMeta] `json:"image"`
Flavor NovaObject[NovaFlavor] `json:"flavor"`
RequestLevelParams NovaObject[NovaRequestLevelParams] `json:"request_level_params"`
NetworkMetadata NovaObject[map[string]any] `json:"network_metadata"`
Limits NovaObject[map[string]any] `json:"limits"`
RequestedNetworks NovaObjectList[map[string]any] `json:"requested_networks"`
SecurityGroups NovaObjectList[map[string]any] `json:"security_groups"`
NumaTopology *NovaObject[NovaNumaTopology] `json:"numa_topology"`
RequestedDestination *NovaObject[NovaRequestedDestination] `json:"requested_destination"`
InstanceGroup *NovaObject[NovaInstanceGroup] `json:"instance_group"`
}
// Hints can be a one-element list or a direct value.
// See: https://github.com/sapcc/nova/blob/3e715db/nova/objects/request_spec.py#L382
//
// This function is a convenience to extract the first element
// from the hints list or return the value directly if it's not a list.
func (s NovaSpec) GetSchedulerHintStr(key string) (string, error) {
if s.SchedulerHints == nil {
return "", errors.New("scheduler hints are not set")
}
raw, ok := s.SchedulerHints[key]
if !ok {
return "", fmt.Errorf("scheduler hint %q not found", key)
}
switch v := raw.(type) {
case []any:
if len(v) > 0 {
if str, ok := v[0].(string); ok {
return str, nil
}
}
case string:
return v, nil
}
return "", errors.New("unknown scheduler hint type")
}
type NovaInstanceGroup struct {
UserID string `json:"user_id"`
ProjectID string `json:"project_id"`
UUID string `json:"uuid"`
Name string `json:"name"`
Policies []string `json:"policies"`
Members []string `json:"members"`
Hosts []string `json:"hosts"`
Policy string `json:"policy"`
Rules map[string]any `json:"_rules"`
CreatedAt string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
DeletedAt *string `json:"deleted_at"`
Deleted bool `json:"deleted"`
}
// Nova image metadata for the specified VM.
type NovaImageMeta struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Checksum string `json:"checksum"`
Owner string `json:"owner"`
Size int `json:"size"`
ContainerFormat string `json:"container_format"`
DiskFormat string `json:"disk_format"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
MinRAM int `json:"min_ram"`
MinDisk int `json:"min_disk"`
Properties NovaObject[map[string]any] `json:"properties"`
}
// Nova flavor metadata for the specified VM.
type NovaFlavor struct {
ID int `json:"id"`
Name string `json:"name"`
MemoryMB uint64 `json:"memory_mb"`
VCPUs uint64 `json:"vcpus"`
RootGB uint64 `json:"root_gb"`
EphemeralGB uint64 `json:"ephemeral_gb"`
FlavorID string `json:"flavorid"`
Swap int `json:"swap"`
RXTXFactor float64 `json:"rxtx_factor"`
VCPUWeight int `json:"vcpu_weight"`
Disabled bool `json:"disabled"`
IsPublic bool `json:"is_public"`
ExtraSpecs map[string]string `json:"extra_specs"`
Description *string `json:"description"`
CreatedAt string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
DeletedAt *string `json:"deleted_at"`
Deleted bool `json:"deleted"`
}
// Nova NUMA topology for the specified VM.
type NovaNumaTopology struct {
Cells []NovaObject[map[string]any] `json:"cells"`
}
type NovaRequestedDestination struct {
Host string `json:"host"`
Node string `json:"node"`
Aggregates []string `json:"aggregates"`
ForbiddenAggregates *[]string `json:"forbidden_aggregates"`
}
type NovaRequestLevelParams struct {
RootRequired []any `json:"root_required"`
RootForbidden []any `json:"root_forbidden"`
SameSubtree []any `json:"same_subtree"`
}
// Nova request context object. For the spec of this object, see:
//
// - This: https://github.com/sapcc/nova/blob/a56409/nova/context.py#L166
// - And: https://github.com/openstack/oslo.context/blob/db20dd/oslo_context/context.py#L329
//
// Some fields are omitted: "service_catalog", "read_deleted" (same as "show_deleted")
type NovaRequestContext struct {
// Fields added by oslo.context
UserID string `json:"user"`
ProjectID string `json:"project_id"`
SystemScope *string `json:"system_scope"`
Project string `json:"project"`
DomainID *string `json:"domain"`
UserDomainID string `json:"user_domain"`
ProjectDomainID string `json:"project_domain"`
IsAdmin bool `json:"is_admin"`
ReadOnly bool `json:"read_only"`
ShowDeleted bool `json:"show_deleted"`
RequestID string `json:"request_id"`
GlobalRequestID *string `json:"global_request_id"`
ResourceUUID *string `json:"resource_uuid"`
Roles []string `json:"roles"`
UserIdentity string `json:"user_identity"`
IsAdminProject bool `json:"is_admin_project"`
ReadDeleted string `json:"read_deleted"`
// Fields added by the Nova scheduler
RemoteAddress string `json:"remote_address"`
Timestamp string `json:"timestamp"`
QuotaClass *string `json:"quota_class"`
UserName string `json:"user_name"`
ProjectName string `json:"project_name"`
}