-
Notifications
You must be signed in to change notification settings - Fork 157
Replace docker exec gnoi_client with native Python gRPC calls #367
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
Open
sigabrtv1-ui
wants to merge
13
commits into
sonic-net:master
Choose a base branch
from
sigabrtv1-ui:feature/gnoi-native-grpc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
22d0937
Add design doc: replace docker exec gnoi_client with native gRPC calls
hdwhdw 90df610
design doc: add reference code for gnoi_client and openconfig system …
hdwhdw 8bbf237
design doc: remove unrelated SetPackage references from reference sec…
hdwhdw 0bf9f21
design doc: address Copilot review comments
hdwhdw bf4c41c
Address Copilot review: fix stderr description, add packaging note
hdwhdw 72eb52a
Replace inline reference code with links to source files
hdwhdw f47c596
design doc: trim for human readability — lead with the bug, cut boile…
hdwhdw 4940fab
design doc: reframe as footgun pattern, trim implementation details
hdwhdw eb0cf40
design doc: soften language, use neutral framing
hdwhdw 110bf59
Replace docker exec gnoi_client with native Python gRPC calls
hdwhdw 18577b9
Merge origin/master and resolve test conflicts
hdwhdw 5b12fba
Refactor GnoiClient to be service-agnostic
hdwhdw 9fa8000
Route RPCs through service stubs: client.system.Reboot()
hdwhdw 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| # Design: Replace `docker exec gnoi_client` with Native gRPC Calls | ||
|
|
||
| ## TL;DR | ||
|
|
||
| `gnoi_shutdown_daemon` issues gNOI RPCs by shelling out to `docker exec gnmi gnoi_client`. This introduces several layers of indirection that make failures hard to diagnose and completion detection unreliable. This document proposes replacing the subprocess path with direct Python gRPC calls using vendored proto stubs. | ||
|
|
||
| ## 1. Limitations of the Current Approach | ||
|
|
||
| | Observation | Consequence | | ||
| |-------------|-------------| | ||
| | Requires NPU `gnmi` container running and healthy | DPU shutdown depends on an unrelated NPU container's availability, even though the RPC target is the DPU's own gnmi server | | ||
| | Subprocess + Docker CLI overhead per RPC | Extra process creation, Docker round-trip, stdout capture on each call | | ||
| | Output is unstructured text with a header line | Parsing is coupled to `gnoi_client`'s print format, which has no stability guarantee | | ||
| | gRPC status codes are not propagated | Caller only sees `rc != 0` — no status code, no error details | | ||
| | Errors surface as Go `panic()` stack traces on stderr | Diagnosing RPC failures requires SSH + manual docker exec | | ||
| | `suppress_stderr=True` on the Reboot call | Panic output is discarded; logs show only "command failed" | | ||
| | Completion check uses string matching | `"reboot complete" in out_s.lower()` does not match actual output format (see §2) | | ||
| | Tight coupling to CLI flag interface | `-module System -rpc Reboot -jsonin '{...}'` adds a serialization layer between caller and protobuf | | ||
| | Broader privilege surface | Shell-out through Docker CLI vs. a direct gRPC socket | | ||
|
|
||
| ## 2. Existing Issue: RebootStatus Completion Detection | ||
|
|
||
| The poll loop checks: | ||
|
|
||
| ```python | ||
| if rc_s == 0 and out_s and ("reboot complete" in out_s.lower()): | ||
| return True | ||
| ``` | ||
|
|
||
| Actual `gnoi_client` output ([source](https://github.com/sonic-net/sonic-gnmi/blob/master/gnoi_client/system/reboot.go)): | ||
|
|
||
| ``` | ||
| System RebootStatus | ||
| {"active":false,"status":{"status":"STATUS_SUCCESS","message":"..."}} | ||
| ``` | ||
|
|
||
| `"reboot complete"` does not appear in this output → the poll always exhausts its timeout regardless of DPU state. | ||
|
|
||
| ## 3. Proposed Change | ||
|
|
||
| Replace subprocess calls with a thin Python gRPC client using vendored [gNOI System proto](https://github.com/openconfig/gnoi/blob/main/system/system.proto) stubs. | ||
|
|
||
| **Before** (subprocess): | ||
| ``` | ||
| docker exec gnmi gnoi_client -target=<ip>:<port> -notls -module System -rpc Reboot -jsonin '{"method":3}' | ||
| ``` | ||
|
|
||
| **After** (direct gRPC): | ||
| ```python | ||
| with GnoiClient(f"{dpu_ip}:{port}") as client: | ||
| client.reboot(method=REBOOT_METHOD_HALT, message="graceful shutdown") | ||
| ``` | ||
|
|
||
| For RebootStatus, check the protobuf response directly instead of string matching: | ||
| ```python | ||
| resp = client.reboot_status() | ||
| if not resp.active and resp.status.status == STATUS_SUCCESS: | ||
| return True | ||
| ``` | ||
|
|
||
| ### What this gives us | ||
|
|
||
| **Better error detection** — gRPC errors carry status codes and details natively: | ||
| ```python | ||
| except grpc.RpcError as e: | ||
| logger.log_error(f"{dpu_name}: Reboot failed: {e.code()} {e.details()}") | ||
| # e.g. "UNAVAILABLE: connection refused" vs today's "command failed" | ||
| ``` | ||
|
|
||
| **Better testing** — mocks operate on typed protobuf objects instead of crafting subprocess stdout strings: | ||
| ```python | ||
| # Today: mock must reproduce gnoi_client's exact text output | ||
| mock_execute.return_value = (0, "reboot complete", "") # this doesn't even match reality | ||
|
|
||
| # After: mock returns a typed response | ||
| mock_client.reboot_status.return_value = RebootStatusResponse( | ||
| active=False, status=RebootStatus(status=STATUS_SUCCESS)) | ||
| ``` | ||
|
|
||
| **Correct completion check** — inspect `resp.active` and `resp.status.status` directly instead of string matching. | ||
|
|
||
| **Removes unnecessary NPU gnmi container dependency** — the current approach shells into the NPU's `gnmi` container to run `gnoi_client`, but there's no reason the NPU daemon needs the NPU gnmi container as an intermediary. The DPU's own gnmi server is the actual RPC endpoint; direct gRPC connects to it without involving the NPU container. | ||
|
|
||
| **Scales to future RPCs** — the same pattern extends to any gNOI or gNMI call without adding more subprocess wrappers: | ||
| ```python | ||
| # Adding a new gNOI RPC is just another method on the client | ||
| class GnoiClient: | ||
| def reboot(self, ...): ... | ||
| def reboot_status(self, ...): ... | ||
| def cancel_reboot(self, ...): ... # future | ||
| def system_time(self, ...): ... # future | ||
|
|
||
| # Or a gNMI client alongside it | ||
| with GnmiClient(f"{dpu_ip}:{port}") as client: | ||
| client.get(path="/system/state/...") | ||
| ``` | ||
| Each new RPC is a typed method with protobuf request/response — no new shell commands, no new output formats to parse. | ||
|
|
||
| ## 4. Scope | ||
|
|
||
| ### In scope | ||
| - Vendor Python gRPC stubs for `gnoi.system.System` (Reboot, RebootStatus) | ||
| - Lightweight `GnoiClient` wrapper | ||
| - Refactor the two RPC call sites in `GnoiRebootHandler` | ||
| - Update unit tests | ||
|
|
||
| New directory structure: | ||
| ``` | ||
| host_modules/gnoi/ | ||
| ├── __init__.py | ||
| ├── client.py # GnoiClient: reboot(), reboot_status(), context manager | ||
| ├── system_pb2.py # vendored from openconfig/gnoi system.proto | ||
| ├── system_pb2_grpc.py | ||
| ├── types_pb2.py # dependency of system.proto | ||
| └── types_pb2_grpc.py | ||
| ``` | ||
|
|
||
| The daemon change is essentially replacing `execute_command(["docker", "exec", ...])` with: | ||
| ```python | ||
| with GnoiClient(f"{dpu_ip}:{port}") as client: | ||
| client.reboot(method=REBOOT_METHOD_HALT, ...) | ||
| # ... | ||
| resp = client.reboot_status() | ||
| ``` | ||
|
|
||
| ### Out of scope | ||
| - TLS/mTLS on midplane (future work; midplane is trusted today) | ||
| - Main loop, config DB subscription, halt flag handling — unchanged | ||
| - Other gNOI services beyond System | ||
|
|
||
| ## 5. Why Vendor Stubs? | ||
|
|
||
| sonic-host-services has no proto compilation infra. The gNOI System proto is stable (no changes in years). Vendoring keeps the build simple; can migrate to build-time generation later if more protos are needed. | ||
|
|
||
| ## 6. Risks | ||
|
|
||
| | Risk | Mitigation | | ||
| |------|------------| | ||
| | grpcio/protobuf not in host environment | Already used by other SONiC components | | ||
| | Proto drift from upstream gnoi | Pin to a specific commit; System service is stable | | ||
| | Insecure channel on midplane | Same trust model as today's `-notls`; TLS is future work | |
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 @@ | ||
| # gNOI Python client - vendored proto stubs and client wrapper |
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,48 @@ | ||
| """ | ||
| Lightweight Python gRPC client for gNOI services. | ||
|
|
||
| Manages the gRPC channel and provides access to gNOI service stubs. | ||
| All RPCs are accessed through service properties, e.g.: | ||
|
|
||
| with GnoiClient("10.0.0.1:8080") as client: | ||
| client.system.Reboot(request, timeout=60) | ||
| client.system.RebootStatus(request, timeout=10) | ||
| """ | ||
|
|
||
| import grpc | ||
| from host_modules.gnoi import system_pb2_grpc | ||
|
|
||
|
|
||
| class GnoiClient: | ||
| """gNOI client managing a gRPC channel with access to service stubs.""" | ||
|
|
||
| def __init__(self, target): | ||
| """ | ||
| Args: | ||
| target: gRPC target address, e.g. "10.0.0.1:8080" | ||
| """ | ||
| self._target = target | ||
| self._channel = None | ||
|
|
||
| def __enter__(self): | ||
| self._channel = grpc.insecure_channel(self._target) | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| self.close() | ||
| return False | ||
|
|
||
| def close(self): | ||
| if self._channel: | ||
| self._channel.close() | ||
| self._channel = None | ||
|
|
||
| @property | ||
| def channel(self): | ||
| """Access the underlying gRPC channel for custom stubs.""" | ||
| return self._channel | ||
|
|
||
| @property | ||
| def system(self): | ||
| """gNOI System service stub (gnoi.system.System).""" | ||
| return system_pb2_grpc.SystemStub(self._channel) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
| # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! | ||
| """Client and server classes corresponding to protobuf-defined services.""" | ||
| import grpc | ||
| import warnings | ||
|
|
||
|
|
||
| GRPC_GENERATED_VERSION = '1.80.0' | ||
| GRPC_VERSION = grpc.__version__ | ||
| _version_not_supported = False | ||
|
|
||
| try: | ||
| from grpc._utilities import first_version_is_lower | ||
| _version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION) | ||
| except ImportError: | ||
| _version_not_supported = True | ||
|
|
||
| if _version_not_supported: | ||
| raise RuntimeError( | ||
| f'The grpc package installed is at version {GRPC_VERSION},' | ||
| + ' but the generated code in github.com/openconfig/gnoi/common/common_pb2_grpc.py depends on' | ||
| + f' grpcio>={GRPC_GENERATED_VERSION}.' | ||
| + f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}' | ||
| + f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.' | ||
| ) |
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.
We might have other gnoi services. Should not pin it to System.
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.
Good call — refactored in 5b12fba.
GnoiClientnow manages just the gRPC channel and exposes service stubs via properties (client.system). Future gNOI services (Healthz, Cert, etc.) can add their own@propertyor useclient.channeldirectly for custom stubs.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.
Call should be routed from through service. client.system.reboot instead of client.reboot.
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.
Updated in 9fa8000 — removed all convenience methods. Callers now go through
client.system.Reboot()/client.system.RebootStatus()directly. GnoiClient just manages the channel + exposes service stubs via properties.