1# -*- coding: utf-8 -*- # 2# Copyright 2018 Google LLC. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15"""Flags and helpers for the compute node templates commands.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import unicode_literals 20 21from googlecloudsdk.calliope import arg_parsers 22from googlecloudsdk.command_lib.compute import flags as compute_flags 23from googlecloudsdk.command_lib.util.apis import arg_utils 24from googlecloudsdk.command_lib.util.args import labels_util 25from googlecloudsdk.core.util import scaled_integer 26import six 27 28 29def MakeNodeTemplateArg(): 30 return compute_flags.ResourceArgument( 31 resource_name='node templates', 32 regional_collection='compute.nodeTemplates', 33 region_explanation=compute_flags.REGION_PROPERTY_EXPLANATION) 34 35 36def _BinarySizeOrAny(default_unit): 37 """Parses the value 'any' or a binary size converted to the default unit.""" 38 # pylint: disable=protected-access 39 bytes_per_unit = scaled_integer.GetBinaryUnitSize(default_unit) 40 def _Parse(value): 41 value = value.lower() 42 if value == 'any': 43 return value 44 size = arg_parsers.BinarySize(default_unit=default_unit)(value) 45 converted_size = size // bytes_per_unit 46 return six.text_type(converted_size) 47 return _Parse 48 49 50def _IntOrAny(): 51 def _Parse(value): 52 value = value.lower() 53 if value == 'any': 54 return value 55 # Validate that an integer is passed. 56 value = int(value) 57 return six.text_type(value) 58 return _Parse 59 60 61def _BinarySize(default_unit, lower_bound=None, upper_bound=None): 62 """Parses the value as a binary size converted to the default unit.""" 63 # pylint: disable=protected-access 64 bytes_per_unit = scaled_integer.GetBinaryUnitSize(default_unit) 65 def _Parse(value): 66 value = value.lower() 67 size = arg_parsers.BinarySize( 68 lower_bound=lower_bound, upper_bound=upper_bound, 69 default_unit=default_unit)(value) 70 converted_size = size // bytes_per_unit 71 return converted_size 72 return _Parse 73 74 75def _Choice(valid_choices): 76 def _Parse(value): 77 value = six.text_type(value.lower()) 78 if value not in valid_choices: 79 raise arg_parsers.ArgumentTypeError( 80 '[type] must be one of [{0}]'.format( 81 ','.join(valid_choices))) 82 return value 83 return _Parse 84 85 86def AddCreateArgsToParser(parser): 87 """Add flags for creating a node template to the argument parser.""" 88 parser.add_argument( 89 '--description', 90 help='An optional description of this resource.') 91 parser.add_argument( 92 '--node-affinity-labels', 93 metavar='KEY=VALUE', 94 type=arg_parsers.ArgDict( 95 key_type=labels_util.KEY_FORMAT_VALIDATOR, 96 value_type=labels_util.VALUE_FORMAT_VALIDATOR), 97 action=arg_parsers.UpdateAction, 98 help='Labels to use for node affinity, which will be used in instance ' 99 'scheduling. This corresponds to the `--node-affinity` flag on ' 100 '`compute instances create` and `compute instance-templates ' 101 'create`.') 102 node_type_group = parser.add_group(mutex=True, required=True) 103 node_type_group.add_argument( 104 '--node-type', 105 help="""\ 106 The node type to use for nodes in node groups using this template. 107 The type of a node determines what resources are available to 108 instances running on the node. 109 110 See the following for more information: 111 112 $ {grandparent_command} node-types list""") 113 node_type_group.add_argument( 114 '--node-requirements', 115 type=arg_parsers.ArgDict( 116 spec={ 117 'vCPU': _IntOrAny(), 118 'memory': _BinarySizeOrAny('MB'), 119 'localSSD': _BinarySizeOrAny('GB'), 120 }), 121 help="""\ 122The requirements for nodes. Google Compute Engine will automatically 123choose a node type that fits the requirements on Node Group creation. 124If multiple node types match your defined criteria, the NodeType with 125the least amount of each resource will be selected. You can specify 'any' 126to indicate any non-zero value for a certain resource. 127 128The following keys are allowed: 129 130*vCPU*:::: The number of committed cores available to the node. 131 132*memory*:::: The amount of memory available to the node. This value 133should include unit (eg. 3072MB or 9GB). If no units are specified, 134*MB is assumed*. 135 136*localSSD*:::: Optional. The amount of SSD space available on the 137node. This value should include unit (eg. 3072MB or 9GB). If no 138units are specified, *GB is assumed*. If this key is not specified, local SSD is 139unconstrained. 140 """) 141 142 143def AddAcceleratorArgs(parser): 144 """Adds Accelerator-related args.""" 145 parser.add_argument( 146 '--accelerator', 147 type=arg_parsers.ArgDict(spec={ 148 'type': str, 149 'count': int, 150 }), 151 help="""\ 152 Attaches accelerators (e.g. GPUs) to the node template. 153 154 *type*::: The specific type (e.g. nvidia-tesla-k80 for nVidia Tesla K80) 155 of accelerator to attach to the node template. Use 'gcloud compute 156 accelerator-types list' to learn about all available accelerator types. 157 158 *count*::: Number of accelerators to attach to each 159 node template. The default value is 1. 160 """) 161 162 163def AddDiskArgToParser(parser): 164 """Add flag for specifying disk information.""" 165 parser.add_argument( 166 '--disk', 167 type=arg_parsers.ArgDict( 168 spec={ 169 'type': _Choice(['local-ssd']), 170 'size': _BinarySize( 171 'GB', lower_bound='375GB', upper_bound='375GB'), 172 'count': int, 173 }, 174 required_keys=[ 175 'type', 176 'count', 177 ]), 178 help="""\ 179Option to specify disk properties. It is mutually exclusive with 180'--node-requirements=[localSSD=LOCALSSD]' but 181'--node-requirements=[memory=MEMORY],[vCPU=VCPU],any' are still available. 182 183*type*::: Specifies the desired disk type on the node. This disk type must be a 184local storage type. This should be the name of the disk type. Currently 185only `local-ssd` is allowed. 186 187*size*::: The size of the disk in GiB. Currently you can specify only 375 GiB 188or no value at all. 189 190*count*::: Specifies the number of such disks. Set to `16` or `24`. 191 192""") 193 194 195def GetServerBindingMapperFlag(messages): 196 """Helper to get a choice flag from server binding type enum.""" 197 return arg_utils.ChoiceEnumMapper( 198 '--server-binding', 199 messages.ServerBinding.TypeValueValuesEnum, 200 custom_mappings={ 201 'RESTART_NODE_ON_ANY_SERVER': ( 202 'restart-node-on-any-server', 203 ('Nodes using this template will restart on any physical server ' 204 'following a maintenance event.')), 205 'RESTART_NODE_ON_MINIMAL_SERVERS': ( 206 'restart-node-on-minimal-servers', """\ 207Nodes using this template will restart on the same physical server following a 208maintenance event, instead of being live migrated to or restarted on a new 209physical server. This means that VMs on such nodes will experience outages while 210maintenance is applied. This option may be useful if you are using software 211licenses tied to the underlying server characteristics such as physical sockets 212or cores, to avoid the need for additional licenses when maintenance occurs. 213 214Note that in some cases, Google Compute Engine may need to move your VMs to a 215new underlying server. During these situations your VMs will be restarted on a 216new physical server and assigned a new sole tenant physical server ID.""")}, 217 help_str=( 218 'The server binding policy for nodes using this template, which ' 219 'determines where the nodes should restart following a maintenance ' 220 'event.'), 221 default='restart-node-on-any-server') 222 223 224def AddCpuOvercommitTypeFlag(parser): 225 parser.add_argument( 226 '--cpu-overcommit-type', 227 choices=['enabled', 'none'], 228 help=('CPU overcommit type for nodes created based on this template. To ' 229 'overcommit CPUs on a VM, set --cpu-overcommit-type equal to ' 230 'either standard or none, and then when creating a VM, specify a ' 231 'value for the --min-node-cpu flag. Lower values for ' 232 '--min-node-cpu specify a higher overcommit ratio, that is, ' 233 'proportionally more vCPUs in relation to physical CPUs. You can ' 234 'only overcommit CPUs on VMs that are scheduled on nodes that ' 235 'support it.') 236 ) 237