-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[confcom] Add containers from_image command #9505
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ab1c94e
Restore the behaviour of `--upload-fragment` for acifragmentgen (#13)
DomAyre 35e560c
Add containers from_image command
DomAyre 778f341
Satisfy azdev style
DomAyre 7a351b2
Add tests
DomAyre cd15bf6
Split command lib versions of command
DomAyre 3878dc0
Rename the containers lib code file
DomAyre 5ed2d6d
Build images with buildkit for deterministic hashes
DomAyre e19667b
Revert "Build images with buildkit for deterministic hashes"
DomAyre 07bc86c
Change the working_dir sample for extra_layers
DomAyre 40dd106
.
DomAyre 02de77b
Remove extra layers test
DomAyre d8247b7
Merge branch '16-12-25' into containers-from-image
DomAyre 7647188
Review tidy ups
DomAyre 646e4a5
Restore the behaviour of `--upload-fragment` for acifragmentgen (#13)
DomAyre 12bc7c7
.
DomAyre 0af5fca
Merge remote-tracking branch 'origin/main' into containers-from-image
DomAyre 1c7cee4
Bump confcom version
DomAyre 21b7c51
Address review comments
DomAyre a2d5f24
Merge branch 'main' into containers-from-image
DomAyre 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
12 changes: 12 additions & 0 deletions
12
src/confcom/azext_confcom/command/containers_from_image.py
This file contains hidden or 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,12 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import json | ||
|
|
||
| from azext_confcom.lib.containers import from_image as lib_containers_from_image | ||
|
|
||
|
|
||
| def containers_from_image(image: str, platform: str) -> None: | ||
| print(json.dumps(lib_containers_from_image(image, platform))) |
This file contains hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from dataclasses import asdict | ||
| from azext_confcom.lib.images import get_image_layers, get_image_config | ||
| from azext_confcom.lib.platform import ACI_MOUNTS | ||
|
|
||
|
|
||
| def from_image(image: str, platform: str) -> dict: | ||
|
|
||
| mounts = { | ||
| "aci": [asdict(mount) for mount in ACI_MOUNTS], | ||
| }.get(platform, None) | ||
|
|
||
| return { | ||
| "id": image, | ||
| "name": image, | ||
| "layers": get_image_layers(image), | ||
| **({"mounts": mounts} if mounts else {}), | ||
| **get_image_config(image), | ||
| } |
This file contains hidden or 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,65 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import functools | ||
| import subprocess | ||
| import docker | ||
|
|
||
| from pathlib import Path | ||
|
|
||
|
|
||
| @functools.lru_cache() | ||
| def get_image(image_ref: str) -> docker.models.images.Image: | ||
|
|
||
| client = docker.from_env() | ||
|
|
||
| try: | ||
| image = client.images.get(image_ref) | ||
| except docker.errors.ImageNotFound: | ||
| client.images.pull(image_ref) | ||
|
|
||
| image = client.images.get(image_ref) | ||
| return image | ||
|
|
||
|
|
||
| def get_image_layers(image: str) -> list[str]: | ||
|
|
||
| binary_path = Path(__file__).parent.parent / "bin" / "dmverity-vhd" | ||
|
|
||
| get_image(image) | ||
| result = subprocess.run( | ||
| [binary_path.as_posix(), "-d", "roothash", "-i", image], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.DEVNULL, | ||
| check=True, | ||
| text=True, | ||
| ) | ||
|
|
||
| return [line.split("hash: ")[-1] for line in result.stdout.splitlines()] | ||
|
|
||
|
|
||
| def get_image_config(image: str) -> dict: | ||
|
|
||
| image_config = get_image(image).attrs.get("Config") | ||
|
|
||
| config = {} | ||
|
|
||
| if image_config.get("Cmd") or image_config.get("Entrypoint"): | ||
| config["command"] = ( | ||
| image_config.get("Entrypoint") or [] + | ||
| image_config.get("Cmd") or [] | ||
DomAyre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| if image_config.get("Env"): | ||
| config["env_rules"] = [{ | ||
| "pattern": p, | ||
| "strategy": "string", | ||
| "required": False, | ||
| } for p in image_config.get("Env")] | ||
|
|
||
| if image_config.get("WorkingDir"): | ||
| config["working_dir"] = image_config.get("WorkingDir") | ||
|
|
||
| return config | ||
This file contains hidden or 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,19 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from azext_confcom.lib.policy import ContainerMount | ||
|
|
||
| ACI_MOUNTS = [ | ||
| ContainerMount( | ||
| destination="/etc/resolv.conf", | ||
| options=[ | ||
| "rbind", | ||
| "rshared", | ||
| "rw" | ||
| ], | ||
| source="sandbox:///tmp/atlas/resolvconf/.+", | ||
| type="bind" | ||
| ) | ||
| ] |
72 changes: 72 additions & 0 deletions
72
src/confcom/azext_confcom/tests/latest/test_confcom_containers_from_image.py
This file contains hidden or 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,72 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import json | ||
| import tempfile | ||
| import docker | ||
| import pytest | ||
| import portalocker | ||
|
|
||
| from contextlib import redirect_stdout | ||
| from io import StringIO | ||
| from itertools import product | ||
| from pathlib import Path | ||
| from deepdiff import DeepDiff | ||
|
|
||
| from azext_confcom.command.containers_from_image import containers_from_image | ||
|
|
||
|
|
||
| TEST_DIR = Path(__file__).parent | ||
| CONFCOM_DIR = TEST_DIR.parent.parent.parent | ||
| SAMPLES_ROOT = CONFCOM_DIR / "samples" / "images" | ||
| DOCKER_LOCK = Path(tempfile.gettempdir()) / "confcom-docker.lock" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def build_test_containers(): | ||
|
|
||
| docker_client = docker.from_env() | ||
| with portalocker.Lock(DOCKER_LOCK.as_posix(), timeout=20): | ||
| for image_sample in SAMPLES_ROOT.iterdir(): | ||
| docker_client.images.build( | ||
| path=str(image_sample), | ||
| tag=f"confcom_test_{image_sample.name}", | ||
| quiet=True, | ||
| rm=True, | ||
| ) | ||
|
|
||
| yield | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "sample_directory, platform", | ||
| product( | ||
| [p.name for p in SAMPLES_ROOT.iterdir()], | ||
| ["aci"], | ||
| ) | ||
| ) | ||
| def test_containers_from_image(sample_directory: str, platform: str): | ||
|
|
||
| sample_directory = Path(SAMPLES_ROOT) / sample_directory | ||
|
|
||
| expected_container_def_path = sample_directory / f"{platform}_container.inc.rego" | ||
| with expected_container_def_path.open("r", encoding="utf-8") as handle: | ||
| expected_container_def = json.load(handle) | ||
|
|
||
| buffer = StringIO() | ||
| with redirect_stdout(buffer): | ||
| containers_from_image( | ||
| image=f"confcom_test_{sample_directory.name}", | ||
| platform=platform, | ||
| ) | ||
|
|
||
| actual_container_def = json.loads(buffer.getvalue()) | ||
|
|
||
| diff = DeepDiff( | ||
| actual_container_def, | ||
| expected_container_def, | ||
| ignore_order=True, | ||
| ) | ||
| assert diff == {}, diff |
This file contains hidden or 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,3 @@ | ||
| FROM hello-world | ||
|
|
||
| CMD ["/command"] |
This file contains hidden or 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,30 @@ | ||
| { | ||
| "id": "confcom_test_command", | ||
| "name": "confcom_test_command", | ||
| "layers": [ | ||
| "8b4664979ffe3c5188efbbbb30e31716c03bfe880f15f455be0fc3beb4741de9" | ||
| ], | ||
| "mounts": [ | ||
| { | ||
| "destination": "/etc/resolv.conf", | ||
| "options": [ | ||
| "rbind", | ||
| "rshared", | ||
| "rw" | ||
| ], | ||
| "source": "sandbox:///tmp/atlas/resolvconf/.+", | ||
| "type": "bind" | ||
| } | ||
| ], | ||
| "command": [ | ||
| "/command" | ||
| ], | ||
| "env_rules": [ | ||
| { | ||
| "pattern": "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", | ||
| "strategy": "string", | ||
| "required": false | ||
| } | ||
| ], | ||
| "working_dir": "/" | ||
| } |
This file contains hidden or 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,3 @@ | ||
| FROM hello-world | ||
|
|
||
| ENV TEST_ENV_VAR="Test Env Value" |
35 changes: 35 additions & 0 deletions
35
src/confcom/samples/images/environment_variables/aci_container.inc.rego
This file contains hidden or 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,35 @@ | ||
| { | ||
| "id": "confcom_test_environment_variables", | ||
| "name": "confcom_test_environment_variables", | ||
| "layers": [ | ||
| "8b4664979ffe3c5188efbbbb30e31716c03bfe880f15f455be0fc3beb4741de9" | ||
| ], | ||
| "mounts": [ | ||
| { | ||
| "destination": "/etc/resolv.conf", | ||
| "options": [ | ||
| "rbind", | ||
| "rshared", | ||
| "rw" | ||
| ], | ||
| "source": "sandbox:///tmp/atlas/resolvconf/.+", | ||
| "type": "bind" | ||
| } | ||
| ], | ||
| "command": [ | ||
| "/hello" | ||
| ], | ||
| "env_rules": [ | ||
| { | ||
| "pattern": "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", | ||
| "strategy": "string", | ||
| "required": false | ||
| }, | ||
| { | ||
| "pattern": "TEST_ENV_VAR=Test Env Value", | ||
| "strategy": "string", | ||
| "required": false | ||
| } | ||
| ], | ||
| "working_dir": "/" | ||
| } |
This file contains hidden or 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 @@ | ||
| FROM hello-world |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.