-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix: Explicitly exit when container is out-of-memory #6476
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
757edda
Revert "chore: Upgrade Mac installer to python3.11 (#6424)"
mildaniel 899b8b3
Update GH action python version to 3.8
mildaniel 7adf8ec
fix: Explicitly exit when container is out-of-memory
mildaniel 0bb9f44
fix: Explicitly exit when container is out-of-memory
mildaniel 4893467
Merge branch 'out-of-memory' of github.com:mildaniel/aws-sam-cli into…
mildaniel f160cc6
Add log, add unit tests
mildaniel d7b7843
Merge branch 'develop' into out-of-memory
mildaniel c531b5a
Format files
mildaniel c243b3d
Merge branch 'out-of-memory' of github.com:mildaniel/aws-sam-cli into…
mildaniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Class for handling the analysis and inspection of Docker containers | ||
""" | ||
import logging | ||
from dataclasses import dataclass | ||
|
||
from samcli.local.docker.container import Container | ||
from samcli.local.docker.manager import ContainerManager | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
DEFAULT_OUT_OF_MEMORY = False | ||
|
||
|
||
@dataclass | ||
class ContainerState: | ||
out_of_memory: bool | ||
|
||
|
||
class ContainerAnalyzer: | ||
def __init__(self, container_manager: ContainerManager, container: Container): | ||
self.container_manager = container_manager | ||
self.container = container | ||
|
||
def inspect(self) -> ContainerState: | ||
""" | ||
Inspect the state of a container by calling the "inspect()" API that Docker provides. | ||
Extract relevant information into a ContainerState object. | ||
|
||
Returns | ||
------- | ||
ContainerState: | ||
Returns a ContainerState object with relevant container data | ||
""" | ||
if not self.container.id: | ||
LOG.debug("Container ID not defined, unable to fetch container state") | ||
return ContainerState(DEFAULT_OUT_OF_MEMORY) | ||
|
||
state = self.container_manager.inspect(self.container.id) | ||
|
||
if isinstance(state, bool): | ||
LOG.debug("Unable to fetch container state") | ||
return ContainerState(DEFAULT_OUT_OF_MEMORY) | ||
|
||
container_state = ContainerState(state.get("State", {}).get("OOMKilled", DEFAULT_OUT_OF_MEMORY)) | ||
LOG.debug("[Container state] OOMKilled %s", container_state.out_of_memory) | ||
|
||
return container_state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from unittest import TestCase | ||
from unittest.mock import Mock, patch | ||
|
||
from samcli.lib.utils.packagetype import IMAGE | ||
from samcli.local.docker.container import Container | ||
from samcli.local.docker.container_analyzer import ContainerAnalyzer, ContainerState | ||
from samcli.local.docker.manager import ContainerManager | ||
|
||
|
||
class TestContainerAnalyzer(TestCase): | ||
def setUp(self) -> None: | ||
self.image = IMAGE | ||
self.cmd = "cmd" | ||
self.working_dir = "working_dir" | ||
self.host_dir = "host_dir" | ||
self.memory_mb = 123 | ||
self.exposed_ports = {123: 123} | ||
self.entrypoint = ["a", "b", "c"] | ||
self.env_vars = {"key": "value"} | ||
|
||
self.mock_docker_client = Mock() | ||
|
||
self.container = Container( | ||
self.image, | ||
self.cmd, | ||
self.working_dir, | ||
self.host_dir, | ||
self.memory_mb, | ||
self.exposed_ports, | ||
self.entrypoint, | ||
self.env_vars, | ||
self.mock_docker_client, | ||
) | ||
|
||
@patch("samcli.local.docker.container_analyzer.LOG") | ||
def test_inspect_returns_container_state(self, mock_log): | ||
self.container.id = "id" | ||
manager = ContainerManager() | ||
manager.inspect = Mock() | ||
manager.inspect.return_value = {"State": {"OOMKilled": True}} | ||
|
||
analyzer = ContainerAnalyzer(container_manager=manager, container=self.container) | ||
state = analyzer.inspect() | ||
|
||
manager.inspect.assert_called_once_with("id") | ||
mock_log.debug.assert_called_once_with("[Container state] OOMKilled %s", True) | ||
self.assertEqual(state, ContainerState(out_of_memory=True)) | ||
|
||
def test_inspect_no_container_id(self): | ||
manager = ContainerManager() | ||
manager.inspect = Mock() | ||
|
||
analyzer = ContainerAnalyzer(container_manager=manager, container=self.container) | ||
state = analyzer.inspect() | ||
|
||
manager.inspect.assert_not_called() | ||
self.assertEqual(state, ContainerState(out_of_memory=False)) | ||
|
||
def test_inspect_docker_call_fails(self): | ||
self.container.id = "id" | ||
manager = ContainerManager() | ||
manager.inspect = Mock() | ||
manager.inspect.return_value = False | ||
|
||
analyzer = ContainerAnalyzer(container_manager=manager, container=self.container) | ||
state = analyzer.inspect() | ||
|
||
manager.inspect.assert_called_once_with("id") | ||
self.assertEqual(state, ContainerState(out_of_memory=False)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is admittedly a little weird, but mypy won't like it if we don't do this, and the reason we return a bool here is to follow the legacy pattern in
container_manager
.