Add deployment

This commit is contained in:
David Boreham 2023-10-26 06:03:20 -06:00
parent c1ffe28d14
commit d5e188c4d2
2 changed files with 42 additions and 0 deletions

View File

@ -13,6 +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 kubernetes import client
from typing import Any, List, Set from typing import Any, List, Set
from app.opts import opts from app.opts import opts
@ -43,3 +44,34 @@ 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}")
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

View File

@ -39,6 +39,7 @@ class K8sDeployer(Deployer):
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}")
self.k8s_client = client.CoreV1Api() self.k8s_client = client.CoreV1Api()
self.k8s_api = client.AppsV1Api()
def up(self, detach, services): def up(self, detach, services):
# Create the kind cluster # Create the kind cluster
@ -47,7 +48,16 @@ class K8sDeployer(Deployer):
# Ensure the referenced containers are copied into kind # Ensure the referenced containers are copied into kind
load_images_into_kind(self.kind_cluster_name, self.cluster_info.image_set) load_images_into_kind(self.kind_cluster_name, self.cluster_info.image_set)
# Process compose files into a Deployment # Process compose files into a Deployment
deployment = self.cluster_info.get_deployment()
# Create the k8s objects # 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): def down(self, timeout, volumes):
# Delete the k8s objects # Delete the k8s objects