Skip to content
Draft
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
[agntcy/dir]: https://github.com/agntcy/dir
[agntcy/dir-sdk-python]: https://github.com/agntcy/dir-sdk-python

## Unreleased

### Added

- `RuntimeClient` for the runtime discovery API (`agntcy.dir.runtime.v1.DiscoveryService`): `get_workload` and `list_workloads` (with all `ListWorkloadsRequest` filters). It connects to dir-runtime's own address, separately from `Client`, so Directory credentials are never sent to it. Models are exposed as `agntcy.dir_sdk.models.runtime_v1`.

## 1.8.1 (2026-10-21)

### Changed
Expand Down
53 changes: 52 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ vars:

KIND_CLUSTER_NAME: "sdk-py-test"

# dir-runtime serves the runtime discovery API (agntcy.dir.runtime.v1) on its own address.
# renovate: datasource=github-releases depName=agntcy/dir-runtime versioning=semver
RUNTIME_VERSION: '{{ .RUNTIME_VERSION | default "v1.5.0" }}'
RUNTIME_API_HOSTPORT: '{{ .RUNTIME_API_HOSTPORT | default "18888" }}'
RUNTIME_API_NODEPORT: '{{ .RUNTIME_API_NODEPORT | default "30180" }}'

tasks:
# ---------------------------------------------------------------------------
# SDK tasks (top-level, user-facing)
Expand Down Expand Up @@ -76,6 +82,8 @@ tasks:
- |
export COSIGN_PATH="$(printf "%s" "${COSIGN_PATH:-{{ .COSIGN_BIN }}}")"
export DIRCTL_PATH="$(printf "%s" "${DIRCTL_PATH:-{{ .DIRCTL_BIN }}}")"
export DIRECTORY_RUNTIME_ADDRESS="localhost:{{ .RUNTIME_API_HOSTPORT }}"
export RUNTIME_E2E_WORKLOAD_NAME="sdk-runtime-e2e"

# Run SDK tests
'{{ .UV_BIN }}' run pytest \
Expand Down Expand Up @@ -210,7 +218,7 @@ tasks:
deps:
- task: test-env:setup-cluster
vars:
KIND_EXTRA_PORT_MAPPING: "{{.API_HOSTPORT}}:{{.API_NODEPORT}},{{.METRICS_HOSTPORT}}:{{.METRICS_NODEPORT}}"
KIND_EXTRA_PORT_MAPPING: "{{.API_HOSTPORT}}:{{.API_NODEPORT}},{{.METRICS_HOSTPORT}}:{{.METRICS_NODEPORT}},{{.RUNTIME_API_HOSTPORT}}:{{.RUNTIME_API_NODEPORT}}"
vars:
# Kind args
KIND_CLUSTER_NAME: '{{ .KIND_CLUSTER_NAME | default "sdk-py-test" }}'
Expand Down Expand Up @@ -270,6 +278,49 @@ tasks:
--wait-for-jobs \
--timeout "15m"

# Deploy dir-runtime and the workload it discovers
- task: test-env:runtime:deploy

test-env:runtime:deploy:
desc: Deploy dir-runtime and a labelled workload for the runtime discovery E2E tests
deps:
- deps:helm
- deps:kubectl
- deps:kind
vars:
KIND_CLUSTER_NAME: '{{ .KIND_CLUSTER_NAME | default "sdk-py-test" }}'
preconditions:
- which docker
- which curl
cmds:
- |
set -euo pipefail
SRC="$(mktemp -d)"
trap 'rm -rf "$SRC"' EXIT
curl -sfL "https://github.com/agntcy/dir-runtime/archive/refs/tags/{{ .RUNTIME_VERSION }}.tar.gz" | tar -xz -C "$SRC" --strip-components=1

# The published dir-runtime-server image contains the discovery binary
# (same digest as dir-runtime-discovery), so build the server from source.
docker build -q -f "$SRC/server/Dockerfile" --target production -t dir-runtime-server:sdk-e2e "$SRC"
{{ .KIND_BIN }} load docker-image dir-runtime-server:sdk-e2e --name {{ .KIND_CLUSTER_NAME }}

{{ .HELM_BIN }} install runtime "$SRC/install/chart" \
--namespace dir-runtime \
--create-namespace \
--set discovery.image.tag="{{ trimPrefix "v" .RUNTIME_VERSION }}" \
--set server.image.repository=dir-runtime-server \
--set server.image.tag=sdk-e2e \
--set server.image.pullPolicy=Never \
--set discovery.config.store.crd.namespace=dir-runtime \
--set server.config.store.crd.namespace=dir-runtime \
--set server.service.type=NodePort \
--set server.service.nodePort="{{ .RUNTIME_API_NODEPORT }}" \
--wait \
--timeout "5m"

