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

Multi-Node-Builder support #668

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions python_on_whales/components/buildx/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
ReloadableObject,
)
from python_on_whales.components.buildx.imagetools.cli_wrapper import ImagetoolsCLI
from python_on_whales.components.buildx.models import BuilderInspectResult
from python_on_whales.components.buildx.models import (
BuilderInspectResult,
BuilderNode,
)
from python_on_whales.utils import (
ValidPath,
format_mapping_for_cli,
Expand Down Expand Up @@ -71,13 +74,23 @@ def name(self) -> str:
def driver(self) -> str:
return self._get_inspect_result().driver

@property
def nodes(self) -> List[BuilderNode]:
return self._get_inspect_result().nodes

@property
def status(self) -> str:
return self._get_inspect_result().status
result_str = "Status:\n"
for node in self.nodes:
result_str += " " + node.name + ": " + node.status + "\n"
return result_str.strip("\n")

@property
def platforms(self) -> List[str]:
return self._get_inspect_result().platforms
result_list = []
for node in self.nodes:
result_list.extend(node.platforms)
return list(set(result_list))

def __repr__(self):
return f"python_on_whales.Builder(name='{self.name}', driver='{self.driver}')"
Expand Down Expand Up @@ -466,6 +479,7 @@ def create(
driver_options: Dict[str, str] = {},
name: Optional[str] = None,
use: bool = False,
append: bool = False,
) -> Builder:
"""Create a new builder instance

Expand All @@ -481,13 +495,15 @@ def create(
e.g `driver_options=dict(network="host")`
name: Builder instance name
use: Set the current builder instance to this builder
append: Append a node to builder instead of changing it

# Returns
A `python_on_whales.Builder` object.
"""
full_cmd = self.docker_cmd + ["buildx", "create"]

full_cmd.add_flag("--bootstrap", bootstrap)
full_cmd.add_flag("--append", append)
full_cmd.add_simple_arg("--buildkitd-flags", buildkitd_flags)
full_cmd.add_simple_arg("--config", config)
if platforms is not None:
Expand Down
48 changes: 35 additions & 13 deletions python_on_whales/components/buildx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,45 @@
class BuilderInspectResult:
name: str
driver: str
status: str
platforms: List[str] = field(default_factory=lambda: [])
nodes: List[BuilderNode] = field(default_factory=lambda: [])

@classmethod
def from_str(cls, string: str) -> BuilderInspectResult:
string = string.strip()
result_dict = {}
result_dict = {
"name": "",
"driver": "",
"nodes": [],
}
nodes_reached = False
node = BuilderNode()
for line in string.splitlines():
if line.startswith("Name:") and "name" not in result_dict:
result_dict["name"] = line.split(":")[1].strip()
if line.startswith("Driver:"):
result_dict["driver"] = line.split(":")[1].strip()
if line.startswith("Status:"):
result_dict["status"] = line.split(":")[1].strip()
if line.startswith("Platforms:"):
platforms = line.split(":")[1].strip()
if platforms:
result_dict["platforms"] = platforms.split(", ")
if line.startswith("Nodes:"):
nodes_reached = True
if not nodes_reached:
if line.startswith("Name:"):
result_dict["name"] = line.split(":")[1].strip()
if line.startswith("Driver:"):
result_dict["driver"] = line.split(":")[1].strip()

if nodes_reached:
if line.startswith("Name:"):
node.name = line.split(":")[1].strip()
if line.startswith("Status:"):
node.status = line.split(":")[1].strip()
if line.startswith("Platforms:"):
platforms = line.split(":")[1].strip()
if platforms:
node.platforms = platforms.split(", ")
result_dict["nodes"].append(node)
node = BuilderNode()

Choose a reason for hiding this comment

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

Rather than doing all this parsing, I believe we should leverage docker buildx ls --format "{{json . }}", I will look into it.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, but you mentioned that docker buildx ls --format "{{json . }}" loses the information of what builder is being used, so I parsed it from docker buildx inspect

Choose a reason for hiding this comment

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

Maybe we can grab the builder name from docker buildx inspect, and then run docker buildx ls --format "{{json . }}" , we loop over the results and take the entry corresponding to our builder. Then parsing the json is easy peasy and can be loaded into a pydantic model

Copy link
Author

Choose a reason for hiding this comment

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

Do you want to do this in the _fetch_and_parse_inspect_result function? Or do you want to create another function to do this?

return cls(**result_dict)


class BuilderNode:
name: str = ""
status: str = ""
platforms: List[str] = []

def __str__(self):
return self.name
Empty file added test.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ def test_buildx_inspect_bootstrap():
my_builder = docker.buildx.create()
with my_builder:
docker.buildx.inspect(my_builder.name, bootstrap=True)
assert my_builder.status == "running"
for node in my_builder.nodes:
assert node.status == "running"
# Must contain at least the host native platform
assert my_builder.platforms

Expand Down Expand Up @@ -487,15 +488,16 @@ def test_buildx_create_remove():
def test_buildx_create_bootstrap():
my_builder = docker.buildx.create(bootstrap=True)
with my_builder:
assert my_builder.status == "running"
for node in my_builder.nodes:
assert node.status == "running"
# Must contain at least the host native platform
assert my_builder.platforms


def test_buildx_create_remove_with_platforms():
builder = docker.buildx.create(platforms=["linux/amd64", "linux/arm64"])

assert builder.platforms == ["linux/amd64*", "linux/arm64*"]
for platform in builder.platforms:
assert platform in ["linux/amd64*", "linux/arm64*"]

docker.buildx.remove(builder)

Expand Down Expand Up @@ -527,16 +529,18 @@ def test_builder_inspect_result_from_string():
a = BuilderInspectResult.from_str(some_builder_info)
assert a.name == "blissful_swartz"
assert a.driver == "docker-container"
assert a.status == "inactive"
assert a.platforms == []
for node in a.nodes:
assert node.status == "inactive"
assert node.platforms == []


def test_builder_inspect_result_platforms_from_string():
a = BuilderInspectResult.from_str(some_builder_info_with_platforms)
assert a.name == "blissful_swartz"
assert a.driver == "docker-container"
assert a.status == "running"
assert a.platforms == ["linux/amd64", "linux/arm64"]
for node in a.nodes:
assert node.status == "running"
assert node.platforms == ["linux/amd64", "linux/arm64"]


bake_test_dir = PROJECT_ROOT / "tests/python_on_whales/components/bake_tests"
Expand Down