-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for binoculars to python client
Signed-off-by: Clif Houck <me@clifhouck.com>
- Loading branch information
Showing
4 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,45 @@ | ||
import datetime | ||
from typing import Optional | ||
|
||
|
||
from armada_client.armada import ( | ||
binoculars_pb2, | ||
binoculars_pb2_grpc, | ||
) | ||
|
||
from armada_client.k8s.io.api.core.v1 import generated_pb2 as core_v1 | ||
|
||
|
||
class BinocularsClient: | ||
""" | ||
Client for accessing Armada's Binoculars service over gRPC. | ||
:param channel: gRPC channel used for authentication. See | ||
https://grpc.github.io/grpc/python/grpc.html | ||
for more information. | ||
:return: an Binoculars client instance | ||
""" | ||
|
||
def __init__(self, channel): | ||
self.binoculars_stub = binoculars_pb2_grpc.BinocularsStub(channel) | ||
|
||
def logs( | ||
self, | ||
job_id: str, | ||
pod_namespace: str, | ||
since_time: datetime.datetime, | ||
pod_number: Optional[int] = 0, | ||
log_options: Optional[core_v1.PodLogOptions] = None, | ||
): | ||
log_request = binoculars_pb2.LogRequest( | ||
job_id=job_id, | ||
pod_number=pod_number, | ||
pod_namespace=pod_namespace, | ||
since_time=since_time.isoformat(), | ||
log_options=log_options, | ||
) | ||
return self.binoculars_stub.Logs(log_request) | ||
|
||
def cordon(self, node_name: str): | ||
cordon_request = binoculars_pb2.CordonRequest(node_name=node_name) | ||
return self.binoculars_stub.Cordon(cordon_request) |
This file contains 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 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,44 @@ | ||
from concurrent import futures | ||
import datetime | ||
|
||
import grpc | ||
import pytest | ||
|
||
from google.protobuf import empty_pb2 | ||
|
||
from server_mock import BinocularsService | ||
|
||
from armada_client.armada import binoculars_pb2_grpc | ||
from armada_client.binoculars_client import BinocularsClient | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def binoculars_server_mock(): | ||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) | ||
binoculars_pb2_grpc.add_BinocularsServicer_to_server(BinocularsService(), server) | ||
server.add_insecure_port("[::]:4000") | ||
server.start() | ||
|
||
yield | ||
server.stop(False) | ||
|
||
|
||
channel = grpc.insecure_channel(target="127.0.0.1:4000") | ||
tester = BinocularsClient( | ||
grpc.insecure_channel( | ||
target="127.0.0.1:4000", | ||
options={ | ||
"grpc.keepalive_time_ms": 30000, | ||
}.items(), | ||
) | ||
) | ||
|
||
|
||
def test_logs(): | ||
resp = tester.logs("fake-job-id", "fake-namespace", datetime.datetime.now()) | ||
assert len(resp.log) == 3 | ||
|
||
|
||
def test_cordon(): | ||
result = tester.cordon("fake-node-name") | ||
assert result == empty_pb2.Empty() |
This file contains 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