
    9                     4   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r
  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SrSrSrSrS rS rS r S#S jrS#S jr S r!S r"S r#S r$S r%S r&S$S jr'S  r(S! r)S" r*g)%z,Utilities for generating kubeconfig entries.    )absolute_import)division)unicode_literalsN)
kubeconfig)util)gateway)gwkubeconfig_util)errors)config)log)
properties)	platforms)semvera  
Fetch credentials for a running {cluster_type}.

This command updates a kubeconfig file with appropriate credentials and
endpoint information to point kubectl at a specific {cluster_type}.

By default, credentials are written to ``HOME/.kube/config''.
You can provide an alternate path by setting the ``KUBECONFIG'' environment
variable. If ``KUBECONFIG'' contains multiple paths, the first one is used.

This command enables switching to a specific cluster, when working
with multiple clusters. It can also be used to access a previously created
cluster from a new workstation.

By default, the command will configure kubectl to automatically refresh its
credentials using the same identity as the gcloud command-line tool.
If you are running kubectl as part of an application, it is recommended to use
[application default credentials](https://cloud.google.com/docs/authentication/production).
To configure a kubeconfig file to use application default credentials, set
the ``container/use_application_default_credentials''
[Google Cloud CLI property](https://cloud.google.com/sdk/docs/properties) to
``true'' before running the command.

See [](https://cloud.google.com/kubernetes-engine/docs/kubectl) for
kubectl documentation.
z
To get credentials of a cluster named ``my-cluster'' managed in location ``us-west1'',
run:

$ {command} my-cluster --location=us-west1
zyCluster {} is not RUNNING. The Kubernetes API may or may not be available. Check the cluster status for more information.zIs it still PROVISIONING?c                 &    SnUR                  XX#S9$ )a?  Generates a kubeconfig context for an Anthos Multi-Cloud cluster.

Args:
  kind: str, kind of the cluster e.g. aws, azure.
  project_id: str, project ID accociated with the cluster.
  location: str, Google location of the cluster.
  cluster_id: str, ID of the cluster.

Returns:
  The context for the kubeconfig entry.
z/gke_{kind}_{project_id}_{location}_{cluster_id})kind
project_idlocation
cluster_idformat)r   r   r   r   templates        Dlib/googlecloudsdk/command_lib/container/gkemulticloud/kubeconfig.pyGenerateContextr   M   s$     ?(	 
 
     c                 &    SnUR                  XX#S9$ )aU  Generates command arguments for kubeconfig's authorization provider.

Args:
  kind: str, kind of the cluster e.g. aws, azure.
  cluster_id: str, ID of the cluster.
  location: str, Google location of the cluster.
  project: str, Google Cloud project of the cluster.

Returns:
  The command arguments for kubeconfig's authorization provider.
zcontainer {kind} clusters print-access-token {cluster_id} --project={project} --location={location} --format=json --exec-credential)r   r   r   projectr   )r   r   r   r   r   s        r   GenerateAuthProviderCmdArgsr   _   s)    ( 
 
 
 
 r   c                 T   [         R                  R                  5       n[         R                  " X"U5      UR                  U'   [        5         [        X@XU5        UR                  U5        UR                  5         [        R                  R                  SR                  U5      5        g)a  Generates a kubeconfig entry for an Anthos Multi-cloud attached cluster.

Args:
  cluster: object, Anthos Multi-cloud cluster.
  cluster_id: str, the cluster ID.
  context: str, context for the kubeconfig entry.
  cmd_path: str, authentication provider command path.
NA new kubeconfig entry "{}" has been generated and set as the current context.N)kubeconfig_util
KubeconfigDefaultContextcontexts_CheckPreqs_ConnectGatewayKubeconfigSetCurrentContext
SaveToFiler   statusPrintr   )clusterr   contextcmd_pathr   s        r   GenerateAttachedKubeConfigr.   u   s     ))113*!0!8!8"*g -JhOw'**r   c                    [         R                  R                  5       n[         R                  " X"U5      UR                  U'   [        X5      nU(       d  U[        R                  " S5      :  a  [        SS9  [        X`XX45        O[        5         [        X`XU5        UR                  U5        UR                  5         [        R                  R                  SR!                  U5      5        g)a  Generates a kubeconfig entry for an Anthos Multi-cloud cluster.

Args:
  cluster: object, Anthos Multi-cloud cluster.
  cluster_id: str, the cluster ID.
  context: str, context for the kubeconfig entry.
  cmd_path: str, authentication provider command path.
  cmd_args: str, authentication provider command arguments.
  private_ep: bool, whether to use private VPC for authentication.

Raises:
    Error: don't have the permission to open kubeconfig file.
z1.21.0T)private_endpointr   N)r    r!   r"   r#   r$   
_GetSemverr   SemVerr%   _PrivateVPCKubeconfigr&   r'   r(   r   r)   r*   r   )r+   r   r,   r-   cmd_args
private_epr   versions           r   GenerateKubeconfigr7      s      ))113*!0!8!8"*g
 w+'7V]]844&Z( MZ( w'**r   c                     [         R                  " 5         U (       dR  [        R                  R                  R
                  R                  5       n[        R                  " U[        5       5        gg)z9Checks the prerequisites to run get-credentials commands.N)
r   CheckKubectlInstalledr   VALUEScorer   	GetOrFailr   CheckGatewayApiEnablement_GetConnectGatewayEndpoint)r0   r   s     r   r%   r%      sM    	""''//99;J%%.0 
r   c                 x   UR                   b  UR                   R                  c  [        R                  " US[        5      eSR                  [        5       UR                   R                  5      nSUS.n[        R                  " U40 UD6U R                  U'   [        R                  " X55      U R                  U'   g)ah  Generates the Connect Gateway kubeconfig entry.

Args:
  kubeconfig: object, Kubeconfig object.
  cluster: object, Anthos Multi-cloud cluster.
  cluster_id: str, the cluster ID.
  context: str, context for the kubeconfig entry.
  cmd_path: str, authentication provider command path.

Raises:
    errors.MissingClusterField: cluster is missing required fields.
NzFleet membershipzhttps://{}/v1/{}gcp)auth_providerauth_provider_cmd_path)fleet
membershipr
   MissingClusterFieldSTILL_PROVISIONING_MSGr   r>   r    Userusersr	   Clusterclusters)r   r+   r   r,   r-   serveruser_kwargss          r   r&   r&      s     ]]gmm66>

