o
    Rh                     @   sd   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d G dd	 d	ZdS )
    )ApiException)clientconfig)	ApiClient   )LeaderElectionRecordN)levelc                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )ConfigMapLockc                 C   s@   t  | _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    r   v/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/kubernetes/leaderelection/resourcelock/configmaplock.py__init__   s   

zConfigMapLock.__init__c              
   C   s   zF| j ||}|jj}|du s|dkr!| jdi|j_|| _W dS || js4| jdi|j_|| _W dS | t	|| j }|| _d|fW S  t
y\ } z
d|fW  Y d}~S d}~w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   er   r   r   r   -   s$   
zConfigMapLock.getc              
   C   st   t j|| jt| |idd}z| jj||dd}W dS  ty9 } zt	
d| W Y d}~dS d}~w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$   r   r   r   createJ   s   zConfigMapLock.createc              
   C   sn   zt | || jjj| j< | jj||| jd}W dS  t	y6 } zt
d| W Y d}~dS d}~w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   r.   TzFailed to update lock as {}NF)r!   r'   r(   r   r   r   r   r   replace_namespaced_config_mapr   r*   r+   r,   )r   r   r   updated_recordr#   r$   r   r   r   update\   s   
zConfigMapLock.updatec                 C   sb   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_recordr   r   r   r    m   s   







zConfigMapLock.get_lock_objectc                 C   s6   |j | jd< |j| jd< |j| jd< |j| jd< | jS r3   )r4   r   r5   r6   r7   )r   r8   r   r   r   r(   {   s
   zConfigMapLock.get_lock_dictN)	__name__
__module____qualname__r   r   r/   r2   r    r(   r   r   r   r   r	      s    r	   )kubernetes.client.restr   
kubernetesr   r   kubernetes.client.api_clientr   leaderelectionrecordr   r!   r*   basicConfigINFOr	   r   r   r   r   <module>   s   