k8s deploy #614

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

View File

@ -13,6 +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 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

View File

@ -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