github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/concepts/plugins/devices.mdx (about) 1 --- 2 layout: docs 3 page_title: Device Plugins 4 description: Learn how to author a Nomad device plugin. 5 --- 6 7 # Devices 8 9 Nomad has built-in support for scheduling compute resources such as CPU, memory, 10 and networking. Nomad device plugins are used to support scheduling tasks with 11 other devices, such as GPUs. They are responsible for fingerprinting these 12 devices and working with the Nomad client to make them available to assigned 13 tasks. 14 15 For a real world example of a Nomad device plugin implementation, see the [Nvidia 16 GPU plugin](https://github.com/hashicorp/nomad-device-nvidia). 17 18 ## Authoring Device Plugins 19 20 Authoring a device plugin in Nomad consists of implementing the 21 [DevicePlugin][deviceplugin] interface alongside 22 a main package to launch the plugin. 23 24 The [device plugin skeleton project][skeletonproject] exists to help bootstrap 25 the development of new device plugins. It provides most of the boilerplate 26 necessary for a device plugin, along with detailed comments. 27 28 ### Lifecycle and State 29 30 A device plugin is long-lived. Nomad will ensure that one instance of the plugin is 31 running. If the plugin crashes or otherwise terminates, Nomad will launch another 32 instance of it. 33 34 However, unlike [task drivers](/docs/concepts/plugins/task-drivers), device plugins do not currently 35 have an interface for persisting state to the Nomad client. Instead, the device 36 plugin API emphasizes fingerprinting devices and reporting their status. After 37 helping to provision a task with a scheduled device, a device plugin does not 38 have any responsibility (or ability) to monitor the task. 39 40 ## Device Plugin API 41 42 The [base plugin][baseplugin] must be implemented in addition to the following 43 functions. 44 45 ### `Fingerprint(context.Context) (<-chan *FingerprintResponse, error)` 46 47 The `Fingerprint` [function][fingerprintfn] is called by the client when the plugin is started. 48 It allows the plugin to provide Nomad with a list of discovered devices, along with their 49 attributes, for the purpose of scheduling workloads using devices. 50 The channel returned should immediately send an initial 51 [`FingerprintResponse`][fingerprintresponse], then send periodic updates at 52 an appropriate interval until the context is canceled. 53 54 Each fingerprint response consists of either an error or a list of device groups. 55 A device group is a list of detected devices that are identical for the purpose of 56 scheduling; that is, they will have identical attributes. 57 58 ### `Stats(context.Context, time.Duration) (<-chan *StatsResponse, error)` 59 60 The `Stats` [function][statsfn] returns a channel on which the plugin should 61 emit device statistics, at the specified interval, until either an error is 62 encountered or the specified context is cancelled. The `StatsReponse` object 63 allows [dimensioned][dimensioned] statistics to be returned for each device in a device group. 64 65 ### `Reserve(deviceIDs []string) (*ContainerReservation, error)` 66 67 The `Reserve` [function][reservefn] accepts a list of device IDs and returns the information 68 necessary for the client to make those devices available to a task. Currently, 69 the `ContainerReservation` object allows the plugin to specify environment 70 variables for the task, as well as a list of host devices and files to be mounted 71 into the task's filesystem. Any orchestration required to prepare the device for 72 use should also be performed in this function. 73 74 [deviceplugin]: https://github.com/hashicorp/nomad/blob/v0.9.0/plugins/device/device.go#L20-L33 75 [baseplugin]: /docs/concepts/plugins/base 76 [skeletonproject]: https://github.com/hashicorp/nomad-skeleton-device-plugin 77 [fingerprintresponse]: https://github.com/hashicorp/nomad/blob/v0.9.0/plugins/device/device.go#L37-L43 78 [fingerprintfn]: https://github.com/hashicorp/nomad-skeleton-device-plugin/blob/v0.1.0/device/device.go#L159-L165 79 [statsfn]: https://github.com/hashicorp/nomad-skeleton-device-plugin/blob/v0.1.0/device/device.go#L169-L176 80 [reservefn]: https://github.com/hashicorp/nomad-skeleton-device-plugin/blob/v0.1.0/device/device.go#L189-L245 81 [dimensioned]: https://github.com/hashicorp/nomad/blob/v0.9.0/plugins/shared/structs/stats.go#L33-L34