-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Kubernetes runner and S3 storage adapters #93
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
6 commits
Select commit
Hold shift + click to select a range
cc0aeb7
refactor: replace OCI-specific infrastructure with configurable runne…
rorybyrne 2e05628
ci: add k8s extra dependencies to all workflow jobs
rorybyrne b789de6
refactor: replace string SRN parameters with typed SRN objects
rorybyrne be379ae
refactor: remove unused core_api parameter from _wait_for_completion …
rorybyrne 17edba4
feat: add K8s config validation and improve error handling
rorybyrne 4ba4474
feat: add k8s memory quantity conversion for proper resource limits
rorybyrne 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
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
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
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,6 @@ | ||
| """Kubernetes runner infrastructure. | ||
|
|
||
| kubernetes-asyncio is an optional dependency. Modules that require it | ||
| (di.py, runner.py, source_runner.py, health.py) perform lazy imports | ||
| and raise ConfigurationError if the package is not installed. | ||
| """ |
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,132 @@ | ||
| """Dishka DI provider for runner infrastructure (OCI or Kubernetes). | ||
|
|
||
| Uses Dishka's conditional activation (Marker + when=) to register only | ||
| the factories needed for the configured backend. When backend is "oci", | ||
| only Docker-related factories activate. When "k8s", only K8s factories | ||
| activate. No None placeholders, no unused dependencies resolved. | ||
| """ | ||
|
|
||
| import logging | ||
| from typing import AsyncIterable | ||
|
|
||
| import aiodocker | ||
| from dishka import Marker, activate, provide | ||
|
|
||
| from osa.config import Config | ||
| from osa.domain.source.port.source_runner import SourceRunner | ||
| from osa.domain.validation.port.hook_runner import HookRunner | ||
| from osa.infrastructure.oci.runner import OciHookRunner | ||
| from osa.infrastructure.oci.source_runner import OciSourceRunner | ||
| from osa.util.di.base import Provider | ||
| from osa.util.di.scope import Scope | ||
|
|
||
| try: | ||
| from kubernetes_asyncio.client import ApiClient | ||
| except ImportError: | ||
| ApiClient = object # type: ignore[misc,assignment] | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| K8S = Marker("k8s") | ||
|
|
||
|
|
||
| class RunnerProvider(Provider): | ||
| """Config-driven runner provider. | ||
|
|
||
| Uses Dishka conditional activation: factories decorated with | ||
| ``when=K8S`` only activate when the activator returns True | ||
| (i.e. ``config.runner.backend == "k8s"``). Undecorated factories | ||
| serve as the default OCI path. | ||
| """ | ||
|
|
||
| @activate(K8S) | ||
| def is_k8s(self, config: Config) -> bool: | ||
| return config.runner.backend == "k8s" | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # OCI backend (default — no when= condition) | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| @provide(scope=Scope.APP) | ||
| async def get_docker(self, config: Config) -> AsyncIterable[aiodocker.Docker]: | ||
| docker = aiodocker.Docker() | ||
| yield docker | ||
| await docker.close() | ||
|
|
||
| @provide(scope=Scope.UOW) | ||
| def get_hook_runner_oci( | ||
| self, | ||
| docker: aiodocker.Docker, | ||
| config: Config, | ||
| ) -> HookRunner: | ||
| return OciHookRunner(docker=docker, host_data_dir=config.host_data_dir) | ||
|
|
||
| @provide(scope=Scope.UOW) | ||
| def get_source_runner_oci( | ||
| self, | ||
| docker: aiodocker.Docker, | ||
| config: Config, | ||
| ) -> SourceRunner: | ||
| return OciSourceRunner(docker=docker, host_data_dir=config.host_data_dir) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # K8s backend (activated when config.runner.backend == "k8s") | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| @provide(when=K8S, scope=Scope.APP) | ||
| async def get_k8s_api_client(self, config: Config) -> AsyncIterable[ApiClient]: | ||
| from osa.domain.shared.error import ConfigurationError | ||
|
|
||
| try: | ||
| import kubernetes_asyncio # noqa: F401 | ||
| except ImportError: | ||
| raise ConfigurationError( | ||
| "kubernetes-asyncio is required for K8s runner. Install with: pip install osa[k8s]" | ||
| ) | ||
|
|
||
| from kubernetes_asyncio import client as k8s_client | ||
| from kubernetes_asyncio import config as k8s_config | ||
|
|
||
| try: | ||
| k8s_config.load_incluster_config() | ||
| except k8s_config.ConfigException: | ||
| await k8s_config.load_kube_config() | ||
|
|
||
| api_client = k8s_client.ApiClient() | ||
|
|
||
| # Startup health check | ||
| from osa.infrastructure.k8s.health import check_k8s_health | ||
|
|
||
| k8s_cfg = config.runner.k8s | ||
| batch_api = k8s_client.BatchV1Api(api_client) | ||
| core_api = k8s_client.CoreV1Api(api_client) | ||
| await check_k8s_health( | ||
| batch_api, | ||
| core_api, | ||
| namespace=k8s_cfg.namespace, | ||
| pvc_name=k8s_cfg.data_pvc_name, | ||
| ) | ||
|
|
||
| logger.info("K8s API client initialized (namespace=%s)", k8s_cfg.namespace) | ||
| yield api_client | ||
| await api_client.close() | ||
|
|
||
| @provide(when=K8S, scope=Scope.UOW) | ||
| def get_hook_runner_k8s( | ||
| self, | ||
| k8s_api_client: ApiClient, | ||
| config: Config, | ||
| ) -> HookRunner: | ||
| from osa.infrastructure.k8s.runner import K8sHookRunner | ||
|
|
||
| return K8sHookRunner(api_client=k8s_api_client, config=config.runner.k8s) | ||
|
|
||
| @provide(when=K8S, scope=Scope.UOW) | ||
| def get_source_runner_k8s( | ||
| self, | ||
| k8s_api_client: ApiClient, | ||
| config: Config, | ||
| ) -> SourceRunner: | ||
| from osa.infrastructure.k8s.source_runner import K8sSourceRunner | ||
|
|
||
| return K8sSourceRunner(api_client=k8s_api_client, config=config.runner.k8s) |
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,29 @@ | ||
| """K8s API error classification. | ||
|
|
||
| Maps kubernetes-asyncio ApiException status codes to OSA error types. | ||
| """ | ||
|
|
||
| from osa.domain.shared.error import ConfigurationError, InfrastructureError, OSAError | ||
|
|
||
|
|
||
| def classify_api_error(exc: Exception) -> OSAError: | ||
| """Classify a K8s API error by HTTP status code. | ||
|
|
||
| - 403 → ConfigurationError (RBAC misconfiguration, not retried) | ||
| - 404 → ConfigurationError (namespace/resource missing, not retried) | ||
| - 500, 503 → InfrastructureError (transient, retried by outbox) | ||
| - Other → InfrastructureError | ||
| """ | ||
| status = getattr(exc, "status", 0) | ||
| reason = getattr(exc, "reason", str(exc)) | ||
|
|
||
| if status == 403: | ||
| return ConfigurationError( | ||
| f"K8s RBAC permission denied: {reason}. " | ||
| "Check ServiceAccount permissions for the OSA namespace." | ||
| ) | ||
| if status == 404: | ||
| return ConfigurationError( | ||
| f"K8s resource not found: {reason}. Check that the namespace and resources exist." | ||
| ) | ||
| return InfrastructureError(f"K8s API error ({status}): {reason}") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data_pvc_namerequired whenbackend == "k8s"K8sConfig.data_pvc_namedefaults to"". If an operator enables the K8s backend but forgets to set this field, the PVC claim name embedded in every Job spec will be an empty string. The error will only surface at startup via the health-check call toread_namespaced_persistent_volume_claim("", namespace), which returns a cryptic K8s 404. A@model_validatoronRunnerConfigwould surface the problem at config-parse time with a clear message: