|
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +import json |
| 4 | +import platform |
| 5 | +import requests |
| 6 | +import os |
| 7 | +from crmetrics import CRBase |
| 8 | + |
| 9 | +class CRLogs(CRBase): |
| 10 | + |
| 11 | + def _get_container_logs(self, pod, namespace, containers, kubeconfig): |
| 12 | + container_logs = [] |
| 13 | + for c in containers: |
| 14 | + container = c['name'] |
| 15 | + cmd = 'kubectl logs ' + pod + ' -n ' + namespace + ' -c ' + container + ' ' + kubeconfig |
| 16 | + container_logs.append("======== Pod::" + pod + "/container::" + container + " ===========") |
| 17 | + try: |
| 18 | + out = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 19 | + stderr=subprocess.PIPE, shell=True).communicate()[0] |
| 20 | + if out: |
| 21 | + container_logs.append(str(out)) |
| 22 | + container_logs.append("================================================\n\n") |
| 23 | + except Exception as e: |
| 24 | + container_logs.append(str(e)) |
| 25 | + |
| 26 | + return "\n".join(container_logs) |
| 27 | + |
| 28 | + def get_logs(self, pod, namespace, kubeconfig): |
| 29 | + cmd = 'kubectl get pods ' + pod + ' -n ' + namespace + ' -o json ' + kubeconfig |
| 30 | + joined_logs = [] |
| 31 | + try: |
| 32 | + out = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 33 | + stderr=subprocess.PIPE, shell=True).communicate()[0] |
| 34 | + |
| 35 | + if out: |
| 36 | + json_output = json.loads(out) |
| 37 | + containers = json_output['spec']['containers'] |
| 38 | + joined_logs.append(self._get_container_logs(pod, namespace, containers, kubeconfig)) |
| 39 | + |
| 40 | + if 'initContainers' in json_output['spec']: |
| 41 | + init_containers = json_output['spec']['initContainers'] |
| 42 | + joined_logs.append(self._get_container_logs(pod, namespace, init_containers, kubeconfig)) |
| 43 | + |
| 44 | + except Exception as e: |
| 45 | + joined_logs.append(str(e)) |
| 46 | + |
| 47 | + return "\n".join(joined_logs) |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + crLogs = CRLogs() |
| 51 | + kind = sys.argv[1] |
| 52 | + instance = sys.argv[2] |
| 53 | + kubeconfig = sys.argv[3] |
| 54 | + resources = {} |
| 55 | + |
| 56 | + joined_logs = [] |
| 57 | + pods = crLogs.get_pods_in_ns(kind, instance, kubeconfig) |
| 58 | + for pod in pods: |
| 59 | + pod_name = pod['Name'] |
| 60 | + pod_namespace = pod['Namespace'] |
| 61 | + joined_logs.append(crLogs.get_logs(pod_name, pod_namespace, kubeconfig)) |
| 62 | + joined_logs.append("---------------------------------------") |
| 63 | + |
| 64 | + all_logs = "\n".join(joined_logs) |
| 65 | + url = "http://localhost:8080/crailogs" |
| 66 | + payload = {"logs": all_logs} |
| 67 | + |
| 68 | + try: |
| 69 | + response = requests.post(url, json=payload) |
| 70 | + response.raise_for_status() |
| 71 | + result = response.json() |
| 72 | + if 'output' in result: |
| 73 | + print(json.dumps(result['output'], indent=2)) |
| 74 | + except requests.exceptions.RequestException as e: |
| 75 | + print(f"Error communicating with model service: {e}") |
| 76 | + except ValueError: |
| 77 | + print(f"Response was not valid JSON: {response.text}") |
0 commit comments