2023-10-24 20:44:48 +00:00
|
|
|
# Copyright © 2022, 2023 Vulcanize
|
|
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
import typing
|
2024-02-08 06:43:41 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
import humanfriendly
|
|
|
|
|
|
2023-11-21 23:04:36 +00:00
|
|
|
from stack_orchestrator import constants
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
from stack_orchestrator.util import get_yaml
|
2023-10-24 20:44:48 +00:00
|
|
|
|
|
|
|
|
|
2024-02-08 06:43:41 +00:00
|
|
|
class ResourceLimits:
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
cpus: float | None = None
|
|
|
|
|
memory: int | None = None
|
|
|
|
|
storage: int | None = None
|
2024-02-08 06:43:41 +00:00
|
|
|
|
2024-03-07 17:38:36 +00:00
|
|
|
def __init__(self, obj=None):
|
|
|
|
|
if obj is None:
|
|
|
|
|
obj = {}
|
2024-02-08 06:43:41 +00:00
|
|
|
if "cpus" in obj:
|
|
|
|
|
self.cpus = float(obj["cpus"])
|
|
|
|
|
if "memory" in obj:
|
|
|
|
|
self.memory = humanfriendly.parse_size(obj["memory"])
|
|
|
|
|
if "storage" in obj:
|
|
|
|
|
self.storage = humanfriendly.parse_size(obj["storage"])
|
|
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
|
return len(self.__dict__)
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
for k in self.__dict__:
|
|
|
|
|
yield k, self.__dict__[k]
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return str(self.__dict__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Resources:
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
limits: ResourceLimits | None = None
|
|
|
|
|
reservations: ResourceLimits | None = None
|
2024-02-08 06:43:41 +00:00
|
|
|
|
2024-03-07 17:38:36 +00:00
|
|
|
def __init__(self, obj=None):
|
|
|
|
|
if obj is None:
|
|
|
|
|
obj = {}
|
2024-02-08 06:43:41 +00:00
|
|
|
if "reservations" in obj:
|
|
|
|
|
self.reservations = ResourceLimits(obj["reservations"])
|
|
|
|
|
if "limits" in obj:
|
|
|
|
|
self.limits = ResourceLimits(obj["limits"])
|
|
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
|
return len(self.__dict__)
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
for k in self.__dict__:
|
|
|
|
|
yield k, self.__dict__[k]
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return str(self.__dict__)
|
|
|
|
|
|
|
|
|
|
|
2023-10-24 20:44:48 +00:00
|
|
|
class Spec:
|
|
|
|
|
obj: typing.Any
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
file_path: Path | None
|
2023-10-24 20:44:48 +00:00
|
|
|
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
def __init__(self, file_path: Path | None = None, obj=None) -> None:
|
2024-03-07 17:38:36 +00:00
|
|
|
if obj is None:
|
|
|
|
|
obj = {}
|
2024-02-14 21:45:01 +00:00
|
|
|
self.file_path = file_path
|
|
|
|
|
self.obj = obj
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
|
return self.obj[item]
|
|
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
|
return item in self.obj
|
|
|
|
|
|
|
|
|
|
def get(self, item, default=None):
|
|
|
|
|
return self.obj.get(item, default)
|
2023-10-24 20:44:48 +00:00
|
|
|
|
|
|
|
|
def init_from_file(self, file_path: Path):
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
self.obj = get_yaml().load(open(file_path))
|
2025-10-16 17:55:44 +00:00
|
|
|
self.file_path = file_path
|
2023-11-21 23:04:36 +00:00
|
|
|
|
|
|
|
|
def get_image_registry(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.image_registry_key)
|
2023-11-21 23:04:36 +00:00
|
|
|
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
def get_image_registry_config(self) -> dict | None:
|
2026-02-03 17:25:33 +00:00
|
|
|
"""Returns registry auth config: {server, username, token-env}.
|
|
|
|
|
|
|
|
|
|
Used for private container registries like GHCR. The token-env field
|
|
|
|
|
specifies an environment variable containing the API token/PAT.
|
2026-02-03 17:30:35 +00:00
|
|
|
|
|
|
|
|
Note: Uses 'registry-credentials' key to avoid collision with
|
|
|
|
|
'image-registry' key which is for pushing images.
|
2026-02-03 17:25:33 +00:00
|
|
|
"""
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
result: dict[str, str] | None = self.obj.get("registry-credentials")
|
|
|
|
|
return result
|
2026-02-03 17:25:33 +00:00
|
|
|
|
2024-01-31 05:09:48 +00:00
|
|
|
def get_volumes(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.volumes_key, {})
|
2024-01-31 05:09:48 +00:00
|
|
|
|
|
|
|
|
def get_configmaps(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.configmaps_key, {})
|
2024-01-31 05:09:48 +00:00
|
|
|
|
2024-02-07 22:48:02 +00:00
|
|
|
def get_container_resources(self):
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
return Resources(self.obj.get(constants.resources_key, {}).get("containers", {}))
|
2024-02-07 22:48:02 +00:00
|
|
|
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
def get_container_resources_for(self, container_name: str) -> Resources | None:
|
2026-03-07 10:26:10 +00:00
|
|
|
"""Look up per-container resource overrides from spec.yml.
|
|
|
|
|
|
|
|
|
|
Checks resources.containers.<container_name> in the spec. Returns None
|
|
|
|
|
if no per-container override exists (caller falls back to other sources).
|
|
|
|
|
"""
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
containers_block = self.obj.get(constants.resources_key, {}).get("containers", {})
|
2026-03-07 10:26:10 +00:00
|
|
|
if container_name in containers_block:
|
|
|
|
|
entry = containers_block[container_name]
|
|
|
|
|
# Only treat it as a per-container override if it's a dict with
|
|
|
|
|
# reservations/limits nested inside (not a top-level global key)
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
if isinstance(entry, dict) and ("reservations" in entry or "limits" in entry):
|
2026-03-07 10:26:10 +00:00
|
|
|
return Resources(entry)
|
|
|
|
|
return None
|
|
|
|
|
|
2024-02-07 22:48:02 +00:00
|
|
|
def get_volume_resources(self):
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
return Resources(self.obj.get(constants.resources_key, {}).get(constants.volumes_key, {}))
|
2024-02-07 22:48:02 +00:00
|
|
|
|
2023-11-21 23:04:36 +00:00
|
|
|
def get_http_proxy(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.network_key, {}).get(constants.http_proxy_key, [])
|
2024-02-09 00:11:07 +00:00
|
|
|
|
|
|
|
|
def get_annotations(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.annotations_key, {})
|
2024-02-09 00:11:07 +00:00
|
|
|
|
2024-08-09 02:32:06 +00:00
|
|
|
def get_replicas(self):
|
|
|
|
|
return self.obj.get(constants.replicas_key, 1)
|
|
|
|
|
|
2024-08-15 20:32:58 +00:00
|
|
|
def get_node_affinities(self):
|
|
|
|
|
return self.obj.get(constants.node_affinities_key, [])
|
|
|
|
|
|
|
|
|
|
def get_node_tolerations(self):
|
|
|
|
|
return self.obj.get(constants.node_tolerations_key, [])
|
|
|
|
|
|
2024-02-09 00:11:07 +00:00
|
|
|
def get_labels(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.labels_key, {})
|
2024-02-09 00:11:07 +00:00
|
|
|
|
|
|
|
|
def get_privileged(self):
|
2026-01-22 01:20:19 +00:00
|
|
|
return (
|
|
|
|
|
"true"
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
== str(self.obj.get(constants.security_key, {}).get("privileged", "false")).lower()
|
2026-01-22 01:20:19 +00:00
|
|
|
)
|
2024-02-09 00:11:07 +00:00
|
|
|
|
|
|
|
|
def get_capabilities(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.security_key, {}).get("capabilities", [])
|
2024-02-14 21:45:01 +00:00
|
|
|
|
2026-01-22 01:20:19 +00:00
|
|
|
def get_unlimited_memlock(self):
|
|
|
|
|
return (
|
|
|
|
|
"true"
|
|
|
|
|
== str(
|
|
|
|
|
self.obj.get(constants.security_key, {}).get(
|
|
|
|
|
constants.unlimited_memlock_key, "false"
|
|
|
|
|
)
|
|
|
|
|
).lower()
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-22 06:58:38 +00:00
|
|
|
def get_runtime_class(self):
|
|
|
|
|
"""Get runtime class name from spec, or derive from security settings.
|
|
|
|
|
|
|
|
|
|
The runtime class determines which containerd runtime handler to use,
|
|
|
|
|
allowing different pods to have different rlimit profiles (e.g., for
|
|
|
|
|
unlimited RLIMIT_MEMLOCK).
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Runtime class name string, or None to use default runtime.
|
|
|
|
|
"""
|
|
|
|
|
# Explicit runtime class takes precedence
|
fix: add pre-commit hooks and fix all lint/type/format errors
Process bug fix: no pre-commit existed for this repo's Python code.
Added pyproject.toml with unified dependencies (ruff, mypy, ansible-lint),
.pre-commit-config.yaml with repo-based hooks (ruff) and local uv-run
hooks (mypy, ansible-lint).
Fixed 249 ruff errors (B023, B904, B006, B007, UP008, UP031, C408),
~13 mypy type errors, 11 ansible-lint violations, and ruff-format
across all Python files including stack-orchestrator subtree.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 14:56:22 +00:00
|
|
|
explicit = self.obj.get(constants.security_key, {}).get(constants.runtime_class_key, None)
|
2026-01-22 06:58:38 +00:00
|
|
|
if explicit:
|
|
|
|
|
return explicit
|
|
|
|
|
|
|
|
|
|
# Auto-derive from unlimited-memlock setting
|
|
|
|
|
if self.get_unlimited_memlock():
|
|
|
|
|
return constants.high_memlock_runtime
|
|
|
|
|
|
|
|
|
|
return None # Use default runtime
|
|
|
|
|
|
2024-02-14 21:45:01 +00:00
|
|
|
def get_deployment_type(self):
|
2024-03-07 17:38:36 +00:00
|
|
|
return self.obj.get(constants.deploy_to_key)
|
2024-02-14 21:45:01 +00:00
|
|
|
|
2026-02-03 00:13:10 +00:00
|
|
|
def get_acme_email(self):
|
2026-02-03 03:18:19 +00:00
|
|
|
return self.obj.get(constants.network_key, {}).get(constants.acme_email_key, "")
|
2026-02-03 00:13:10 +00:00
|
|
|
|
2024-02-14 21:45:01 +00:00
|
|
|
def is_kubernetes_deployment(self):
|
2026-01-22 01:20:19 +00:00
|
|
|
return self.get_deployment_type() in [
|
|
|
|
|
constants.k8s_kind_deploy_type,
|
|
|
|
|
constants.k8s_deploy_type,
|
|
|
|
|
]
|
2024-02-14 21:45:01 +00:00
|
|
|
|
|
|
|
|
def is_kind_deployment(self):
|
|
|
|
|
return self.get_deployment_type() in [constants.k8s_kind_deploy_type]
|
|
|
|
|
|
|
|
|
|
def is_docker_deployment(self):
|
|
|
|
|
return self.get_deployment_type() in [constants.compose_deploy_type]
|