
    +                        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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      Z G d de      Zd Zd Zd Z ejB                   ejD                  ejF                  jH                         G d dejJ                                      Z&ddde&_'         ejD                  ejF                  jP                         G d de&             Z) ejD                  ejF                  jT                         G d  d!e&             Z+y")#zUpgrade cluster command.    )absolute_import)division)unicode_literals)
exceptions)api_adapter)util)base)container_command_util)flags)log)
properties)console_attr)
console_io)iso_duration)times)SemVerc                        e Zd ZdZdZdZdZdZy)UpgradeHelpTextz%Upgrade available help text messages.z8
* - There is an upgrade available for your cluster(s).
zZ
** - The current version of your cluster(s) will soon be out of support, please upgrade.
zN
*** - The current version of your cluster(s) is unsupported, please upgrade.
zc
To upgrade nodes to the latest available version, run
  $ gcloud container clusters upgrade {name}N)__name__
__module____qualname____doc__UPGRADE_AVAILABLESUPPORT_ENDINGUNSUPPORTEDUPGRADE_COMMAND     )lib/surface/container/clusters/upgrade.pyr   r   &   s"    -.+0/r   r   c                   &    e Zd ZdZdZdZdZdZd Zy)VersionVerifierzACompares the cluster and master versions for upgrade availablity.r            c                     ||k(  r| j                   S t        |      }t        |      }|j                  |      \  }}}|dk7  s|dkD  r| j                  S |dkD  r| j                  S | j
                  S )z=Compares the cluster and master versions and returns an enum.r   r#   r"   )
UP_TO_DATEr   Distancer   r   r   )selfcurrent_master_versioncurrent_cluster_versionmaster_versioncluster_versionmajorminor_s           r   ComparezVersionVerifier.Compare@   sy    !88__23N45O$--o>OE5!zUQY	   ###r   N)	r   r   r   r   r&   r   r   r   r0   r   r   r   r!   r!   9   s    I*.+$r   r!   c           	      T   t        j                  | j                  | j                  | j                   | j                  | j
                  | j                  | j                        }| j                  r8t        j                  t        j                  | j                              |_        |S )z;Parses the flags provided with the cluster upgrade command.)versionupdate_masterupdate_nodes	node_pool
image_typeimageimage_project)seconds)r   UpdateClusterOptionsr,   masterr5   r6   r7   r8   control_plane_soak_durationr   FormatDurationForJsonr   Duration)argsoptss     r   ParseUpgradeOptionsBaserA   O   s    		)	)""KKOJJ&&
$ 
%%','B'Bd&F&FG(D$ 
+r   c                    | j                  ddd       t        j                  | d       | j                  dd       | j                  d	d
d       | j                  dt        ddd       t        j                  |        t        j
                  | d       t        j                  | d       t        j                  | d       t        j                  | d       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.
  nameNAMEz#The name of the cluster to upgrade.)metavarhelpa  The GKE release version to which to upgrade the cluster's node pools or master.

If desired cluster version is omitted, *node pool* upgrades default to the current
*master* version and *master* upgrades default to the default cluster version,
which can be found in the server config.

You can find the list of allowed versions for upgrades by running:

  $ gcloud container get-server-config
)rF   z--node-poolzThe node pool to upgrade.z--masterz\Upgrade the cluster's master. Node pools cannot be upgraded at the  same time as the master.
store_true)rF   actionz	--timeouti  Tz;Timeout (seconds) for waiting on the operation to complete.)typedefaulthiddenrF   zcluster/node pool)rK   N)	add_argumentr   AddClusterVersionFlagintAddAsyncFlagAddImageTypeFlagAddImageFlagAddImageProjectFlagAddControlPlaneSoakDurationFlagparsers    r   _ArgsrV   a   s     	f#H  J
 	m*EF&    	H  J V!45VD)F40''t<r   c                     | $t        | j                        }|t        d      k\  ryt        j                  j	                  d       y)z0Logs deprecation warning for GKE v1.22 upgrades.Nz1.22.0-gke.0aB  Upcoming breaking change: Starting with v1.22, Kubernetes has removed several v1beta1 APIs for more stable v1 APIs. Read more about this change - https://cloud.google.com/kubernetes-engine/docs/deprecations/apis-1-22. Please ensure that your cluster is not using any deprecated v1beta1 APIs prior to upgrading to GKE 1.22.)r   currentMasterVersionr   statusPrint)clustercmvs     r   MaybeLog122UpgradeWarningr]      s?    
--
.C
f^$$**-.r   c                   ,    e Zd ZdZed        Zd Zd Zy)Upgrade@Upgrade the Kubernetes version of an existing container cluster.c                     t        |        y NrV   rT   s    r   ArgszUpgrade.Args   	    	&Mr   c                     t        |      S rb   )rA   )r(   r?   s     r   ParseUpgradeOptionszUpgrade.ParseUpgradeOptions   s    "4((r   c           
      ~   | j                   d   }| j                   d   } ||      }|j                  |j                  |      }t        j                  j
                  j                  j                  d      }	 |j                  |      }	 |j)                  ||      }	|j*                  r"|j,                  st        j.                  dd	      t1        j2                  |j                  |	||j,                  |j*                  |j4                  |j6                  |j8                  
      }
