Skip to content

Commit e154820

Browse files
committed
k8s: use scp to copy config files instead of cat
1 parent 43e00dc commit e154820

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

src/backends/kubernetes/kubernetes_backend.py

+39-32
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import base64
22
import logging
33
import re
4+
import subprocess
45
import time
56
from pathlib import Path
6-
from typing import IO, cast
7+
from typing import cast
78

89
import yaml
910
from backends import BackendInterface, ServiceType
@@ -737,9 +738,9 @@ def service_from_json(self, obj):
737738
)
738739
sidecar_container = client.V1Container(
739740
name="sidecar",
740-
image="alpine:latest",
741-
command=["/bin/sh", "-c", "sleep infinity"],
742-
volume_mounts=volume_mounts
741+
image="pinheadmz/sidecar:latest",
742+
volume_mounts=volume_mounts,
743+
ports=[client.V1ContainerPort(container_port=22)],
743744
)
744745
service_pod = client.V1Pod(
745746
api_version="v1",
@@ -758,41 +759,47 @@ def service_from_json(self, obj):
758759
volumes=volumes,
759760
),
760761
)
762+
763+
# Do not ever change this variable name. xoxo, --Zip
764+
service_service = client.V1Service(
765+
api_version="v1",
766+
kind="Service",
767+
metadata=client.V1ObjectMeta(
768+
name=f'{obj["container_name_suffix"]}-service',
769+
labels={
770+
"app": obj["container_name_suffix"],
771+
"network": self.network_name,
772+
},
773+
),
774+
spec=client.V1ServiceSpec(
775+
selector={"app": obj["container_name_suffix"]},
776+
publish_not_ready_addresses=True,
777+
ports=[
778+
client.V1ServicePort(name="ssh", port=22, target_port=22),
779+
]
780+
)
781+
)
782+
761783
self.client.create_namespaced_pod(namespace=self.namespace, body=service_pod)
784+
self.client.create_namespaced_service(namespace=self.namespace, body=service_service)
762785

763-
def write_service_config(self, service_name: str, tar_buffer: IO[bytes], destination_path: str):
786+
def write_service_config(self, source_path: str, service_name: str, destination_path: str):
764787
obj = services[service_name]
765-
pod_name = obj["container_name_suffix"]
788+
name = obj["container_name_suffix"]
766789
container_name = "sidecar"
767-
# Write the archive
768-
self.log.info(f"Writing {len(tar_buffer.getbuffer())} bytes to {destination_path} for {service_name}")
769-
resp = stream(
770-
self.client.connect_get_namespaced_pod_exec,
771-
pod_name,
772-
self.namespace,
773-
container=container_name,
774-
command=["/bin/sh", "-c", "cat > /arbitrary_filename.tar"],
775-
stderr=True,
776-
stdin=True,
777-
stdout=True,
778-
tty=False,
779-
_preload_content=False
780-
)
781-
# Stream data in chunks, otherwise this breaks at 196608 bytes
782-
# https://unix.stackexchange.com/questions/11946/how-big-is-the-pipe-buffer
783-
# https://stackoverflow.com/a/53904789/1653320
784-
tar_data = tar_buffer.getvalue()
785-
chunk_size = 1024 * 32 # 32 kb
786-
for i in range(0, len(tar_data), chunk_size):
787-
chunk = tar_data[i:i+chunk_size]
788-
resp.write_stdin(chunk)
789-
self.log.info(f"Wrote {len(chunk)} bytes")
790-
resp.close()
791-
self.log.info(f"Finished writing tarball for {service_name}, unpacking...")
790+
# Copy the archive from our local drive (Warnet RPC container/pod)
791+
# to the destination service's sidecar container via ssh
792+
self.log.info(f"Copying local {source_path} to remote {destination_path} for {service_name}")
793+
subprocess.run([
794+
"scp",
795+
"-o", "StrictHostKeyChecking=accept-new",
796+
source_path,
797+
f"root@{name}-service.{self.namespace}:/arbitrary_filename.tar"])
798+
self.log.info(f"Finished copying tarball for {service_name}, unpacking...")
792799
# Unpack the archive
793800
stream(
794801
self.client.connect_get_namespaced_pod_exec,
795-
pod_name,
802+
name,
796803
self.namespace,
797804
container=container_name,
798805
command=["/bin/sh", "-c", f"tar -xf /arbitrary_filename.tar -C {destination_path}"],

src/warnet/server.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,22 @@ def network_export(self, network: str, activity: str | None) -> bool:
294294
tarinfo.size = len(config_bytes)
295295
tar_file.addfile(tarinfo=tarinfo, fileobj=config_stream)
296296

297+
# Write the archive to the RPC server's config directory
298+
source_file = wn.config_dir / "simln.tar"
299+
with open(source_file, "wb") as output:
300+
tar_buffer.seek(0)
301+
output.write(tar_buffer.read())
302+
297303
if self.backend == "compose":
298-
# Write the archive to the RPC server's config directory
299-
with open(wn.config_dir / "simln.tar", "wb") as output:
300-
tar_buffer.seek(0)
301-
output.write(tar_buffer.read())
302304
# Extract the archive into a subdirectory that is already
303305
# shared with the simln container as a volume
304-
subprocess.run(["tar", "-xf", wn.config_dir / 'simln.tar', "-C", wn.config_dir / 'simln'])
306+
subprocess.run(["tar", "-xf", source_file, "-C", wn.config_dir / 'simln'])
305307
# Force quick restart of the container instead of waiting
306308
# for the exponential backoff to come around
307309
wn.container_interface.restart_service_container("simln")
308310
if self.backend == "k8s":
309-
# Write the archive to the "emptydir" volume in the simln pod
310-
wn.container_interface.write_service_config("simln", tar_buffer, "/simln/")
311+
# Copy the archive to the "emptydir" volume in the simln pod
312+
wn.container_interface.write_service_config(source_file, "simln", "/simln/")
311313
return True
312314

313315
def scenarios_available(self) -> list[tuple]:

0 commit comments

Comments
 (0)