attachment.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package container
  2. import (
  3. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  4. "github.com/docker/swarmkit/agent/exec"
  5. "github.com/docker/swarmkit/api"
  6. "golang.org/x/net/context"
  7. )
  8. // networkAttacherController implements agent.Controller against docker's API.
  9. //
  10. // networkAttacherController manages the lifecycle of network
  11. // attachment of a docker unmanaged container managed as a task from
  12. // agent point of view. It provides network attachment information to
  13. // the unmanaged container for it to attach to the network and run.
  14. type networkAttacherController struct {
  15. backend executorpkg.Backend
  16. task *api.Task
  17. adapter *containerAdapter
  18. closed chan struct{}
  19. }
  20. func newNetworkAttacherController(b executorpkg.Backend, task *api.Task, node *api.NodeDescription, dependencies exec.DependencyGetter) (*networkAttacherController, error) {
  21. adapter, err := newContainerAdapter(b, task, node, dependencies)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &networkAttacherController{
  26. backend: b,
  27. task: task,
  28. adapter: adapter,
  29. closed: make(chan struct{}),
  30. }, nil
  31. }
  32. func (nc *networkAttacherController) Update(ctx context.Context, t *api.Task) error {
  33. return nil
  34. }
  35. func (nc *networkAttacherController) Prepare(ctx context.Context) error {
  36. // Make sure all the networks that the task needs are created.
  37. return nc.adapter.createNetworks(ctx)
  38. }
  39. func (nc *networkAttacherController) Start(ctx context.Context) error {
  40. return nc.adapter.networkAttach(ctx)
  41. }
  42. func (nc *networkAttacherController) Wait(pctx context.Context) error {
  43. ctx, cancel := context.WithCancel(pctx)
  44. defer cancel()
  45. return nc.adapter.waitForDetach(ctx)
  46. }
  47. func (nc *networkAttacherController) Shutdown(ctx context.Context) error {
  48. return nil
  49. }
  50. func (nc *networkAttacherController) Terminate(ctx context.Context) error {
  51. return nil
  52. }
  53. func (nc *networkAttacherController) Remove(ctx context.Context) error {
  54. // Try removing the network referenced in this task in case this
  55. // task is the last one referencing it
  56. return nc.adapter.removeNetworks(ctx)
  57. }
  58. func (nc *networkAttacherController) Close() error {
  59. return nil
  60. }