o .&ßaH%ã@sÔddlZddlZddlZddlZddlmZddlmZddlm Z m Z Gdd„deƒZ Gdd„de ƒZ Gd d „d e ƒZ d d „ZGd d„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZdS)éN©Ú OrderedDict)ÚEKSError)Úordered_yaml_loadÚordered_yaml_dumpc@óeZdZdZdS)ÚKubeconfigErrorz& Base class for all kubeconfig errors.N©Ú__name__Ú __module__Ú __qualname__Ú__doc__©rrúF/usr/lib/python3/dist-packages/awscli/customizations/eks/kubeconfig.pyrórc@r)ÚKubeconfigCorruptedErrorz+ Raised when a kubeconfig cannot be parsed.Nr rrrrrrrc@r)ÚKubeconfigInaccessableErrorz< Raised when a kubeconfig cannot be opened for read/writing.Nr rrrrr!rrc Cs(tddgfdgfdddtƒfdgfgƒS)N)Ú apiVersionÚv1ÚclustersÚcontexts)úcurrent-contextÚ)ÚkindÚConfigÚ preferencesÚusersrrrrrÚ_get_new_kubeconfig_content%sùrc@s&eZdZddd„Zdd„Zdd„ZdS) Ú KubeconfigNcCs||_|dur tƒ}||_dS©N)ÚpathrÚcontent)Úselfr r!rrrÚ__init__2s zKubeconfig.__init__cCs t|jƒS)z+ Return the stored content in yaml format. )rr!©r"rrrÚ dump_content8s zKubeconfig.dump_contentcCs&d|jvrdS|dd„|jdDƒvS)zg Return true if this kubeconfig contains an entry For the passed cluster name. rFcSsg|]}|d‘qS)Únamer)Ú.0ÚclusterrrrÚ Cs ÿz*Kubeconfig.has_cluster..)r!)r"r&rrrÚ has_cluster<s ÿzKubeconfig.has_clusterr)r r r r#r%r*rrrrr1s  rc@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) ÚKubeconfigValidatorcCstddƒj|_dSr)rr!Ú_validation_contentr$rrrr#HszKubeconfigValidator.__init__cCs*t|tƒs tdƒ‚| |¡| |¡dS)z¢ Raises KubeconfigCorruptedError if the passed content is invalid :param config: The config to validate :type config: Kubeconfig z(Internal error: Not a Kubeconfig object.N)Ú isinstancerrÚ_validate_config_typesÚ_validate_list_entry_types)r"ÚconfigrrrÚvalidate_configMs  z#KubeconfigValidator.validate_configcCsxt|jtƒs tdƒ‚|j ¡D]*\}}||jvr9|j|dur9t|j|t|ƒƒs9td |t|j|ƒt|ƒ¡ƒ‚qdS)z¼ Raises KubeconfigCorruptedError if any of the entries in config are the wrong type :param config: The config to validate :type config: Kubeconfig zContent not a dictionary.Nz%{0} is wrong type:{1} (Should be {2}))r-r!Údictrr,ÚitemsÚtypeÚformat)r"r0ÚkeyÚvaluerrrr.Zs"  ÿ ýþ€üz*KubeconfigValidator._validate_config_typescCs\|j ¡D]&\}}||jvr+t|j|ƒtkr+|j|D]}t|tƒs*td |¡ƒ‚qqdS)zË Raises KubeconfigCorruptedError if any lists in config contain objects which are not dictionaries :param config: The config to validate :type config: Kubeconfig zEntry in {0} not a dictionary.N) r,r3r!r4Úlistr-rrr5)r"r0r6r7Úelementrrrr/qs  ÿÿ€üz.KubeconfigValidator._validate_list_entry_typesN)r r r r#r1r.r/rrrrr+Gs  r+c@seZdZddd„Zdd„ZdS)ÚKubeconfigLoaderNcCs|durtƒ}||_dSr)r+Ú _validator)r"Ú validatorrrrr#ƒs zKubeconfigLoader.__init__c Cs¼zt|dƒ }t|ƒ}Wdƒn1swYWn5ty=}z|jtjkr,d}ntd |¡ƒ‚WYd}~nd}~wtjyP}zt d |¡ƒ‚d}~wwt ||ƒ}|j   |¡|S)aP Loads the kubeconfig found at the given path. If no file is found at the given path, Generate a new kubeconfig to write back. If the kubeconfig is valid, loads the content from it. If the kubeconfig is invalid, throw the relevant exception. :param path: The path to load a kubeconfig from :type path: string :raises KubeconfigInaccessableError: if the kubeconfig can't be opened :raises KubeconfigCorruptedError: if the kubeconfig is invalid :return: The loaded kubeconfig :rtype: Kubeconfig ÚrNz&Can't open kubeconfig for reading: {0}z'YamlError while loading kubeconfig: {0}) ÚopenrÚIOErrorÚerrnoÚENOENTrr5ÚyamlÚ YAMLErrorrrr;r1)r"r ÚstreamÚloaded_contentÚeÚ loaded_configrrrÚload_kubeconfigˆs,  ÿ€ ÿ þ€ÿ€ÿ  z KubeconfigLoader.load_kubeconfigr)r r r r#rHrrrrr:‚s  r:c@seZdZdd„ZdS)ÚKubeconfigWriterc Csätj |j¡}zt |¡Wnty-}z|jtjkr#td |¡ƒ‚WYd}~nd}~wwz.t  t  |jtj tj Btj Bd¡d¡}t|j|ƒWdƒWdS1sUwYWdSttfyq}ztd |¡ƒ‚d}~ww)a Write config to disk. OK if the file doesn't exist. :param config: The kubeconfig to write :type config: Kubeconfig :raises KubeconfigInaccessableError: if the kubeconfig can't be opened for writing z'Can't create directory for writing: {0}Ni€zw+z&Can't open kubeconfig for writing: {0})Úosr ÚdirnameÚmakedirsÚOSErrorr@ÚEEXISTrr5Úfdopenr>ÚO_CREATÚO_RDWRÚO_TRUNCrr!r?)r"r0Ú directoryrFrDrrrÚwrite_kubeconfig­s:  ÿ ÿ€ÿýû&úÿ€ÿz!KubeconfigWriter.write_kubeconfigN)r r r rTrrrrrI¬s rIc@s(eZdZdd„Zddd„Zddd„ZdS) ÚKubeconfigAppendercCsœ||jvr g|j|<|j|}t|tƒstd |t|ƒt¡ƒ‚d}t|ƒD]\}}d|vr?d|vr?|d|dkr?|||<d}q%|sG| |¡||j|<|S)zÝ Insert entry into the array at content[key] Overwrite an existing entry if they share the same name :param config: The kubeconfig to insert an entry into :type config: Kubeconfig z1Tried to insert into {0},which is a {1} not a {2}Fr&T)r!r-r8rr5r4Ú enumerateÚappend)r"r0r6ÚentryÚarrayÚfoundÚcounterÚexisting_entryrrrÚ insert_entryÎs,    þþ€  zKubeconfigAppender.insert_entryNcCs4tdtd|dfd|dfgƒfd|p|dfgƒS)zE Generate a context to associate cluster and user with a given alias.Úcontextr(r&Úuserr)r"r(r_ÚaliasrrrÚ _make_contextís  þûz KubeconfigAppender._make_contextcCsL|j|||d}| |d|¡| |d|¡| |d|¡|d|jd<|S)ag Insert the passed cluster entry and user entry, then make a context to associate them and set current-context to be the new context. Returns the new context :param config: the Kubeconfig to insert the pair into :type config: Kubeconfig :param cluster: the cluster entry :type cluster: OrderedDict :param user: the user entry :type user: OrderedDict :param alias: the alias for the context; defaults top user entry name :type context: str :return: The generated context :rtype: OrderedDict )r`rrrr&r)rar]r!)r"r0r(r_r`r^rrrÚinsert_cluster_user_pair÷s z+KubeconfigAppender.insert_cluster_user_pairr)r r r r]rarbrrrrrUÍs  rU)rJrBÚloggingr@Úbotocore.compatrÚ$awscli.customizations.eks.exceptionsrÚ&awscli.customizations.eks.ordered_yamlrrrrrrÚobjectrr+r:rIrUrrrrÚs     ;*!