Skip to content

Commit

Permalink
Parse and serialize Step resources as WorkloadResources
Browse files Browse the repository at this point in the history
  • Loading branch information
hylje committed Nov 29, 2023
1 parent 42856b9 commit 7c76fbf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
6 changes: 4 additions & 2 deletions tests/test_serialize_step.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def test_serialize_workload_resources(step_with_resources):
"""Must not flatten workload resource data."""
config = step_with_resources
resources = config.steps["contains kubernetes resources"].resources
resources = config.steps["contains kubernetes resources"].serialize()["resources"]

assert isinstance(resources, dict), "Resources should be defined."
assert "cpu" in resources, "Resources should contain data."
Expand All @@ -10,7 +10,9 @@ def test_serialize_workload_resources(step_with_resources):
def test_serialize_partial_resources(step_with_partial_resources):
"""Serialized data only contains keys found in the config."""
config = step_with_partial_resources
resources = config.steps["contains partial workload resources"].resources
resources = config.steps["contains partial workload resources"].serialize()[
"resources"
]

assert "min" in resources["cpu"]
assert "max" not in resources["cpu"]
2 changes: 1 addition & 1 deletion tests/test_workload_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_missing_sub_resources(resource_name, missing_key):
resources = create_resources(resource_name, missing_key)

for this_resource_name, sub_resources in resources.get_data().items():
for name, value in sub_resources.get_data().items():
for name, value in sub_resources.items():
if this_resource_name == resource_name and name == missing_key:
assert value is None
else:
Expand Down
7 changes: 6 additions & 1 deletion valohai_yaml/objs/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def parse(cls, data: SerializedDict) -> "Step":
kwargs["source_path"] = kwargs.pop("source-path", None)
kwargs["stop_condition"] = kwargs.pop("stop-condition", None)
kwargs["upload_store"] = kwargs.pop("upload-store", None)
if "resources" in kwargs:
kwargs["resources"] = WorkloadResources.parse(kwargs["resources"])
inst = cls(**kwargs)
inst._original_data = data
return inst
Expand Down Expand Up @@ -121,7 +123,10 @@ def serialize(self) -> OrderedDict: # type: ignore[type-arg]
("icon", self.icon),
("category", self.category),
("source-path", self.source_path),
("resources", self.resources),
(
"resources",
self.resources and self.resources.get_data(),
),
("stop-condition", self.stop_condition),
("upload-store", self.upload_store),
]:
Expand Down
17 changes: 17 additions & 0 deletions valohai_yaml/objs/workload_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def __repr__(self) -> str:
"""CPU data."""
return f'ResourceCPU("max": {self.max}, "min": {self.min})'

def get_data(self) -> SerializedDict:
return {
key: value for key, value in super().get_data().items() if value is not None
}


class ResourceMemory(Item):
"""Memory configuration."""
Expand All @@ -35,6 +40,11 @@ def __repr__(self) -> str:
"""Memory data."""
return f'ResourceMemory("max": {self.max}, "min": {self.min})'

def get_data(self) -> SerializedDict:
return {
key: value for key, value in super().get_data().items() if value is not None
}


class ResourceDevices(Item):
"""Devices configuration."""
Expand Down Expand Up @@ -98,6 +108,13 @@ def parse(cls, data: SerializedDict) -> "WorkloadResources":
)
return super().parse(data_with_resources)

def get_data(self) -> SerializedDict:
return {
key: value.get_data()
for key, value in super().get_data().items()
if value is not None
}

def __repr__(self) -> str:
"""Resources contents."""
return f'WorkloadResources("cpu": {self.cpu}, "memory": {self.memory}, "devices": {self.devices})'

0 comments on commit 7c76fbf

Please sign in to comment.