f99aaa9134
In the commit 622d3ddb92ea7e656ef831641c02024cb5a5d6d1 "Fixed test and moved constants into standalone func" the minimum CPU resource value for a job was increased from 100 to 20. This can break the nomad setup for people that used lower CPU values and are at the maximum MHz value of the available CPU on a machine. Change the minimum back to 20 MHz to ensure downwards compatibility.
101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
package api
|
|
|
|
import "github.com/hashicorp/nomad/helper"
|
|
|
|
// Resources encapsulates the required resources of
|
|
// a given task or task group.
|
|
type Resources struct {
|
|
CPU *int
|
|
MemoryMB *int `mapstructure:"memory"`
|
|
DiskMB *int `mapstructure:"disk"`
|
|
IOPS *int
|
|
Networks []*NetworkResource
|
|
}
|
|
|
|
// Canonicalize will supply missing values in the cases
|
|
// where they are not provided.
|
|
func (r *Resources) Canonicalize() {
|
|
defaultResources := DefaultResources()
|
|
if r.CPU == nil {
|
|
r.CPU = defaultResources.CPU
|
|
}
|
|
if r.MemoryMB == nil {
|
|
r.MemoryMB = defaultResources.MemoryMB
|
|
}
|
|
if r.IOPS == nil {
|
|
r.IOPS = defaultResources.IOPS
|
|
}
|
|
for _, n := range r.Networks {
|
|
n.Canonicalize()
|
|
}
|
|
}
|
|
|
|
// DefaultResources is a small resources object that contains the
|
|
// default resources requests that we will provide to an object.
|
|
// --- THIS FUNCTION IS REPLICATED IN nomad/structs/structs.go
|
|
// 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.
|
|
// This should not be confused with the defaults which are
|
|
// provided in DefaultResources() --- THIS LOGIC IS REPLICATED
|
|
// IN nomad/structs/structs.go and should be kept in sync.
|
|
func MinResources() *Resources {
|
|
return &Resources{
|
|
CPU: helper.IntToPtr(20),
|
|
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
|
|
}
|
|
}
|
|
|
|
type Port struct {
|
|
Label string
|
|
Value int `mapstructure:"static"`
|
|
}
|
|
|
|
// NetworkResource is used to describe required network
|
|
// resources of a given task.
|
|
type NetworkResource struct {
|
|
Device string
|
|
CIDR string
|
|
IP string
|
|
MBits *int
|
|
ReservedPorts []Port
|
|
DynamicPorts []Port
|
|
}
|
|
|
|
func (n *NetworkResource) Canonicalize() {
|
|
if n.MBits == nil {
|
|
n.MBits = helper.IntToPtr(10)
|
|
}
|
|
}
|