{{ .KUBECTL_BIN }} apply -f "{{ .ROOT_DIR }}/tests/runtime-e2e/workload.yaml"
{{ .KUBECTL_BIN }} wait --for=condition=Ready pod/sdk-runtime-e2e --timeout=2m

test-env:deploy:cleanup:
desc: Cleanup Kubernetes environment for local deployment
deps:
Expand Down
3 changes: 2 additions & 1 deletion dir-sdk-python/agntcy/dir_sdk/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
from agntcy.dir_sdk.client.auth.oauth_pkce import OAuthPkceError
from agntcy.dir_sdk.client.client import Client
from agntcy.dir_sdk.client.config import Config
from agntcy.dir_sdk.client.runtime_client import RuntimeClient

__all__ = ["Client", "Config", "OAuthPkceError"]
__all__ = ["Client", "Config", "OAuthPkceError", "RuntimeClient"]
55 changes: 55 additions & 0 deletions dir-sdk-python/agntcy/dir_sdk/client/runtime_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright AGNTCY Contributors (https://github.com/agntcy)
# SPDX-License-Identifier: Apache-2.0

"""Client for the runtime discovery API served by dir-runtime."""

from __future__ import annotations

import logging
from collections.abc import Sequence

import grpc
from agntcy.dir_sdk.client.services.runtime import RuntimeService
from agntcy.dir_sdk.models import runtime_v1

logger = logging.getLogger("agntcy.dir_sdk.runtime")


class RuntimeClient:
"""Client for ``agntcy.dir.runtime.v1.DiscoveryService``.

dir-runtime (and ``dirctl daemon``) runs at its own address, so this is
separate from :class:`~agntcy.dir_sdk.client.Client`: the Directory's
credentials, such as the OIDC token, are never sent to it.
"""

DEFAULT_SERVER_ADDRESS = "localhost:8888"

def __init__(
self,
server_address: str = DEFAULT_SERVER_ADDRESS,
channel: grpc.Channel | None = None,
) -> None:
self.server_address = server_address
self.channel = channel or grpc.insecure_channel(server_address)

# Expose the raw stub for advanced callers.
self.discovery_client = runtime_v1.DiscoveryServiceStub(self.channel)
self.runtime_service = RuntimeService(self.discovery_client, logger)

def close(self) -> None:
self.channel.close()

def get_workload(
self,
req: runtime_v1.GetWorkloadRequest,
metadata: Sequence[tuple[str, str]] | None = None,
) -> runtime_v1.Workload:
return self.runtime_service.get_workload(req, metadata)

def list_workloads(
self,
req: runtime_v1.ListWorkloadsRequest,
metadata: Sequence[tuple[str, str]] | None = None,
) -> list[runtime_v1.Workload]:
return self.runtime_service.list_workloads(req, metadata)
2 changes: 2 additions & 0 deletions dir-sdk-python/agntcy/dir_sdk/client/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from agntcy.dir_sdk.client.services.identity import IdentityService
from agntcy.dir_sdk.client.services.publication import PublicationService
from agntcy.dir_sdk.client.services.routing import RoutingService
from agntcy.dir_sdk.client.services.runtime import RuntimeService
from agntcy.dir_sdk.client.services.search import SearchService
from agntcy.dir_sdk.client.services.signing import SignService
from agntcy.dir_sdk.client.services.store import StoreService
Expand All @@ -17,6 +18,7 @@
"IdentityService",
"PublicationService",
"RoutingService",
"RuntimeService",
"SearchService",
"SignService",
"StoreService",
Expand Down
44 changes: 44 additions & 0 deletions dir-sdk-python/agntcy/dir_sdk/client/services/runtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright AGNTCY Contributors (https://github.com/agntcy)
# SPDX-License-Identifier: Apache-2.0

"""Runtime discovery service wrappers."""

from __future__ import annotations

import logging
from collections.abc import Sequence

from agntcy.dir_sdk.client.services.base import RpcServiceBase
from agntcy.dir_sdk.models import runtime_v1


class RuntimeService(RpcServiceBase):
def __init__(
self,
discovery_client: runtime_v1.DiscoveryServiceStub,
logger: logging.Logger,
) -> None:
super().__init__(logger)
self._discovery_client = discovery_client

def get_workload(
self,
req: runtime_v1.GetWorkloadRequest,
metadata: Sequence[tuple[str, str]] | None = None,
) -> runtime_v1.Workload:
return self._invoke(
"get_workload",
"Failed to get workload",
lambda: self._discovery_client.GetWorkload(req, metadata=metadata),
)

def list_workloads(
self,
req: runtime_v1.ListWorkloadsRequest,
metadata: Sequence[tuple[str, str]] | None = None,
) -> list[runtime_v1.Workload]:
return self._collect_stream(
"list_workloads",
"Failed to list workloads",
lambda: self._discovery_client.ListWorkloads(req, metadata=metadata),
)
1 change: 1 addition & 0 deletions dir-sdk-python/agntcy/dir_sdk/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from . import events_v1 as events_v1
from . import identity_v1 as identity_v1
from . import routing_v1 as routing_v1
from . import runtime_v1 as runtime_v1
from . import search_v1 as search_v1
from . import sign_v1 as sign_v1
from . import store_v1 as store_v1
6 changes: 6 additions & 0 deletions dir-sdk-python/agntcy/dir_sdk/models/runtime_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright AGNTCY Contributors (https://github.com/agntcy)
# SPDX-License-Identifier: Apache-2.0

from agntcy.dir.runtime.v1.discovery_service_pb2 import *
from agntcy.dir.runtime.v1.discovery_service_pb2_grpc import *
from agntcy.dir.runtime.v1.workload_pb2 import *
81 changes: 81 additions & 0 deletions dir-sdk-python/agntcy/dir_sdk/tests/test_runtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright AGNTCY Contributors (https://github.com/agntcy)
# SPDX-License-Identifier: Apache-2.0

"""Tests for the runtime discovery client against an in-process server."""

from __future__ import annotations

import unittest
from collections.abc import Iterator
from concurrent import futures

import grpc
from agntcy.dir_sdk.client import RuntimeClient
from agntcy.dir_sdk.models import runtime_v1

WORKLOADS = [
runtime_v1.Workload(
id="a",
runtime="docker",
locators=[
runtime_v1.WorkloadLocator(protocol="http", url="http://10.0.0.1:80")
],
),
runtime_v1.Workload(id="b", runtime="kubernetes"),
]


class FakeDiscovery(runtime_v1.DiscoveryServiceServicer):
"""In-process dir-runtime; applies only the runtime_types filter."""

def GetWorkload( # noqa: N802
self,
request: runtime_v1.GetWorkloadRequest,
context: grpc.ServicerContext,
) -> runtime_v1.Workload:
for w in WORKLOADS:
if w.id == request.id:
return w
context.abort(grpc.StatusCode.NOT_FOUND, request.id)
raise AssertionError # abort() raises; unreachable

def ListWorkloads( # noqa: N802
self,
request: runtime_v1.ListWorkloadsRequest,
_context: grpc.ServicerContext,
) -> Iterator[runtime_v1.Workload]:
for w in WORKLOADS:
if not request.runtime_types or w.runtime in request.runtime_types:
yield w


class TestRuntimeClient(unittest.TestCase):
def setUp(self) -> None:
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=2))
runtime_v1.add_DiscoveryServiceServicer_to_server(FakeDiscovery(), self.server)
port = self.server.add_insecure_port("127.0.0.1:0")
self.server.start()
self.client = RuntimeClient(f"127.0.0.1:{port}")

def tearDown(self) -> None:
self.client.close()
self.server.stop(None)

def test_list_workloads_with_filters(self) -> None:
everything = self.client.list_workloads(runtime_v1.ListWorkloadsRequest())
assert [w.id for w in everything] == ["a", "b"]

docker = self.client.list_workloads(
runtime_v1.ListWorkloadsRequest(runtime_types=["docker"])
)
assert len(docker) == 1
assert docker[0].locators[0].url == "http://10.0.0.1:80"

def test_get_workload(self) -> None:
w = self.client.get_workload(runtime_v1.GetWorkloadRequest(id="b"))
assert w.runtime == "kubernetes"

def test_get_workload_not_found(self) -> None:
with self.assertRaises(grpc.RpcError) as ctx:
self.client.get_workload(runtime_v1.GetWorkloadRequest(id="missing"))
assert ctx.exception.code() == grpc.StatusCode.NOT_FOUND
Loading