task.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. }
  61. // Resources represents resources (CPU/Memory).
  62. type Resources struct {
  63. NanoCPUs int64 `json:",omitempty"`
  64. MemoryBytes int64 `json:",omitempty"`
  65. }
  66. // ResourceRequirements represents resources requirements.
  67. type ResourceRequirements struct {
  68. Limits *Resources `json:",omitempty"`
  69. Reservations *Resources `json:",omitempty"`
  70. }
  71. // Placement represents orchestration parameters.
  72. type Placement struct {
  73. Constraints []string `json:",omitempty"`
  74. Preferences []PlacementPreference `json:",omitempty"`
  75. }
  76. // PlacementPreference provides a way to make the scheduler aware of factors
  77. // such as topology.
  78. type PlacementPreference struct {
  79. Spread *SpreadOver
  80. }
  81. // SpreadOver is a scheduling preference that instructs the scheduler to spread
  82. // tasks evenly over groups of nodes identified by labels.
  83. type SpreadOver struct {
  84. // label descriptor, such as engine.labels.az
  85. SpreadDescriptor string
  86. }
  87. // RestartPolicy represents the restart policy.
  88. type RestartPolicy struct {
  89. Condition RestartPolicyCondition `json:",omitempty"`
  90. Delay *time.Duration `json:",omitempty"`
  91. MaxAttempts *uint64 `json:",omitempty"`
  92. Window *time.Duration `json:",omitempty"`
  93. }
  94. // RestartPolicyCondition represents when to restart.
  95. type RestartPolicyCondition string
  96. const (
  97. // RestartPolicyConditionNone NONE
  98. RestartPolicyConditionNone RestartPolicyCondition = "none"
  99. // RestartPolicyConditionOnFailure ON_FAILURE
  100. RestartPolicyConditionOnFailure RestartPolicyCondition = "on-failure"
  101. // RestartPolicyConditionAny ANY
  102. RestartPolicyConditionAny RestartPolicyCondition = "any"
  103. )
  104. // TaskStatus represents the status of a task.
  105. type TaskStatus struct {
  106. Timestamp time.Time `json:",omitempty"`
  107. State TaskState `json:",omitempty"`
  108. Message string `json:",omitempty"`
  109. Err string `json:",omitempty"`
  110. ContainerStatus ContainerStatus `json:",omitempty"`
  111. PortStatus PortStatus `json:",omitempty"`
  112. }
  113. // ContainerStatus represents the status of a container.
  114. type ContainerStatus struct {
  115. ContainerID string `json:",omitempty"`
  116. PID int `json:",omitempty"`
  117. ExitCode int `json:",omitempty"`
  118. }
  119. // PortStatus represents the port status of a task's host ports whose
  120. // service has published host ports
  121. type PortStatus struct {
  122. Ports []PortConfig `json:",omitempty"`
  123. }