forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices.go
104 lines (84 loc) · 2.04 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
package shared
type Device map[string]string
type Devices map[string]Device
func (list Devices) ContainsName(k string) bool {
if list[k] != nil {
return true
}
return false
}
func (d Device) get(key string) string {
return d[key]
}
func (list Devices) Contains(k string, d Device) bool {
// If it didn't exist, it's different
if list[k] == nil {
return false
}
old := list[k]
return deviceEquals(old, d)
}
func deviceEquals(old Device, d Device) bool {
// Check for any difference and addition/removal of properties
for k, _ := range d {
if d[k] != old[k] {
return false
}
}
for k, _ := range old {
if d[k] != old[k] {
return false
}
}
return true
}
func (old Devices) Update(newlist Devices) (map[string]Device, map[string]Device, map[string]Device) {
rmlist := map[string]Device{}
addlist := map[string]Device{}
updatelist := map[string]Device{}
for key, d := range old {
if !newlist.Contains(key, d) {
rmlist[key] = d
}
}
for key, d := range newlist {
if !old.Contains(key, d) {
addlist[key] = d
}
}
for key, d := range addlist {
srcOldDevice := rmlist[key]
var oldDevice Device
err := DeepCopy(&srcOldDevice, &oldDevice)
if err != nil {
continue
}
srcNewDevice := newlist[key]
var newDevice Device
err = DeepCopy(&srcNewDevice, &newDevice)
if err != nil {
continue
}
for _, k := range []string{"limits.max", "limits.read", "limits.write", "limits.egress", "limits.ingress"} {
delete(oldDevice, k)
delete(newDevice, k)
}
if deviceEquals(oldDevice, newDevice) {
delete(rmlist, key)
delete(addlist, key)
updatelist[key] = d
}
}
return rmlist, addlist, updatelist
}
func (newBaseDevices Devices) ExtendFromProfile(currentFullDevices Devices, newDevicesFromProfile Devices) error {
// For any entry which exists in a profile and doesn't in the container config, add it
for name, newDev := range newDevicesFromProfile {
if curDev, ok := currentFullDevices[name]; ok {
newBaseDevices[name] = curDev
} else {
newBaseDevices[name] = newDev
}
}
return nil
}