o .&aS/@sddlZddlZddlmZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZmZddlmZeeZejdZd ZGd d d eZGd d d eZGdddeZdS)N) OrderedDict) BasicCommand) uni_print)EKSClusterError) KubeconfigKubeconfigErrorKubeconfigLoaderKubeconfigWriterKubeconfigValidatorKubeconfigAppender)ordered_yaml_dumpz~/.kube/configz%client.authentication.k8s.io/v1alpha1c @steZdZdZedddZdddddd d dd d d dd dd dd dddd ddddd dgZddZddZ dS)UpdateKubeconfigCommandzupdate-kubeconfigeksz_description.rstnamezThe 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_textrequired kubeconfigaOptionally 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.Fzrole-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.zdry-run store_truezRPrint the merged kubeconfig to stdout instead of writing it to the specified file.)ractiondefaultrrverbosez_Print more detailed output when writing to the kubeconfig file, including the appended entries.)rrrraliaszBAlias for the cluster context name. Defaults to match cluster ARN.cCs*td|D] }tt|tdqdS)z Display entries in yaml format :param entries: a list of OrderedDicts to be printed :type entries: list z Entries:  N)rr )selfentriesentryrM/usr/lib/python3/dist-packages/awscli/customizations/eks/update_kubeconfig.py_display_entriesgs   z(UpdateKubeconfigCommand._display_entriesc Cst|j|j|j|}|}|}ttj dd|j }| |d}| |d}t } | ||||j} |jrBt|dSt} | ||rXtd| d|jn td| d|j|jrp|| ||gdSdS)N KUBECONFIGrzUpdated context {0} in {1} zAdded new context {0} to {1} ) EKSClient_sessionrrole_arnget_cluster_entryget_user_entryKubeconfigSelectorosenvirongetrchoose_kubeconfig has_clusterr insert_cluster_user_pairrdry_runr dump_contentr write_kubeconfigformatpathrr) r parsed_argsparsed_globalsclientnew_cluster_dict new_user_dictconfig_selectorconfigupdating_existingappendernew_context_dictwriterrrr _run_mainssN     z!UpdateKubeconfigCommand._run_mainN) __name__ __module__ __qualname__NAMEr FROM_FILE DESCRIPTION ARG_TABLErr=rrrrr 'sJ     7 r c@s*eZdZ  dddZddZddZdS) r&Ncs|durt}|_|durt|}|_|dur"|g_dS|dkr(t}fdd|tj D_t jdkrCtg_dSdS)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 cs&g|]}t|dkr|qS)r)lenstrip _expand_path).0elementrrr s z/KubeconfigSelector.__init__..r) r _validatorr_loaderrG_paths DEFAULT_PATHsplitr'pathseprE)r env_variablepath_in validatorloaderrrJr__init__s     zKubeconfigSelector.__init__c Cs|jD]7}z|j|}||rtd||WSWqty:}ztd||WYd}~qd}~ww|j|jdS)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 zFound entry to update at {0}zPassing {0}:{1}Nr) rNrMload_kubeconfigr+LOGdebugr0rwarning)r cluster_namecandidate_path loaded_configerrrr*s    z$KubeconfigSelector.choose_kubeconfigcCstjtj|S)z4 A helper to expand a path to a full absolute path. )r'r1abspath expanduser)rr1rrrrGszKubeconfigSelector._expand_path)NN)r>r?r@rVr*rGrrrrr&s  ! r&c@s.eZdZd ddZddZddZdd ZdS) r!NcCs"||_||_||_d|_||_dSN)r" _cluster_name _role_arn_cluster_description_globals)rsessionr[r#r3rrrrVs  zEKSClient.__init__cCs|jdurG|jdur|jd}n|jjd|jj|jj|jjd}|j|jd}|d|_d|jvr6t d|jddvrGt d |jd|jS) 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_urlverify)rclusterstatuszCluster not found)ACTIVEUPDATINGzCluster status is {0}) rdrer" create_clientregionrh verify_ssldescribe_clusterrbrr0)rr4full_descriptionrrr_get_cluster_descriptions$    z"EKSClient._get_cluster_descriptioncCsX|dddid}|d}|d}tdtd|fd|fgfd |fgS) ze Return a cluster entry generated using the previously obtained description. certificateAuthoritydatar endpointarnrjzcertificate-authority-dataserverr)rsr)r)r cert_datarvrwrrrr$ s zEKSClient.get_cluster_entrycCs|ddd}td|ddfdtdtdtfd d |d d d |jgfdgfgfg}|jdurD|ddd d|jg|jj rYtdd|jj fgg|ddd<|S)zb Return a user entry generated using the previously obtained description. rw:rr userexec apiVersionargsz--regionrz get-tokenz--cluster-name)commandawsNz--role)r AWS_PROFILEvalueenv) rsr)rPr API_VERSIONrbrcextendr"profile)rrogenerated_userrrrr%s<   zEKSClient.get_user_entryra)r>r?r@rVrsr$r%rrrrr!s   r!)r'loggingbotocore.compatrawscli.customizations.commandsrawscli.customizations.utilsr$awscli.customizations.eks.exceptionsr$awscli.customizations.eks.kubeconfigrrrr r r &awscli.customizations.eks.ordered_yamlr getLoggerr>rXr1r`rOrr objectr&r!rrrrs         zG