From 016082a3bf9052c041ca334423dee238486e0e65 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Wed, 8 Nov 2023 17:31:38 -0700 Subject: [PATCH] Add env var support for k8s --- requirements.txt | 1 + stack_orchestrator/deploy/deploy_types.py | 7 ++++++- stack_orchestrator/deploy/k8s/cluster_info.py | 9 ++++++++- stack_orchestrator/deploy/k8s/deploy_k8s.py | 2 +- stack_orchestrator/deploy/k8s/helpers.py | 14 +++++++++++++- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index bf4845a1..bbf97b4a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ python-decouple>=3.8 +python-dotenv==1.0.0 GitPython>=3.1.32 tqdm>=4.65.0 python-on-whales>=0.64.0 diff --git a/stack_orchestrator/deploy/deploy_types.py b/stack_orchestrator/deploy/deploy_types.py index fd14e90e..f97b2649 100644 --- a/stack_orchestrator/deploy/deploy_types.py +++ b/stack_orchestrator/deploy/deploy_types.py @@ -13,7 +13,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import List +from typing import List, Mapping from dataclasses import dataclass from stack_orchestrator.command_types import CommandOptions from stack_orchestrator.deploy.deployer import Deployer @@ -59,3 +59,8 @@ class LaconicStackSetupCommand: @dataclass class LaconicStackCreateCommand: network_dir: str + + +@dataclass +class DeployEnvVars: + map: Mapping[str, str] diff --git a/stack_orchestrator/deploy/k8s/cluster_info.py b/stack_orchestrator/deploy/k8s/cluster_info.py index deb0859d..9275db2b 100644 --- a/stack_orchestrator/deploy/k8s/cluster_info.py +++ b/stack_orchestrator/deploy/k8s/cluster_info.py @@ -19,6 +19,8 @@ from typing import Any, List, Set from stack_orchestrator.opts import opts from stack_orchestrator.deploy.k8s.helpers import named_volumes_from_pod_files, volume_mounts_for_service, volumes_for_pod_files from stack_orchestrator.deploy.k8s.helpers import parsed_pod_files_map_from_file_names, get_node_pv_mount_path +from stack_orchestrator.deploy.k8s.helpers import env_var_map_from_file, envs_from_environment_variables_map +from stack_orchestrator.deploy.deploy_types import DeployEnvVars class ClusterInfo: @@ -26,11 +28,12 @@ class ClusterInfo: image_set: Set[str] = set() app_name: str = "test-app" deployment_name: str = "test-deployment" + environment_variables: DeployEnvVars def __init__(self) -> None: pass - def int_from_pod_files(self, pod_files: List[str]): + def int(self, pod_files: List[str], compose_env_file): 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: @@ -42,6 +45,9 @@ class ClusterInfo: self.image_set.add(image) if opts.o.debug: print(f"image_set: {self.image_set}") + self.environment_variables = DeployEnvVars(env_var_map_from_file(compose_env_file)) + if (opts.o.debug): + print(f"Env vars: {self.environment_variables.map}") def get_pvcs(self): result = [] @@ -97,6 +103,7 @@ class ClusterInfo: container = client.V1Container( name=container_name, image=image, + env=envs_from_environment_variables_map(self.environment_variables.map), ports=[client.V1ContainerPort(container_port=80)], volume_mounts=volume_mounts, resources=client.V1ResourceRequirements( diff --git a/stack_orchestrator/deploy/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index 5181e163..bc256b6b 100644 --- a/stack_orchestrator/deploy/k8s/deploy_k8s.py +++ b/stack_orchestrator/deploy/k8s/deploy_k8s.py @@ -41,7 +41,7 @@ class K8sDeployer(Deployer): self.deployment_dir = deployment_dir self.kind_cluster_name = compose_project_name self.cluster_info = ClusterInfo() - self.cluster_info.int_from_pod_files(compose_files) + self.cluster_info.int(compose_files, compose_env_file) def connect_api(self): config.load_kube_config(context=f"kind-{self.kind_cluster_name}") diff --git a/stack_orchestrator/deploy/k8s/helpers.py b/stack_orchestrator/deploy/k8s/helpers.py index ad48957b..db1ef075 100644 --- a/stack_orchestrator/deploy/k8s/helpers.py +++ b/stack_orchestrator/deploy/k8s/helpers.py @@ -14,10 +14,11 @@ # along with this program. If not, see . from kubernetes import client +from dotenv import dotenv_values import os from pathlib import Path import subprocess -from typing import Any, Set +from typing import Any, Set, Mapping, List from stack_orchestrator.opts import opts from stack_orchestrator.util import get_yaml @@ -194,6 +195,13 @@ def _generate_kind_port_mappings(parsed_pod_files): ) +def envs_from_environment_variables_map(map: Mapping[str, str]) -> List[client.V1EnvVar]: + result = [] + for env_var, env_val in map.items(): + result.append(client.V1EnvVar(env_var, env_val)) + return result + + # This needs to know: # The service ports for the cluster # The bind mounted volumes for the cluster @@ -227,3 +235,7 @@ def generate_kind_config(deployment_dir: Path): f"{port_mappings_yml}\n" f"{mounts_yml}\n" ) + + +def env_var_map_from_file(file: Path) -> Mapping[str, str]: + return dotenv_values(file)