forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices.go
152 lines (131 loc) · 4.22 KB
/
devices.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/charm/v9"
"github.com/juju/errors"
"github.com/juju/mgo/v2"
"github.com/juju/mgo/v2/txn"
"github.com/juju/juju/environs/config"
)
type DeviceType string
// DeviceConstraints describes a set of device constraints.
type DeviceConstraints struct {
// Type is the device type or device-class.
// currently supported types are
// - gpu
// - nvidia.com/gpu
// - amd.com/gpu
Type DeviceType `bson:"type"`
// Count is the number of devices that the user has asked for - count min and max are the
// number of devices the charm requires.
Count int64 `bson:"count"`
// Attributes is a collection of key value pairs device related (node affinity labels/tags etc.).
Attributes map[string]string `bson:"attributes"`
}
// NewDeviceBackend creates a backend for managing device.
func NewDeviceBackend(st *State) (*deviceBackend, error) {
m, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
return &deviceBackend{
mb: st,
settings: NewStateSettings(st),
modelType: m.Type(),
config: m.ModelConfig,
application: st.Application,
unit: st.Unit,
machine: st.Machine,
}, nil
}
type deviceBackend struct {
mb modelBackend
config func() (*config.Config, error)
application func(string) (*Application, error)
unit func(string) (*Unit, error)
machine func(string) (*Machine, error)
modelType ModelType
settings *StateSettings
}
// deviceConstraintsDoc contains device constraints for an entity.
type deviceConstraintsDoc struct {
DocID string `bson:"_id"`
Constraints map[string]DeviceConstraints `bson:"constraints"`
}
func createDeviceConstraintsOp(id string, cons map[string]DeviceConstraints) txn.Op {
return txn.Op{
C: deviceConstraintsC,
Id: id,
Assert: txn.DocMissing,
Insert: &deviceConstraintsDoc{
Constraints: cons,
},
}
}
func removeDeviceConstraintsOp(id string) txn.Op {
return txn.Op{
C: deviceConstraintsC,
Id: id,
Remove: true,
}
}
// DeviceConstraints returns the device constraints for the specified application.
func (db *deviceBackend) DeviceConstraints(id string) (map[string]DeviceConstraints, error) {
devices, err := readDeviceConstraints(db.mb, id)
if err == nil {
return devices, nil
} else if errors.IsNotFound(err) {
return map[string]DeviceConstraints{}, nil
}
return nil, err
}
func readDeviceConstraints(mb modelBackend, id string) (map[string]DeviceConstraints, error) {
coll, closer := mb.db().GetCollection(deviceConstraintsC)
defer closer()
var doc deviceConstraintsDoc
err := coll.FindId(id).One(&doc)
if err == mgo.ErrNotFound {
return nil, errors.NotFoundf("device constraints for %q", id)
}
if err != nil {
return nil, errors.Annotatef(err, "cannot get device constraints for %q", id)
}
return doc.Constraints, nil
}
func validateDeviceConstraints(db *deviceBackend, allCons map[string]DeviceConstraints, charmMeta *charm.Meta) error {
err := validateDeviceConstraintsAgainstCharm(db, allCons, charmMeta)
if err != nil {
return errors.Trace(err)
}
// Ensure all devices have constraints specified. Defaults should have
// been set by this point, if the user didn't specify constraints.
for name, charmDevice := range charmMeta.Devices {
if _, ok := allCons[name]; !ok && charmDevice.CountMin > 0 {
return errors.Errorf("no constraints specified for device %q", name)
}
}
return nil
}
func validateDeviceConstraintsAgainstCharm(
db *deviceBackend,
allCons map[string]DeviceConstraints,
charmMeta *charm.Meta,
) error {
for name, cons := range allCons {
charmDevice, ok := charmMeta.Devices[name]
if !ok {
return errors.Errorf("charm %q has no device called %q", charmMeta.Name, name)
}
if err := validateCharmDeviceCount(charmDevice, cons.Count); err != nil {
return errors.Annotatef(err, "charm %q device %q", charmMeta.Name, name)
}
}
return nil
}
func validateCharmDeviceCount(charmDevice charm.Device, count int64) error {
if charmDevice.CountMin > 0 && count < charmDevice.CountMin {
return errors.Errorf("minimum device size is %d, %d specified", charmDevice.CountMin, count)
}
return nil
}