task.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package swarm
  2. import (
  3. "time"
  4. "github.com/docker/docker/api/types/swarm/runtime"
  5. )
  6. // TaskState represents the state of a task.
  7. type TaskState string
  8. const (
  9. // TaskStateNew NEW
  10. TaskStateNew TaskState = "new"
  11. // TaskStateAllocated ALLOCATED
  12. TaskStateAllocated TaskState = "allocated"
  13. // TaskStatePending PENDING
  14. TaskStatePending TaskState = "pending"
  15. // TaskStateAssigned ASSIGNED
  16. TaskStateAssigned TaskState = "assigned"
  17. // TaskStateAccepted ACCEPTED
  18. TaskStateAccepted TaskState = "accepted"
  19. // TaskStatePreparing PREPARING
  20. TaskStatePreparing TaskState = "preparing"
  21. // TaskStateReady READY
  22. TaskStateReady TaskState = "ready"
  23. // TaskStateStarting STARTING
  24. TaskStateStarting TaskState = "starting"
  25. // TaskStateRunning RUNNING
  26. TaskStateRunning TaskState = "running"
  27. // TaskStateComplete COMPLETE
  28. TaskStateComplete TaskState = "complete"
  29. // TaskStateShutdown SHUTDOWN
  30. TaskStateShutdown TaskState = "shutdown"
  31. // TaskStateFailed FAILED
  32. TaskStateFailed TaskState = "failed"
  33. // TaskStateRejected REJECTED
  34. TaskStateRejected TaskState = "rejected"
  35. // TaskStateRemove REMOVE
  36. TaskStateRemove TaskState = "remove"
  37. // TaskStateOrphaned ORPHANED
  38. TaskStateOrphaned TaskState = "orphaned"
  39. )
  40. // Task represents a task.
  41. type Task struct {
  42. ID string
  43. Meta
  44. Annotations
  45. Spec TaskSpec `json:",omitempty"`
  46. ServiceID string `json:",omitempty"`
  47. Slot int `json:",omitempty"`
  48. NodeID string `json:",omitempty"`
  49. Status TaskStatus `json:",omitempty"`
  50. DesiredState TaskState `json:",omitempty"`
  51. NetworksAttachments []NetworkAttachment `json:",omitempty"`
  52. GenericResources []GenericResource `json:",omitempty"`
  53. }
  54. // TaskSpec represents the spec of a task.
  55. type TaskSpec struct {
  56. // ContainerSpec and PluginSpec are mutually exclusive.
  57. // PluginSpec will only be used when the `Runtime` field is set to `plugin`
  58. ContainerSpec *ContainerSpec `json:",omitempty"`
  59. PluginSpec *runtime.PluginSpec `json:",omitempty"`
  60. Resources *ResourceRequirements `json:",omitempty"`
  61. RestartPolicy *RestartPolicy `json:",omitempty"`
  62. Placement *Placement `json:",omitempty"`
  63. Networks []NetworkAttachmentConfig `json:",omitempty"`
  64. // LogDriver specifies the LogDriver to use for tasks created from this
  65. // spec. If not present, the one on cluster default on swarm.Spec will be
  66. // used, finally falling back to the engine default if not specified.
  67. LogDriver *Driver `json:",omitempty"`
  68. // ForceUpdate is a counter that triggers an update even if no relevant
  69. // parameters have been changed.
  70. ForceUpdate uint64
  71. Runtime RuntimeType `json:",omitempty"`
  72. }
  73. // Resources represents resources (CPU/Memory).
  74. type Resources struct {
  75. NanoCPUs int64 `json:",omitempty"`
  76. MemoryBytes int64 `json:",omitempty"`
  77. GenericResources []GenericResource `json:",omitempty"`
  78. }
  79. // GenericResource represents a "user defined" resource which can
  80. // be either an integer (e.g: SSD=3) or a string (e.g: SSD=sda1)
  81. type GenericResource struct {
  82. NamedResourceSpec *NamedGenericResource `json:",omitempty"`
  83. DiscreteResourceSpec *DiscreteGenericResource `json:",omitempty"`
  84. }
  85. // NamedGenericResource represents a "user defined" resource which is defined
  86. // as a string.
  87. // "Kind" is used to describe the Kind of a resource (e.g: "GPU", "FPGA", "SSD", ...)
  88. // Value is used to identify the resource (GPU="UUID-1", FPGA="/dev/sdb5", ...)
  89. type NamedGenericResource struct {
  90. Kind string `json:",omitempty"`
  91. Value string `json:",omitempty"`
  92. }
  93. // DiscreteGenericResource represents a "user defined" resource which is defined
  94. // as an integer
  95. // "Kind" is used to describe the Kind of a resource (e.g: "GPU", "FPGA", "SSD", ...)
  96. // Value is used to count the resource (SSD=5, HDD=3, ...)
  97. type DiscreteGenericResource struct {
  98. Kind string `json:",omitempty"`
  99. Value int64 `json:",omitempty"`
  100. }
  101. // ResourceRequirements represents resources requirements.
  102. type ResourceRequirements struct {
  103. Limits *Resources `json:",omitempty"`
  104. Reservations *Resources `json:",omitempty"`
  105. }
  106. // Placement represents orchestration parameters.
  107. type Placement struct {
  108. Constraints []string `json:",omitempty"`
  109. Preferences []PlacementPreference `json:",omitempty"`
  110. // Platforms stores all the platforms that the image can run on.
  111. // This field is used in the platform filter for scheduling. If empty,
  112. // then the platform filter is off, meaning there are no scheduling restrictions.
  113. Platforms []Platform `json:",omitempty"`
  114. }
  115. // PlacementPreference provides a way to make the scheduler aware of factors
  116. // such as topology.
  117. type PlacementPreference struct {
  118. Spread *SpreadOver
  119. }
  120. // SpreadOver is a scheduling preference that instructs the scheduler to spread
  121. // tasks evenly over groups of nodes identified by labels.
  122. type SpreadOver struct {
  123. // label descriptor, such as engine.labels.az
  124. SpreadDescriptor string
  125. }
  126. // RestartPolicy represents the restart policy.
  127. type RestartPolicy struct {
  128. Condition RestartPolicyCondition `json:",omitempty"`
  129. Delay *time.Duration `json:",omitempty"`
  130. MaxAttempts *uint64 `json:",omitempty"`
  131. Window *time.Duration `json:",omitempty"`
  132. }
  133. // RestartPolicyCondition represents when to restart.
  134. type RestartPolicyCondition string
  135. const (
  136. // RestartPolicyConditionNone NONE
  137. RestartPolicyConditionNone RestartPolicyCondition = "none"
  138. // RestartPolicyConditionOnFailure ON_FAILURE
  139. RestartPolicyConditionOnFailure RestartPolicyCondition = "on-failure"
  140. // RestartPolicyConditionAny ANY
  141. RestartPolicyConditionAny RestartPolicyCondition = "any"
  142. )
  143. // TaskStatus represents the status of a task.
  144. type TaskStatus struct {
  145. Timestamp time.Time `json:",omitempty"`
  146. State TaskState `json:",omitempty"`
  147. Message string `json:",omitempty"`
  148. Err string `json:",omitempty"`
  149. ContainerStatus ContainerStatus `json:",omitempty"`
  150. PortStatus PortStatus `json:",omitempty"`
  151. }
  152. // ContainerStatus represents the status of a container.
  153. type ContainerStatus struct {
  154. ContainerID string `json:",omitempty"`
  155. PID int `json:",omitempty"`
  156. ExitCode int `json:",omitempty"`
  157. }
  158. // PortStatus represents the port status of a task's host ports whose
  159. // service has published host ports
  160. type PortStatus struct {
  161. Ports []PortConfig `json:",omitempty"`
  162. }