|
15 | 15 | package main
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "encoding/json" |
18 | 19 | "flag"
|
19 | 20 | "fmt"
|
20 | 21 | "os"
|
@@ -53,8 +54,11 @@ const (
|
53 | 54 | scanPeriod = 5 * time.Second
|
54 | 55 |
|
55 | 56 | // CDI hook attributes.
|
56 |
| - HookName = "createRuntime" |
57 |
| - HookPath = "/opt/intel/fpga-sw/intel-fpga-crihook" |
| 57 | + CDIKind = "intel.com/fpga" |
| 58 | + CDIVersion = "0.5.0" // Kubernetes 1.27 / CRI-O 1.27 / Containerd 1.7 use this version. |
| 59 | + CDIDir = "/var/run/cdi" |
| 60 | + HookName = "createRuntime" |
| 61 | + HookPath = "/opt/intel/fpga-sw/intel-fpga-crihook" |
58 | 62 | )
|
59 | 63 |
|
60 | 64 | type newPortFunc func(fname string) (fpga.Port, error)
|
@@ -117,14 +121,11 @@ func getRegionTree(devices []device) dpapi.DeviceTree {
|
117 | 121 | }
|
118 | 122 | }
|
119 | 123 |
|
120 |
| - hooks := []*cdispec.Hook{ |
121 |
| - { |
122 |
| - HookName: HookName, |
123 |
| - Path: HookPath, |
124 |
| - }, |
| 124 | + cdiDevices := []*pluginapi.CDIDevice{ |
| 125 | + {Name: fmt.Sprintf("%s=%s", CDIKind, region.id)}, |
125 | 126 | }
|
126 | 127 |
|
127 |
| - regionTree.AddDevice(devType, region.id, dpapi.NewDeviceInfo(health, devNodes, nil, nil, nil, hooks)) |
| 128 | + regionTree.AddDevice(devType, region.id, dpapi.NewDeviceInfo(health, devNodes, nil, nil, nil, cdiDevices)) |
128 | 129 | }
|
129 | 130 | }
|
130 | 131 |
|
@@ -187,6 +188,8 @@ type devicePlugin struct {
|
187 | 188 |
|
188 | 189 | sysfsDir string
|
189 | 190 | devfsDir string
|
| 191 | + mode string |
| 192 | + cdiPath string |
190 | 193 |
|
191 | 194 | deviceReg *regexp.Regexp
|
192 | 195 | portReg *regexp.Regexp
|
@@ -225,13 +228,32 @@ func newDevicePlugin(mode string, rootPath string) (*devicePlugin, error) {
|
225 | 228 | return nil, err
|
226 | 229 | }
|
227 | 230 |
|
| 231 | + dp.mode = mode |
| 232 | + dp.cdiPath = CDIDir |
| 233 | + |
228 | 234 | dp.newPort = fpga.NewPort
|
229 | 235 | dp.scanTicker = time.NewTicker(scanPeriod)
|
230 | 236 | dp.scanDone = make(chan bool, 1) // buffered as we may send to it before Scan starts receiving from it
|
231 | 237 |
|
232 | 238 | return dp, nil
|
233 | 239 | }
|
234 | 240 |
|
| 241 | +func (dp *devicePlugin) Allocate(request *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) { |
| 242 | + // Write CDI device specs if in region mode. |
| 243 | + if dp.mode == regionMode { |
| 244 | + for _, cont := range request.ContainerRequests { |
| 245 | + for _, id := range cont.DevicesIDs { |
| 246 | + if err := generateAndWriteCDISpec(id, dp.cdiPath); err != nil { |
| 247 | + return nil, err |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + // And return error to continue with the default Allocate |
| 254 | + return nil, &dpapi.UseDefaultMethodError{} |
| 255 | +} |
| 256 | + |
235 | 257 | func (dp *devicePlugin) PostAllocate(response *pluginapi.AllocateResponse) error {
|
236 | 258 | // Set container annotations when programming is allowed
|
237 | 259 | if len(dp.annotationValue) > 0 {
|
@@ -358,6 +380,38 @@ func getPluginParams(mode string) (getDevTreeFunc, string, error) {
|
358 | 380 | return getDevTree, annotationValue, nil
|
359 | 381 | }
|
360 | 382 |
|
| 383 | +func generateAndWriteCDISpec(deviceID, cdiPath string) error { |
| 384 | + spec := cdispec.Spec{ |
| 385 | + Version: CDIVersion, |
| 386 | + Kind: CDIKind, |
| 387 | + Devices: []cdispec.Device{ |
| 388 | + { |
| 389 | + Name: deviceID, |
| 390 | + ContainerEdits: cdispec.ContainerEdits{ |
| 391 | + Hooks: []*cdispec.Hook{ |
| 392 | + { |
| 393 | + HookName: HookName, |
| 394 | + Path: HookPath, |
| 395 | + }, |
| 396 | + }, |
| 397 | + }, |
| 398 | + }, |
| 399 | + }, |
| 400 | + } |
| 401 | + |
| 402 | + jsonSpec, err := json.Marshal(spec) |
| 403 | + if err != nil { |
| 404 | + return err |
| 405 | + } |
| 406 | + |
| 407 | + cdiFileName := path.Join(cdiPath, deviceID) + ".json" |
| 408 | + if err = os.WriteFile(cdiFileName, jsonSpec, 0o644); err != nil { |
| 409 | + return err |
| 410 | + } |
| 411 | + |
| 412 | + return nil |
| 413 | +} |
| 414 | + |
361 | 415 | func main() {
|
362 | 416 | var (
|
363 | 417 | mode string
|
|
0 commit comments