github.com/djenriquez/nomad-1@v0.8.1/client/structs/structs.go (about)

     1  package structs
     2  
     3  //go:generate codecgen -d 102 -o structs.generated.go structs.go
     4  
     5  import (
     6  	"crypto/md5"
     7  	"io"
     8  	"strconv"
     9  	"time"
    10  
    11  	"github.com/hashicorp/nomad/client/config"
    12  	"github.com/hashicorp/nomad/client/stats"
    13  	"github.com/hashicorp/nomad/nomad/structs"
    14  )
    15  
    16  // RpcError is used for serializing errors with a potential error code
    17  type RpcError struct {
    18  	Message string
    19  	Code    *int64
    20  }
    21  
    22  func NewRpcError(err error, code *int64) *RpcError {
    23  	return &RpcError{
    24  		Message: err.Error(),
    25  		Code:    code,
    26  	}
    27  }
    28  
    29  func (r *RpcError) Error() string {
    30  	return r.Message
    31  }
    32  
    33  // ClientStatsResponse is used to return statistics about a node.
    34  type ClientStatsResponse struct {
    35  	HostStats *stats.HostStats
    36  	structs.QueryMeta
    37  }
    38  
    39  // AllocFileInfo holds information about a file inside the AllocDir
    40  type AllocFileInfo struct {
    41  	Name     string
    42  	IsDir    bool
    43  	Size     int64
    44  	FileMode string
    45  	ModTime  time.Time
    46  }
    47  
    48  // FsListRequest is used to list an allocation's directory.
    49  type FsListRequest struct {
    50  	// AllocID is the allocation to list from
    51  	AllocID string
    52  
    53  	// Path is the path to list
    54  	Path string
    55  
    56  	structs.QueryOptions
    57  }
    58  
    59  // FsListResponse is used to return the listings of an allocation's directory.
    60  type FsListResponse struct {
    61  	// Files are the result of listing a directory.
    62  	Files []*AllocFileInfo
    63  
    64  	structs.QueryMeta
    65  }
    66  
    67  // FsStatRequest is used to stat a file
    68  type FsStatRequest struct {
    69  	// AllocID is the allocation to stat the file in
    70  	AllocID string
    71  
    72  	// Path is the path to list
    73  	Path string
    74  
    75  	structs.QueryOptions
    76  }
    77  
    78  // FsStatResponse is used to return the stat results of a file
    79  type FsStatResponse struct {
    80  	// Info is the result of stating a file
    81  	Info *AllocFileInfo
    82  
    83  	structs.QueryMeta
    84  }
    85  
    86  // FsStreamRequest is the initial request for streaming the content of a file.
    87  type FsStreamRequest struct {
    88  	// AllocID is the allocation to stream logs from
    89  	AllocID string
    90  
    91  	// Path is the path to the file to stream
    92  	Path string
    93  
    94  	// Offset is the offset to start streaming data at.
    95  	Offset int64
    96  
    97  	// Origin can either be "start" or "end" and determines where the offset is
    98  	// applied.
    99  	Origin string
   100  
   101  	// PlainText disables base64 encoding.
   102  	PlainText bool
   103  
   104  	// Limit is the number of bytes to read
   105  	Limit int64
   106  
   107  	// Follow follows the file.
   108  	Follow bool
   109  
   110  	structs.QueryOptions
   111  }
   112  
   113  // FsLogsRequest is the initial request for accessing allocation logs.
   114  type FsLogsRequest struct {
   115  	// AllocID is the allocation to stream logs from
   116  	AllocID string
   117  
   118  	// Task is the task to stream logs from
   119  	Task string
   120  
   121  	// LogType indicates whether "stderr" or "stdout" should be streamed
   122  	LogType string
   123  
   124  	// Offset is the offset to start streaming data at.
   125  	Offset int64
   126  
   127  	// Origin can either be "start" or "end" and determines where the offset is
   128  	// applied.
   129  	Origin string
   130  
   131  	// PlainText disables base64 encoding.
   132  	PlainText bool
   133  
   134  	// Follow follows logs.
   135  	Follow bool
   136  
   137  	structs.QueryOptions
   138  }
   139  
   140  // StreamErrWrapper is used to serialize output of a stream of a file or logs.
   141  type StreamErrWrapper struct {
   142  	// Error stores any error that may have occurred.
   143  	Error *RpcError
   144  
   145  	// Payload is the payload
   146  	Payload []byte
   147  }
   148  
   149  // AllocStatsRequest is used to request the resource usage of a given
   150  // allocation, potentially filtering by task
   151  type AllocStatsRequest struct {
   152  	// AllocID is the allocation to retrieves stats for
   153  	AllocID string
   154  
   155  	// Task is an optional filter to only request stats for the task.
   156  	Task string
   157  
   158  	structs.QueryOptions
   159  }
   160  
   161  // AllocStatsResponse is used to return the resource usage of a given
   162  // allocation.
   163  type AllocStatsResponse struct {
   164  	Stats *AllocResourceUsage
   165  	structs.QueryMeta
   166  }
   167  
   168  // MemoryStats holds memory usage related stats
   169  type MemoryStats struct {
   170  	RSS            uint64
   171  	Cache          uint64
   172  	Swap           uint64
   173  	MaxUsage       uint64
   174  	KernelUsage    uint64
   175  	KernelMaxUsage uint64
   176  
   177  	// A list of fields whose values were actually sampled
   178  	Measured []string
   179  }
   180  
   181  func (ms *MemoryStats) Add(other *MemoryStats) {
   182  	ms.RSS += other.RSS
   183  	ms.Cache += other.Cache
   184  	ms.Swap += other.Swap
   185  	ms.MaxUsage += other.MaxUsage
   186  	ms.KernelUsage += other.KernelUsage
   187  	ms.KernelMaxUsage += other.KernelMaxUsage
   188  	ms.Measured = joinStringSet(ms.Measured, other.Measured)
   189  }
   190  
   191  // CpuStats holds cpu usage related stats
   192  type CpuStats struct {
   193  	SystemMode       float64
   194  	UserMode         float64
   195  	TotalTicks       float64
   196  	ThrottledPeriods uint64
   197  	ThrottledTime    uint64
   198  	Percent          float64
   199  
   200  	// A list of fields whose values were actually sampled
   201  	Measured []string
   202  }
   203  
   204  func (cs *CpuStats) Add(other *CpuStats) {
   205  	cs.SystemMode += other.SystemMode
   206  	cs.UserMode += other.UserMode
   207  	cs.TotalTicks += other.TotalTicks
   208  	cs.ThrottledPeriods += other.ThrottledPeriods
   209  	cs.ThrottledTime += other.ThrottledTime
   210  	cs.Percent += other.Percent
   211  	cs.Measured = joinStringSet(cs.Measured, other.Measured)
   212  }
   213  
   214  // ResourceUsage holds information related to cpu and memory stats
   215  type ResourceUsage struct {
   216  	MemoryStats *MemoryStats
   217  	CpuStats    *CpuStats
   218  }
   219  
   220  func (ru *ResourceUsage) Add(other *ResourceUsage) {
   221  	ru.MemoryStats.Add(other.MemoryStats)
   222  	ru.CpuStats.Add(other.CpuStats)
   223  }
   224  
   225  // TaskResourceUsage holds aggregated resource usage of all processes in a Task
   226  // and the resource usage of the individual pids
   227  type TaskResourceUsage struct {
   228  	ResourceUsage *ResourceUsage
   229  	Timestamp     int64
   230  	Pids          map[string]*ResourceUsage
   231  }
   232  
   233  // AllocResourceUsage holds the aggregated task resource usage of the
   234  // allocation.
   235  type AllocResourceUsage struct {
   236  	// ResourceUsage is the summation of the task resources
   237  	ResourceUsage *ResourceUsage
   238  
   239  	// Tasks contains the resource usage of each task
   240  	Tasks map[string]*TaskResourceUsage
   241  
   242  	// The max timestamp of all the Tasks
   243  	Timestamp int64
   244  }
   245  
   246  // joinStringSet takes two slices of strings and joins them
   247  func joinStringSet(s1, s2 []string) []string {
   248  	lookup := make(map[string]struct{}, len(s1))
   249  	j := make([]string, 0, len(s1))
   250  	for _, s := range s1 {
   251  		j = append(j, s)
   252  		lookup[s] = struct{}{}
   253  	}
   254  
   255  	for _, s := range s2 {
   256  		if _, ok := lookup[s]; !ok {
   257  			j = append(j, s)
   258  		}
   259  	}
   260  
   261  	return j
   262  }
   263  
   264  // FSIsolation is an enumeration to describe what kind of filesystem isolation
   265  // a driver supports.
   266  type FSIsolation int
   267  
   268  const (
   269  	// FSIsolationNone means no isolation. The host filesystem is used.
   270  	FSIsolationNone FSIsolation = 0
   271  
   272  	// FSIsolationChroot means the driver will use a chroot on the host
   273  	// filesystem.
   274  	FSIsolationChroot FSIsolation = 1
   275  
   276  	// FSIsolationImage means the driver uses an image.
   277  	FSIsolationImage FSIsolation = 2
   278  )
   279  
   280  func (f FSIsolation) String() string {
   281  	switch f {
   282  	case 0:
   283  		return "none"
   284  	case 1:
   285  		return "chroot"
   286  	case 2:
   287  		return "image"
   288  	default:
   289  		return "INVALID"
   290  	}
   291  }
   292  
   293  // DriverNetwork is the network created by driver's (eg Docker's bridge
   294  // network) during Prestart.
   295  type DriverNetwork struct {
   296  	// PortMap can be set by drivers to replace ports in environment
   297  	// variables with driver-specific mappings.
   298  	PortMap map[string]int
   299  
   300  	// IP is the IP address for the task created by the driver.
   301  	IP string
   302  
   303  	// AutoAdvertise indicates whether the driver thinks services that
   304  	// choose to auto-advertise-addresses should use this IP instead of the
   305  	// host's. eg If a Docker network plugin is used
   306  	AutoAdvertise bool
   307  }
   308  
   309  // Advertise returns true if the driver suggests using the IP set. May be
   310  // called on a nil Network in which case it returns false.
   311  func (d *DriverNetwork) Advertise() bool {
   312  	return d != nil && d.AutoAdvertise
   313  }
   314  
   315  // Copy a DriverNetwork struct. If it is nil, nil is returned.
   316  func (d *DriverNetwork) Copy() *DriverNetwork {
   317  	if d == nil {
   318  		return nil
   319  	}
   320  	pm := make(map[string]int, len(d.PortMap))
   321  	for k, v := range d.PortMap {
   322  		pm[k] = v
   323  	}
   324  	return &DriverNetwork{
   325  		PortMap:       pm,
   326  		IP:            d.IP,
   327  		AutoAdvertise: d.AutoAdvertise,
   328  	}
   329  }
   330  
   331  // Hash the contents of a DriverNetwork struct to detect changes. If it is nil,
   332  // an empty slice is returned.
   333  func (d *DriverNetwork) Hash() []byte {
   334  	if d == nil {
   335  		return []byte{}
   336  	}
   337  	h := md5.New()
   338  	io.WriteString(h, d.IP)
   339  	io.WriteString(h, strconv.FormatBool(d.AutoAdvertise))
   340  	for k, v := range d.PortMap {
   341  		io.WriteString(h, k)
   342  		io.WriteString(h, strconv.Itoa(v))
   343  	}
   344  	return h.Sum(nil)
   345  }
   346  
   347  // FingerprintRequest is a request which a fingerprinter accepts to fingerprint
   348  // the node
   349  type FingerprintRequest struct {
   350  	Config *config.Config
   351  	Node   *structs.Node
   352  }
   353  
   354  // FingerprintResponse is the response which a fingerprinter annotates with the
   355  // results of the fingerprint method
   356  type FingerprintResponse struct {
   357  	Attributes map[string]string
   358  	Links      map[string]string
   359  	Resources  *structs.Resources
   360  
   361  	// Detected is a boolean indicating whether the fingerprinter detected
   362  	// if the resource was available
   363  	Detected bool
   364  }
   365  
   366  // AddAttribute adds the name and value for a node attribute to the fingerprint
   367  // response
   368  func (f *FingerprintResponse) AddAttribute(name, value string) {
   369  	// initialize Attributes if it has not been already
   370  	if f.Attributes == nil {
   371  		f.Attributes = make(map[string]string, 0)
   372  	}
   373  
   374  	f.Attributes[name] = value
   375  }
   376  
   377  // RemoveAttribute sets the given attribute to empty, which will later remove
   378  // it entirely from the node
   379  func (f *FingerprintResponse) RemoveAttribute(name string) {
   380  	// initialize Attributes if it has not been already
   381  	if f.Attributes == nil {
   382  		f.Attributes = make(map[string]string, 0)
   383  	}
   384  
   385  	f.Attributes[name] = ""
   386  }
   387  
   388  // AddLink adds a link entry to the fingerprint response
   389  func (f *FingerprintResponse) AddLink(name, value string) {
   390  	// initialize Links if it has not been already
   391  	if f.Links == nil {
   392  		f.Links = make(map[string]string, 0)
   393  	}
   394  
   395  	f.Links[name] = value
   396  }
   397  
   398  // RemoveLink removes a link entry from the fingerprint response. This will
   399  // later remove it entirely from the node
   400  func (f *FingerprintResponse) RemoveLink(name string) {
   401  	// initialize Links if it has not been already
   402  	if f.Links == nil {
   403  		f.Links = make(map[string]string, 0)
   404  	}
   405  
   406  	f.Links[name] = ""
   407  }
   408  
   409  // HealthCheckRequest is the request type for a type that fulfils the Health
   410  // Check interface
   411  type HealthCheckRequest struct{}
   412  
   413  // HealthCheckResponse is the response type for a type that fulfills the Health
   414  // Check interface
   415  type HealthCheckResponse struct {
   416  	// Drivers is a map of driver names to current driver information
   417  	Drivers map[string]*structs.DriverInfo
   418  }
   419  
   420  type HealthCheckIntervalRequest struct{}
   421  type HealthCheckIntervalResponse struct {
   422  	Eligible bool
   423  	Period   time.Duration
   424  }
   425  
   426  // AddDriverInfo adds information about a driver to the fingerprint response.
   427  // If the Drivers field has not yet been initialized, it does so here.
   428  func (h *HealthCheckResponse) AddDriverInfo(name string, driverInfo *structs.DriverInfo) {
   429  	// initialize Drivers if it has not been already
   430  	if h.Drivers == nil {
   431  		h.Drivers = make(map[string]*structs.DriverInfo, 0)
   432  	}
   433  
   434  	h.Drivers[name] = driverInfo
   435  }