attachment.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, dependencies exec.DependencyGetter) (*networkAttacherController, error) {
  21. adapter, err := newContainerAdapter(b, task, 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. if err := nc.adapter.createNetworks(ctx); err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. func (nc *networkAttacherController) Start(ctx context.Context) error {
  43. return nc.adapter.networkAttach(ctx)
  44. }
  45. func (nc *networkAttacherController) Wait(pctx context.Context) error {
  46. ctx, cancel := context.WithCancel(pctx)
  47. defer cancel()
  48. return nc.adapter.waitForDetach(ctx)
  49. }
  50. func (nc *networkAttacherController) Shutdown(ctx context.Context) error {
  51. return nil
  52. }
  53. func (nc *networkAttacherController) Terminate(ctx context.Context) error {
  54. return nil
  55. }
  56. func (nc *networkAttacherController) Remove(ctx context.Context) error {
  57. // Try removing the network referenced in this task in case this
  58. // task is the last one referencing it
  59. if err := nc.adapter.removeNetworks(ctx); err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func (nc *networkAttacherController) Close() error {
  65. return nil
  66. }