aws / amazon-ecs-init

Amazon Elastic Container Service RPM
http://aws.amazon.com/ecs
Apache License 2.0
200 stars 118 forks source link

Prevent restart on exit code 5 #386

Closed shubham2892 closed 3 years ago

shubham2892 commented 3 years ago

Summary

Init is currently configured to restart on any failure. Today what happens is --

Agent should not restart when it exits with exit code 5 as it is a terminal error code. This PR fixes this behavior.

The behavior will be changed to --

Testing

Agent exiting with exit code 5 can be replicated by adding an arbitrary capability to ecs.config -

ECS_INSTANCE_ATTRIBUTES={"custom attribute !@#$%^&*": "custom_attribute_value"}

Montoring ecs-init after adding this capability, I can see Agent restarting again and again --

2021-01-21T19:50:36Z [INFO] Removing existing agent container ID: 2724ace14f79ebbb5f32c4b5614f85b36885377ee975b3378f287796c2b639d8
2021-01-21T19:50:36Z [INFO] Starting Amazon Elastic Container Service Agent
2021-01-21T19:50:38Z [INFO] Agent exited with code 5
2021-01-21T19:50:38Z [ERROR] agent exited with terminal exit code
2021-01-21T19:50:38Z [INFO] stop
2021-01-21T19:50:38Z [INFO] Stopping Amazon Elastic Container Service Agent
2021-01-21T19:50:38Z [INFO] Container name: /ecs-agent
2021-01-21T19:50:38Z [INFO] Agent is already stopped
2021-01-21T19:50:38Z [INFO] post-stop
2021-01-21T19:50:38Z [INFO] Cleaning up the credentials endpoint setup for Amazon Elastic Container Service Agent
2021-01-21T19:50:48Z [INFO] pre-start
2021-01-21T19:50:48Z [INFO] start
2021-01-21T19:50:48Z [INFO] Container name: /ecs-agent
2021-01-21T19:50:48Z [INFO] Removing existing agent container ID: 8582cbf6ebe0af6ff066139a22dea311a9c1c6995b6a5921a5bb593482ed2fe0
2021-01-21T19:50:48Z [INFO] Starting Amazon Elastic Container Service Agent

2021-01-21T19:50:49Z [INFO] Agent exited with code 5
2021-01-21T19:50:49Z [ERROR] agent exited with terminal exit code
2021-01-21T19:50:49Z [INFO] stop
2021-01-21T19:50:49Z [INFO] Stopping Amazon Elastic Container Service Agent
2021-01-21T19:50:49Z [INFO] Container name: /ecs-agent
2021-01-21T19:50:49Z [INFO] Agent is already stopped
2021-01-21T19:50:50Z [INFO] post-stop
2021-01-21T19:50:50Z [INFO] Cleaning up the credentials endpoint setup for Amazon Elastic Container Service Agent

2021-01-21T19:51:00Z [INFO] pre-start
2021-01-21T19:51:00Z [INFO] start
2021-01-21T19:51:00Z [INFO] Container name: /ecs-agent
2021-01-21T19:51:00Z [INFO] Removing existing agent container ID: 31a800c8fa6f810d5ecb7da9f801501ee6e0535b02bfe8eca39d6d0149fc8077
2021-01-21T19:51:00Z [INFO] Starting Amazon Elastic Container Service Agent
2021-01-21T19:51:01Z [INFO] Agent exited with code 5
2021-01-21T19:51:01Z [ERROR] agent exited with terminal exit code
2021-01-21T19:51:01Z [INFO] stop

With this change Agent is not restarted once it exits --

[ec2-user@ip-172-31-11-239 amazon-ecs-init]$ tail -f /var/log/ecs/ecs-init.log
2021-01-21T19:52:51Z [INFO] Removing existing agent container ID: e5310f89b1803dd3e1a3fb19623e872f11b5a2421bd5f1a0af712b79e0cd0ed3
2021-01-21T19:52:51Z [INFO] starting Amazon Elastic Container Service Agent
2021-01-21T19:52:52Z [INFO] Agent exited with code 5
2021-01-21T19:52:52Z [ERROR] terminal error with exit code: 5: agent exited with terminal exit code
2021-01-21T19:52:52Z [INFO] stop
2021-01-21T19:52:52Z [INFO] Stopping Amazon Elastic Container Service Agent
2021-01-21T19:52:52Z [INFO] Container name: /ecs-agent
2021-01-21T19:52:52Z [INFO] Agent is already stopped
2021-01-21T19:52:52Z [INFO] post-stop
2021-01-21T19:52:52Z [INFO] Cleaning up the credentials endpoint setup for Amazon Elastic Container Service Agent

New tests cover the changes: yes

Description for the changelog

Licensing

This contribution is under the terms of the Apache 2.0 License: