Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teardowns the local network on "local stop" #775

Merged
merged 5 commits into from
May 24, 2019

Conversation

efekarakus
Copy link
Contributor

@efekarakus efekarakus commented May 20, 2019

Description of changes: Remove the local endpoints container and the local network if the user doesn't need them anymore. Freeing up resources on their machine.


Enter [N/A] in the box, if an item is not applicable to your change.

Testing

Documentation

  • [N/A] Contacted our doc writer
  • [N/A] Updated our README

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@efekarakus efekarakus changed the title Teardowns the local network on "local down" Teardowns the local network on "local stop" May 20, 2019
@efekarakus
Copy link
Contributor Author

Manual Tests

1. local stop when only the endpoints container is running

# Given
$ docker ps               
CONTAINER ID        IMAGE                                         COMMAND                  CREATED             STATUS              PORTS               NAMES
697ab62e760c        amazon/amazon-ecs-local-container-endpoints   "/local-container-en…"   55 seconds ago      Up 54 seconds       80/tcp              amazon-ecs-local-container-endpoints
$ docker network ls     
NETWORK ID          NAME                DRIVER              SCOPE
94ac9ec2ce2b        bridge              bridge              local
949837dcb884        ecs-local-network   bridge              local
3803595a49d9        host                host                local
59a17e60cd02        none                null                local

# When
$ dev-ecs-cli local stop                
INFO[0000] The network ecs-local-network has no more running tasks, stopping the endpoints containers... 
INFO[0000] Stopped the amazon-ecs-local-container-endpoints container successfully, removing it... 
INFO[0000] Removed the amazon-ecs-local-container-endpoints container successfully, removing the ecs-local-network network... 
INFO[0000] Removed the ecs-local-network network successfully 

# Then
$ docker ps                  
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ docker network ls     
NETWORK ID          NAME                DRIVER              SCOPE
94ac9ec2ce2b        bridge              bridge              local
3803595a49d9        host                host                local
59a17e60cd02        none                null                local

2. local stop when a task is running

# Given
$ docker ps                 
CONTAINER ID        IMAGE                                         COMMAND                  CREATED              STATUS              PORTS               NAMES
01bd513e4d4f        ecs-local-other_app                           "/bin/sh -c '/bin/ba…"   10 seconds ago       Up 9 seconds                            ecs-local-other_app_1
03e24e741a3f        amazon/amazon-ecs-local-container-endpoints   "/local-container-en…"   About a minute ago   Up About a minute   80/tcp              amazon-ecs-local-container-endpoints

# When
$ dev-ecs-cli local stop  # does nothing

# Then
$ docker ps                      
CONTAINER ID        IMAGE                                         COMMAND                  CREATED              STATUS              PORTS               NAMES
01bd513e4d4f        ecs-local-other_app                           "/bin/sh -c '/bin/ba…"   About a minute ago   Up About a minute                       ecs-local-other_app_1
03e24e741a3f        amazon/amazon-ecs-local-container-endpoints   "/local-container-en…"   About a minute ago   Up About a minute   80/tcp              amazon-ecs-local-container-endpoints

3. local stop when a task is running without local endpoints container

# Given
$ docker ps                  
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
01bd513e4d4f        ecs-local-other_app   "/bin/sh -c '/bin/ba…"   2 minutes ago       Up 2 minutes                            ecs-local-other_app_1

# When
$ dev-ecs-cli local stop     
WARN[0000] The ecs-local-other_app_1 container is running in the ecs-local-network network without the amazon-ecs-local-container-endpoints container, please stop it first 

# Then
$ docker ps                      
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
01bd513e4d4f        ecs-local-other_app   "/bin/sh -c '/bin/ba…"   3 minutes ago       Up 3 minutes                            ecs-local-other_app_1

4. local stop with no running containers in the network

# Given

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
94ac9ec2ce2b        bridge              bridge              local
4cb93d5d5a88        ecs-local-network   bridge              local
3803595a49d9        host                host                local
59a17e60cd02        none                null                local
$ docker container ps --all       
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# When
$ dev-ecs-cli local stop       
INFO[0000] The network ecs-local-network has no more running tasks, stopping the endpoints containers... 
INFO[0000] Removed the ecs-local-network network successfully 

# Then
$ docker network ls       
NETWORK ID          NAME                DRIVER              SCOPE
94ac9ec2ce2b        bridge              bridge              local
3803595a49d9        host                host                local
59a17e60cd02        none                null                local


if len(resp.Containers) > 1 {
// Has other containers running in the network
return true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding a log statement here so that users have more visibility in the case where teardown is being skipped because they had launched other containers in the network

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Added "%d other task(s) running locally, skipping network removal."

//
// If the user stops the last running task in the local network then also remove the network.
func Stop(c *cli.Context) {
docker, err := client.NewEnvClient() // Temporary client created to test network.Teardown()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider doing something like I did here: https://github.com/awslabs/amazon-ecs-local-container-endpoints/blob/master/local-container-endpoints/clients/docker/client.go#L49

I once or twice ran into weird problems where the client API version was too new for my local docker. If you lock it to the min version that is needed, then customers are safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sorry it wasn't clear, the comment meant to denote that this Docker client initialization is temporary. I'll implement the suggestion in a later PR once we actually start working on the Stop and Up commands.

I updated the comment with a TODO and with the link you shared to make this more obvious :)


err := d.ContainerStop(ctx, localEndpointsContainerName, nil)
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "no such container") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question, looks like the comment below wasn't precise enough :P

This error happens if the user stopped all their task containers with docker stop instead of ecs-cli local stop. In that situation, hasRunningTasksInNetwork() will return false and we will try to stop the endpoints container again. But since it was already stopped this error is returned.

We don't want to log.Fatal while trying to stop an already stopped container, it should be a no-op hence this if-statement.

I updated the comment to // The containers in the network were already stopped by the user using "docker stop", do nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is to not invoke stopEndpointsContainer() or removeEndpointsContainer() if there are 0 containers in the local network.

However, I think that approach pushes complexity upwards instead of downwards (into the function) so I don't prefer it as much. Also if you run ecs-cli local stop on the same task concurrently (two terminals for example) then one of those could trigger this error and fail with fatal instead of continuing cleanly by not doing any work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it... my only concern is that this is yet another place where we are using string matching on errors returned by Docker :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree but I think Docker locked themselves and can't change their error messages otherwise they'll break everyone's code :P

Here is a similar example from the agent: https://github.com/aws/amazon-ecs-agent/blob/24c71162368b3a9090689532e5c035fe2bccf133/agent/dockerclient/dockerapi/docker_client.go#L693

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my least favorite thing in Go...

@@ -77,7 +77,7 @@ const (
)

// Setup creates a user-defined bridge network with a running Local Container Endpoints container. If the network
// already exists or the container is already running then the operation is a no-op.
// already exists or the container is already running then this function does nothing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor nit: this change and the other one in teardown.go probably shouldn't be in the "generate mocks" commit.

If you plan to squash all changes to one commit prior to merge, then don't worry about it

@PettitWesley
Copy link
Contributor

                                  # #  ( )
                               ___#_#___|__
                           _  |____________|  _
                    _=====| | |            | | |==== _
              =====| |.---------------------------. | |====
<--------------------'   .  .  .  .  .  .  .  .   '--------------/
  \                                                             /
   \___________________________________________________________/
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

@efekarakus efekarakus merged commit e5bba16 into aws:ecs-local May 24, 2019
@efekarakus efekarakus deleted the local/teardown branch May 24, 2019 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants