task.go 4.9 KB

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