$
$&(>  $$ "GMM$<$<& #(8L+-227JkJ*7!2!:!:7!K*gr   c                    0 n[        XE5      US'   X6S.U R                  U'   0 nUR                  c  [        R                  " S5        O[        UR                  5      US'   UR                  c  [        R                  " US[        5      e[        R                  " USR                  UR                  5      40 UD6U R                  U'   g)ad  Generates the kubeconfig entry to connect using private VPC.

Args:
  kubeconfig: object, Kubeconfig object.
  cluster: object, Anthos Multi-cloud cluster.
  cluster_id: str, the cluster ID.
  context: str, context for the kubeconfig entry.
  cmd_path: str, authentication provider command path.
  cmd_args: str, authentication provider command arguments.
exec)nameuserNz.Cluster is missing certificate authority data.ca_dataendpointz
https://{})_ExecAuthPluginrH   clusterCaCertificater   warning
_GetCaDatarR   r
   rE   rF   r    rI   r   rJ   )r   r+   r   r,   r-   r4   rP   cluster_kwargss           r   r3   r3      s     
$ 4$v,'.=*7.!!)KK@A *7+G+G HN9

$
$J 6  "1!8!8|""7#3#34"8F"*gr   c                 z    [        X5      nU[        R                  " S5      :  a  [        R                  " S5      eg)zValidates the cluster version.

Args:
  cluster: object, Anthos Multi-cloud cluster.
  cluster_id: str, the cluster ID.

Raises:
    UnsupportedClusterVersion: cluster version is not supported.
    MissingClusterField: expected cluster field is missing.
z1.20.0zsThe command get-credentials is supported in cluster version 1.20 and newer. For older versions, use get-kubeconfig.N)r1   r   r2   r
   UnsupportedClusterVersionr+   r   r6   s      r   ValidateClusterVersionr[      s<     w+'v}}X&&

*
*	=  'r   c                 j    [         R                  " U R                  S5      5      R                  S5      $ )Nzutf-8)base64	b64encodeencodedecode)pems    r   rV   rV     s)     
		#**W-	.	5	5g	>>r   c                 N   U R                   b  U R                   R                  c  [        R                  " US5      eU R                   R                  nUR	                  S5      (       a)  UR                  SSS5      n[        R                  " U5      $ [        R                  " U5      $ )Nr6   z-nextz.0   )controlPlaner6   r
   rE   endswithreplacer   r2   )r+   r   r6   vs       r   r1   r1     s    !W%9%9%A%A%I

