github.com/taylorchu/nomad@v0.5.3-rc1.0.20170407200202-db11e7dd7b55/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  func (r *Resources) Canonicalize() {
    16  	if r.CPU == nil {
    17  		r.CPU = helper.IntToPtr(100)
    18  	}
    19  	if r.MemoryMB == nil {
    20  		r.MemoryMB = helper.IntToPtr(10)
    21  	}
    22  	if r.IOPS == nil {
    23  		r.IOPS = helper.IntToPtr(0)
    24  	}
    25  	for _, n := range r.Networks {
    26  		n.Canonicalize()
    27  	}
    28  }
    29  
    30  func MinResources() *Resources {
    31  	return &Resources{
    32  		CPU:      helper.IntToPtr(100),
    33  		MemoryMB: helper.IntToPtr(10),
    34  		IOPS:     helper.IntToPtr(0),
    35  	}
    36  
    37  }
    38  
    39  // Merge merges this resource with another resource.
    40  func (r *Resources) Merge(other *Resources) {
    41  	if other == nil {
    42  		return
    43  	}
    44  	if other.CPU != nil {
    45  		r.CPU = other.CPU
    46  	}
    47  	if other.MemoryMB != nil {
    48  		r.MemoryMB = other.MemoryMB
    49  	}
    50  	if other.DiskMB != nil {
    51  		r.DiskMB = other.DiskMB
    52  	}
    53  	if other.IOPS != nil {
    54  		r.IOPS = other.IOPS
    55  	}
    56  	if len(other.Networks) != 0 {
    57  		r.Networks = other.Networks
    58  	}
    59  }
    60  
    61  type Port struct {
    62  	Label string
    63  	Value int `mapstructure:"static"`
    64  }
    65  
    66  // NetworkResource is used to describe required network
    67  // resources of a given task.
    68  type NetworkResource struct {
    69  	Public        bool
    70  	CIDR          string
    71  	ReservedPorts []Port
    72  	DynamicPorts  []Port
    73  	IP            string
    74  	MBits         *int
    75  }
    76  
    77  func (n *NetworkResource) Canonicalize() {
    78  	if n.MBits == nil {
    79  		n.MBits = helper.IntToPtr(10)
    80  	}
    81  }