Add env var support for k8s #634

Merged
telackey merged 1 commits from dboreham/k8s-container-config into main 2023-11-09 00:53:46 +00:00
5 changed files with 29 additions and 4 deletions

View File

@ -1,4 +1,5 @@
python-decouple>=3.8 python-decouple>=3.8
python-dotenv==1.0.0
GitPython>=3.1.32 GitPython>=3.1.32
tqdm>=4.65.0 tqdm>=4.65.0
python-on-whales>=0.64.0 python-on-whales>=0.64.0

View File

@ -13,7 +13,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
from typing import List from typing import List, Mapping
from dataclasses import dataclass from dataclasses import dataclass
from stack_orchestrator.command_types import CommandOptions from stack_orchestrator.command_types import CommandOptions
from stack_orchestrator.deploy.deployer import Deployer from stack_orchestrator.deploy.deployer import Deployer
@ -59,3 +59,8 @@ class LaconicStackSetupCommand:
@dataclass @dataclass
class LaconicStackCreateCommand: class LaconicStackCreateCommand:
network_dir: str network_dir: str
@dataclass
class DeployEnvVars:
map: Mapping[str, str]

View File

@ -19,6 +19,8 @@ from typing import Any, List, Set
from stack_orchestrator.opts import opts 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 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 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: class ClusterInfo:
@ -26,11 +28,12 @@ class ClusterInfo:
image_set: Set[str] = set() image_set: Set[str] = set()
app_name: str = "test-app" app_name: str = "test-app"
deployment_name: str = "test-deployment" deployment_name: str = "test-deployment"
environment_variables: DeployEnvVars
def __init__(self) -> None: def __init__(self) -> None:
pass 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) self.parsed_pod_yaml_map = parsed_pod_files_map_from_file_names(pod_files)
# Find the set of images in the pods # Find the set of images in the pods
for pod_name in self.parsed_pod_yaml_map: for pod_name in self.parsed_pod_yaml_map:
@ -42,6 +45,9 @@ class ClusterInfo:
self.image_set.add(image) self.image_set.add(image)
if opts.o.debug: if opts.o.debug:
print(f"image_set: {self.image_set}") 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): def get_pvcs(self):
result = [] result = []
@ -97,6 +103,7 @@ class ClusterInfo:
container = client.V1Container( container = client.V1Container(
name=container_name, name=container_name,
image=image, image=image,
env=envs_from_environment_variables_map(self.environment_variables.map),
ports=[client.V1ContainerPort(container_port=80)], ports=[client.V1ContainerPort(container_port=80)],
volume_mounts=volume_mounts, volume_mounts=volume_mounts,
resources=client.V1ResourceRequirements( resources=client.V1ResourceRequirements(

View File

@ -41,7 +41,7 @@ class K8sDeployer(Deployer):
self.deployment_dir = deployment_dir self.deployment_dir = deployment_dir
self.kind_cluster_name = compose_project_name self.kind_cluster_name = compose_project_name
self.cluster_info = ClusterInfo() 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): def connect_api(self):
config.load_kube_config(context=f"kind-{self.kind_cluster_name}") config.load_kube_config(context=f"kind-{self.kind_cluster_name}")

View File

@ -14,10 +14,11 @@
# along with this program. If not, see <http:#www.gnu.org/licenses/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
from kubernetes import client from kubernetes import client
from dotenv import dotenv_values
import os import os
from pathlib import Path from pathlib import Path
import subprocess import subprocess
from typing import Any, Set from typing import Any, Set, Mapping, List
from stack_orchestrator.opts import opts from stack_orchestrator.opts import opts
from stack_orchestrator.util import get_yaml 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: # This needs to know:
# The service ports for the cluster # The service ports for the cluster
# The bind mounted volumes 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"{port_mappings_yml}\n"
f"{mounts_yml}\n" f"{mounts_yml}\n"
) )
def env_var_map_from_file(file: Path) -> Mapping[str, str]:
return dotenv_values(file)