$
$Z
;;  ((' gq)A==	w	r   c                  Z   [         R                  R                  R                  R	                  5       n U b,  U R                  S5      (       d  U R                  S5      (       a  gSU ;   a  gU R                  S5      (       d  U R                  S5      (       a  g[        R                  " S	5      e)
zGets the corresponding Connect Gateway endpoint for Multicloud environment.

http://g3doc/cloud/kubernetes/multicloud/g3doc/oneplatform/team/how-to/hub

Returns:
  The Connect Gateway endpoint.

Raises:
  Error: Unknown API override.
zgkemulticloud.googleapis.com/z-preprod-gkemulticloud.sandbox.googleapis.com/zconnectgateway.googleapis.comzstaging-gkemulticloudz-staging-connectgateway.sandbox.googleapis.comzhttp://localhostz%gkemulticloud.sandbox.googleapis.com/z.autopush-connectgateway.sandbox.googleapis.comgkemulticloud)	r   r:   api_endpoint_overridesri   Getre   
startswithr
   UnknownApiEndpointOverrideError)rR   s    r   r>   r>   #  s     55CCGGI( 			:	;	;			J	K	K*(:+,,0A0A-1 1 <..??r   c                     SSU US.S.$ )z-Generates a Kubernetes execCredential object.ExecCredentialclient.authentication.k8s.io/v1)expirationTimestamptoken)r   
apiVersionr)    )expiration_timestampaccess_tokens     r   ro   ro   B  s     5!5
 r   c           	         U cb  Sn[         R                  R                  5       (       a  SnUn [        R                  " US/SS[        R
                  [        R
                  S9  Un U SSUR%                  S	5      S
S.n[&        R(                  R*                  R,                  R/                  5       nU(       a9  [&        R(                  R*                  R,                  R1                  5       US./US'   U$ ! [         a     [        R                  " 5       R                  nUcH  [        R                  " [        R                  5        [        R                  " [        R                  5      e[        R                   R#                  XC5      n[        R                  " US/SS[        R
                  [        R
                  S9  Un  GNZ! [         a)    [        R                  " [        R                  5          GNf = ff = f)zGenerates and returns an exec auth plugin config.

Args:
  cmd_path: str, exec command path.
  cmd_args: str, exec command arguments.

Returns:
  dict, valid exec auth plugin config entry.
gcloudz
gcloud.cmdz	--version   F)timeoutcheckstdoutstderrrp   T Never)commandrs   provideClusterInfoargsinteractiveMode)rO   valueenv)r   OperatingSystem	IsWindows
subprocessrunDEVNULL	Exceptionr   Pathssdk_bin_pathr   criticalr    SDK_BIN_PATH_NOT_FOUNDErrorospathjoinsplitr   r:   rj   ri   rk   EnvironmentName)r-   r4   bin_namer   r   sdk_path_bin_namecfgrR   s           r   rS   rS   N  s    H  **,,hG =nnK
 #### h6 5 nnS! 	# 55CCGGI( 44BBRRT	 CJ 
*S  == ||~22
,,==
>%%o&L&LM
M ggll<A

.. +.'''' '( =_;;<<=-=s*   8C1 1
G7<CG  .G3.G72G33G7c                 n     U R                  U5      (       d  [        R                  " S5        gg!    g= f)zDChecks and gives a warning if the cluster does not have a node pool.zpCluster does not have a node pool. To use Connect Gateway, ensure you have at least one Linux node pool running.N)HasNodePoolsr   rU   )cluster_clientcluster_refs     r   CheckClusterHasNodePoolsr     s9    		&&{33	kkB 4	s   ,0 4c                 J    [        X5      nU[        R                  " S5      :  $ )Nz1.25.0)r1   r   r2   rZ   s      r   ConnectGatewayInNodePoolsr     s     w+'	6==*	**r   )F)NN)+__doc__
__future__r   r   r   r]   r   r    googlecloudsdk.api_lib.containerr   r    r   *googlecloudsdk.command_lib.container.fleetr   r	   2googlecloudsdk.command_lib.container.gkemulticloudr
   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr   r   COMMAND_DESCRIPTIONCOMMAND_EXAMPLENOT_RUNNING_MSGrF   r   r   r.   r7   r%   r&   r3   r[   rV   r1   r>   ro   rS   r   r   rt   r   r   <module>r      s    3 &  '  	  J 1 > H E & # * . + 62 5 $,6 BG(VL6>&?	 @>	DN	+r   