@@ -20,33 +20,28 @@ import (
2020 "strings"
2121 "time"
2222
23- "github.com/docker/docker/client"
24-
23+ "github.com/docker/docker/api/types"
2524 "github.com/docker/docker/api/types/container"
26-
2725 "github.com/docker/docker/api/types/filters"
28-
26+ "github.com/docker/docker/api/types/network"
27+ "github.com/docker/docker/client"
2928 "github.com/sirupsen/logrus"
3029 "golang.org/x/net/context"
31-
32- "github.com/docker/docker/api/types/network"
33-
34- "github.com/docker/docker/api/types"
3530)
3631
37- // IdempotentLocalEndpointsStarter groups Docker functions to create the ECS local network and
32+ // LocalEndpointsStarter groups Docker functions to create the ECS local network and
3833// the Local Container Endpoints container if they don't exist.
39- type IdempotentLocalEndpointsStarter interface {
40- idempotentNetworkCreator
41- idempotentContainerStarter
34+ type LocalEndpointsStarter interface {
35+ networkCreator
36+ containerStarter
4237}
4338
44- type idempotentNetworkCreator interface {
39+ type networkCreator interface {
4540 NetworkInspect (ctx context.Context , networkID string , options types.NetworkInspectOptions ) (types.NetworkResource , error )
4641 NetworkCreate (ctx context.Context , name string , options types.NetworkCreate ) (types.NetworkCreateResponse , error )
4742}
4843
49- type idempotentContainerStarter interface {
44+ type containerStarter interface {
5045 ContainerList (ctx context.Context , options types.ContainerListOptions ) ([]types.Container , error )
5146 ContainerCreate (ctx context.Context , config * container.Config ,
5247 hostConfig * container.HostConfig , networkingConfig * network.NetworkingConfig ,
@@ -85,26 +80,24 @@ const (
8580// already exists or the container is already running then the operation is a no-op.
8681//
8782// If there is any unexpected errors, we exit the program with a fatal log.
88- func Setup (docker IdempotentLocalEndpointsStarter ) {
89- setupLocalNetwork (docker )
90- setupLocalEndpointsContainer (docker )
83+ func Setup (dockerClient LocalEndpointsStarter ) {
84+ setupLocalNetwork (dockerClient )
85+ setupLocalEndpointsContainer (dockerClient )
9186}
9287
93- // setupLocalNetwork creates the local network if it doesn't already exist.
94- func setupLocalNetwork (docker idempotentNetworkCreator ) {
95- if localNetworkExists (docker ) {
88+ func setupLocalNetwork (dockerClient networkCreator ) {
89+ if localNetworkExists (dockerClient ) {
9690 logrus .Infof ("The network %s already exists" , EcsLocalNetworkName )
9791 return
9892 }
99- createLocalNetwork (docker )
93+ createLocalNetwork (dockerClient )
10094}
10195
102- // localNetworkExists returns true if the local network already exists, false otherwise.
103- func localNetworkExists (docker idempotentNetworkCreator ) bool {
96+ func localNetworkExists (dockerClient networkCreator ) bool {
10497 ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
10598 defer cancel ()
10699
107- _ , err := docker .NetworkInspect (ctx , EcsLocalNetworkName , types.NetworkInspectOptions {})
100+ _ , err := dockerClient .NetworkInspect (ctx , EcsLocalNetworkName , types.NetworkInspectOptions {})
108101 if err != nil {
109102 if client .IsErrNotFound (err ) {
110103 return false
@@ -115,12 +108,11 @@ func localNetworkExists(docker idempotentNetworkCreator) bool {
115108 return true
116109}
117110
118- // createLocalNetwork creates the local network.
119- func createLocalNetwork (docker idempotentNetworkCreator ) {
111+ func createLocalNetwork (dockerClient networkCreator ) {
120112 ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
121113 defer cancel ()
122114
123- resp , err := docker .NetworkCreate (ctx , EcsLocalNetworkName , types.NetworkCreate {
115+ resp , err := dockerClient .NetworkCreate (ctx , EcsLocalNetworkName , types.NetworkCreate {
124116 IPAM : & network.IPAM {
125117 Config : []network.IPAMConfig {
126118 {
@@ -135,21 +127,20 @@ func createLocalNetwork(docker idempotentNetworkCreator) {
135127 logrus .Infof ("Created network %s with ID %s" , EcsLocalNetworkName , resp .ID )
136128}
137129
138- // setupLocalEndpointsContainer creates the Local Endpoints container if one doesn't exist already.
139- func setupLocalEndpointsContainer (docker idempotentContainerStarter ) {
130+ func setupLocalEndpointsContainer (docker containerStarter ) {
140131 containerID := createLocalEndpointsContainer (docker )
141132 startContainer (docker , containerID )
142133}
143134
144135// createLocalEndpointsContainer returns the ID of the newly created container.
145136// If the container already exists, returns the ID of the existing container.
146- func createLocalEndpointsContainer (docker idempotentContainerStarter ) string {
137+ func createLocalEndpointsContainer (dockerClient containerStarter ) string {
147138 ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
148139 defer cancel ()
149140
150141 // See First Scenario in https://aws.amazon.com/blogs/compute/a-guide-to-locally-testing-containers-with-amazon-ecs-local-endpoints-and-docker-compose/
151142 // for an explanation of these fields.
152- resp , err := docker .ContainerCreate (ctx ,
143+ resp , err := dockerClient .ContainerCreate (ctx ,
153144 & container.Config {
154145 Image : localEndpointsImageName ,
155146 Env : []string {
@@ -178,7 +169,7 @@ func createLocalEndpointsContainer(docker idempotentContainerStarter) string {
178169 if err != nil {
179170 if strings .Contains (err .Error (), "Conflict" ) {
180171 // We already created this container before since there is a name conflict, fetch its ID and return it.
181- containerID := localEndpointsContainerID (docker )
172+ containerID := localEndpointsContainerID (dockerClient )
182173 logrus .Infof ("The %s container already exists with ID %s" , localEndpointsContainerName , containerID )
183174 return containerID
184175 }
@@ -189,12 +180,11 @@ func createLocalEndpointsContainer(docker idempotentContainerStarter) string {
189180 return resp .ID
190181}
191182
192- // localEndpointsContainerID returns the ID of the Local Endpoints container.
193- func localEndpointsContainerID (docker idempotentContainerStarter ) string {
183+ func localEndpointsContainerID (dockerClient containerStarter ) string {
194184 ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
195185 defer cancel ()
196186
197- resp , err := docker .ContainerList (ctx , types.ContainerListOptions {
187+ resp , err := dockerClient .ContainerList (ctx , types.ContainerListOptions {
198188 All : true ,
199189 Filters : filters .NewArgs (
200190 filters .Arg ("name" , localEndpointsContainerName ),
@@ -203,16 +193,18 @@ func localEndpointsContainerID(docker idempotentContainerStarter) string {
203193 if err != nil {
204194 logrus .Fatalf ("Failed to list containers with name %s due to %v" , localEndpointsContainerName , err )
205195 }
196+ if len (resp ) != 1 {
197+ logrus .Fatalf ("Expected to find one container named %s but found %d" , localEndpointsContainerName , len (resp ))
198+ }
206199 return resp [0 ].ID
207200}
208201
209- // startContainer starts the container with the containerID.
210- // If the container is already running, Docker does not return an error response.
211- func startContainer (docker idempotentContainerStarter , containerID string ) {
202+ func startContainer (dockerClient containerStarter , containerID string ) {
212203 ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
213204 defer cancel ()
214205
215- if err := docker .ContainerStart (ctx , containerID , types.ContainerStartOptions {}); err != nil {
206+ // If the container is already running, Docker does not return an error response.
207+ if err := dockerClient .ContainerStart (ctx , containerID , types.ContainerStartOptions {}); err != nil {
216208 logrus .Fatalf ("Failed to start container with ID %s due to %v" , containerID , err )
217209 }
218210 logrus .Infof ("Started container with ID %s" , containerID )
0 commit comments