-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.go
191 lines (163 loc) · 4.32 KB
/
widget.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
package main
import (
"encoding/json"
"flag"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/piersharding/k8s-ghost-device-plugin/file"
"golang.org/x/net/context"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
"os"
"path/filepath"
"runtime"
)
const RdmaDeviceRource = "/sys/class/infiniband/%s/device/resource"
const NetDeviceRource = "/sys/class/net/%s/device/resource"
var flagStrictPerms = flag.Bool("strict.perms", true, "Strict permission checking on config files")
func IsStrictPerms() bool {
if !*flagStrictPerms || os.Getenv("STRICT_PERMS") == "false" {
return false
}
return true
}
// ownerHasExclusiveWritePerms asserts that the current user or root is the
// owner of the config file and that the config file is (at most) writable by
// the owner or root (e.g. group and other cannot have write access).
func ownerHasExclusiveWritePerms(name string) error {
if runtime.GOOS == "windows" {
return nil
}
info, err := file.Stat(name)
if err != nil {
return err
}
euid := os.Geteuid()
fileUID, _ := info.UID()
perm := info.Mode().Perm()
if fileUID != 0 && euid != fileUID {
return fmt.Errorf(`config file ("%v") must be owned by the beat user `+
`(uid=%v) or root`, name, euid)
}
// Test if group or other have write permissions.
if perm&0022 > 0 {
nameAbs, err := filepath.Abs(name)
if err != nil {
nameAbs = name
}
return fmt.Errorf(`config file ("%v") can only be writable by the `+
`owner but the permissions are "%v" (to fix the permissions use: `+
`'chmod go-w %v')`,
name, perm, nameAbs)
}
return nil
}
// widget.devices:
// - type: snaffler
// model: v1
// device: /dev/wibble1
type WidgetConfig struct {
Devices []struct {
Type string
Model string
Device string
}
}
func LoadFile(path string) (*WidgetConfig, error) {
if IsStrictPerms() {
if err := ownerHasExclusiveWritePerms(path); err != nil {
return nil, err
}
}
var cfg WidgetConfig
reader, _ := os.Open(path)
buf, _ := ioutil.ReadAll(reader)
err := yaml.Unmarshal(buf, &cfg)
if err != nil {
log.Errorf("Error parsing YAML config: %v", err)
return nil, err
}
cfgOut, _ := json.Marshal(cfg)
log.Debugf("loading config from file '%v' => %v", path, string(cfgOut))
return &cfg, err
}
func GetWidgetDevices(resourceConfigs string) ([]Device, error) {
log.Debugf("Going to read config")
cfg, err := LoadFile(resourceConfigs)
if err != nil {
log.Errorf("Error reading config: %v", err)
return nil, err
}
return generateWidgetDevices(*cfg)
}
func generateWidgetDevices(widgetConfigs WidgetConfig) ([]Device, error) {
var devs []Device
// Get all RDMA device list
for i, w := range widgetConfigs.Devices {
devs = append(devs, Device{
// RdmaDevice: d,
// NetDevice: n,
Id: i, // starts at 0
Name: fmt.Sprintf("%s_%s_%d", w.Type, w.Model, i),
DeviceType: w.Type,
DeviceModel: w.Model,
WidgetDevice: w.Device,
})
}
// ibvDevList, err := ibverbs.IbvGetDeviceList()
// if err != nil {
// return nil, err
// }
// netDevList, err := GetVfNetDevice(resourceConfigs)
// if err != nil {
// return nil, err
// }
// for _, d := range ibvDevList {
// for _, n := range netDevList {
// dResource, err := getRdmaDeviceResoure(d.Name)
// if err != nil {
// return nil, err
// }
// nResource, err := getNetDeviceResoure(n)
// if err != nil {
// return nil, err
// }
// // the same device
// if bytes.Compare(dResource, nResource) == 0 {
// devs = append(devs, Device{
// RdmaDevice: d,
// NetDevice: n,
// })
// }
// }
// }
return devs, nil
}
// func getRdmaDeviceResoure(name string) ([]byte, error) {
// resourceFile := fmt.Sprintf(RdmaDeviceRource, name)
// data, err := ioutil.ReadFile(resourceFile)
// return data, err
// }
// func getNetDeviceResoure(name string) ([]byte, error) {
// resourceFile := fmt.Sprintf(NetDeviceRource, name)
// data, err := ioutil.ReadFile(resourceFile)
// return data, err
// }
func deviceExists(devs []*pluginapi.Device, id string) bool {
for _, d := range devs {
if d.ID == id {
return true
}
}
return false
}
func watchXIDs(ctx context.Context, devs []*pluginapi.Device, xids chan<- *pluginapi.Device) {
for {
select {
case <-ctx.Done():
return
}
// TODO: check Widget device healthy status
}
}