k8s deploy #614

Merged
telackey merged 5 commits from dboreham/k8s-deploy into main 2023-10-27 16:19:44 +00:00
5 changed files with 150 additions and 6 deletions
Showing only changes of commit c1ffe28d14 - Show all commits

View File

@ -0,0 +1,45 @@
# Copyright © 2023 Vulcanize
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# 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 Any, List, Set
from app.opts import opts
from app.util import get_yaml
class ClusterInfo:
parsed_pod_yaml_map: Any = {}
image_set: Set[str] = set()
def __init__(self) -> None:
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}")
# 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]
services = pod["services"]
for service_name in services:
service_info = services[service_name]
image = service_info["image"]
self.image_set.add(image)
if opts.o.debug:
print(f"image_set: {self.image_set}")

View File

@ -14,33 +14,69 @@
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
from kubernetes import client, config
from app.deploy.deployer import Deployer
from app.deploy.k8s.helpers import create_cluster, destroy_cluster, load_images_into_kind
from app.deploy.k8s.cluster_info import ClusterInfo
from app.opts import opts
class K8sDeployer(Deployer):
name: str = "k8s"
k8s_client: client
kind_cluster_name: str
cluster_info : ClusterInfo
def __init__(self, compose_files, compose_project_name, compose_env_file) -> None:
config.load_kube_config()
self.client = client.CoreV1Api()
if (opts.o.debug):
print(f"Compose files: {compose_files}")
print(f"Project name: {compose_project_name}")
print(f"Env file: {compose_env_file}")
self.kind_cluster_name = compose_project_name
self.cluster_info = ClusterInfo()
self.cluster_info.int_from_pod_files(compose_files)
def connect_api(self):
config.load_kube_config(context=f"kind-{self.kind_cluster_name}")
self.k8s_client = client.CoreV1Api()
def up(self, detach, services):
pass
# Create the kind cluster
create_cluster(self.kind_cluster_name)
self.connect_api()
# Ensure the referenced containers are copied into kind
load_images_into_kind(self.kind_cluster_name, self.cluster_info.image_set)
# Process compose files into a Deployment
# Create the k8s objects
def down(self, timeout, volumes):
pass
# Delete the k8s objects
# Destroy the kind cluster
destroy_cluster(self.kind_cluster_name)
def ps(self):
pass
self.connect_api()
# Call whatever API we need to get the running container list
ret = self.k8s_client.list_pod_for_all_namespaces(watch=False)
if ret.items:
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
ret = self.k8s_client.list_node(pretty=True, watch=False)
return []
def port(self, service, private_port):
# Since we handle the port mapping, need to figure out where this comes from
# Also look into whether it makes sense to get ports for k8s
pass
def execute(self, service_name, command, envs):
# Call the API to execute a command in a running container
pass
def logs(self, services, tail, follow, stream):
# Call the API to get logs
pass
def run(self, image, command, user, volumes, entrypoint=None):
# We need to figure out how to do this -- check why we're being called first
pass

40
app/deploy/k8s/helpers.py Normal file
View File

@ -0,0 +1,40 @@
# Copyright © 2023 Vulcanize
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# 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/>.
import subprocess
from typing import Set
from app.opts import opts
def _run_command(command: str):
if opts.o.debug:
print(f"Running: {command}")
result = subprocess.run(command, shell=True)
if opts.o.debug:
print(f"Result: {result}")
def create_cluster(name: str):
_run_command(f"kind create cluster --name {name}")
def destroy_cluster(name: str):
_run_command(f"kind delete cluster --name {name}")
def load_images_into_kind(kind_cluster_name: str, image_set: Set[str]):
for image in image_set:
_run_command(f"kind load docker-image {image} --name {kind_cluster_name}")

20
app/opts.py Normal file
View File

@ -0,0 +1,20 @@
# Copyright © 2023 Vulcanize
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# 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 app.command_types import CommandOptions
class opts:
o: CommandOptions = None

5
cli.py
View File

@ -22,6 +22,7 @@ from app.build import build_npms
from app.deploy import deploy
from app import version
from app.deploy import deployment
from app import opts
from app import update
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@ -39,7 +40,9 @@ CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.pass_context
def cli(ctx, stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error):
"""Laconic Stack Orchestrator"""
ctx.obj = CommandOptions(stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error)
command_options = CommandOptions(stack, quiet, verbose, dry_run, local_stack, debug, continue_on_error)
opts.opts.o = command_options
ctx.obj = command_options
cli.add_command(setup_repositories.command, "setup-repositories")