
    Q0                        d Z ddlmZ ddlmZ ddlmZ ddl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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  G d dej:                        Zy)zDThe unregister-cluster command for removing clusters from the fleet.    )absolute_import)division)unicode_literalsN)
exceptions)util)base)
agent_util)api_util)exclusivity_util)	kube_util)	resources)gke_util)	arg_utils)log)
console_ioc                   ,    e Zd ZdZed        Zd Zd Zy)
Unregistera  Unregister a cluster from a fleet.

  This command unregisters a cluster with the fleet by:

    1. Deleting the Fleet Membership resource for this cluster (a.k.a
       `{parent_command} delete`).
    2. Removing the corresponding in-cluster Kubernetes Resources that make the
       cluster exclusive to one fleet (a.k.a `kubectl delete memberships
       membership`).
    3. [Optional for GKE cluster] Uninstalling the Connect agent from this
       cluster (a.k.a `kubectl delete on the gke-connect namespace`).

  The unregister command makes additional internal checks to ensure that all
  three steps can be safely done to properly clean-up in-Fleet and in-cluster
  resources.

  To unregister a GKE cluster use `--gke-cluster` or `--gke-uri` flag (no
  `--kubeconfig` flag is required).

  To unregister a third-party cluster use `--context` flag (with an optional
  -`-kubeconfig`s flag).

  To only delete the Fleet Membership resource, consider using the command:
  `{parent_command} delete`. This command is intended to delete stale Fleet
  Membership resources as doing so on a fully registered cluster will skip some
  of the steps above and orphan in-cluster resources and agent connections to
  Google.

  ## EXAMPLES

    Unregister a third-party cluster referenced from a specific kubeconfig file:

      $ {command} my-membership \
        --context=my-cluster-context \
        --kubeconfig=/home/user/custom_kubeconfig

    Unregister a third-party cluster referenced from the default kubeconfig file:

      $ {command} my-membership --context=my-cluster-context

    Unregister a GKE cluster referenced from a GKE URI:

      $ {command} my-membership \
        --gke-uri=my-cluster-gke-uri

    Unregister a GKE cluster referenced from a GKE Cluster location and name:

      $ {command} my-membership \
        --gke-cluster=my-cluster-region-or-zone/my-cluster

    Unregister a GKE cluster and uninstall Connect agent:

      $ {command} my-membership \
        --gke-cluster=my-cluster-region-or-zone/my-cluster
        --uninstall-connect-agent

  c                     t        j                  |t        j                  d      t        j                  d      dd       |j	                  ddt        j                  d      d	       t        j                  |       y )
Nz          The membership name that you choose to uniquely represent the cluster
          being registered in the fleet.
        z          The location of the membership resource, e.g. `us-central1`.
          If not specified, defaults to `global`.
        T)membership_helplocation_helpmembership_required
positionalz--uninstall-connect-agent
store_truez          If set to True for a GKE cluster, Connect agent will be uninstalled
          from the cluster. No-op for third-party clusters, where Connect agent
          will always be uninstalled.
          F)actionhelpdefault)r   AddMembershipResourceArgtextwrapdedentadd_argumenthub_utilAddClusterConnectionCommonArgs)clsparsers     5lib/surface/container/fleet/memberships/unregister.pyArgszUnregister.Argsb   s    &&  )  oo '  ! #__  
   	 ++F3    c                 	   t        j                  |dd      }t        j                  |j	                  d      |j	                  d            \  }}d}|j
                  }t        j                  |      r@t        j                  |      }t        j                  |      }t        j                  |      }n|r~t        j                  |      }dj                  |||      }		 t        j                   |	| j#                               }
