o
    .&ßaS/  ã                   @   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/v1alpha1c                	   @   st   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œgZdd„ Zdd„ Z	dS )ÚUpdateKubeconfigCommandzupdate-kubeconfigÚeksz_description.rstÚnamez¸The 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-arnzæTo 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.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   úM/usr/lib/python3/dist-packages/awscli/customizations/eks/update_kubeconfig.pyÚ_display_entriesg   s
   
þz(UpdateKubeconfigCommand._display_entriesc                 C   sä   t | j|j|j|ƒ}| ¡ }| ¡ }ttj 	dd¡|j
ƒ}| |d ¡}| |d ¡}tƒ }	|	 ||||j¡}
|jrBt| ¡ ƒ d S tƒ }| |¡ |rXtd |
d |j¡ƒ ntd |
d |j¡ƒ |jrp|  |
||g¡ d S d S )NÚ
KUBECONFIGÚ r   zUpdated context {0} in {1}
zAdded new context {0} to {1}
)Ú	EKSClientÚ_sessionr   Úrole_arnÚget_cluster_entryÚget_user_entryÚKubeconfigSelectorÚosÚenvironÚgetr   Úchoose_kubeconfigÚhas_clusterr   Úinsert_cluster_user_pairr   Údry_runr   Údump_contentr	   Úwrite_kubeconfigÚformatÚpathr   r   )r   Úparsed_argsÚparsed_globalsÚclientÚnew_cluster_dictÚnew_user_dictÚconfig_selectorÚconfigÚupdating_existingÚappenderÚnew_context_dictÚwriterr   r   r   Ú	_run_mains   sN   ýþÿý

ÿ
ÿ
ýÿz!UpdateKubeconfigCommand._run_mainN)
Ú__name__Ú
__module__Ú__qualname__ÚNAMEr   Ú	FROM_FILEÚDESCRIPTIONÚ	ARG_TABLEr   r=   r   r   r   r   r   '   sJ    ýù
÷øú	ü	üÑ7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   Ú_loaderrG   Ú_pathsÚDEFAULT_PATHÚsplitr'   ÚpathseprE   )r   Úenv_variableÚpath_inÚ	validatorÚloaderr   rJ   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   )	rN   rM   Úload_kubeconfigr+   ÚLOGÚdebugr0   r   Úwarning)r   Úcluster_nameÚcandidate_pathÚ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'   r1   ÚabspathÚ
expanduser)r   r1   r   r   r   rG   ã   s   zKubeconfigSelector._expand_path)NN)r>   r?   r@   rV   r*   rG   r   r   r   r   r&   ¡   s    
ÿ!r&   c                   @   s.   e Zd Zd
dd„Zdd„ Zdd„ Zdd	„ ZdS )r!   Nc                 C   s"   || _ || _|| _d | _|| _d S ©N)r"   Ú_cluster_nameÚ	_role_arnÚ_cluster_descriptionÚ_globals)r   Úsessionr[   r#   r3   r   r   r   rV   é   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   )Úregion_nameÚendpoint_urlÚverify)r   ÚclusterÚstatuszCluster not found)ÚACTIVEÚUPDATINGzCluster status is {0})rd   re   r"   Úcreate_clientÚregionrh   Ú
verify_sslÚdescribe_clusterrb   r   r0   )r   r4   Ú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.
        ÚcertificateAuthorityÚdatar    ÚendpointÚarnrj   zcertificate-authority-dataÚserverr   )rs   r)   r   )r   Ú	cert_datarv   rw   r   r   r   r$     s   
ÿÿþûzEKSClient.get_cluster_entryc                 C   s¶   |   ¡  d¡ d¡d }td|   ¡  dd¡fdtdtdtfd	d
|ddd| jgfdgƒfgƒfgƒ}| jdurD|d d d	  d| jg¡ | jj	rYtdd| jj	fgƒg|d d d< |S )zb
        Return a user entry generated using
        the previously obtained description.
        rw   ú:é   r   r    ÚuserÚexecÚ
apiVersionÚargsz--regionr   z	get-tokenz--cluster-name)ÚcommandÚawsNz--role)r   ÚAWS_PROFILEÚvalueÚenv)
rs   r)   rP   r   ÚAPI_VERSIONrb   rc   Úextendr"   Úprofile)r   ro   Úgenerated_userr   r   r   r%     s<   úÿ	õÿþ
þ
þzEKSClient.get_user_entryra   )r>   r?   r@   rV   rs   r$   r%   r   r   r   r   r!   è   s
    
r!   )r'   ÚloggingÚbotocore.compatr   Úawscli.customizations.commandsr   Úawscli.customizations.utilsr   Ú$awscli.customizations.eks.exceptionsr   Ú$awscli.customizations.eks.kubeconfigr   r   r   r	   r
   r   Ú&awscli.customizations.eks.ordered_yamlr   Ú	getLoggerr>   rX   r1   r`   rO   r…   r   Úobjectr&   r!   r   r   r   r   Ú<module>   s    
zG