
    K                         S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJr  S r	S	 r
 " S
 S5      r " S S5      rg)z5Utilities for building the dataproc clusters gke CLI.    )absolute_import)division)unicode_literalsN)arg_parsers)
exceptionsc                     U R                  S[        R                  " SS/[        [        [        [        R                  " 5       [
        [        [        [        [        [
        [
        S.S9S/ SSS	9  g )
N--poolsnameroles)r
   r   machineTypepreemptiblelocalSsdCountacceleratorsminCpuPlatformbootDiskKmsKey	locationsminmaxrequired_keysspecappendKEY=VALUE[;VALUE]a  
        Each `--pools` flag represents a GKE node pool associated with
        the virtual cluster. It is comprised of a CSV in the form
        `KEY=VALUE[;VALUE]`, where certain keys may have multiple values.

The following KEYs must be specified:

        -----------------------------------------------------------------------------------------------------------
        KEY    Type             Example                  Description
        ------ ---------------- ------------------------ ----------------------------------------------------------
        name   string           `my-node-pool`          Name of the node pool.

        roles  repeated string  `default;spark-driver`  Roles that this node pool should perform. Valid values are
                                                         `default`, `controller`, `spark-driver`, `spark-executor`.
        -----------------------------------------------------------------------------------------------------------

The following KEYs may be specified:

        ----------------------------------------------------------------------------------------------------------------------------------------------------------------
        KEY                Type             Example                                       Description
        --------------- ---------------- --------------------------------------------- ---------------------------------------------------------------------------------
        machineType        string           `n1-standard-8`                               Compute Engine machine type to use.

        preemptible        boolean          `false`                                       If true, then this node pool uses preemptible VMs.
                                                                                          This cannot be true on the node pool with the `controllers` role
                                                                                          (or `default` role if `controllers` role is not specified).

        localSsdCount      int              `2`                                           The number of local SSDs to attach to each node.

        accelerator        repeated string  `nvidia-tesla-a100=1`                         Accelerators to attach to each node. In the format NAME=COUNT.

        minCpuPlatform     string           `Intel Skylake`                               Minimum CPU platform for each node.

        bootDiskKmsKey     string           `projects/project-id/locations/us-central1    The Customer Managed Encryption Key (CMEK) used to encrypt
                                            /keyRings/keyRing-name/cryptoKeys/key-name`   the boot disk attached to each node in the node pool.

        locations          repeated string  `us-west1-a;us-west1-c`                       Zones within the location of the GKE cluster.
                                                                                          All `--pools` flags for a Dataproc cluster must have identical locations.

        min                int              `0`                                           Minimum number of nodes per zone that this node pool can scale down to.

        max                int              `10`                                          Maximum number of nodes per zone that this node pool can scale up to.
        ----------------------------------------------------------------------------------------------------------------------------------------------------------------
        typeactiondefaultmetavarhelpadd_argumentr   ArgDictstr
ArgBooleanintparsers    7lib/googlecloudsdk/command_lib/dataproc/gke_clusters.pyAddPoolsArgr)      ss    
  (335"! # #& !+1  C    c                     U R                  S[        R                  " SS/[        [        [        [        R                  " 5       [
        [
        [        [        [        [        [
        [
        S.S9S/ SSS	9  g )
Nr	   r
   r   )r
   r   r   r   r   localNvmeSsdCountr   r   r   r   r   r   r   r   r   a  
        Each `--pools` flag represents a GKE node pool associated with
        the virtual cluster. It is a comma-separated list in the form
        `KEY=VALUE[;VALUE]`, where certain keys may have multiple values.

The following KEYs must be specified:

        -----------------------------------------------------------------------------------------------------------
        KEY    Type             Example                  Description
        ------ ---------------- ------------------------ ----------------------------------------------------------
        name   string           `my-node-pool`          Name of the node pool.

        roles  repeated string  `default;spark-driver`  Roles that each node pool will perform.
                                                        [One Pool must have DEFAULT role] Valid values are
                                                        `default`, `controller`, `spark-driver`, `spark-executor`.
        -----------------------------------------------------------------------------------------------------------

The following KEYs may be specified:

        ----------------------------------------------------------------------------------------------------------------------------------------------------------------
        KEY                Type             Example                                       Description
        --------------- ---------------- --------------------------------------------- ---------------------------------------------------------------------------------
        machineType        string           `n1-standard-8`                               Compute Engine machine type to use.

        preemptible        boolean          `false`                                       If true, then this node pool uses preemptible VMs.
                                                                                          This Must be `false` for a node pool with the CONTROLLER role or
                                                                                          for a node pool with the DEFAULT role in no node pool has the CONTROLLER role.

        localSsdCount      int              `2`                                           The number of local SSDs to attach to each node.

        localNvmeSsdCount  int              `2`                                           The number of local NVMe SSDs to attach to each node.

        accelerator        repeated string  `nvidia-tesla-a100=1`                         Accelerators to attach to each node, in NODE=COUNT format.

        minCpuPlatform     string           `Intel Skylake`                               Minimum CPU platform for each node.

        bootDiskKmsKey     string           `projects/project-id/locations/us-central1    The Customer Managed Encryption Key (CMEK) used to encrypt
                                            /keyRings/keyRing-name/cryptoKeys/key-name`   the boot disk attached to each node in the node pool.

        locations          repeated string  `us-west1-a;us-west1-c`                       Zones within the location of the GKE cluster.
                                                                                          All `--pools` flags for a Dataproc cluster must have identical locations.

        min                int              `0`                                           Minimum number of nodes per zone that this node pool can scale down to.

        max                int              `10`                                          Maximum number of nodes per zone that this node pool can scale up to.
        ----------------------------------------------------------------------------------------------------------------------------------------------------------------
        r   r    r&   s    r(   AddPoolsAlphaArgr-   b   sv    
  (335"#&! # #( !.3  Gr*   c                   l    \ rS rSrSr\S
S j5       r\S 5       r\S 5       r\S 5       r	\S 5       r
Srg	)GkeNodePoolTargetsParser   zGParses all the --pools flags into a list of GkeNodePoolTarget messages.c           	         U Vs/ s H  n[         R                  XUU5      PM     nn[        R                  U5        [        R	                  X5        [        R                  U5        [        R                  U5        U$ s  snf )a  Parses all the --pools flags into a list of GkeNodePoolTarget messages.

Args:
  dataproc: The Dataproc API version to use for GkeNodePoolTarget messages.
  gke_cluster: The GKE cluster's relative name, for example,
    'projects/p1/locations/l1/clusters/c1'.
  arg_pools: The list of dict[str, any] generated from all --pools flags.
  support_shuffle_service: support shuffle service.

Returns:
  A list of GkeNodePoolTargets message, one for each entry in the arg_pools
  list.
)_GkeNodePoolTargetParserParser/   _ValidateUniqueNames_ValidateRoles_ValidatePoolsHaveSameLocation_ValidateBootDiskKmsKeyPattern)dataprocgke_cluster	arg_poolssupport_shuffle_servicearg_poolpoolss         r(   r3   GkeNodePoolTargetsParser.Parse   s}    $ " "H 	!&&xh'>	@! 
 
 11%8++H<;;EB;;EBLs   !A>c                     [        5       nU  H?  nUR                  nX1;   a  [        R                  " SSU-  5      eUR	                  U5        MA     g)z'Validates that pools have unique names.r	   z#Pool name "%s" used more than once.N)setnodePoolr   InvalidArgumentExceptionadd)r=   
used_namespoolr
   s       r(   r4   -GkeNodePoolTargetsParser._ValidateUniqueNames   sP     J]]d		11<tCE 	EnnT r*   c                 B   U(       d  g[        5       nU HF  nUR                   H3  nXB;   a  [        R                  " SSU-  5      eUR	                  U5        M5     MH     U R
                  R                  R                  S5      nXR;  a  [        R                  " SS5      eg)zAValidates that roles are exclusive and that one pool has DEFAULT.Nr	   z,Multiple pools contained the same role "%s".DEFAULTzJIf any pools are specified, then exactly one must have the "default" role.)r@   r   r   rB   rC   messagesGkeNodePoolTargetRolesValueListEntryValuesEnum)r8   r=   
seen_rolesrE   roler   s         r(   r5   'GkeNodePoolTargetsParser._ValidateRoles   s     J**$33G$NP P ..
   11OOG //
  !r*   c                     U (       d  gSnU  HK  nUR                   c  M  UR                   R                  nUc  UnM/  X:w  d  M6  [        R                  " SS5      e   g)z7Validates that all pools specify an identical location.Nr	   z(All pools must have identical locations.)nodePoolConfigr   r   rB   )r=   initial_locationsrE   r   s       r(   r6   7GkeNodePoolTargetsParser._ValidatePoolsHaveSameLocation   sg     				(''11	$'

+33CE E r*   c                 x   U (       d  g[         R                  " S5      nU  H  nUR                   b8  UR                  R                  b!  UR                  R                  R                  c  MK  UR                  UR                  R                  R                  5      (       a  M  [        R                  " SS5      e   g)z>Validates that the bootDiskKmsKey matches the correct pattern.Nz>projects/[^/]+/locations/[^/]+/keyRings/[^/]+/cryptoKeys/[^/]+r	   z|bootDiskKmsKey must match pattern: projects/[KEY_PROJECT_ID]/locations/[LOCATION]/keyRings/[RING_NAME]/cryptoKeys/[KEY_NAME])recompilerP   configr   matchr   rB   )r=   boot_disk_kms_key_patternrE   s      r(   r7   7GkeNodePoolTargetsParser._ValidateBootDiskKmsKeyPattern   s      "

H!J



''..6!!((77?&,,



$
$
3
35 511 h
 	
 r*    NF)__name__
__module____qualname____firstlineno____doc__staticmethodr3   r4   r5   r6   r7   __static_attributes__rZ   r*   r(   r/   r/      si    O 2    , E E 
 
r*   r/   c                       \ rS rSrSrSSSSS.r\SS j5       r\ SS	 j5       r\SS
 j5       r	\S 5       r
\S 5       r\S 5       r\S 5       rSrg)r2   i  z@Helper to parse a --pools flag into a GkeNodePoolTarget message.rH   
CONTROLLERSPARK_DRIVERSPARK_EXECUTOR)r   
controllerzspark-driverzspark-executorc                 .    [         R                  XX#5      $ )a  Parses a --pools flag into a GkeNodePoolTarget message.

Args:
  dataproc: The Dataproc API version to use for the GkeNodePoolTarget
    message.
  gke_cluster: The GKE cluster's relative name, for example,
    'projects/p1/locations/l1/clusters/c1'.
  arg_pool: The dict[str, any] generated from the --pools flag.
  support_shuffle_service: support shuffle service.

Returns:
  A GkeNodePoolTarget message.
)r2   _GkeNodePoolTargetFromArgPoolr8   r9   r<   r;   s       r(   r3   _GkeNodePoolTargetParser.Parse  s     $AAxB Br*   c                     U R                   R                  SR                  XS   5      [        R	                  XS   U5      [        R                  X5      S9$ )z3Creates a GkeNodePoolTarget from a --pool argument.z{0}/nodePools/{1}r
   r   )rA   r   rP   )rI   rJ   formatr2   _SplitRoles_GkeNodePoolConfigFromArgPoolrj   s       r(   ri   6_GkeNodePoolTargetParser._GkeNodePoolTargetFromArgPool,  s_     ..$++K&9IJ&228g=N3JL/MM 	 / ! !r*   c                    / n[         R                  nU(       a1  [         R                  R                  5       nUR                  SS05        UR	                  S5       Hu  nUR                  5       W;  a  [        R                  " SSU-  5      eUR                  U R                  R                  R                  XER                  5          5      5        Mw     U$ )zFSplits the role string given as an argument into a list of Role enums.zshuffle-serviceSHUFFLE_SERVICE;r	   zUnrecognized role "%s".)r2   _ARG_ROLE_TO_API_ROLEcopyupdatesplitlowerr   rB   r   rI   rJ   rK   )r8   	arg_rolesr;   r   defined_rolesarg_roles         r(   rn   $_GkeNodePoolTargetParser._SplitRoles9  s     E6LL.DDIIKm-/@ABOOC(			.1108;= 	=ll



-
-
K
KNN,-/0	 ) Lr*   c                     U R                   R                  [        R                  X5      [        R	                  X5      S9nSU;   a  US   R                  S5      Ul        X R                   R                  5       :w  a  U$ g)zECreates the GkeNodePoolConfig via the arguments specified in --pools.)rV   autoscalingr   rs   N)rI   GkeNodePoolConfigr2   _GkeNodeConfigFromArgPool(_GkeNodePoolAutoscalingConfigFromArgPoolrw   r   r8   r<   rV   s      r(   ro   6_GkeNodePoolTargetParser._GkeNodePoolConfigFromArgPoolJ  s~     00'AA ,	1	1(	E	 1 GF
 h!+.44S9f""4466mr*   c                    U R                   R                  5       nSU;   a
  US   Ul        SU;   a
  US   Ul        SU;   a
  US   Ul        SU;   a!  U R                   R                  US   S9Ul        SU;   a  [        R                  XS   5      Ul	        SU;   a
  US   Ul
        SU;   a
  US   Ul        X R                   R                  5       :w  a  U$ g	)
zACreates the GkeNodeConfig via the arguments specified in --pools.r   r   r   r,   )r   r   r   r   N)rI   GkeNodeConfigr   r   r   GkeEphemeralStorageConfigephemeralStorageConfigr2   (_GkeNodePoolAcceleratorConfigFromArgPoolr   r   r   )r8   r<   rE   s      r(   r   2_GkeNodePoolTargetParser._GkeNodeConfigFromArgPoolX  s     **,D !-0d !-0d("#O4dh&$,$5$5$O$O !45 %P %7d!!2[[
^,.d8#$%56d8#$%56d  ..00kr*   c                 \   / nUR                  S5       Hl  nSU;  a  [        R                  " SSU-  5      eUR                  SS5      u  pE [        U5      nUR	                  U R
                  R                  UUS95        Mn     U$ ! [         a    [        R                  " SSU-  5      ef = f)zPCreates the GkeNodePoolAcceleratorConfig via the arguments specified in --pools.rs   =r	   zaaccelerators value "%s" does not match the expected "ACCELERATOR_TYPE=ACCELERATOR_VALUE" pattern.   )acceleratorCountacceleratorTypez6Unable to parse accelerators count "%s" as an integer.)rw   r   rB   r%   r   rI   GkeNodePoolAcceleratorConfig
ValueError)r8   arg_acceleratorsr   arg_acceleratoraccelerator_typecount_stringcounts          r(   r   A_GkeNodePoolTargetParser._GkeNodePoolAcceleratorConfigFromArgPoolr  s     L+11#6	O	#11 <>MNO 	O (7'<'<S!'D$L!::!& 0 ; 	 7&   11D 	s   4B%B+c                     U R                   R                  5       nSU;   a
  US   Ul        SU;   a
  US   Ul        X R                   R                  5       :w  a  U$ g)zPCreates the GkeNodePoolAutoscalingConfig via the arguments specified in --pools.r   r   N)rI   GkeNodePoolAutoscalingConfigminNodeCountmaxNodeCountr   s      r(   r   A_GkeNodePoolTargetParser._GkeNodePoolAutoscalingConfigFromArgPool  s]     ;;=F$UOf$UOf""??AAmr*   rZ   Nr[   )r\   r]   r^   r_   r`   rt   ra   r3   ri   rn   ro   r   r   r   rb   rZ   r*   r(   r2   r2     s    H  $(	 B B"  =B
! 
!       2  0  r*   r2   )r`   
__future__r   r   r   rT   googlecloudsdk.callioper   r   r)   r-   r/   r2   rZ   r*   r(   <module>r      sC    < &  ' 	 / .DNHV`
 `
FG Gr*   