task.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. )
  36. // Task represents a task.
  37. type Task struct {
  38. ID string
  39. Meta
  40. Annotations
  41. Spec TaskSpec `json:",omitempty"`
  42. ServiceID string `json:",omitempty"`
  43. Slot int `json:",omitempty"`
  44. NodeID string `json:",omitempty"`
  45. Status TaskStatus `json:",omitempty"`
  46. DesiredState TaskState `json:",omitempty"`
  47. NetworksAttachments []NetworkAttachment `json:",omitempty"`
  48. GenericResources []GenericResource `json:",omitempty"`
  49. }
  50. // TaskSpec represents the spec of a task.
  51. type TaskSpec struct {
  52. // ContainerSpec and PluginSpec are mutually exclusive.
  53. // PluginSpec will only be used when the `Runtime` field is set to `plugin`
  54. ContainerSpec *ContainerSpec `json:",omitempty"`
  55. PluginSpec *runtime.PluginSpec `json:",omitempty"`
  56. Resources *ResourceRequirements `json:",omitempty"`
  57. RestartPolicy *RestartPolicy `json:",omitempty"`
  58. Placement *Placement `json:",omitempty"`
  59. Networks []NetworkAttachmentConfig `json:",omitempty"`
  60. // LogDriver specifies the LogDriver to use for tasks created from this
  61. // spec. If not present, the one on cluster default on swarm.Spec will be
  62. // used, finally falling back to the engine default if not specified.
  63. LogDriver *Driver `json:",omitempty"`
  64. // ForceUpdate is a counter that triggers an update even if no relevant
  65. // parameters have been changed.
  66. ForceUpdate uint64
  67. Runtime RuntimeType `json:",omitempty"`
  68. }
  69. // Resources represents resources (CPU/Memory).
  70. type Resources struct {
  71. NanoCPUs int64 `json:",omitempty"`
  72. MemoryBytes int64 `json:",omitempty"`
  73. GenericResources []GenericResource `json:",omitempty"`
  74. }
  75. // GenericResource represents a "user defined" resource which can
  76. // be either an integer (e.g: SSD=3) or a string (e.g: SSD=sda1)
  77. type GenericResource struct {
  78. NamedResourceSpec *NamedGenericResource `json:",omitempty"`
  79. DiscreteResourceSpec *DiscreteGenericResource `json:",omitempty"`
  80. }
  81. // NamedGenericResource represents a "user defined" resource which is defined
  82. // as a string.
  83. // "Kind" is used to describe the Kind of a resource (e.g: "GPU", "FPGA", "SSD", ...)
  84. // Value is used to identify the resource (GPU="UUID-1", FPGA="/dev/sdb5", ...)
  85. type NamedGenericResource struct {
  86. Kind string `json:",omitempty"`
  87. Value string `json:",omitempty"`
  88. }
  89. // DiscreteGenericResource represents a "user defined" resource which is defined
  90. // as an integer
  91. // "Kind" is used to describe the Kind of a resource (e.g: "GPU", "FPGA", "SSD", ...)
  92. // Value is used to count the resource (SSD=5, HDD=3, ...)
  93. type DiscreteGenericResource struct {
  94. Kind string `json:",omitempty"`
  95. Value int64 `json:",omitempty"`
  96. }
  97. // ResourceRequirements represents resources requirements.
  98. type ResourceRequirements struct {
  99. Limits *Resources `json:",omitempty"`
  100. Reservations *Resources `json:",omitempty"`
  101. }
  102. // Placement represents orchestration parameters.
  103. type Placement struct {
  104. Constraints []string `json:",omitempty"`
  105. Preferences []PlacementPreference `json:",omitempty"`
  106. // Platforms stores all the platforms that the image can run on.
  107. // This field is used in the platform filter for scheduling. If empty,
  108. // then the platform filter is off, meaning there are no scheduling restrictions.
  109. Platforms []Platform `json:",omitempty"`
  110. }
  111. // PlacementPreference provides a way to make the scheduler aware of factors
  112. // such as topology.
  113. type PlacementPreference struct {
  114. Spread *SpreadOver
  115. }
  116. // SpreadOver is a scheduling preference that instructs the scheduler to spread
  117. // tasks evenly over groups of nodes identified by labels.
  118. type SpreadOver struct {
  119. // label descriptor, such as engine.labels.az
  120. SpreadDescriptor string
  121. }
  122. // RestartPolicy represents the restart policy.
  123. type RestartPolicy struct {
  124. Condition RestartPolicyCondition `json:",omitempty"`
  125. Delay *time.Duration `json:",omitempty"`
  126. MaxAttempts *uint64 `json:",omitempty"`
  127. Window *time.Duration `json:",omitempty"`
  128. }
  129. // RestartPolicyCondition represents when to restart.
  130. type RestartPolicyCondition string
  131. const (
  132. // RestartPolicyConditionNone NONE
  133. RestartPolicyConditionNone RestartPolicyCondition = "none"
  134. // RestartPolicyConditionOnFailure ON_FAILURE
  135. RestartPolicyConditionOnFailure RestartPolicyCondition = "on-failure"
  136. // RestartPolicyConditionAny ANY
  137. RestartPolicyConditionAny RestartPolicyCondition = "any"
  138. )
  139. // TaskStatus represents the status of a task.
  140. type TaskStatus struct {
  141. Timestamp time.Time `json:",omitempty"`
  142. State TaskState `json:",omitempty"`
  143. Message string `json:",omitempty"`
  144. Err string `json:",omitempty"`
  145. ContainerStatus ContainerStatus `json:",omitempty"`
  146. PortStatus PortStatus `json:",omitempty"`
  147. }
  148. // ContainerStatus represents the status of a container.
  149. type ContainerStatus struct {
  150. ContainerID string `json:",omitempty"`
  151. PID int `json:",omitempty"`
  152. ExitCode int `json:",omitempty"`
  153. }
  154. // PortStatus represents the port status of a task's host ports whose
  155. // service has published host ports
  156. type PortStatus struct {
  157. Ports []PortConfig `json:",omitempty"`
  158. }