
    [j5                         d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
  ej        e          ZdZdZd	Zd
ZdZ G d d          ZdS )zInformer implementation for the Kubernetes Python client.

Provides SharedInformer: a background watcher that keeps a local
ObjectCache in sync with the Kubernetes API server and notifies
registered event-handler callbacks.
    N)ApiException)Watch   )ObjectCache_meta_namespace_keyADDEDMODIFIEDDELETEDBOOKMARKERRORc                   j    e Zd ZdZ	 	 	 	 	 ddZed             Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd ZdS )SharedInformera;  Watch a Kubernetes resource and maintain a local cache.

    The informer starts a daemon thread that continuously watches the
    given resource via ``list_func``.  On each event the local
    :class:`ObjectCache` is updated and registered
    event-handler callbacks are invoked.

    Parameters
    ----------
    list_func:
        Bound API method used for the initial list **and** as the watch
        source.  It must accept a watch keyword argument (e.g.
        CoreV1Api().list_namespaced_pod).
    namespace:
        Kubernetes namespace to watch.  Pass None for cluster-scoped
        or all-namespace list functions.
    resync_period:
        How often (seconds) to perform a full re-list from the API server.
        Defaults to 0 which disables periodic resyncs.
    label_selector:
        Optional label selector string forwarded to the API server.
    field_selector:
        Optional field selector string forwarded to the API server.
    key_func:
        Optional callable (obj) -> str used to key objects in the
        cache.  Defaults to namespace/name.
    Nr   c           
      T   || _         || _        || _        || _        || _        t          |          | _        t          g t          g t          g t          g t          g i| _        t          j                    | _        d | _        d | _        t          j                    | _        d | _        d S )N)key_func)
_list_func
_namespace_resync_period_label_selector_field_selectorr   _cacher   r	   r
   r   r   	_handlers	threadingLock_handler_lock_watch_threadEvent_stop_event_resource_version)self	list_func	namespaceresync_periodlabel_selectorfield_selectorr   s          Y/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/kubernetes/informer/informer.py__init__zSharedInformer.__init__G   s     $#+--!8444Xr7B"eUWX&^--$?,,!%    c                     | j         S )z5The :class:`ObjectCache` maintained by this informer.)r   r    s    r&   cachezSharedInformer.cachec   s     {r(   c           
      (   || j         vrHt          d                    |d                    t	          | j                                                 | j        5  | j         |                             |           ddd           dS # 1 swxY w Y   dS )a_  Register a callback for a specific event type.

        Parameters
        ----------
        event_type:
            One of :data:`ADDED`, :data:`MODIFIED`, :data:`DELETED`,
            :data:`BOOKMARK` or :data:`ERROR`.
        handler:
            Callable invoked with the event object (or the raw exception for
            ERROR events).
        z'Unknown event_type {!r}. Use one of: {}z, N)r   
ValueErrorformatjoinsortedr   appendr    
event_typehandlers      r&   add_event_handlerz SharedInformer.add_event_handlerh   s     T^++9@@		&*@*@ A A   
  	7 	7N:&--g666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   !BBBc                     | j         5  	 | j        |                             |           n# t          t          f$ r Y nw xY wddd           dS # 1 swxY w Y   dS )zeDeregister a previously registered *handler*.

        No-op if *handler* is not registered.
        N)r   r   removeKeyErrorr-   r2   s      r&   remove_event_handlerz#SharedInformer.remove_event_handler}   s    
  	 	z*11'::::j)   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s,   A +A?A?AAAc                     | j         | j                                         rdS | j                                         t	          j        | j        dd          | _         | j                                          dS )zStart the background watch loop in a daemon thread.

        Calling :meth:`start` more than once without an intervening
        :meth:`stop` is a no-op.
        Nr   T)targetnamedaemon)r   is_aliver   clearr   Thread	_run_loopstartr*   s    r&   rB   zSharedInformer.start   sy     <#(=(=(?(?#F    '>!
 
 

 	r(   c                     | j                                          | j        | j                                         | j        | j                                         d| _        dS )z:Ask the background watch loop to stop and join the thread.N)r   setr   stopr   r/   r*   s    r&   rE   zSharedInformer.stop   sZ    ;"K<#Lr(   c                 p    i }| j         
| j         |d<   | j        
| j        |d<   | j        
| j        |d<   |S )Nr"   r$   r%   )r   r   r   )r    kws     r&   _build_kwargszSharedInformer._build_kwargs   sM    ?&"oB{O+#'#7B +#'#7B 	r(   c                    | j         5  t          | j                            |g                     }ddd           n# 1 swxY w Y   |D ]9}	  ||           # t          $ r t
                              d|           Y 6w xY wdS )a  Execute all registered callbacks for *event_type*, passing *obj*.

        Callbacks are invoked sequentially on the informer's background thread.
        Any exception raised by an individual handler is logged and swallowed so
        that remaining handlers still run.
        Nz$Exception in informer handler for %s)r   listr   get	Exceptionlogger	exception)r    r3   objhandlersfns        r&   _firezSharedInformer._fire   s     	@ 	@DN..z2>>??H	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	 	B3     :J    	 	s!   )=AAA%B Bc                    |                                  } | j        di |}t          |dg           pg }i }|D ]!}| j                            |          }|||<   "t          | j                                                  }|D ]=}||vr7| j                            |          }||                     t          |           >| j        
                    |           |                                D ]@\  }}||v r|                     t          |           %|                     t          |           Ad}	t          |dd          }
