|
9 | 9 | logger = logging.getLogger(__name__) |
10 | 10 |
|
11 | 11 |
|
| 12 | +def get_pod_details(namespace='drdroid'): |
| 13 | + # Import kubernetes here to avoid loading at module import time |
| 14 | + from kubernetes import client, config |
| 15 | + |
| 16 | + try: |
| 17 | + # Try to load in-cluster config first, then local config |
| 18 | + try: |
| 19 | + config.load_incluster_config() |
| 20 | + except config.ConfigException: |
| 21 | + config.load_kube_config() |
| 22 | + |
| 23 | + v1 = client.CoreV1Api() |
| 24 | + |
| 25 | + # Get all pods in the namespace |
| 26 | + pods = v1.list_namespaced_pod(namespace) |
| 27 | + events = v1.list_namespaced_event(namespace) |
| 28 | + |
| 29 | + pod_data = [] |
| 30 | + |
| 31 | + for pod in pods.items: |
| 32 | + # Extract container statuses |
| 33 | + container_statuses = [] |
| 34 | + if pod.status.container_statuses: |
| 35 | + for container in pod.status.container_statuses: |
| 36 | + state = "unknown" |
| 37 | + if container.state.running: |
| 38 | + state = "running" |
| 39 | + elif container.state.terminated: |
| 40 | + state = "terminated" |
| 41 | + elif container.state.waiting: |
| 42 | + state = "waiting" |
| 43 | + |
| 44 | + last_state = "unknown" |
| 45 | + if container.last_state.running: |
| 46 | + last_state = "running" |
| 47 | + elif container.last_state.terminated: |
| 48 | + last_state = "terminated" |
| 49 | + elif container.last_state.waiting: |
| 50 | + last_state = "waiting" |
| 51 | + |
| 52 | + container_statuses.append({ |
| 53 | + "name": container.name, |
| 54 | + "state": state, |
| 55 | + "last_state": last_state, |
| 56 | + "ready": container.ready, |
| 57 | + "restart_count": container.restart_count |
| 58 | + }) |
| 59 | + |
| 60 | + # Extract events for this pod |
| 61 | + pod_events = [] |
| 62 | + for event in events.items: |
| 63 | + if event.involved_object.kind == 'Pod' and event.involved_object.name == pod.metadata.name: |
| 64 | + pod_events.append({ |
| 65 | + "type": event.type, |
| 66 | + "reason": event.reason, |
| 67 | + "message": event.message, |
| 68 | + "count": event.count, |
| 69 | + "last_timestamp": event.last_timestamp.isoformat() if event.last_timestamp else None |
| 70 | + }) |
| 71 | + |
| 72 | + pod_data.append({ |
| 73 | + "name": pod.metadata.name, |
| 74 | + "status": pod.status.phase, |
| 75 | + "containers": container_statuses, |
| 76 | + "events": pod_events |
| 77 | + }) |
| 78 | + |
| 79 | + return pod_data |
| 80 | + |
| 81 | + except Exception as e: |
| 82 | + logger.error(f"Error fetching pod details: {e}") |
| 83 | + return [] |
| 84 | + |
| 85 | + |
12 | 86 | @shared_task(max_retries=3, default_retry_delay=10) |
13 | 87 | def send_ping_to_drd_cloud(): |
14 | 88 | drd_cloud_host = settings.DRD_CLOUD_API_HOST |
15 | 89 | drd_cloud_api_token = settings.DRD_CLOUD_API_TOKEN |
16 | 90 | commit_hash = settings.VPC_AGENT_COMMIT_HASH |
17 | 91 | current_epoch = current_epoch_timestamp() |
18 | 92 |
|
| 93 | + # Get pod details |
| 94 | + pod_details = get_pod_details() |
| 95 | + |
19 | 96 | # Establish reachability with DRD Cloud |
20 | | - response = requests.get(f'{drd_cloud_host}/connectors/proxy/ping', |
| 97 | + payload = { |
| 98 | + 'commit_hash': commit_hash, |
| 99 | + 'pods': pod_details |
| 100 | + } |
| 101 | + |
| 102 | + response = requests.post(f'{drd_cloud_host}/connectors/proxy/ping', |
21 | 103 | headers={'Authorization': f'Bearer {drd_cloud_api_token}'}, |
22 | | - params={'commit_hash': commit_hash}) |
| 104 | + json=payload) |
23 | 105 |
|
24 | 106 | if response.status_code != 200: |
25 | 107 | logger.error(f'Failed to connect to DRD Cloud at {current_epoch} with code: {response.status_code} ' |
|
0 commit comments