Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env var support for k8s #634

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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 requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion stack_orchestrator/deploy/deploy_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# 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/>.

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
Expand Down Expand Up @@ -59,3 +59,8 @@ class LaconicStackSetupCommand:
@dataclass
class LaconicStackCreateCommand:
network_dir: str


@dataclass
class DeployEnvVars:
map: Mapping[str, str]
9 changes: 8 additions & 1 deletion stack_orchestrator/deploy/k8s/cluster_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@
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:
parsed_pod_yaml_map: Any = {}
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:
Expand All @@ -42,6 +45,9 @@ def int_from_pod_files(self, pod_files: List[str]):
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 = []
Expand Down Expand Up @@ -97,6 +103,7 @@ def get_deployment(self):
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(
Expand Down
2 changes: 1 addition & 1 deletion stack_orchestrator/deploy/k8s/deploy_k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, deployment_dir, compose_files, compose_project_name, compose_
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}")
Expand Down
14 changes: 13 additions & 1 deletion stack_orchestrator/deploy/k8s/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
# along with this program. If not, see <http:#www.gnu.org/licenses/>.

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)