o .&aH%@sddlZddlZddlZddlZddlmZddlmZddlm Z m Z GdddeZ Gddde Z Gd d d e Z d d ZGd ddeZGdddeZGdddeZGdddeZGdddeZdS)N OrderedDict)EKSError)ordered_yaml_loadordered_yaml_dumpc@eZdZdZdS)KubeconfigErrorz& Base class for all kubeconfig errors.N__name__ __module__ __qualname____doc__rrF/usr/lib/python3/dist-packages/awscli/customizations/eks/kubeconfig.pyrrc@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(tddgfdgfdddtfdgfgS)N) apiVersionv1clusterscontexts)current-context)kindConfig preferencesusersrrrrr_get_new_kubeconfig_content%src@s&eZdZdddZddZddZdS) KubeconfigNcCs||_|dur t}||_dSN)pathrcontent)selfr r!rrr__init__2s zKubeconfig.__init__cCs t|jS)z+ Return the stored content in yaml format. )rr!r"rrr dump_content8s zKubeconfig.dump_contentcCs&d|jvrdS|dd|jdDvS)zg Return true if this kubeconfig contains an entry For the passed cluster name. rFcSsg|]}|dqS)namer).0clusterrrr Cs z*Kubeconfig.has_cluster..)r!)r"r&rrr has_cluster<s zKubeconfig.has_clusterr)r r r r#r%r*rrrrr1s  rc@s,eZdZddZddZddZddZd S) KubeconfigValidatorcCstddj|_dSr)rr!_validation_contentr$rrrr#HszKubeconfigValidator.__init__cCs*t|ts 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"configrrrvalidate_configMs  z#KubeconfigValidator.validate_configcCsxt|jts td|jD]*\}}||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,itemstypeformat)r"r0keyvaluerrrr.Zs"   z*KubeconfigValidator._validate_config_typescCs\|jD]&\}}||jvr+t|j|tkr+|j|D]}t|ts*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!r4listr-rrr5)r"r0r6r7elementrrrr/qs  z.KubeconfigValidator._validate_list_entry_typesN)r r r r#r1r.r/rrrrr+Gs  r+c@seZdZdddZddZdS)KubeconfigLoaderNcCs|durt}||_dSr)r+ _validator)r" validatorrrrr#s zKubeconfigLoader.__init__c Cszt|d }t|}Wdn1swYWn5ty=}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}) openrIOErrorerrnoENOENTrr5yaml YAMLErrorrrr;r1)r"r streamloaded_contente loaded_configrrrload_kubeconfigs,      z KubeconfigLoader.load_kubeconfigr)r r r r#rHrrrrr:s  r:c@seZdZddZdS)KubeconfigWriterc Cstj|j}zt|Wnty-}z|jtjkr#td|WYd}~nd}~wwz.t t |jtj tj Btj Bdd}t|j|WdWdS1sUwYWdSttfyq}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}Nizw+z&Can't open kubeconfig for writing: {0})osr dirnamemakedirsOSErrorr@EEXISTrr5fdopenr>O_CREATO_RDWRO_TRUNCrr!r?)r"r0 directoryrFrDrrrwrite_kubeconfigs:   &z!KubeconfigWriter.write_kubeconfigN)r r r rTrrrrrIs rIc@s(eZdZddZdddZdddZdS) KubeconfigAppendercCs||jvr g|j|<|j|}t|tstd|t|td}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 enumerateappend)r"r0r6entryarrayfoundcounterexisting_entryrrr insert_entrys,      zKubeconfigAppender.insert_entryNcCs4tdtd|dfd|dfgfd|p|dfgS)zE Generate a context to associate cluster and user with a given alias.contextr(r&userr)r"r(r_aliasrrr _make_contexts  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^rrrinsert_cluster_user_pairs z+KubeconfigAppender.insert_cluster_user_pairr)r r r r]rarbrrrrrUs  rU)rJrBloggingr@botocore.compatr$awscli.customizations.eks.exceptionsr&awscli.customizations.eks.ordered_yamlrrrrrrobjectrr+r:rIrUrrrrs     ;*!