o
    !d2                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZmZ d dlmZ eeZe jdZd	ZG d
d deZG dd deZG dd deZdS )    N)OrderedDict)BasicCommand)	uni_print)EKSClusterError)
KubeconfigKubeconfigErrorKubeconfigLoaderKubeconfigWriterKubeconfigValidatorKubeconfigAppender)ordered_yaml_dumpz~/.kube/configz$client.authentication.k8s.io/v1beta1c                
   @   s~   e Zd ZdZedddZdddddd	d
dddd
dddd
dd
dddd
ddddd
dddd
dgZdd Zdd Z	dS )UpdateKubeconfigCommandzupdate-kubeconfigeksz_description.rstnamezThe name of the cluster for which to create a kubeconfig entry. This cluster must exist in your account and in the specified or configured default Region for your AWS CLI installation.T)r   	help_textrequired
kubeconfiga  Optionally specify a kubeconfig file to append with your configuration. By default, the configuration is written to the first file path in the KUBECONFIG environment variable (if it is set) or the default kubeconfig path (.kube/config) in your home directory.Fzrole-arnzTo assume a role for cluster authentication, specify an IAM role ARN with this option. For example, if you created a cluster while assuming an IAM role, then you must also assume that role to connect to the cluster the first time.zdry-run
store_truezRPrint the merged kubeconfig to stdout instead of writing it to the specified file.)r   actiondefaultr   r   verbosez_Print more detailed output when writing to the kubeconfig file, including the appended entries.)r   r   r   r   aliaszBAlias for the cluster context name. Defaults to match cluster ARN.z
user-aliaszAAlias for the generated user name. Defaults to match cluster ARN.c                 C   s*   t d |D ]}t t| t d qdS )z
        Display entries in yaml format

        :param entries: a list of OrderedDicts to be printed
        :type entries: list
        z
Entries:


N)r   r   )selfentriesentry r   Ousr/lib/python3.10/site-packages/awscli/customizations/eks/update_kubeconfig.py_display_entriesl   s
   
z(UpdateKubeconfigCommand._display_entriesc                 C   s   t | j|j|j|}| }|j|jd}ttj	
dd|j}||d }||d }t }	|	||||j}
|jrEt|  d S t }|| |r[td|
d |j ntd|
d |j |jrs| |
||g d S d S )N)
user_aliasZ
KUBECONFIG r   zUpdated context {0} in {1}
zAdded new context {0} to {1}
)	EKSClient_sessionr   role_arnget_cluster_entryget_user_entryr   KubeconfigSelectorosenvirongetr   choose_kubeconfighas_clusterr   Zinsert_cluster_user_pairr   dry_runr   Zdump_contentr	   Zwrite_kubeconfigformatpathr   r   )r   Zparsed_argsparsed_globalsclientZnew_cluster_dictZnew_user_dictZconfig_selectorconfigZupdating_existingZappenderZnew_context_dictwriterr   r   r   	_run_mainx   sN   



z!UpdateKubeconfigCommand._run_mainN)
__name__
__module____qualname__NAMEr   Z	FROM_FILEZDESCRIPTIONZ	ARG_TABLEr   r3   r   r   r   r   r   &   sR    
		=r   c                   @   s*   e Zd Z		dddZdd Zdd ZdS )	r&   Nc                    s   |du rt  }| _|du rt|}| _|dur" |g _dS |dkr(t} fdd|tj	D  _t
 jdkrCtg _dS dS )a6  
        Parse KUBECONFIG into a list of absolute paths.
        Also replace the empty list with DEFAULT_PATH

        :param env_variable: KUBECONFIG as a long string
        :type env_variable: string

        :param path_in: The path passed in through the CLI
        :type path_in: string or None
        Nr    c                    s&   g | ]}t | d kr |qS )r   )lenstrip_expand_path).0elementr   r   r   
<listcomp>   s
    z/KubeconfigSelector.__init__.<locals>.<listcomp>r   )r
   
_validatorr   _loaderr:   _pathsDEFAULT_PATHsplitr'   pathsepr8   )r   Zenv_variableZpath_in	validatorloaderr   r=   r   __init__   s    

