2015-09-09 20:02:39 +00:00
|
|
|
package api
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
import "github.com/hashicorp/nomad/helper"
|
|
|
|
|
2015-09-09 20:02:39 +00:00
|
|
|
// Resources encapsulates the required resources of
|
|
|
|
// a given task or task group.
|
|
|
|
type Resources struct {
|
2017-02-06 19:48:28 +00:00
|
|
|
CPU *int
|
2017-02-22 20:30:05 +00:00
|
|
|
MemoryMB *int `mapstructure:"memory"`
|
|
|
|
DiskMB *int `mapstructure:"disk"`
|
2017-02-06 19:48:28 +00:00
|
|
|
IOPS *int
|
2015-09-09 20:02:39 +00:00
|
|
|
Networks []*NetworkResource
|
2018-10-08 22:38:03 +00:00
|
|
|
Devices []*RequestedDevice
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 17:05:30 +00:00
|
|
|
// Canonicalize will supply missing values in the cases
|
|
|
|
// where they are not provided.
|
2017-02-13 23:18:17 +00:00
|
|
|
func (r *Resources) Canonicalize() {
|
2017-11-13 17:05:30 +00:00
|
|
|
defaultResources := DefaultResources()
|
2017-02-22 20:30:05 +00:00
|
|
|
if r.CPU == nil {
|
2017-11-13 17:05:30 +00:00
|
|
|
r.CPU = defaultResources.CPU
|
2017-02-22 20:30:05 +00:00
|
|
|
}
|
|
|
|
if r.MemoryMB == nil {
|
2017-11-13 17:05:30 +00:00
|
|
|
r.MemoryMB = defaultResources.MemoryMB
|
2017-02-22 20:30:05 +00:00
|
|
|
}
|
|
|
|
if r.IOPS == nil {
|
2017-11-13 17:05:30 +00:00
|
|
|
r.IOPS = defaultResources.IOPS
|
2017-02-22 20:30:05 +00:00
|
|
|
}
|
2017-02-13 23:18:17 +00:00
|
|
|
for _, n := range r.Networks {
|
|
|
|
n.Canonicalize()
|
|
|
|
}
|
2018-10-08 23:09:41 +00:00
|
|
|
for _, d := range r.Devices {
|
|
|
|
d.Canonicalize()
|
|
|
|
}
|
2017-02-13 23:18:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 17:05:30 +00:00
|
|
|
// DefaultResources is a small resources object that contains the
|
|
|
|
// default resources requests that we will provide to an object.
|
2017-11-13 17:25:02 +00:00
|
|
|
// --- THIS FUNCTION IS REPLICATED IN nomad/structs/structs.go
|
2017-11-13 17:05:30 +00:00
|
|
|
// and should be kept in sync.
|
|
|
|
func DefaultResources() *Resources {
|
|
|
|
return &Resources{
|
|
|
|
CPU: helper.IntToPtr(100),
|
|
|
|
MemoryMB: helper.IntToPtr(300),
|
|
|
|
IOPS: helper.IntToPtr(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MinResources is a small resources object that contains the
|
|
|
|
// absolute minimum resources that we will provide to an object.
|
2017-11-13 17:25:02 +00:00
|
|
|
// This should not be confused with the defaults which are
|
|
|
|
// provided in DefaultResources() --- THIS LOGIC IS REPLICATED
|
2017-11-13 17:05:30 +00:00
|
|
|
// IN nomad/structs/structs.go and should be kept in sync.
|
2017-02-06 19:48:28 +00:00
|
|
|
func MinResources() *Resources {
|
|
|
|
return &Resources{
|
2018-01-02 14:56:02 +00:00
|
|
|
CPU: helper.IntToPtr(20),
|
2017-02-06 19:48:28 +00:00
|
|
|
MemoryMB: helper.IntToPtr(10),
|
|
|
|
IOPS: helper.IntToPtr(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges this resource with another resource.
|
|
|
|
func (r *Resources) Merge(other *Resources) {
|
|
|
|
if other == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if other.CPU != nil {
|
|
|
|
r.CPU = other.CPU
|
|
|
|
}
|
|
|
|
if other.MemoryMB != nil {
|
|
|
|
r.MemoryMB = other.MemoryMB
|
|
|
|
}
|
|
|
|
if other.DiskMB != nil {
|
|
|
|
r.DiskMB = other.DiskMB
|
|
|
|
}
|
|
|
|
if other.IOPS != nil {
|
|
|
|
r.IOPS = other.IOPS
|
|
|
|
}
|
|
|
|
if len(other.Networks) != 0 {
|
|
|
|
r.Networks = other.Networks
|
|
|
|
}
|
2018-10-08 22:38:03 +00:00
|
|
|
if len(other.Devices) != 0 {
|
|
|
|
r.Devices = other.Devices
|
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 04:51:30 +00:00
|
|
|
type Port struct {
|
|
|
|
Label string
|
2017-02-22 20:30:05 +00:00
|
|
|
Value int `mapstructure:"static"`
|
2015-11-14 04:51:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 20:02:39 +00:00
|
|
|
// NetworkResource is used to describe required network
|
|
|
|
// resources of a given task.
|
|
|
|
type NetworkResource struct {
|
2017-04-08 01:36:43 +00:00
|
|
|
Device string
|
2015-09-09 20:02:39 +00:00
|
|
|
CIDR string
|
2016-03-11 00:20:51 +00:00
|
|
|
IP string
|
2017-02-13 23:18:17 +00:00
|
|
|
MBits *int
|
2017-04-08 01:36:43 +00:00
|
|
|
ReservedPorts []Port
|
|
|
|
DynamicPorts []Port
|
2017-02-13 23:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkResource) Canonicalize() {
|
|
|
|
if n.MBits == nil {
|
|
|
|
n.MBits = helper.IntToPtr(10)
|
|
|
|
}
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
2018-10-08 22:38:03 +00:00
|
|
|
|
|
|
|
// RequestedDevice is used to request a device for a task.
|
|
|
|
type RequestedDevice struct {
|
|
|
|
// Name is the request name. The possible values are as follows:
|
|
|
|
// * <type>: A single value only specifies the type of request.
|
|
|
|
// * <vendor>/<type>: A single slash delimiter assumes the vendor and type of device is specified.
|
|
|
|
// * <vendor>/<type>/<name>: Two slash delimiters assume vendor, type and specific model are specified.
|
|
|
|
//
|
|
|
|
// Examples are as follows:
|
|
|
|
// * "gpu"
|
|
|
|
// * "nvidia/gpu"
|
|
|
|
// * "nvidia/gpu/GTX2080Ti"
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Count is the number of requested devices
|
2018-10-08 23:09:41 +00:00
|
|
|
Count *uint64
|
2018-10-11 21:05:19 +00:00
|
|
|
|
|
|
|
// Constraints are a set of constraints to apply when selecting the device
|
|
|
|
// to use.
|
|
|
|
Constraints []*Constraint
|
|
|
|
|
|
|
|
// Affinities are a set of affinites to apply when selecting the device
|
|
|
|
// to use.
|
|
|
|
Affinities []*Affinity
|
2018-10-08 23:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *RequestedDevice) Canonicalize() {
|
|
|
|
if d.Count == nil {
|
|
|
|
d.Count = helper.Uint64ToPtr(1)
|
|
|
|
}
|
2018-10-08 22:38:03 +00:00
|
|
|
}
|