o
    'ThD                     @   s^   d dl Z d dlZd dlmZ ddlmZ dZdZdZdZ	d	d
 Z
G dd deZdddZdS )    N)Configuration   )ConfigExceptionKUBERNETES_SERVICE_HOSTKUBERNETES_SERVICE_PORTz3/var/run/secrets/kubernetes.io/serviceaccount/tokenz4/var/run/secrets/kubernetes.io/serviceaccount/ca.crtc                 C   s(   d}d| v p	d| v }|rd}|| |f S )z!Adapted golang's net.JoinHostPortz%s:%s:%z[%s]:%s )hostporttemplatehost_requires_bracketingr	   r	   c/home/air/segue/gemini/back/venv/lib/python3.10/site-packages/kubernetes/config/incluster_config.py_join_host_port   s
   r   c                   @   s>   e Zd ZdejfddZdddZdd Zd	d
 Zdd Z	dS )InClusterConfigLoaderTc                 C   s*   || _ || _|| _|| _tjdd| _d S )Nr   )minutes)_token_filename_cert_filename_environ_try_refresh_tokendatetime	timedelta_token_refresh_period)selftoken_filenamecert_filenametry_refresh_tokenenvironr	   r	   r   __init__&   s
   zInClusterConfigLoader.__init__Nc                 C   sB   d}|d u rt t}d}|   | | |rt| d S d S )NFT)type__call__r   _load_config_set_configset_default)r   client_configurationtry_set_defaultr	   r	   r   load_and_set1   s   

z"InClusterConfigLoader.load_and_setc                 C   s   t | jvs
t| jvrtd| jt  r| jt stddt| jt  | jt  | _tj| j	s5td| 
  tj| jsDtdt| j}| sRtdW d    n1 s\w   Y  | j| _d S )NzService host/port is not set.z#Service host/port is set but empty.zhttps://z"Service token file does not exist.z*Service certification file does not exist.zCert file exists but empty.)SERVICE_HOST_ENV_NAMEr   SERVICE_PORT_ENV_NAMEr   r   r
   ospathisfiler   _read_token_filer   openreadssl_ca_cert)r   fr	   r	   r   r!   ;   s2   



z"InClusterConfigLoader._load_configc                    sF    j |_  j|_ jd ur j|jd<  jsd S  fdd}||_d S )Nauthorizationc                    s&    j tj kr    |  d S N)token_expires_atr   nowr,   r"   )r$   r   r	   r   _refresh_api_key_   s   z;InClusterConfigLoader._set_config.<locals>._refresh_api_key)r
   r/   tokenapi_keyr   refresh_api_key_hook)r   r$   r6   r	   r5   r   r"   W   s   

z!InClusterConfigLoader._set_configc                 C   s`   t | j!}| }|stdd| | _tj | j | _W d    d S 1 s)w   Y  d S )NzToken file exists but empty.zbearer )	r-   r   r.   r   r7   r   r4   r   r3   )r   r0   contentr	   r	   r   r,   f   s   
"z&InClusterConfigLoader._read_token_filer2   )
__name__
__module____qualname__r)   r   r   r&   r!   r"   r,   r	   r	   r	   r   r   %   s    


r   Tc                 C   s   t tt|d|  dS )a  
    Use the service account kubernetes gives to pods to connect to kubernetes
    cluster. It's intended for clients that expect to be running inside a pod
    running on kubernetes. It will raise an exception if called from a process
    not running in a kubernetes environment.)r   r   r   N)r   SERVICE_TOKEN_FILENAMESERVICE_CERT_FILENAMEr&   )r$   r   r	   r	   r   load_incluster_configp   s   r@   )NT)r   r)   kubernetes.clientr   config_exceptionr   r'   r(   r>   r?   r   objectr   r@   r	   r	   r	   r   <module>   s   	K