-
Notifications
You must be signed in to change notification settings - Fork 23
/
constraints.go
371 lines (317 loc) · 9.31 KB
/
constraints.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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// HasConstraints defines the common methods for setting and
// getting constraints for the various entities.
type HasConstraints interface {
Constraints() Constraints
SetConstraints(ConstraintsArgs)
}
// Constraints holds information about particular deployment
// constraints for entities.
type Constraints interface {
AllocatePublicIP() bool
Architecture() string
Container() string
CpuCores() uint64
CpuPower() uint64
ImageID() string
InstanceType() string
Memory() uint64
RootDisk() uint64
RootDiskSource() string
Spaces() []string
Tags() []string
Zones() []string
VirtType() string
}
// ConstraintsArgs is an argument struct to construct Constraints.
type ConstraintsArgs struct {
AllocatePublicIP bool
Architecture string
Container string
CpuCores uint64
CpuPower uint64
ImageID string
InstanceType string
Memory uint64
RootDisk uint64
RootDiskSource string
Spaces []string
Tags []string
Zones []string
VirtType string
}
func newConstraints(args ConstraintsArgs) *constraints {
// If the ConstraintsArgs are all empty, then we return
// nil to indicate that there are no constraints.
if args.empty() {
return nil
}
tags := make([]string, len(args.Tags))
copy(tags, args.Tags)
spaces := make([]string, len(args.Spaces))
copy(spaces, args.Spaces)
zones := make([]string, len(args.Zones))
copy(zones, args.Zones)
return &constraints{
Version: 5,
AllocatePublicIP_: args.AllocatePublicIP,
Architecture_: args.Architecture,
Container_: args.Container,
CpuCores_: args.CpuCores,
CpuPower_: args.CpuPower,
ImageID_: args.ImageID,
InstanceType_: args.InstanceType,
Memory_: args.Memory,
RootDisk_: args.RootDisk,
RootDiskSource_: args.RootDiskSource,
Spaces_: spaces,
Tags_: tags,
Zones_: zones,
VirtType_: args.VirtType,
}
}
type constraints struct {
Version int `yaml:"version"`
AllocatePublicIP_ bool `yaml:"allocate-public-ip,omitempty"`
Architecture_ string `yaml:"architecture,omitempty"`
Container_ string `yaml:"container,omitempty"`
CpuCores_ uint64 `yaml:"cores,omitempty"`
CpuPower_ uint64 `yaml:"cpu-power,omitempty"`
ImageID_ string `yaml:"image-id,omitempty"`
InstanceType_ string `yaml:"instance-type,omitempty"`
Memory_ uint64 `yaml:"memory,omitempty"`
RootDisk_ uint64 `yaml:"root-disk,omitempty"`
RootDiskSource_ string `yaml:"root-disk-source,omitempty"`
Spaces_ []string `yaml:"spaces,omitempty"`
Tags_ []string `yaml:"tags,omitempty"`
Zones_ []string `yaml:"zones,omitempty"`
VirtType_ string `yaml:"virt-type,omitempty"`
}
// AllocatePublicIP implements Constraints.
func (c *constraints) AllocatePublicIP() bool {
return c.AllocatePublicIP_
}
// Architecture implements Constraints.
func (c *constraints) Architecture() string {
return c.Architecture_
}
// Container implements Constraints.
func (c *constraints) Container() string {
return c.Container_
}
// CpuCores implements Constraints.
func (c *constraints) CpuCores() uint64 {
return c.CpuCores_
}
// CpuPower implements Constraints.
func (c *constraints) CpuPower() uint64 {
return c.CpuPower_
}
// ImageID implements Constraints.
func (c *constraints) ImageID() string {
return c.ImageID_
}
// InstanceType implements Constraints.
func (c *constraints) InstanceType() string {
return c.InstanceType_
}
// Memory implements Constraints.
func (c *constraints) Memory() uint64 {
return c.Memory_
}
// RootDisk implements Constraints.
func (c *constraints) RootDisk() uint64 {
return c.RootDisk_
}
// RootDiskSource implements Constraints.
func (c *constraints) RootDiskSource() string {
return c.RootDiskSource_
}
// Spaces implements Constraints.
func (c *constraints) Spaces() []string {
var spaces []string
if count := len(c.Spaces_); count > 0 {
spaces = make([]string, count)
copy(spaces, c.Spaces_)
}
return spaces
}
// Tags implements Constraints.
func (c *constraints) Tags() []string {
var tags []string
if count := len(c.Tags_); count > 0 {
tags = make([]string, count)
copy(tags, c.Tags_)
}
return tags
}
// Zones implements Constraints.
func (c *constraints) Zones() []string {
var zones []string
if count := len(c.Zones_); count > 0 {
zones = make([]string, count)
copy(zones, c.Zones_)
}
return zones
}
// VirtType implements Constraints.
func (c *constraints) VirtType() string {
return c.VirtType_
}
func importConstraints(source map[string]interface{}) (*constraints, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "constraints version schema check failed")
}
getFields, ok := constraintsFieldsFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
checker := schema.FieldMap(getFields())
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "constraints v%d schema check failed", version)
}
valid := coerced.(map[string]interface{})
cores, err := constraintsValidCPUCores(valid)
if err != nil {
return nil, err
}
return validatedConstraints(version, valid, cores), nil
}
var constraintsFieldsFuncs = map[int]fieldsFunc{
1: constraintsV1Fields,
2: constraintsV2Fields,
3: constraintsV3Fields,
4: constraintsV4Fields,
5: constraintsV5Fields,
}
func constraintsV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"architecture": schema.String(),
"container": schema.String(),
"cpu-cores": schema.ForceUint(),
"cores": schema.ForceUint(),
"cpu-power": schema.ForceUint(),
"instance-type": schema.String(),
"memory": schema.ForceUint(),
"root-disk": schema.ForceUint(),
"spaces": schema.List(schema.String()),
"tags": schema.List(schema.String()),
"virt-type": schema.String(),
}
defaults := schema.Defaults{
"architecture": "",
"container": "",
"cpu-cores": schema.Omit,
"cores": schema.Omit,
"cpu-power": uint64(0),
"instance-type": "",
"memory": uint64(0),
"root-disk": uint64(0),
"spaces": schema.Omit,
"tags": schema.Omit,
"virt-type": "",
}
return fields, defaults
}
func constraintsV2Fields() (schema.Fields, schema.Defaults) {
fields, defaults := constraintsV1Fields()
fields["zones"] = schema.List(schema.String())
defaults["zones"] = schema.Omit
return fields, defaults
}
func constraintsV3Fields() (schema.Fields, schema.Defaults) {
fields, defaults := constraintsV2Fields()
fields["root-disk-source"] = schema.String()
defaults["root-disk-source"] = ""
return fields, defaults
}
func constraintsV4Fields() (schema.Fields, schema.Defaults) {
fields, defaults := constraintsV3Fields()
fields["allocate-public-ip"] = schema.Bool()
defaults["allocate-public-ip"] = schema.Omit
return fields, defaults
}
func constraintsV5Fields() (schema.Fields, schema.Defaults) {
fields, defaults := constraintsV4Fields()
fields["image-id"] = schema.String()
defaults["image-id"] = ""
return fields, defaults
}
// constraintsValidCPUCores returns an error if both aliases for CPU core count
// are present in the list of fields.
// If correctly specified, the cores value is returned.
func constraintsValidCPUCores(valid map[string]interface{}) (uint64, error) {
var cores uint64
_, hasCPU := valid["cpu-cores"]
_, hasCores := valid["cores"]
if hasCPU && hasCores {
return cores, errors.Errorf("can not specify both cores and cores constraints")
}
if hasCPU {
cores = valid["cpu-cores"].(uint64)
}
if hasCores {
cores = valid["cores"].(uint64)
}
return cores, nil
}
// validatedConstraints returns a constraints reference from the supplied
// *valid* fields.
func validatedConstraints(version int, valid map[string]interface{}, cores uint64) *constraints {
cons := &constraints{
Version: version,
Architecture_: valid["architecture"].(string),
Container_: valid["container"].(string),
CpuCores_: cores,
CpuPower_: valid["cpu-power"].(uint64),
InstanceType_: valid["instance-type"].(string),
Memory_: valid["memory"].(uint64),
RootDisk_: valid["root-disk"].(uint64),
Spaces_: convertToStringSlice(valid["spaces"]),
Tags_: convertToStringSlice(valid["tags"]),
VirtType_: valid["virt-type"].(string),
}
if version > 1 {
cons.Zones_ = convertToStringSlice(valid["zones"])
}
if version > 2 {
cons.RootDiskSource_ = valid["root-disk-source"].(string)
}
if version > 3 {
if value, ok := valid["allocate-public-ip"]; ok {
cons.AllocatePublicIP_ = value.(bool)
}
}
if version > 4 {
cons.ImageID_ = valid["image-id"].(string)
}
return cons
}
func addConstraintsSchema(fields schema.Fields, defaults schema.Defaults) {
fields["constraints"] = schema.StringMap(schema.Any())
defaults["constraints"] = schema.Omit
}
func (c ConstraintsArgs) empty() bool {
return c.Architecture == "" &&
c.Container == "" &&
c.CpuCores == 0 &&
c.CpuPower == 0 &&
c.ImageID == "" &&
c.InstanceType == "" &&
c.Memory == 0 &&
c.RootDisk == 0 &&
c.RootDiskSource == "" &&
c.Spaces == nil &&
c.Tags == nil &&
c.Zones == nil &&
c.VirtType == "" &&
c.AllocatePublicIP == false
}