OpenFactory Shared Schemas#

Shared Deployment Configuration Schemas for OpenFactory

This module defines reusable Pydantic models that represent container deployment settings and resource constraints. These models are intended to be shared across device and connector configuration schemas.

Components:#

  • ResourcesDefinition: Represents CPU and memory values for a container.

  • Resources: Groups reservations and limits for container resources.

  • Placement: Defines constraints for container placement on specific nodes.

  • Deploy: Complete deployment configuration including replicas, resource configs, and placement rules.

Key Features:#

  • Express CPU and memory constraints using common formats (e.g., 0.5 CPUs, “1Gi” memory)

  • Define both resource requests and limits for containers

  • Support placement constraints for scheduling on labeled nodes

  • Modular, reusable across multiple OpenFactory schema modules

Example Usage:#

Define a deployment configuration:

>>> Deploy(
...     replicas=2,
...     resources=Resources(
...         reservations=ResourcesDefinition(cpus=0.5, memory="512Mi"),
...         limits=ResourcesDefinition(cpus=1.0, memory="1Gi")
...     ),
...     placement=Placement(
...         constraints=["node.labels.zone == eu-west"]
...     )
... )

This module is typically used as part of device, connector, or application schemas that involve resource scheduling or orchestration.

class openfactory.schemas.common.Deploy(**data)[source]#

Bases: BaseModel

Defines deployment configuration such as replicas, resources, and placement.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

placement: Placement | None#
replicas: int | None#
resources: Resources | None#
class openfactory.schemas.common.Placement(**data)[source]#

Bases: BaseModel

Defines placement constraints for scheduling containers.

constraints: List[str] | None#
model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class openfactory.schemas.common.Resources(**data)[source]#

Bases: BaseModel

Specifies resource requests and limits for a container deployment.

limits: ResourcesDefinition | None#
model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

reservations: ResourcesDefinition | None#
class openfactory.schemas.common.ResourcesDefinition(**data)[source]#

Bases: BaseModel

Defines resource limits or reservations such as CPU and memory.

cpus: float | None#
memory: str | None#
model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

openfactory.schemas.common.constraints(deploy)[source]#

Retrieve and normalize placement constraints from a Deploy object for Docker deployments.

Return type:

Optional[List[str]]

This function:
  • Extracts the constraints list from deploy.placement if available.

  • Converts single '=' into ' == ' for Docker/Docker Swarm API compatibility.

  • Normalizes spacing around '=='.

  • Returns None if no constraints are defined.

Parameters:

deploy (Optional[Deploy]) – Deployment configuration containing optional placement info.

Returns:

Optional[List[str]] – List of normalized constraint strings, or None if empty or undefined.

openfactory.schemas.common.cpus_limit(deploy, default=1.0)[source]#

Retrieve the CPU limit value from a Deploy object, returning a default if unavailable.

Return type:

float

Parameters:
  • deploy (Optional[Deploy]) – The deployment configuration object.

  • default (float) – The default CPU limit value to return if not set. Defaults to 1.0.

Returns:

float – The CPU limit value from the deployment or the default if not specified.

openfactory.schemas.common.cpus_reservation(deploy, default=0.5)[source]#

Retrieve the CPU reservation value from a Deploy object, returning a default if unavailable.

Return type:

float

Parameters:
  • deploy (Optional[Deploy]) – The deployment configuration object.

  • default (float) – The default CPU reservation value to return if not set. Defaults to 0.5.

Returns:

float – The CPU reservation value from the deployment or the default if not specified.

openfactory.schemas.common.parse_memory_to_bytes(mem_str)[source]#

Convert a memory size string into an integer number of bytes.

This function accepts memory strings using common units like bytes (B), kilobytes (K, KB), megabytes (M, MB, Mi), and gigabytes (G, GB, Gi), case-insensitive. It also handles fractional values (e.g., 0.5Gi).

If no unit is provided, the string is interpreted as bytes.

Usage example

>>> parse_memory_to_bytes("512Mi")
536870912
>>> parse_memory_to_bytes("1Gi")
1073741824
>>> parse_memory_to_bytes("0.5Gi")
536870912
>>> parse_memory_to_bytes("1024")
1024
Parameters:

mem_str (str) – Memory size string, e.g., 512Mi, 1Gi, 1024.

Returns:

int – Memory size in bytes.

Raises:

ValueError – If the string cannot be parsed into a valid number.

Return type:

int

openfactory.schemas.common.resources(deploy)[source]#

Retrieve and normalize resources from a Deploy object for Docker deployments.

Extracts CPU and memory settings from deploy.resources and converts them into the dictionary format expected by Docker.

Return type:

Optional[Dict[str, Dict[str, Any]]]

Parameters:

deploy (Optional[Deploy]) – Deployment configuration for an application.

Returns:

Optional[Dict[str, Dict[str, Any]]] – Dictionary with Limits and Reservations keys, containing CPU (NanoCPUs) and Memory (MemoryBytes) in Docker format. Returns None if no resource info is provided.