github.com/emate/nomad@v0.8.2-wo-binpacking/api/resources.go (about)

     1  package api
     2  
     3  import "github.com/hashicorp/nomad/helper"
     4  
     5  // Resources encapsulates the required resources of
     6  // a given task or task group.
     7  type Resources struct {
     8  	CPU      *int
     9  	MemoryMB *int `mapstructure:"memory"`
    10  	DiskMB   *int `mapstructure:"disk"`
    11  	IOPS     *int
    12  	Networks []*NetworkResource
    13  }
    14  
    15  // Canonicalize will supply missing values in the cases
    16  // where they are not provided.
    17  func (r *Resources) Canonicalize() {
    18  	defaultResources := DefaultResources()
    19  	if r.CPU == nil {
    20  		r.CPU = defaultResources.CPU
    21  	}
    22  	if r.MemoryMB == nil {
    23  		r.MemoryMB = defaultResources.MemoryMB
    24  	}
    25  	if r.IOPS == nil {
    26  		r.IOPS = defaultResources.IOPS
    27  	}
    28  	for _, n := range r.Networks {
    29  		n.Canonicalize()
    30  	}
    31  }
    32  
    33  // DefaultResources is a small resources object that contains the
    34  // default resources requests that we will provide to an object.
    35  // ---  THIS FUNCTION IS REPLICATED IN nomad/structs/structs.go
    36  // and should be kept in sync.
    37  func DefaultResources() *Resources {
    38  	return &Resources{
    39  		CPU:      helper.IntToPtr(100),
    40  		MemoryMB: helper.IntToPtr(300),
    41  		IOPS:     helper.IntToPtr(0),
    42  	}
    43  }
    44  
    45  // MinResources is a small resources object that contains the
    46  // absolute minimum resources that we will provide to an object.
    47  // This should not be confused with the defaults which are
    48  // provided in DefaultResources() ---  THIS LOGIC IS REPLICATED
    49  // IN nomad/structs/structs.go and should be kept in sync.
    50  func MinResources() *Resources {
    51  	return &Resources{
    52  		CPU:      helper.IntToPtr(20),
    53  		MemoryMB: helper.IntToPtr(10),
    54  		IOPS:     helper.IntToPtr(0),
    55  	}
    56  }
    57  
    58  // Merge merges this resource with another resource.
    59  func (r *Resources) Merge(other *Resources) {
    60  	if other == nil {
    61  		return
    62  	}
    63  	if other.CPU != nil {
    64  		r.CPU = other.CPU
    65  	}
    66  	if other.MemoryMB != nil {
    67  		r.MemoryMB = other.MemoryMB
    68  	}
    69  	if other.DiskMB != nil {
    70  		r.DiskMB = other.DiskMB
    71  	}
    72  	if other.IOPS != nil {
    73  		r.IOPS = other.IOPS
    74  	}
    75  	if len(other.Networks) != 0 {
    76  		r.Networks = other.Networks
    77  	}
    78  }
    79  
    80  type Port struct {
    81  	Label string
    82  	Value int `mapstructure:"static"`
    83  }
    84  
    85  // NetworkResource is used to describe required network
    86  // resources of a given task.
    87  type NetworkResource struct {
    88  	Device        string
    89  	CIDR          string
    90  	IP            string
    91  	MBits         *int
    92  	ReservedPorts []Port
    93  	DynamicPorts  []Port
    94  }
    95  
    96  func (n *NetworkResource) Canonicalize() {
    97  	if n.MBits == nil {
    98  		n.MBits = helper.IntToPtr(10)
    99  	}
   100  }