|
t          |
dd          }	|	pd| _        dS )a  List all objects and populate the cache, firing ADDED/MODIFIED/DELETED events.

        On the first call (empty cache) every returned item fires ADDED.
        On subsequent calls (resync or after a 410 Gone) the new list is
        diffed against the existing cache:
        * Items absent from the new list fire DELETED.
        * Items present in both fire MODIFIED.
        * Items only in the new list fire ADDED.
        itemsNmetadataresource_version0 )rH   r   getattrr   	_key_funcrD   	list_keys
get_by_keyrR   r
   _replace_allrT   r	   r   r   )r    rG   resprT   new_items_mapitemkeyold_keysold_objrvmetas              r&   _initial_listzSharedInformer._initial_list   s    !!t$$$$gr**0b  	& 	&D+''--C!%M# t{,,..//  	1 	1C-''+0055&JJw000 	  ''' ',,.. 	( 	(ICh

8T****

5$''''tZ..1488B!#sr(   c                 	   | j                                         s| j        }	 |                                  ng# t          $ rZ}t
                              d           |                     t          |           | j         	                    d           Y d}~d}~ww xY wt          j                    }t                      | _        |                                 }| j        |d<   | j        dk    r%t!          dt#          | j                            |d<   	  | j        j        | j        fi |D ]}| j                                         r nv|                    d	          }|                    d
          }| j        | j        j        r| j        j        | _        |t,          k    r6| j                            |           |                     t,          |           |t2          k    r6| j                            |           |                     t2          |           |t4          k    r7| j                            |           |                     t4          |           1|t8          k    r1|                     t8          |                    d|                     m|t          k    r|                     t          |           n# t:          $ rr}|j        dk    r"t
                              d           d| _        n t
                              d|j                   |                     t          |           Y d}~nOd}~wt          $ r?}t
                              d           |                     t          |           Y d}~nd}~ww xY w| j        $| j        | j        j        r| j        j        | _        d| _        n7# | j        $| j        | j        j        r| j        j        | _        d| _        w xY w| j                                         s| j        | j        dk    rt          j                    |z
  | j        k    r|t
                               d           	 |                                  nL# t          $ r?}t
                              d           |                     t          |           Y d}~nd}~ww xY w| j                                         dS dS )a  Background loop: list then watch, reconnect on errors.

        A full re-list is only performed when ``self._resource_version`` is
        ``None`` (first start or after a 410 Gone response).  On all other
        reconnects the most recent ``resourceVersion`` is reused so that no
        events are missed and the API server does not need to send a full
        object snapshot.
        Nz#Error during initial list; retrying   )timeoutrV   r   r   timeout_secondstypeobject
raw_objecti  z3Watch expired (410 Gone); will re-list from scratchz>Watch stream ended with ApiException (status=%s); reconnectingz,Unexpected error in watch loop; reconnectingzInformer resync triggeredz$Error during resync list; continuing)!r   is_setr   rf   rL   rM   rN   rR   r   waittime	monotonicr   r   rH   r   maxintstreamr   rK   rV   r   r   _putr	   r
   _remover   r   statuswarningdebug)r    exclast_resyncrG   eventevt_typerO   s          r&   rA   zSharedInformer._run_loop   s    "))++ _	+%-&&((((    $$%JKKKJJuc***$))!)444HHHH	 .**K''DK##%%B%)%;B!" "Q&&(+As43F/G/G(H(H$%7#/T[/FF2FF / /E'..00 $yy00H))H--C
 {.4;3O.151M.5((((---

5#....!X--((---

8S1111!W,,++C000

7C0000!X-- 

8UYY|S-I-IJJJJ!U**

5#... ' ' ':$$NNM   .2D**NNX
   

5#&&&&&&&& ' ' '  !OPPP

5#&&&&&&&&' *6/4 0 .2[-ID*" *6/4 0 .2[-ID*""""" $++--+*6'!++^%%38KKK8999+&&((((  + + +$$%KLLLJJuc********+{ "))++ _	+ _	+ _	+ _	+ _	+sr   8 
BABBF-K N; 
NA(L94N; 9N5N ;N;  NN; ;4O/Q, ,
R565R00R5)Nr   NNN)__name__
__module____qualname____doc__r'   propertyr+   r5   r9   rB   rE   rH   rR   rf   rA   rX   r(   r&   r   r   *   s         > & & & &8   X7 7 7*	 	 	         ",+ ,+ ,+\h+ h+ h+ h+ h+r(   r   )r   loggingr   rp   kubernetes.client.exceptionsr   kubernetes.watchr   r+   r   r   	getLoggerr~   rM   r   r	   r
   r   r   r   rX   r(   r&   <module>r      s           5 5 5 5 5 5 " " " " " " 3 3 3 3 3 3 3 3		8	$	$ 	
l+ l+ l+ l+ l+ l+ l+ l+ l+ l+r(   