|j,                  rt;        |       t=        j>                  |
dd       | jA                  |      }	 |jC                  ||      }|jH                  s|jK                  |dj#                  |jL                        |jN                         	 |j                  |      }|jP                  D ]  }t        jR                  |        	 t        jV                  |       yy# t        j                  t        j                  t        j                  f$ rD}t        j                   dj#                  t%        j&                  |                   d}Y d}~d}~ww xY w# t        j                  t        j                  t        j                  f$ rD}t        j                   dj#                  t%        j&                  |                   d}	Y d}~Wd}~ww xY w# t        jD                  $ r)}t        j                  |t        jF                        d}~ww xY w# t        j                  t        j                  t        j                  f$ rP}t        j                   t        jT                  j#                  t%        j&                  |                   Y d}~d}~ww xY w)zThis 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.

    Returns:
      Some value that we want to have printed later.
    r   location_getT)requiredzbProblem loading details of cluster to upgrade:

{}

You can still attempt to upgrade the cluster.
NzRProblem loading server config:

{}

You can still attempt to upgrade the cluster.
z--control-plane-soak-durationzA--control-plane-soak-duration can only be specified with --master)rC   server_confr[   r;   r<   node_pool_namenew_versionnew_image_type)messagethrow_if_unattendedcancel_on_nozUpgrading {0})	timeout_s),contextParseClusterrC   r   VALUEScoreprojectGet
GetClusterr   HttpExceptionapitools_exceptionsHttpForbiddenErrorr   Errorr   warningformatr   SafeTextGetServerConfigr<   r;   InvalidArgumentExceptionr
   ClusterUpgradeMessager5   r,   r6   r]   r   PromptContinuerg   UpdateCluster	HttpErrorHTTP_ERROR_FORMATasync_WaitForOperation	clusterIdtimeout	nodePoolsCheckForCgroupModeV1CGROUPV1_CHECKING_FAILURE_MSGUpdatedResource)r(   r?   adapterri   locationcluster_ref
project_idr[   errorrk   upgrade_messageoptionsop_refr5   s                 r   RunzUpgrade.Run   s&    ll=)G<</LD!H&&tyy(;K""''//33T3BJ"";/g++JAk ''//
)
MO O -BBYY{{$($D$D~~(((O {{(TN &&t,GD$$[':f ;;


 
 !6!6
7LL  "


$$[1 **I

#
#I
. + 
+&# Y $$&9&L&LJJ 	kk EFLf#,,U3G56 g $$&9&L&LJJ 	kk EFLf#,,U3G56 k@ (( D$$UD,B,BCCD &&(;(N(Njj 
..55%%e,	
 	

sa   8H	 
J $K; :7L: 	2I?;9I::I?2K849K33K8;L7$L22L7:2N<,AN77N<N)r   r   r   r   staticmethodrd   rg   r   r   r   r   r_   r_      s%     I )O'r   r_   a        Upgrades the Kubernetes version of an existing container cluster.

      This command upgrades the Kubernetes version of the *node pools* or *master* of
      a cluster. Note that the Kubernetes version of the cluster's *master* is
      also periodically upgraded automatically as new releases are available.

      If desired cluster version is omitted, *node pool* upgrades default to the
      current *master* version and *master* upgrades default to the default
      cluster version, which can be found in the server config.

      *During node pool upgrades, nodes will be deleted and recreated.* While
      persistent Kubernetes resources, such as
      Pods backed by replication controllers, will be rescheduled onto new
      nodes, a small cluster may experience a few minutes where there are
      insufficient nodes available to run all of the scheduled Kubernetes
      resources.

      *Please ensure that any data you wish to keep is stored on a persistent*
      *disk before upgrading the cluster.* Ephemeral Kubernetes resources--in
      particular, Pods without replication controllers--will be lost, while
      persistent Kubernetes resources will get rescheduled.
    a        Upgrade the node pool `pool-1` of `sample-cluster` to the Kubernetes
      version of the cluster's master.

        $ {command} sample-cluster --node-pool=pool-1

      Upgrade the node pool `pool-1` of `sample-cluster` to Kubernetes version
      1.14.7-gke.14:

        $ {command} sample-cluster --node-pool=pool-1 --cluster-version="1.14.7-gke.14"

      Upgrade the master of `sample-cluster` to the default cluster version:

        $ {command} sample-cluster --master
)DESCRIPTIONEXAMPLESc                        e Zd ZdZed        Zy)UpgradeBetar`   c                     t        |        y rb   rc   rT   s    r   rd   zUpgradeBeta.Args,  re   r   N)r   r   r   r   r   rd   r   r   r   r   r   (  s    H r   r   c                   &    e Zd ZdZed        Zd Zy)UpgradeAlphar`   c                 D    t        |        t        j                  |        y rb   )rV   r   !AddSecurityProfileForUpgradeFlagsrT   s    r   rd   zUpgradeAlpha.Args5  s    	&M	++F3r   c                 `    t        |      }|j                  |_        |j                  |_        |S rb   )rA   security_profilesecurity_profile_runtime_rules)r(   r?   opss      r   rg   z UpgradeAlpha.ParseUpgradeOptions:  s-    
!$
'C00C)-)L)LC&Jr   N)r   r   r   r   r   rd   rg   r   r   r   r   r   1  s    H4 4r   r   N),r   
__future__r   r   r   apitools.base.pyr   r{    googlecloudsdk.api_lib.containerr   r   googlecloudsdk.callioper	   $googlecloudsdk.command_lib.containerr
   r   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   r   googlecloudsdk.core.utilr   r   googlecloudsdk.core.util.semverr   objectr   r!   rA   rV   r]   DefaultUniverseOnlyReleaseTracksReleaseTrackGACommandr_   detailed_helpBETAr   ALPHAr   r   r   r   <module>r      s,    &  ' > 8 1 ( . G 6 # * 4 2 1 * 20f 0&$f $,$*=Z.  D%%(()Y'dll Y' * Y'|	0	5) X D%%**+'  , D%%++,7  -r   