controller.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package container
  2. import (
  3. "fmt"
  4. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  5. "github.com/docker/engine-api/types"
  6. "github.com/docker/swarmkit/agent/exec"
  7. "github.com/docker/swarmkit/api"
  8. "github.com/docker/swarmkit/log"
  9. "github.com/pkg/errors"
  10. "golang.org/x/net/context"
  11. )
  12. // controller implements agent.Controller against docker's API.
  13. //
  14. // Most operations against docker's API are done through the container name,
  15. // which is unique to the task.
  16. type controller struct {
  17. backend executorpkg.Backend
  18. task *api.Task
  19. adapter *containerAdapter
  20. closed chan struct{}
  21. err error
  22. }
  23. var _ exec.Controller = &controller{}
  24. // NewController returns a dockerexec runner for the provided task.
  25. func newController(b executorpkg.Backend, task *api.Task) (*controller, error) {
  26. adapter, err := newContainerAdapter(b, task)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return &controller{
  31. backend: b,
  32. task: task,
  33. adapter: adapter,
  34. closed: make(chan struct{}),
  35. }, nil
  36. }
  37. func (r *controller) Task() (*api.Task, error) {
  38. return r.task, nil
  39. }
  40. // ContainerStatus returns the container-specific status for the task.
  41. func (r *controller) ContainerStatus(ctx context.Context) (*api.ContainerStatus, error) {
  42. ctnr, err := r.adapter.inspect(ctx)
  43. if err != nil {
  44. if isUnknownContainer(err) {
  45. return nil, nil
  46. }
  47. return nil, err
  48. }
  49. return parseContainerStatus(ctnr)
  50. }
  51. // Update tasks a recent task update and applies it to the container.
  52. func (r *controller) Update(ctx context.Context, t *api.Task) error {
  53. log.G(ctx).Warnf("task updates not yet supported")
  54. // TODO(stevvooe): While assignment of tasks is idempotent, we do allow
  55. // updates of metadata, such as labelling, as well as any other properties
  56. // that make sense.
  57. return nil
  58. }
  59. // Prepare creates a container and ensures the image is pulled.
  60. //
  61. // If the container has already be created, exec.ErrTaskPrepared is returned.
  62. func (r *controller) Prepare(ctx context.Context) error {
  63. if err := r.checkClosed(); err != nil {
  64. return err
  65. }
  66. // Make sure all the networks that the task needs are created.
  67. if err := r.adapter.createNetworks(ctx); err != nil {
  68. return err
  69. }
  70. // Make sure all the volumes that the task needs are created.
  71. if err := r.adapter.createVolumes(ctx, r.backend); err != nil {
  72. return err
  73. }
  74. if err := r.adapter.pullImage(ctx); err != nil {
  75. // NOTE(stevvooe): We always try to pull the image to make sure we have
  76. // the most up to date version. This will return an error, but we only
  77. // log it. If the image truly doesn't exist, the create below will
  78. // error out.
  79. //
  80. // This gives us some nice behavior where we use up to date versions of
  81. // mutable tags, but will still run if the old image is available but a
  82. // registry is down.
  83. //
  84. // If you don't want this behavior, lock down your image to an
  85. // immutable tag or digest.
  86. log.G(ctx).WithError(err).Error("pulling image failed")
  87. }
  88. if err := r.adapter.create(ctx, r.backend); err != nil {
  89. if isContainerCreateNameConflict(err) {
  90. if _, err := r.adapter.inspect(ctx); err != nil {
  91. return err
  92. }
  93. // container is already created. success!
  94. return exec.ErrTaskPrepared
  95. }
  96. return err
  97. }
  98. return nil
  99. }
  100. // Start the container. An error will be returned if the container is already started.
  101. func (r *controller) Start(ctx context.Context) error {
  102. if err := r.checkClosed(); err != nil {
  103. return err
  104. }
  105. ctnr, err := r.adapter.inspect(ctx)
  106. if err != nil {
  107. return err
  108. }
  109. // Detect whether the container has *ever* been started. If so, we don't
  110. // issue the start.
  111. //
  112. // TODO(stevvooe): This is very racy. While reading inspect, another could
  113. // start the process and we could end up starting it twice.
  114. if ctnr.State.Status != "created" {
  115. return exec.ErrTaskStarted
  116. }
  117. if err := r.adapter.start(ctx); err != nil {
  118. return errors.Wrap(err, "starting container failed")
  119. }
  120. return nil
  121. }
  122. // Wait on the container to exit.
  123. func (r *controller) Wait(pctx context.Context) error {
  124. if err := r.checkClosed(); err != nil {
  125. return err
  126. }
  127. ctx, cancel := context.WithCancel(pctx)
  128. defer cancel()
  129. err := r.adapter.wait(ctx)
  130. if ctx.Err() != nil {
  131. return ctx.Err()
  132. }
  133. if err != nil {
  134. ee := &exitError{}
  135. if err.Error() != "" {
  136. ee.cause = err
  137. }
  138. if ec, ok := err.(exec.ExitCoder); ok {
  139. ee.code = ec.ExitCode()
  140. }
  141. return ee
  142. }
  143. return nil
  144. }
  145. // Shutdown the container cleanly.
  146. func (r *controller) Shutdown(ctx context.Context) error {
  147. if err := r.checkClosed(); err != nil {
  148. return err
  149. }
  150. if err := r.adapter.shutdown(ctx); err != nil {
  151. if isUnknownContainer(err) || isStoppedContainer(err) {
  152. return nil
  153. }
  154. return err
  155. }
  156. return nil
  157. }
  158. // Terminate the container, with force.
  159. func (r *controller) Terminate(ctx context.Context) error {
  160. if err := r.checkClosed(); err != nil {
  161. return err
  162. }
  163. if err := r.adapter.terminate(ctx); err != nil {
  164. if isUnknownContainer(err) {
  165. return nil
  166. }
  167. return err
  168. }
  169. return nil
  170. }
  171. // Remove the container and its resources.
  172. func (r *controller) Remove(ctx context.Context) error {
  173. if err := r.checkClosed(); err != nil {
  174. return err
  175. }
  176. // It may be necessary to shut down the task before removing it.
  177. if err := r.Shutdown(ctx); err != nil {
  178. if isUnknownContainer(err) {
  179. return nil
  180. }
  181. // This may fail if the task was already shut down.
  182. log.G(ctx).WithError(err).Debug("shutdown failed on removal")
  183. }
  184. // Try removing networks referenced in this task in case this
  185. // task is the last one referencing it
  186. if err := r.adapter.removeNetworks(ctx); err != nil {
  187. if isUnknownContainer(err) {
  188. return nil
  189. }
  190. return err
  191. }
  192. if err := r.adapter.remove(ctx); err != nil {
  193. if isUnknownContainer(err) {
  194. return nil
  195. }
  196. return err
  197. }
  198. return nil
  199. }
  200. // Close the runner and clean up any ephemeral resources.
  201. func (r *controller) Close() error {
  202. select {
  203. case <-r.closed:
  204. return r.err
  205. default:
  206. r.err = exec.ErrControllerClosed
  207. close(r.closed)
  208. }
  209. return nil
  210. }
  211. func (r *controller) checkClosed() error {
  212. select {
  213. case <-r.closed:
  214. return r.err
  215. default:
  216. return nil
  217. }
  218. }
  219. func parseContainerStatus(ctnr types.ContainerJSON) (*api.ContainerStatus, error) {
  220. status := &api.ContainerStatus{
  221. ContainerID: ctnr.ID,
  222. PID: int32(ctnr.State.Pid),
  223. ExitCode: int32(ctnr.State.ExitCode),
  224. }
  225. return status, nil
  226. }
  227. type exitError struct {
  228. code int
  229. cause error
  230. }
  231. func (e *exitError) Error() string {
  232. if e.cause != nil {
  233. return fmt.Sprintf("task: non-zero exit (%v): %v", e.code, e.cause)
  234. }
  235. return fmt.Sprintf("task: non-zero exit (%v)", e.code)
  236. }
  237. func (e *exitError) ExitCode() int {
  238. return int(e.code)
  239. }
  240. func (e *exitError) Cause() error {
  241. return e.cause
  242. }