
    ?g                         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                  e
j                          G d d	      Zy)
    )ApiException)clientconfig)	ApiClient   )LeaderElectionRecordN)levelc                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)ConfigMapLockc                     t        j                         | _        d| _        || _        || _        t        |      | _        d| _        ddddd| _	        y)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       i/var/www/openai/venv/lib/python3.12/site-packages/kubernetes/leaderelection/resourcelock/configmaplock.py__init__zConfigMapLock.__init__   sT     #,,.3]0	"H#' "$(	    c                    	 | j                   j                  ||      }|j                  j                  }||dk(  r%| j                  di|j                  _        || _        y|j                  | j                        s%| j                  di|j                  _        || _        y| j                  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XL '//;;K"kR&7595]5]_a4b%%1+7(! ??4#K#KL595]5]_a4b%%1+7(!..tzz+dFnFn:o/pqK'3D$$$ 	!8O	s*   AC  ?C  ;C 	C3%C.(C3.C3c           	      D   t        j                  || j                  t        j                  | j                  |            id      }	 | j                  j                  ||d      }y# t        $ r.}t        j                  dj                  |             Y d}~y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   logginginfoformat)r   r   r   election_recordbodyr'   r(   s          r   createzConfigMapLock.createJ   s     !!"&*&N&NPTPZPZ[_[m[mn}[~P  &ABC	,,II)UYbfIgL 	LL6==a@A	s   	A( (	B1$BBc                 h   	 t        j                  | j                  |            | j                  j                  j
                  | j                  <   | j                  j                  ||| j                        }y# t        $ r.}t        j                  dj                  |             Y d}~y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    	fjfpfpqu  rD  rD  ES  rT  gUD$$--99$:b:bc,,JJPT`iPTPhPh K jL 	LL6==a@A	s   A7A: :	B1$B,,B1c                     t        d d d d       }|j                  d      r
|d   |_        |j                  d      r
|d   |_        |j                  d      r
|d   |_        |j                  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??+,5@AQ5R"2??124?@V4W"1??=)2=m2L"/??;'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{   sh    -C-S-S)*3I3X3X/0*@*M*M'(>(I(I%r   N)	__name__
__module____qualname__r   r#   r4   r8   r$   r-    r   r   r   r      s     (:$"& r   r   )kubernetes.client.restr   
kubernetesr   r   kubernetes.client.api_clientr   leaderelectionrecordr   r%   r/   basicConfigINFOr   rD   r   r   <module>rK      s9    0 % 2 7     ',, 'i  i r   