From 8e73894cee500298b4e64f402d0913a42b926939 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Fri, 3 Nov 2023 19:11:49 -0600 Subject: [PATCH] Add ports and bind mounts --- app/deploy/k8s/cluster_info.py | 9 +-- app/deploy/k8s/helpers.py | 100 ++++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 16 deletions(-) diff --git a/app/deploy/k8s/cluster_info.py b/app/deploy/k8s/cluster_info.py index dd6df456..dfb1ef53 100644 --- a/app/deploy/k8s/cluster_info.py +++ b/app/deploy/k8s/cluster_info.py @@ -17,8 +17,8 @@ from kubernetes import client from typing import Any, List, Set from app.opts import opts -from app.util import get_yaml from app.deploy.k8s.helpers import named_volumes_from_pod_files, volume_mounts_for_service, volumes_for_pod_files +from app.deploy.k8s.helpers import parsed_pod_files_map_from_file_names class ClusterInfo: @@ -31,12 +31,7 @@ class ClusterInfo: pass def int_from_pod_files(self, pod_files: List[str]): - for pod_file in pod_files: - with open(pod_file, "r") as pod_file_descriptor: - parsed_pod_file = get_yaml().load(pod_file_descriptor) - self.parsed_pod_yaml_map[pod_file] = parsed_pod_file - if opts.o.debug: - print(f"parsed_pod_yaml_map: {self.parsed_pod_yaml_map}") + self.parsed_pod_yaml_map = parsed_pod_files_map_from_file_names(pod_files) # Find the set of images in the pods for pod_name in self.parsed_pod_yaml_map: pod = self.parsed_pod_yaml_map[pod_name] diff --git a/app/deploy/k8s/helpers.py b/app/deploy/k8s/helpers.py index d6bca156..ccd32b3c 100644 --- a/app/deploy/k8s/helpers.py +++ b/app/deploy/k8s/helpers.py @@ -14,10 +14,12 @@ # along with this program. If not, see . from kubernetes import client +from pathlib import Path import subprocess -from typing import Set +from typing import Any, Set from app.opts import opts +from app.util import get_yaml def _run_command(command: str): @@ -28,8 +30,8 @@ def _run_command(command: str): print(f"Result: {result}") -def create_cluster(name: str): - _run_command(f"kind create cluster --name {name}") +def create_cluster(name: str, config_file: str): + _run_command(f"kind create cluster --name {name} --config {config_file}") def destroy_cluster(name: str): @@ -104,11 +106,91 @@ def volumes_for_pod_files(parsed_pod_files): return result -def generate_kind_config(): +def parsed_pod_files_map_from_file_names(pod_files): + parsed_pod_yaml_map : Any = {} + for pod_file in pod_files: + with open(pod_file, "r") as pod_file_descriptor: + parsed_pod_file = get_yaml().load(pod_file_descriptor) + parsed_pod_yaml_map[pod_file] = parsed_pod_file + if opts.o.debug: + print(f"parsed_pod_yaml_map: {parsed_pod_yaml_map}") + return parsed_pod_yaml_map + + +def _generate_kind_mounts(parsed_pod_files): + volume_definitions = [] + for pod in parsed_pod_files: + parsed_pod_file = parsed_pod_files[pod] + if "services" in parsed_pod_file: + services = parsed_pod_file["services"] + for service_name in services: + service_obj = services[service_name] + if "volumes" in service_obj: + volumes = service_obj["volumes"] + for mount_string in volumes: + # Looks like: test-data:/data + (volume_name, mount_path) = mount_string.split(":") + volume_definitions.append(f"- hostPath: \n containerPath:{mount_path}") return ( - '''kind: Cluster -apiVersion: kind.x-k8s.io/v1alpha4 -nodes: -- role: control-plane -''' + "" if len(volume_definitions) == 0 else ( + "extraMounts:\n" + f"{''.join(volume_definitions)}" + ) + ) + + +def _generate_kind_port_mappings(parsed_pod_files): + port_definitions = [] + for pod in parsed_pod_files: + parsed_pod_file = parsed_pod_files[pod] + if "services" in parsed_pod_file: + services = parsed_pod_file["services"] + for service_name in services: + service_obj = services[service_name] + if "ports" in service_obj: + ports = service_obj["ports"] + for port_string in ports: + # TODO handle the complex cases + # Looks like: 80 or something more complicated + port_definitions.append(f"- containerPort: {port_string}\n hostPort:{port_string}") + return ( + "" if len(port_definitions) == 0 else ( + "extraPortMappings:\n" + f"{''.join(port_definitions)}" + ) + ) + + +# This needs to know: +# The service ports for the cluster +# The bind mounted volumes for the cluster +# +# Make ports like this: +# extraPortMappings: +# - containerPort: 80 +# hostPort: 80 +# # optional: set the bind address on the host +# # 0.0.0.0 is the current default +# listenAddress: "127.0.0.1" +# # optional: set the protocol to one of TCP, UDP, SCTP. +# # TCP is the default +# protocol: TCP +# Make bind mounts like this: +# extraMounts: +# - hostPath: /path/to/my/files +# containerPath: /files +def generate_kind_config(deployment_dir: Path): + compose_file_dir = deployment_dir.joinpath("compose") + # TODO: this should come from the stack file, not this way + pod_files = [p for p in compose_file_dir.iterdir() if p.is_file()] + parsed_pod_files_map = parsed_pod_files_map_from_file_names(pod_files) + port_mappings_yml = _generate_kind_port_mappings(parsed_pod_files_map) + mounts_yml = _generate_kind_mounts(parsed_pod_files_map) + return ( + "kind: Cluster\n" + "apiVersion: kind.x-k8s.io/v1alpha4\n" + "nodes:\n" + " - role: control-plane\n" + f"{port_mappings_yml}\n" + f"{mounts_yml}\n" )