|
j$                  r%|
j$                  j&                  j(                  |k(  r|}|r'|j	                  d	      s| j/                  ||||||      S t1        j2                  t5        |dd       t5        |dd       t5        |d
d       t5        |dd       t5        |dd       t5        |dd            }|j7                          t1        j8                  ||       	 dj                  |||      }t        j                   || j#                               }|j:                  s&t=        j>                  dj                  |      d       t1        j@                  |      }|j:                  |k7  r$tC        jD                  dj                  |            t        jF                  || j#                                dj                  tR        jT                  |      }|jW                  |      }|sKtL        jN                  jQ                  dj                  tR        jT                  tR        jT                               	 t        jX                  ||      }|j[                         }t        j\                  |||| j#                               }|jN                  j^                  r=t=        j>                  dj                  ||jN                  j`                  ||      d       tc        jd                  |       tS        jf                  ||       y # t*        j,                  $ r Y w xY w# t*        jH                  $ r*}tC        jD                  dj                  ||            d }~wt*        jJ                  $ r2 tL        jN                  jQ                  dj                               Y w xY w# tB        jD                  $ r9}tL        jN                  jQ                  dj                  ||             Y d }~d }~ww xY w)Nz	--projectT)use_defaultsgke_urigke_cluster)r*   r+   global'projects/{}/locations/{}/memberships/{}uninstall_connect_agent
kubeconfigcontextpublic_issuer_urlenable_workload_identityF)r*   r+   r/   r0   r1   r2   aa  invalid membership {0} does not have external_id field set. We cannot determine if registration is requested against a valid existing Membership. Consult the documentation on container fleet memberships update for more information or run gcloud container fleet memberships delete {0} if you are sure that this is an invalid or otherwise stale Membership)cancel_on_nozMembership [{}] is not associated with the cluster you are trying to unregister. Please double check the cluster identifier that you have supplied.UYou are not authorized to unregister clusters from project [{}]. Underlying error: {}{Membership [{}] for the cluster was not found on the fleet. It may already have been deleted, or it may never have existed.z{}={}zThere's no namespace for the label [{}]. If [gke-connect] is labeled with another project, You'll have to manually delete the namespace. You can find all namespaces by running:
  `kubectl get ns -l {}`aq  Error validating cluster's exclusivity state with the Fleet under parent collection [{}]: {}. The cluster you are trying to unregister is not associated with the membership [{}]. Continuing will delete membership related resources from your cluster, and the cluster will lose its association to the Fleet in project [{}] and can be registered into a different project. aF  {} error in deleting in-cluster membership resources. You can manually delete these membership related resources from your cluster by running the command:
  `kubectl delete memberships membership`.
By doing so, the cluster will lose its association to the Fleet in project [{}] and can be registered into a different project. )4r   GetFromNamespacer   GetGKEClusterResoureLinkAndURIGetValueMEMBERSHIP_NAMEr   MembershipLocationSpecifiedMembershipResourceNamer   MembershipShortnameMembershipLocationr!   LocationFromGKEArgsformatr
   GetMembershipReleaseTrackendpoint
gkeClusterresourceLinkapitools_exceptions	HttpError_UnregisterGKEr   KubernetesClientgetattrCheckClusterAdminPermissionsValidateClusterIdentifierFlags
externalIdr   PromptContinueGetClusterUUIDr   ErrorDeleteMembershipHttpUnauthorizedErrorHttpNotFoundErrorr   statusPrintr	   CONNECT_RESOURCE_LABELNamespacesWithLabelSelector	ParentRefGetMembershipCRValidateExclusivitycodemessager   DeleteMembershipResourcesDeleteConnectNamespace)selfargsprojectgke_cluster_resource_linkgke_cluster_urilocationmembership_id
membershipcluster_locationregional_nameregional_objkube_clientnameobjuuideselector
namespacesparentcr_manifestress                        r%   RunzUnregister.Run|   sW   (({NG//MM),m4	
 / H((M,,T233D9j..z:m((4h 
#!55d;?FF
#]m--4,,.
 !!%%00==() &( !!*#  !:O!((M4I I
 ,,i.D-64t4i.!$(;TB!(/I)."0K ,,.,,[$? 6==
8],d""4):):)<=c^^!!) *0)>
	 %%k2d	4	$fTl, 	,
 d&7&7&9: ~~j??IH88BJ	jj 2 39&%<<%<<3>	?*!!'84f//1k((fm)-):):)<>c	!!5
 6<V

**M76D	 00= %%k48I !** b 44 5!!'!35 5 00 	jjL6$<H  *	jj fQ(* **sL   AP  !CP 5B4R  PPR-%QARRS)0/S$$S)c                    	 dj                  |||      }t        j                  || j                               }|j                  j
                  j                  |k7  rCt        j                  dj                  ||j                  j
                  j                              t        j                  || j                                y# t        j                  $ r*}	t        j                  dj                  ||	            d}	~	wt        j                  $ r1 t        j                  j                  dj                               Y yw xY w)z8Register a GKE cluster without installing Connect agent.r-   ztmembership [{0}] is associated with a different GKE cluster link {1}. You may be unregistering the wrong membership.r4   Nr5   )r?   r
   r@   rA   rB   rC   rD   r   rO   rP   rE   rQ   rR   r   rS   rT   )
r^   ra   rb   r`   rc   rd   r_   rj   rk   rm   s
             r%   rG   zUnregister._UnregisterGKE  s   6==
8],d""4):):)<=c		 	 	-	-1J	JBBH&cll--::C<= 	=
 d&7&7&9:44 5!!'!35 5 00 	jjL6$<s   CC E%C<<AEEN)__name__
__module____qualname____doc__classmethodr&   rs   rG    r'   r%   r   r   '   s(    8t 4 42D9Lr'   r   )rx   
__future__r   r   r   r   apitools.base.pyr   rE   &googlecloudsdk.api_lib.container.fleetr   googlecloudsdk.callioper   calliope_base*googlecloudsdk.command_lib.container.fleetr	   r
   r   r   r   r!   6googlecloudsdk.command_lib.container.fleet.membershipsr   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   googlecloudsdk.core.consoler   DeleteCommandr   rz   r'   r%   <module>r      sU    K &  '  > 7 9 A ? G @ @ G K : * # 2q,, qr'   