diff --git a/app/deploy/k8s/cluster_info.py b/app/deploy/k8s/cluster_info.py index c6e588a3..540f5f8c 100644 --- a/app/deploy/k8s/cluster_info.py +++ b/app/deploy/k8s/cluster_info.py @@ -71,7 +71,7 @@ class ClusterInfo: spec=client.V1PodSpec(containers=containers), ) spec = client.V1DeploymentSpec( - replicas=3, template=template, selector={ + replicas=1, template=template, selector={ "matchLabels": {"app": self.app_name}}) diff --git a/app/deploy/k8s/deploy_k8s.py b/app/deploy/k8s/deploy_k8s.py index c571dd74..339329f8 100644 --- a/app/deploy/k8s/deploy_k8s.py +++ b/app/deploy/k8s/deploy_k8s.py @@ -17,6 +17,7 @@ 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.helpers import pods_in_deployment from app.deploy.k8s.cluster_info import ClusterInfo from app.opts import opts @@ -24,6 +25,7 @@ from app.opts import opts class K8sDeployer(Deployer): name: str = "k8s" k8s_client: client + k8s_api: client.AppsV1Api kind_cluster_name: str cluster_info : ClusterInfo @@ -84,8 +86,13 @@ class K8sDeployer(Deployer): pass def logs(self, services, tail, follow, stream): - # Call the API to get logs - pass + self.connect_api() + pods = pods_in_deployment(self.k8s_api, "test-deployment") + if len(pods) > 1: + print("Warning: more than one pod in the deployment") + k8s_pod_name = pods[0] + log_data = self.k8s_api.read_namespaced_pod_log(k8s_pod_name, namespace="default", container="test") + print(log_data) 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 diff --git a/app/deploy/k8s/helpers.py b/app/deploy/k8s/helpers.py index 7a98eefd..7c2fe14b 100644 --- a/app/deploy/k8s/helpers.py +++ b/app/deploy/k8s/helpers.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 import subprocess from typing import Set @@ -38,3 +39,11 @@ def destroy_cluster(name: str): 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}") + + +def pods_in_deployment(api: client.AppsV1Api, deployment_name: str): + # See: https://stackoverflow.com/a/73525759/1701505 + deployment_info = api.read_namespaced_deployment(deployment_name, "default") + if opts.o.debug: + print(f"deployment: {deployment_info}") + return [] \ No newline at end of file