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