Ë
    eQêj$  ã                   ó,   — d Z dZdZd„ Zd„ Zdd„Zdd„Zy)	zÎ
Metrics utilities for Kubernetes resource monitoring.

Provides helpers for fetching and processing resource usage data from the
metrics.k8s.io API endpoint, enabling monitoring and autoscaling workflows.
zmetrics.k8s.ioÚv1beta1c                 ó   — ddl m}  || «      S )Né    )ÚCustomObjectsApi)Ú(kubernetes.client.api.custom_objects_apir   )Ú
api_clientr   s     ú]/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/kubernetes/utils/metrics.pyÚ_custom_objects_apir	      s   € ÝIá˜JÓ'Ð'ó    c                 óP   — t        | «      }|j                  t        t        d¬«      S )aM  
    Fetch current resource usage for all cluster nodes.
    
    Retrieves CPU and memory consumption metrics from the metrics-server
    for every node in the cluster.
    
    Parameters:
        api_client: An initialized kubernetes.client.ApiClient instance
        
    Returns:
        A dictionary containing the metrics response with structure:
        {
            'kind': 'NodeMetricsList',
            'apiVersion': 'metrics.k8s.io/v1beta1',
            'metadata': {...},
            'items': [
                {
                    'metadata': {'name': 'node-1', ...},
                    'timestamp': '2024-01-01T00:00:00Z',
                    'window': '30s',
                    'usage': {'cpu': '100m', 'memory': '1024Mi'}
                },
                ...
            ]
        }
        
    Raises:
        ApiException: If the metrics server is not available or request fails
        
    Example:
        >>> from kubernetes import client, config
        >>> config.load_kube_config()
        >>> api_client = client.ApiClient()
        >>> metrics = get_nodes_metrics(api_client)
        >>> for node in metrics['items']:
        ...     name = node['metadata']['name']
        ...     cpu = node['usage']['cpu']
        ...     mem = node['usage']['memory']
        ...     print(f"Node {name}: CPU={cpu}, Memory={mem}")
    Únodes)ÚgroupÚversionÚplural)r	   Úlist_cluster_custom_objectÚMETRICS_API_GROUPÚMETRICS_API_VERSION)r   Úapis     r   Úget_nodes_metricsr       s0   € ôR ˜jÓ
)€CØ×)Ñ)ÜÜ#Øð *ó ð r
   Nc                 ó‚   — |st        d«      ‚t        | «      }t        t        |ddœ}|r||d<    |j                  di |¤ŽS )a�  
    Fetch current resource usage for pods in a namespace.
    
    Retrieves CPU and memory consumption metrics from the metrics-server
    for pods in the specified namespace, with optional label filtering.
    
    Parameters:
        api_client: An initialized kubernetes.client.ApiClient instance
        namespace: The namespace name to query (required)
        label_selector: Optional label query to filter pods (e.g., 'app=web,env=prod')
        
    Returns:
        A dictionary containing the metrics response with structure:
        {
            'kind': 'PodMetricsList',
            'apiVersion': 'metrics.k8s.io/v1beta1',
            'metadata': {...},
            'items': [
                {
                    'metadata': {'name': 'pod-1', 'namespace': 'default', ...},
                    'timestamp': '2024-01-01T00:00:00Z',
                    'window': '30s',
                    'containers': [
                        {
                            'name': 'container-1',
                            'usage': {'cpu': '50m', 'memory': '512Mi'}
                        },
                        ...
                    ]
                },
                ...
            ]
        }
        
    Raises:
        ValueError: If namespace is None or empty
        ApiException: If the metrics server is not available or request fails
        
    Example:
        >>> from kubernetes import client, config
        >>> config.load_kube_config()
        >>> api_client = client.ApiClient()
        >>> 
        >>> # Get all pods in namespace
        >>> metrics = get_pods_metrics(api_client, 'default')
        >>> 
        >>> # Get pods with specific labels
        >>> metrics = get_pods_metrics(api_client, 'default', 'app=nginx')
        >>> 
        >>> for pod in metrics['items']:
        ...     pod_name = pod['metadata']['name']
        ...     print(f"Pod: {pod_name}")
        ...     for container in pod['containers']:
        ...         cname = container['name']
        ...         cpu = container['usage']['cpu']
        ...         mem = container['usage']['memory']
        ...         print(f"  Container {cname}: CPU={cpu}, Memory={mem}")
    z3namespace parameter is required and cannot be emptyÚpods)r   r   Ú	namespacer   Úlabel_selector© )Ú
ValueErrorr	   r   r   Úlist_namespaced_custom_object)r   r   r   r   Úkwargss        r   Úget_pods_metricsr   Q   sY   € ñv ÜÐNÓOÐOä
˜jÓ
)€Cô #Ü&ØØñ	€Fñ Ø#1ˆÐÑ à,ˆ3×,Ñ,Ñ6¨vÑ6Ð6r
   c                 óˆ   — i }|D ]  }	 t        | ||«      ||<   Œ |S # t        $ r}dt        |«      dœ||<   Y d}~Œ7d}~ww xY w)aÈ  
    Fetch pod metrics across multiple namespaces.
    
    Queries pod metrics in each specified namespace and returns an aggregated
    result. If a namespace query fails, the error is captured in the result
    rather than raising an exception.
    
    Parameters:
        api_client: An initialized kubernetes.client.ApiClient instance
        namespaces: A list of namespace names to query
        label_selector: Optional label query applied to all namespaces
        
    Returns:
        A dictionary mapping namespace names to their metrics or error info:
        {
            'namespace-1': {
                'items': [...],
                'kind': 'PodMetricsList',
                ...
            },
            'namespace-2': {
                'error': 'error message',
                'kind': 'Error'
            },
            ...
        }
        
    Example:
        >>> from kubernetes import client, config
        >>> config.load_kube_config()
        >>> api_client = client.ApiClient()
        >>> 
        >>> namespaces = ['default', 'kube-system', 'monitoring']
        >>> all_metrics = get_pods_metrics_in_all_namespaces(api_client, namespaces)
        >>> 
        >>> for ns, result in all_metrics.items():
        ...     if 'error' in result:
        ...         print(f"{ns}: ERROR - {result['error']}")
        ...     else:
        ...         pod_count = len(result.get('items', []))
        ...         print(f"{ns}: {pod_count} pods")
    ÚError)ÚkindÚerrorN)r   Ú	ExceptionÚstr)r   Ú
namespacesr   ÚresultsÚnsÚes         r   Ú"get_pods_metrics_in_all_namespacesr(   ž   s^   € ðV €Gàò ˆð	Ü*¨:°r¸>ÓJˆG�BŠKðð €Nøô ò 	àÜ˜Q›ñˆG�BŽKûð	ús   ‰�	A¦<¼A)N)Ú__doc__r   r   r	   r   r   r(   r   r
   r   ú<module>r*      s/   ðñð %Ð ØÐ ò(ò.óbJ7ôZ6r
   