Skip to content

Commit a23d460

Browse files
committed
Adding virttype to constraints.
1 parent 29c542b commit a23d460

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

constraints/constraints.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
InstanceType = "instance-type"
3131
Networks = "networks"
3232
Spaces = "spaces"
33+
VirtType = "virt-type"
3334
)
3435

3536
// Value describes a user's requirements of the hardware on which units
@@ -88,6 +89,10 @@ type Value struct {
8889
// TODO(dimitern): Drop this as soon as spaces can be used for
8990
// deployments instead.
9091
Networks *[]string `json:"networks,omitempty" yaml:"networks,omitempty"`
92+
93+
// VirtType, if not nil or empty, indicates that a machine must run the named
94+
// virtual type. Only valid for clouds with multi-hypervisor support.
95+
VirtType *string `json:"virt-type,omitempty" yaml:"virt-type,omitempty"`
9196
}
9297

9398
// fieldNames records a mapping from the constraint tag to struct field name.
@@ -199,6 +204,11 @@ func (v *Value) HaveNetworks() bool {
199204
return v.Networks != nil && len(*v.Networks) > 0
200205
}
201206

207+
// HasVirtType returns true if the constraints.Value specifies an virtual type.
208+
func (v *Value) HasVirtType() bool {
209+
return v.VirtType != nil && *v.VirtType != ""
210+
}
211+
202212
// String expresses a constraints.Value in the language in which it was specified.
203213
func (v Value) String() string {
204214
var strs []string
@@ -243,6 +253,9 @@ func (v Value) String() string {
243253
s := strings.Join(*v.Networks, ",")
244254
strs = append(strs, "networks="+s)
245255
}
256+
if v.VirtType != nil {
257+
strs = append(strs, "virt-type="+string(*v.VirtType))
258+
}
246259
return strings.Join(strs, " ")
247260
}
248261

@@ -286,6 +299,9 @@ func (v Value) GoString() string {
286299
} else if v.Networks != nil {
287300
values = append(values, "Networks: (*[]string)(nil)")
288301
}
302+
if v.VirtType != nil {
303+
values = append(values, fmt.Sprintf("VirtType: %q", *v.VirtType))
304+
}
289305
return fmt.Sprintf("{%s}", strings.Join(values, ", "))
290306
}
291307

@@ -427,6 +443,8 @@ func (v *Value) setRaw(raw string) error {
427443
err = v.setSpaces(str)
428444
case Networks:
429445
err = v.setNetworks(str)
446+
case VirtType:
447+
err = v.setVirtType(str)
430448
default:
431449
return errors.Errorf("unknown constraint %q", name)
432450
}
@@ -487,6 +505,8 @@ func (v *Value) UnmarshalYAML(unmarshal func(interface{}) error) error {
487505
if err == nil {
488506
v.Networks = networks
489507
}
508+
case VirtType:
509+
v.VirtType = &vstr
490510
default:
491511
return errors.Errorf("unknown constraint value: %v", k)
492512
}
@@ -628,6 +648,16 @@ func (v *Value) validateNetworks(networks *[]string) error {
628648
return nil
629649
}
630650

651+
func (v *Value) setVirtType(str string) error {
652+
if v.VirtType != nil {
653+
return errors.Errorf("already set")
654+
}
655+
// TODO (anastasiamac 2016-03-15)
656+
// Do I need to validate that this is a supported virt type?
657+
v.VirtType = &str
658+
return nil
659+
}
660+
631661
func parseUint64(str string) (*uint64, error) {
632662
var value uint64
633663
if str != "" {

constraints/constraints_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,39 @@ var parseConstraintsTests = []struct {
310310
args: []string{"instance-type="},
311311
},
312312

313+
// "virt-type" in detail.
314+
{
315+
summary: "set virt-type empty",
316+
args: []string{"virt-type="},
317+
}, {
318+
summary: "set virt-type kvm",
319+
args: []string{"virt-type=kvm"},
320+
}, {
321+
summary: "set virt-type lxd",
322+
args: []string{"virt-type=lxd"},
323+
}, {
324+
summary: "double set virt-type together",
325+
args: []string{"virt-type=kvm virt-type=kvm"},
326+
err: `bad "virt-type" constraint: already set`,
327+
}, {
328+
summary: "double set virt-type separately",
329+
args: []string{"virt-type=kvm", "virt-type="},
330+
err: `bad "virt-type" constraint: already set`,
331+
},
332+
313333
// Everything at once.
314334
{
315335
summary: "kitchen sink together",
316336
args: []string{
317337
"root-disk=8G mem=2T arch=i386 cpu-cores=4096 cpu-power=9001 container=lxc " +
318-
"tags=foo,bar spaces=space1,^space2 networks=net,^net2 instance-type=foo"},
338+
"tags=foo,bar spaces=space1,^space2 networks=net,^net2 instance-type=foo",
339+
"virt-type=kvm"},
319340
}, {
320341
summary: "kitchen sink separately",
321342
args: []string{
322343
"root-disk=8G", "mem=2T", "cpu-cores=4096", "cpu-power=9001", "arch=armhf",
323344
"container=lxc", "tags=foo,bar", "spaces=space1,^space2", "networks=net1,^net2",
324-
"instance-type=foo"},
345+
"instance-type=foo", "virt-type=kvm"},
325346
},
326347
}
327348

constraints/validation_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ var validationTests = []struct {
113113
"instance-type": {"foo", "bar"},
114114
"arch": {"amd64", "i386"}},
115115
},
116+
{
117+
cons: "virt-type=bar",
118+
vocab: map[string][]interface{}{"virt-type": {"bar"}},
119+
},
116120
}
117121

118122
func (s *validationSuite) TestValidation(c *gc.C) {

environs/instances/instancetype.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (itype InstanceType) match(cons constraints.Value) (InstanceType, bool) {
6262
if cons.Tags != nil && len(*cons.Tags) > 0 && !tagsMatch(*cons.Tags, itype.Tags) {
6363
return nothing, false
6464
}
65+
if cons.HasVirtType() && (itype.VirtType == nil || *itype.VirtType != *cons.VirtType) {
66+
return nothing, false
67+
}
6568
return itype, true
6669
}
6770

environs/instances/instancetype_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ var getInstanceTypesTest = []struct {
216216
{Id: "1", Name: "it-1", Arches: []string{"amd64"}, Mem: 512, CpuCores: 4, Cost: 100},
217217
},
218218
expectedItypes: []string{"it-2"},
219+
}, {
220+
about: "virt-type filtered by constraint",
221+
cons: "virt-type=hvm",
222+
expectedItypes: []string{"cc1.4xlarge", "cc2.8xlarge"},
223+
itypesToUse: nil,
219224
}, {
220225
about: "deprecated image type requested by name",
221226
cons: "instance-type=dep.small",

0 commit comments

Comments
 (0)