
    Zi                         d dl mZ d dlmZmZ d dlmZ ddlmZ d dl	Z	d dl
Z
 e
j        d          Z G d d	          ZdS )
    )ApiException)clientconfig)	ApiClient   )LeaderElectionRecordNleaderelectionc                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )ConfigMapLockc                     t          j                    | _        d| _        || _        || _        t          |          | _        d| _        ddddd| _	        dS )z
        :param name: name of the lock
        :param namespace: namespace
        :param identity: A unique identifier that the candidate is using
        z(control-plane.alpha.kubernetes.io/leaderN)holderIdentityleaseDurationSecondsacquireTime	renewTime)
r   	CoreV1Apiapi_instance#leader_electionrecord_annotationkeyname	namespacestridentityconfigmap_referencelock_record)selfr   r   r   s       o/root/projects/butler/venv/lib/python3.11/site-packages/kubernetes/leaderelection/resourcelock/configmaplock.py__init__zConfigMapLock.__init__   sa     #,..3]0	"H#' "$(	     c                    	 | j                             ||          }|j        j        }||dk    r| j        di|j        _        || _        dS |                    | j                  s| j        di|j        _        || _        dS |                     t          j	        || j                                     }|| _        d|fS # t          $ r}d|fcY d}~S d}~ww xY w)z
        :param name: Name of the configmap object information to get
        :param namespace: Namespace in which the configmap object is to be searched
        :return: 'True, election record' if object found else 'False, exception response'
        N )TNTF)r   read_namespaced_config_mapmetadataannotationsr   r   getget_lock_objectjsonloadsr   )r   r   r   api_responser"   r   es          r   r#   zConfigMapLock.get-   s   	,GGiXXL '/;K"kR&7&7595]_a4b%1+7(!z ??4#KLL "595]_a4b%1+7(!z..tz+dFn:o/p/pqqK'3D$$$ 	 	 	!8OOOOOO	s*   A	C  4C  <C   
C
CCCc           	      T   t          j        || j        t          j        |                     |                    id          }	 | j                            ||d          }dS # t          $ r8}t          
                    d                    |                     Y d}~dS d}~ww xY w)a  
        :param electionRecord: Annotation string
        :param name: Name of the configmap object to be created
        :param namespace: Namespace in which the configmap object is to be created
        :return: 'True' if object is created else 'False' if failed
        )r   r"   )r!   T)prettyzFailed to create lock as {}NF)r   V1ConfigMapr   r%   dumpsget_lock_dictr   create_namespaced_config_mapr   loggerinfoformat)r   r   r   election_recordbodyr'   r(   s          r   createzConfigMapLock.createJ   s     !"&*&NPTPZ[_[m[mn}[~[~PP  &AB BC C C	,II)UYbfIggL4 	 	 	KK5<<Q??@@@55555	s   A% %
B'/-B""B'c                 R   	 t          j        |                     |                    | j        j        j        | j        <   | j                            ||| j                  }dS # t          $ r8}t                              d                    |                     Y d}~dS d}~ww xY w)z
        :param name: name of the lock to be updated
        :param namespace: namespace the lock is in
        :param updated_record: the updated election record
        :return: True if update is successful False if it fails
        )r   r   r3   TzFailed to update lock as {}NF)r%   r,   r-   r   r!   r"   r   r   replace_namespaced_config_mapr   r/   r0   r1   )r   r   r   updated_recordr'   r(   s         r   updatezConfigMapLock.update\   s    	fjfpqu  rD  rD  ES  rT  rT  gU  gUD$-9$:bc,JJPT`iPTPh K j jL4 	 	 	KK5<<Q??@@@55555	s   A A$ $
B&.-B!!B&c                 :   t          d d d d           }|                    d          r|d         |_        |                    d          r|d         |_        |                    d          r|d         |_        |                    d          r|d         |_        |S Nr   r   r   r   )r   r#   holder_identitylease_durationacquire_time
renew_time)r   r   leader_election_records      r   r$   zConfigMapLock.get_lock_objectm   s    !5dD$!M!M??+,, 	S5@AQ5R"2??122 	X4?@V4W"1??=)) 	M2=m2L"/??;'' 	I0;K0H"-%%r   c                     |j         | j        d<   |j        | j        d<   |j        | j        d<   |j        | j        d<   | j        S r:   )r;   r   r<   r=   r>   )r   r?   s     r   r-   zConfigMapLock.get_lock_dict{   sM    -C-S)*3I3X/0*@*M'(>(I%r   N)	__name__
__module____qualname__r   r#   r4   r8   r$   r-    r   r   r   r      sn          (  :  $  "& & &         r   r   )kubernetes.client.restr   
kubernetesr   r   kubernetes.client.api_clientr   leaderelectionrecordr   r%   logging	getLoggerr/   r   rD   r   r   <module>rK      s    0 / / / / / % % % % % % % % 2 2 2 2 2 2 7 7 7 7 7 7  		+	,	,i  i  i  i  i  i  i  i  i  i r   