
    )                     4   d 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 ddl	m
Z
 ddlmZ d	Zd
 Zej                   ej                   ej"                  j$                         G d dej&                                      Zej                   ej                   ej"                  j*                         G d dej&                                      Zej                   ej                   ej"                  j.                         G d dej&                                      Zy)zFetch cluster credentials.    )absolute_import)division)unicode_literals)util)base)flags)logzzcluster {0} is not RUNNING. The kubernetes API may or may not be available. Check the cluster status for more information.c                 f   t        j                          |d   }|d   } ||       }|j                  | j                  |      }t        j
                  j                  d       |j                  |      }|j                  }|xr |j                  xr |j                   }|rLt         j                  j                         s.t        j                  dj                  |j                              |j!                  |      s2t	        j"                  t$        j                  |j&                               ||fS )z2Base operations for `get-credentials` run command.api_adapterlocation_getz(Fetching cluster endpoint and auth data.zNget-credentials requires `container.clusters.getCredentials` permission on {0})r   CheckKubectlInstalledParseClusternamer	   statusPrint
GetCluster
masterAuthclientCertificate	clientKeyClusterConfigUseGCPAuthProviderErrorformat	projectId	IsRunningwarningNOT_RUNNING_MSG	clusterId)	argscontextadapterr   locationcluster_refclusterauthmissing_credss	            1lib/surface/container/clusters/get_credentials.py_BaseRunr(       s    M"'(,$($$TYY9+**=>{+'			$ I 6 6I4>>J-4--@@B
**	#VK$9$9:  
		7	#KK&&{'<'<=>	+	    c                   .    e Zd ZdZddiZed        Zd Zy)GetCredentialsa  Fetch credentials for a running cluster.

  {command} updates a `kubeconfig` file with appropriate credentials and
  endpoint information to point `kubectl` at a specific cluster in Google
  Kubernetes Engine.

  It takes a project and a zone as parameters, passed through by set
  defaults or flags. 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, {command} will configure kubectl to automatically refresh its
  credentials using the same identity as gcloud. 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
  [Cloud SDK property](https://cloud.google.com/sdk/docs/properties) to true
  before running {command}

  See
  [](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl)
  for kubectl usage with Google Kubernetes Engine and
  [](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands)
  for available kubectl commands.
  EXAMPLES          To switch to working on your cluster 'sample-cluster', run:

            $ {command} sample-cluster --location=us-central1-f
      c                     t        j                  |        t        j                  |        t        j                  |        t        j                  |        yzRegister flags for this command.

    Args:
      parser: An argparse.ArgumentParser-like object. It is mocked out in order
        to capture some information, but behaves like an ArgumentParser.
    N)r   AddGetCredentialsArgsAddDnsEndpointFlagAddKubecontextOverrideFlagAddUseIamTokenFlagparsers    r'   ArgszGetCredentials.Argsb   s<     
'	V$	$$V,	V$r)   c           	          t        || j                        \  }}t        j                  j	                  ||j
                  |j                  |j                  |j                  |j                  |j                         y)  This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Raises:
      util.Error: if the cluster is unreachable or not running.
    )use_dns_endpointimpersonate_service_accountkubecontext_overrideuse_iam_tokenN)r(   r    r   r   Persistr   internal_ipdns_endpointr:   r;   r<   selfr   r$   r#   s       r'   RunzGetCredentials.Runo   sk     $D$,,7G[**$($D$D!66((  r)   N__name__
__module____qualname____doc__detailed_helpstaticmethodr6   rB    r)   r'   r+   r+   9   s1    >  
- 
% 
%r)   r+   c                   .    e Zd ZdZddiZed        Zd Zy)GetCredentialsBeta0  Fetch credentials for a running cluster.

  {command} updates a `kubeconfig` file with appropriate credentials and
  endpoint information to point `kubectl` at a specific cluster in Google
  Kubernetes Engine.

  It takes a project and a zone as parameters, passed through by set
  defaults or flags. 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, {command} will configure kubectl to automatically refresh its
  credentials using the same identity as gcloud. 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
  [Cloud SDK property](https://cloud.google.com/sdk/docs/properties) to true
  before running {command}

  See [](https://cloud.google.com/kubernetes-engine/docs/kubectl) for
  kubectl documentation.
  r,   r-   c                     t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j
                  |        t        j                  |        y)z Register flags for this command.Nr   r0   AddCrossConnectSubnetworkFlagAddPrivateEndpointFQDNFlagr1   r2   r3   r4   s    r'   r6   zGetCredentialsBeta.Args   sX     
'	''/	$$V,	V$	$$V,	V$r)   c           
      :   t        j                  |       t        || j                        \  }}t        j
                  j                  ||j                  |j                  |j                  |j                  |j                  |j                  |j                         yr8   )r;   r<   Nr   VerifyGetCredentialsFlagsr(   r    r   r   r=   r   r>   cross_connect_subnetworkprivate_endpoint_fqdnr?   r;   r<   r@   s       r'   rB   zGetCredentialsBeta.Run        
##D)#D$,,7G[%%""!66((  	r)   NrC   rJ   r)   r'   rL   rL      s1    8  
- % %r)   rL   c                   .    e Zd ZdZddiZed        Zd Zy)GetCredentialsAlpharM   r,   r-   c                     t        j                  |        t        j                  |        t        j                  |        t        j                  |        t        j
                  |        t        j                  |        yr/   rO   r4   s    r'   r6   zGetCredentialsAlpha.Args   sX     
'	''/	$$V,	V$	$$V,	V$r)   c           
      :   t        j                  |       t        || j                        \  }}t        j
                  j                  ||j                  |j                  |j                  |j                  |j                  |j                  |j                         yrS   rT   r@   s       r'   rB   zGetCredentialsAlpha.Run  rX   r)   NrC   rJ   r)   r'   rZ   rZ      s1    8  
- % %r)   rZ   N)rG   
__future__r   r   r    googlecloudsdk.api_lib.containerr   googlecloudsdk.callioper   $googlecloudsdk.command_lib.containerr   googlecloudsdk.corer	   r   r(   UniverseCompatibleReleaseTracksReleaseTrackGACommandr+   BETArL   ALPHArZ   rJ   r)   r'   <module>ri      s   ! &  ' 1 ( 6 #2
2 D%%(()GT\\ G * GT D%%**+C C , CL D%%++,H$,, H - Hr)   