Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Kubernetes test1 #1910

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions grpc_server/cri-api
Submodule cri-api added at 61d378
1,931 changes: 1,931 additions & 0 deletions grpc_server/test/cri.proto

Large diffs are not rendered by default.

469 changes: 469 additions & 0 deletions grpc_server/test/cri_pb2.py

Large diffs are not rendered by default.

1,632 changes: 1,632 additions & 0 deletions grpc_server/test/cri_pb2_grpc.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions grpc_server/test/test_grpc_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import grpc
import cri_pb2_grpc
import cri_pb2

def run():
with grpc.insecure_channel('localhost:50052') as channel:
stub = cri_pb2_grpc.RuntimeServiceStub(channel)
request = cri_pb2.VersionRequest()
response = stub.Version(request)
print("CRI Version:", response.version)

if __name__ == '__main__':
run()
87 changes: 87 additions & 0 deletions grpc_server/test/test_grpc_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# created from cri.proto file
import cri_pb2_grpc
import cri_pb2

import grpc
from concurrent import futures
import subprocess

# ImageService defines the public APIs for managing images.
class ImageServiceServicer(cri_pb2_grpc.ImageServiceServicer):
# Lists existing images
def ListImages(self, request, context):
print("in List Images") # DEBUG

# creates empty respone
response = cri_pb2.ListImagesResponse()

# calls ch-image list and stores stdout
cmd = "/usr/local/src/charliecloud/bin/ch-image"
ca = [cmd, "list"]
output = subprocess.check_output(ca, stderr=subprocess.STDOUT)

# images as strings
# note: decode changes bytes to string, split converts string to array, and finally remove the empty string
images = output.decode("utf-8").split("\n")[:-1]

# for every image in charliecloud cache:
# create an image object with the name as the ID and append to the repeated field
for img in images:
response.images.append(cri_pb2.Image(id=img)) # note: incomplete... image missing fields

print(response.images) # DEBUG
return response

# ImageFSInfo returns information of the filesystem that is used to store images.
def ImageFsInfo(self, request, context):
print("In Image Fs Info") # DEBUG

# dummy placeholders
filesystem_info = cri_pb2.FilesystemUsage()
response = cri_pb2.ImageFsInfoResponse(image_filesystems=[filesystem_info])

return response



# organise version information to return as protobuf
VERSION_INFO = { "KubeVersion": "v1", # version of the kubelet runtime api
"RuntimeName": "Charliecloud", # name of the container runtime (const)
"RuntimeApiVersion": "v0" } # api version of the container runtime (I do not really know what this is)

# this is an implementation of CRI's RuntimeService:
# Runtime service defines the public APIs for remote container runtimes
class RuntimeServiceServicer(cri_pb2_grpc.RuntimeServiceServicer):
# Version returns the runtime name, runtime version and runtime API version.
def Version(self, request, context):
print("version") # DEBUG
response = cri_pb2.VersionResponse(version=VERSION_INFO["KubeVersion"], runtime_name = VERSION_INFO["RuntimeName"], runtime_api_version=VERSION_INFO["RuntimeApiVersion"])

# this is the Charliecloud command we run to get the ch-run version
cmd = "/usr/local/src/charliecloud/bin/ch-run"
ca = [cmd, "--version"]

# for some reason version response is in STDERR
output = subprocess.check_output(ca, stderr=subprocess.STDOUT)
response.runtime_version = output.rstrip() # note: rstrip removes new line

return response

# dummy start container function
def StartContainer(self, request, context):
print("start container:", request.container_id) # DEBUG
return cri_pb2.StartContainerResponse()


def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
cri_pb2_grpc.add_RuntimeServiceServicer_to_server(RuntimeServiceServicer(), server)
cri_pb2_grpc.add_ImageServiceServicer_to_server(ImageServiceServicer(), server)
server.add_insecure_port(f'unix:///tmp/test.sock')
server.start()
print("Server listening on port 50052...") # DEBUG
server.wait_for_termination()

if __name__ == '__main__':
# start gRPC server
serve()
4 changes: 4 additions & 0 deletions grpc_server/test/test_subprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import subprocess

test = subprocess.check_output(['ch-run', '--version'])
print(test)
Loading