diff --git a/app/deploy/k8s/cluster_info.py b/app/deploy/k8s/cluster_info.py index d78b7bf8..486c5f96 100644 --- a/app/deploy/k8s/cluster_info.py +++ b/app/deploy/k8s/cluster_info.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from kubernetes import client from typing import Any, List, Set from app.opts import opts @@ -43,3 +44,34 @@ class ClusterInfo: self.image_set.add(image) if opts.o.debug: print(f"image_set: {self.image_set}") + + def get_deployment(self): + + container = client.V1Container( + name="container-name", + image="image-tag", + ports=[client.V1ContainerPort(container_port=80)], + resources=client.V1ResourceRequirements( + requests={"cpu": "100m", "memory": "200Mi"}, + limits={"cpu": "500m", "memory": "500Mi"}, + ), + ) + + template = client.V1PodTemplateSpec( + metadata=client.V1ObjectMeta(labels={"app": "app-name"}), + spec=client.V1PodSpec(containers=[container]), + ) + + spec = client.V1DeploymentSpec( + replicas=3, template=template, selector={ + "matchLabels": + {"app": "app-name"}}) + + deployment = client.V1Deployment( + api_version="apps/v1", + kind="Deployment", + metadata=client.V1ObjectMeta(name="deployment-name"), + spec=spec, + ) + + return deployment diff --git a/app/deploy/k8s/deploy_k8s.py b/app/deploy/k8s/deploy_k8s.py index 76219edf..c571dd74 100644 --- a/app/deploy/k8s/deploy_k8s.py +++ b/app/deploy/k8s/deploy_k8s.py @@ -39,6 +39,7 @@ class K8sDeployer(Deployer): def connect_api(self): config.load_kube_config(context=f"kind-{self.kind_cluster_name}") self.k8s_client = client.CoreV1Api() + self.k8s_api = client.AppsV1Api() def up(self, detach, services): # Create the kind cluster @@ -47,7 +48,16 @@ class K8sDeployer(Deployer): # 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 + deployment = self.cluster_info.get_deployment() # Create the k8s objects + resp = self.k8s_api.create_namespaced_deployment( + body=deployment, namespace="default" + ) + + if opts.o.debug: + print("Deployment created.\n") + print(f"{resp.metadata.namespace} {resp.metadata.name} \ + {resp.metadata.generation} {resp.spec.template.spec.containers[0].image}") def down(self, timeout, volumes): # Delete the k8s objects