
    Zi                          d dl Z d dlZd dlZd dlZd dlZddlmZ d dlZej        dk    rd dl	m
Z
 nd dlZ ej        d          Z	  G d d          ZdS )	    N   )LeaderElectionRecord   r   )
HTTPStatusleaderelectionc                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )LeaderElectionc                 \    |t          j        d           d | _        || _        d| _        d S )Nzargument config not passedr   )sysexitobserved_recordelection_configobserved_time_milliseconds)selfr   s     c/root/projects/butler/venv/lib/python3.11/site-packages/kubernetes/leaderelection/leaderelection.py__init__zLeaderElection.__init__)   s=    "H1222  $  / +,'''    c                 ~   |                                  rt                              d                    | j        j        j                             dt          _        t          j	        | j        j
                                                   |                                  | j                                         d S d S )Nz{} successfully acquired leaseT)target)acquireloggerinfoformatr   lockidentity	threadingdaemonThreadonstarted_leadingstart
renew_looponstopped_leading)r   s    r   runzLeaderElection.run7   s    <<>> 
	5KK8??@T@Y@bccddd  $ID$8$JKKKQQSSSOO  2244444
	5 
	5r   c                     t                               d                    | j        j        j                             | j        j        }	 |                                 }|rdS t          j	        |           -)Nz{} is a followerT)
r   r   r   r   r   r   retry_periodtry_acquire_or_renewtimesleep)r   r&   	succeededs      r   r   zLeaderElection.acquireE   so    &--d.B.G.PQQRRR+8	%1133I tJ|$$$	%r   c                    t                               d           | j        j        }| j        j        dz  }	 t          t          j                    dz            |z   }d}t          t          j                    dz            |k     rR|                                 }|rn;t          j        |           t          t          j                    dz            |k     R|rt          j        |           d S )NzGLeader has entered renew loop and will try to update lease continuously  TF)	r   r   r   r&   renew_deadlineintr(   r'   r)   )r   r&   r-   timeoutr*   s        r   r"   zLeaderElection.renew_loopR   s    ]^^^+8-<tC	$)++,-->GIdikkD())G33 5577	 
<((( dikkD())G33  
<((( Fr   c                 v   t          j                     }t          j                            |          }| j        j                            | j        j        j        | j        j        j                  \  }}t          | j        j        j	        t          | j        j                  t          |          t          |                    }|st          j        dk    rrt          j        |j                  d         t"          j        k    rDt&                              d                    | j        j        j        |j                             dS nqt          j        |j                  d         t.          j        k    rDt&                              d                    | j        j        j        |j                             dS t&                              d                    |j                             | j        j                            | j        j        j        | j        j        j        |          }|du r4t&                              d                    |j                             dS || _        t7          t          j                     dz            | _        d	S ||                     |          S |j        |j        |j        |j        |                     |          S | j        rG| j        j        |j        k    r2t&                              d
                    |j                             | j        |j         | j        j         k    r/|| _        t7          t          j                     dz            | _        | j        j        j	        | j        j        k    r_| j        | j        j        dz  z   t7          |dz            k    r4t&                              d                    |j                             dS | j        j        j	        | j        j        k    r| j        j        |_        |                     |          S )Nr   codez'Error retrieving resource lock {} as {}Fz{} is trying to create a lock)name	namespaceelection_recordz{} Failed to create lockr,   TzLeader has switched to {}zByet to finish lease_duration, lease held by {} and has not expired)!r(   datetimefromtimestampr   r   getr2   r3   r   r   strlease_durationr   version_infojsonloadsbodyr   	NOT_FOUNDr   r   r   reasonhttplibholder_identitycreater   r.   r   update_lockacquire_time
renew_time__dict__)r   now_timestampnowlock_statusold_election_recordleader_election_recordcreate_statuss          r   r'   z#LeaderElection.try_acquire_or_renewk   s   	--m<< ,0+?+D+H+HI]IbIgHLH\HaHk,m ,m(( "6d6J6O6X589M9\5]5]_bcf_g_gilmpiqiq"s "s  	&((:1677?:CWWWKK I P PQUQeQjQoReRl!n !n o o o 5 X
 :1677?7CTTTKK I P PQUQeQjQoReRl!n !n o o o 5KK7>>?U?effggg 05<<$BVB[B`GKG[G`GjMc = e eM %%6==>T>deefffu#9D .1$)++2D.E.ED+4 &##$:;;;/7;N;];e&3;?R?]?e##$:;;;  	aD$8$HL_Lo$o$oKK3::;N;^__```'+>+G4K_Kh+h+h#6D .1$)++2D.E.ED+  %.$2F2VVV3d6J6Y\`6``cfgtw{g{c|c|||KK\ccdw  eH  I  I  J  J  J5 $-1E1UUU262F2S"/ 6777r   c                    | j         j                            | j         j        j        | j         j        j        |          }|du r4t
                              d                    |j                             dS || _	        t          t          j                    dz            | _        t
                              d                    |j                             dS )NFz{} failed to acquire leaser,   z)leader {} has successfully acquired leaseT)r   r   updater2   r3   r   r   r   rA   r   r.   r(   r   )r   rK   update_statuss      r   rC   zLeaderElection.update_lock   s    ,1889M9R9W9=9M9R9\9OQ Q E!!KK4;;<R<bccddd55*-dikkD.@*A*A'?FFG]Gmnnoootr   N)	__name__
__module____qualname__r   r$   r   r"   r'   rC    r   r   r
   r
   (   sr        , , ,5 5 5% % %  2E8 E8 E8N    r   r
   )r5   r   r(   r;   r   leaderelectionrecordr   loggingr:   httpr   r@   	getLoggerr   r
   rS   r   r   <module>rX      s     



       6 6 6 6 6 6 fNNN		+	,	,W W W W W W W W W Wr   