zKubeconfigSelector.__init__c                 C   s   | j D ]7}z| j|}||rtd| |W   S W q ty: } ztd|| W Y d}~qd}~ww | j| j d S )a  
        Choose which kubeconfig file to read from.
        If name is already an entry in one of the $KUBECONFIG files,
        choose that one.
        Otherwise choose the first file.

        :param cluster_name: The name of the cluster which is going to be added
        :type cluster_name: String

        :return: a chosen Kubeconfig based on above rules
        :rtype: Kubeconfig
        zFound entry to update at {0}zPassing {0}:{1}Nr   )	rA   r@   Zload_kubeconfigr+   LOGdebugr-   r   warning)r   cluster_nameZcandidate_pathZloaded_configer   r   r   r*      s   


z$KubeconfigSelector.choose_kubeconfigc                 C   s   t jt j|S )z4 A helper to expand a path to a full absolute path. )r'   r.   abspath
expanduser)r   r.   r   r   r   r:      s   zKubeconfigSelector._expand_path)NN)r4   r5   r6   rG   r*   r:   r   r   r   r   r&      s    
!r&   c                   @   s0   e Zd Zd
ddZdd Zdd Zd
dd	ZdS )r!   Nc                 C   s"   || _ || _|| _d | _|| _d S N)r"   _cluster_name	_role_arn_cluster_description_globals)r   ZsessionrK   r#   r/   r   r   r   rG      s
   
zEKSClient.__init__c                 C   s   | j du rG| jdu r| jd}n| jjd| jj| jj| jjd}|j| jd}|d | _ d| j vr6t	d| j d dvrGt	d	
| j d | j S )
z
        Use an eks describe-cluster call to get the cluster description
        Cache the response in self._cluster_description.
        describe-cluster will only be called once.
        Nr   )Zregion_nameendpoint_urlZverify)r   clusterstatuszCluster not found)ZACTIVEZUPDATINGzCluster status is {0})rR   rS   r"   Zcreate_clientregionrT   Z
verify_sslZdescribe_clusterrP   r   r-   )r   r0   Zfull_descriptionr   r   r   _get_cluster_description   s$   



z"EKSClient._get_cluster_descriptionc                 C   sX   |   dddid }|   d}|   d}tdtd|fd|fgfd	|fgS )
ze
        Return a cluster entry generated using
        the previously obtained description.
        ZcertificateAuthoritydatar    endpointarnrU   zcertificate-authority-dataserverr   )rX   r)   r   )r   Z	cert_datarZ   r[   r   r   r   r$     s   
zEKSClient.get_cluster_entryc                 C   s   |   }|ddd }|d}|du rd}| j}nd}|d}td	|p/|   dd
fdtdtdtfdd|dd||ddgfdgfgfg}| jdur_|d d d d| jg | jj	rttdd| jj	fgg|d d d< |S )zb
        Return a user entry generated using
        the previously obtained description.
        r[   :   ZoutpostConfigNz--cluster-namez--cluster-ididr   r    userexecZ
apiVersionargsz--regionr   z	get-tokenz--outputjson)commandZawsz--role)r   ZAWS_PROFILEvalueenv)
rX   r)   rC   rP   r   API_VERSIONrQ   extendr"   Zprofile)r   r   Zcluster_descriptionrW   Zoutpost_configZ cluster_identification_parameterZcluster_identification_valueZgenerated_userr   r   r   r%   $  sN   



zEKSClient.get_user_entryrO   )r4   r5   r6   rG   rX   r$   r%   r   r   r   r   r!      s
    
r!   )r'   loggingZbotocore.compatr   Zawscli.customizations.commandsr   Zawscli.customizations.utilsr   Z$awscli.customizations.eks.exceptionsr   Z$awscli.customizations.eks.kubeconfigr   r   r   r	   r
   r   Z&awscli.customizations.eks.ordered_yamlr   	getLoggerr4   rH   r.   rN   rB   rg   r   objectr&   r!   r   r   r   r   <module>   s    
 G