def _GetUserMetadata(self, args, client, instance_template_ref, container_mount_disk_enabled=False, container_mount_disk=None): user_metadata = instance_utils.GetValidatedMetadata(args, client) return containers_utils.CreateKonletMetadataMessage( client.messages, args, instance_template_ref.Name(), user_metadata, container_mount_disk_enabled, container_mount_disk)
def _GetUserMetadata(self, args, client, instance_template_ref): user_metadata = metadata_utils.ConstructMetadataMessage( client.messages, metadata=args.metadata, metadata_from_file=args.metadata_from_file) containers_utils.ValidateUserMetadata(user_metadata) return containers_utils.CreateKonletMetadataMessage( client.messages, args, instance_template_ref.Name(), user_metadata)
def Run(self, args): self._ValidateArgs(args) holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client source_instance_template = instance_utils.GetSourceInstanceTemplate( args, holder.resources, self.SOURCE_INSTANCE_TEMPLATE) skip_defaults = instance_utils.GetSkipDefaults( source_instance_template) scheduling = instance_utils.GetScheduling(args, client, skip_defaults) service_accounts = instance_utils.GetServiceAccounts( args, client, skip_defaults) user_metadata = instance_utils.GetValidatedMetadata(args, client) boot_disk_size_gb = instance_utils.GetBootDiskSizeGb(args) instance_refs = instance_utils.GetInstanceRefs(args, client, holder) network_interfaces = self.GetNetworkInterfaces(args, holder.resources, client, holder, instance_refs, skip_defaults) machine_type_uris = instance_utils.GetMachineTypeUris( args, client, holder, instance_refs, skip_defaults) image_uri = self.GetImageUri(args, client, holder, instance_refs) labels = containers_utils.GetLabelsMessageWithCosVersion( args.labels, image_uri, holder.resources, client.messages.Instance) can_ip_forward = instance_utils.GetCanIpForward(args, skip_defaults) tags = containers_utils.CreateTagsMessage(client.messages, args.tags) requests = [] for instance_ref, machine_type_uri in zip(instance_refs, machine_type_uris): metadata = containers_utils.CreateKonletMetadataMessage( client.messages, args, instance_ref.Name(), user_metadata) disks = instance_utils.CreateDiskMessages(holder, args, boot_disk_size_gb, image_uri, instance_ref, skip_defaults) request = client.messages.ComputeInstancesInsertRequest( instance=client.messages.Instance( canIpForward=can_ip_forward, disks=disks, description=args.description, labels=labels, machineType=machine_type_uri, metadata=metadata, minCpuPlatform=args.min_cpu_platform, name=instance_ref.Name(), networkInterfaces=network_interfaces, serviceAccounts=service_accounts, scheduling=scheduling, tags=tags), sourceInstanceTemplate=source_instance_template, project=instance_ref.project, zone=instance_ref.zone) requests.append( (client.apitools_client.instances, 'Insert', request)) return client.MakeRequests(requests)
def Run(self, args): self._ValidateArgs(args) holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) compute_client = holder.client resource_parser = holder.resources container_mount_disk = instances_flags.GetValidatedContainerMountDisk( holder, args.container_mount_disk, args.disk, args.create_disk) source_instance_template = instance_utils.GetSourceInstanceTemplate( args, resource_parser, self.SOURCE_INSTANCE_TEMPLATE) skip_defaults = instance_utils.GetSkipDefaults( source_instance_template) scheduling = instance_utils.GetScheduling(args, compute_client, skip_defaults, support_min_node_cpu=False) service_accounts = instance_utils.GetServiceAccounts( args, compute_client, skip_defaults) user_metadata = instance_utils.GetValidatedMetadata( args, compute_client) boot_disk_size_gb = instance_utils.GetBootDiskSizeGb(args) instance_refs = instance_utils.GetInstanceRefs(args, compute_client, holder) network_interfaces = self.GetNetworkInterfaces( args, resource_parser, compute_client, holder, instance_refs[0].project, instance_refs[0].zone, compute_scopes.ScopeEnum.ZONE, skip_defaults) image_uri = self.GetImageUri(args, compute_client, resource_parser, instance_refs) labels = containers_utils.GetLabelsMessageWithCosVersion( args.labels, image_uri, resource_parser, compute_client.messages.Instance) can_ip_forward = instance_utils.GetCanIpForward(args, skip_defaults) tags = containers_utils.CreateTagsMessage(compute_client.messages, args.tags) confidential_vm = (args.IsSpecified('confidential_compute') and args.confidential_compute) requests = [] for instance_ref in instance_refs: metadata = containers_utils.CreateKonletMetadataMessage( compute_client.messages, args, instance_ref.Name(), user_metadata, container_mount_disk_enabled=self. _container_mount_disk_enabled, container_mount_disk=container_mount_disk) disks = [] if self.CheckDiskMessageArgs(args, skip_defaults): disks = create_utils.CreateDiskMessages( args=args, instance_name=instance_ref.Name(), project=instance_ref.project, location=instance_ref.zone, scope=compute_scopes.ScopeEnum.ZONE, compute_client=compute_client, resource_parser=resource_parser, boot_disk_size_gb=boot_disk_size_gb, image_uri=image_uri, create_boot_disk=self._support_create_boot_disk, support_nvdimm=self._support_nvdimm, support_match_container_mount_disks=self. _support_match_container_mount_disks) machine_type_uri = None if instance_utils.CheckSpecifiedMachineTypeArgs( args, skip_defaults): machine_type_uri = create_utils.CreateMachineTypeUri( args=args, compute_client=compute_client, resource_parser=resource_parser, project=instance_ref.project, location=instance_ref.zone, scope=compute_scopes.ScopeEnum.ZONE, confidential_vm=confidential_vm) guest_accelerators = create_utils.GetAccelerators( args=args, compute_client=compute_client, resource_parser=resource_parser, project=instance_ref.project, location=instance_ref.zone, scope=compute_scopes.ScopeEnum.ZONE) instance = compute_client.messages.Instance( canIpForward=can_ip_forward, disks=disks, guestAccelerators=guest_accelerators, description=args.description, labels=labels, machineType=machine_type_uri, metadata=metadata, minCpuPlatform=args.min_cpu_platform, name=instance_ref.Name(), networkInterfaces=network_interfaces, serviceAccounts=service_accounts, scheduling=scheduling, tags=tags) if args.private_ipv6_google_access_type is not None: instance.privateIpv6GoogleAccess = ( instances_flags.GetPrivateIpv6GoogleAccessTypeFlagMapper( compute_client.messages).GetEnumForChoice( args.private_ipv6_google_access_type)) confidential_instance_config = ( create_utils.BuildConfidentialInstanceConfigMessage( messages=compute_client.messages, args=args)) if confidential_instance_config: instance.confidentialInstanceConfig = confidential_instance_config has_threads_per_core = (self._support_threads_per_core and args.threads_per_core is not None) if (args.enable_nested_virtualization is not None or has_threads_per_core): threads_per_core = args.threads_per_core if has_threads_per_core else None instance.advancedMachineFeatures = ( instance_utils.CreateAdvancedMachineFeaturesMessage( compute_client.messages, args.enable_nested_virtualization, threads_per_core)) shielded_instance_config = create_utils.BuildShieldedInstanceConfigMessage( messages=compute_client.messages, args=args) if shielded_instance_config: instance.shieldedInstanceConfig = shielded_instance_config if self._support_network_performance_configs and \ args.IsSpecified('network_performance_configs'): instance.networkPerformanceConfig = \ instance_utils.GetNetworkPerformanceConfig(args, compute_client) request = compute_client.messages.ComputeInstancesInsertRequest( instance=instance, sourceInstanceTemplate=source_instance_template, project=instance_ref.project, zone=instance_ref.zone) requests.append( (compute_client.apitools_client.instances, 'Insert', request)) return compute_client.MakeRequests(requests)
def Run(self, args): """Issues an InstanceTemplates.Insert request. Args: args: the argparse arguments that this command was invoked with. Returns: an InstanceTemplate message object """ holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client instances_flags.ValidateKonletArgs(args) instances_flags.ValidateDiskCommonFlags(args) instances_flags.ValidateLocalSsdFlags(args) instances_flags.ValidateServiceAccountAndScopeArgs(args) instances_flags.ValidateNetworkTierArgs(args, support_network_tier=True) if instance_utils.UseExistingBootDisk(args.disk or []): raise exceptions.InvalidArgumentException( '--disk', 'Boot disk specified for containerized VM.') boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size) utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type) instance_template_ref = ( CreateWithContainer.InstanceTemplateArg.ResolveAsResource( args, holder.resources)) user_metadata = metadata_utils.ConstructMetadataMessage( client.messages, metadata=args.metadata, metadata_from_file=args.metadata_from_file) containers_utils.ValidateUserMetadata(user_metadata) network_interface = instance_template_utils.CreateNetworkInterfaceMessage( resources=holder.resources, scope_lister=flags.GetDefaultScopeLister(client), messages=client.messages, network=args.network, region=args.region, subnet=args.subnet, address=(instance_template_utils.EPHEMERAL_ADDRESS if not args.no_address and not args.address else args.address), network_tier=getattr(args, 'network_tier', None)) scheduling = instance_utils.CreateSchedulingMessage( messages=client.messages, maintenance_policy=args.maintenance_policy, preemptible=args.preemptible, restart_on_failure=args.restart_on_failure) if args.no_service_account: service_account = None else: service_account = args.service_account service_accounts = instance_utils.CreateServiceAccountMessages( messages=client.messages, scopes=[] if args.no_scopes else args.scopes, service_account=service_account) if (args.IsSpecified('image') or args.IsSpecified('image_family') or args.IsSpecified('image_project')): image_expander = image_utils.ImageExpander(client, holder.resources) image_uri, _ = image_expander.ExpandImageFlag( user_project=instance_template_ref.project, image=args.image, image_family=args.image_family, image_project=args.image_project) if holder.resources.Parse(image_uri).project != 'cos-cloud': log.warn( 'This container deployment mechanism requires a ' 'Container-Optimized OS image in order to work. Select an ' 'image from a cos-cloud project (cost-stable, cos-beta, ' 'cos-dev image families).') else: image_uri = containers_utils.ExpandKonletCosImageFlag(client) machine_type = instance_utils.InterpretMachineType( machine_type=args.machine_type, custom_cpu=args.custom_cpu, custom_memory=args.custom_memory, ext=getattr(args, 'custom_extensions', None)) metadata = containers_utils.CreateKonletMetadataMessage( client.messages, args, instance_template_ref.Name(), user_metadata) request = client.messages.ComputeInstanceTemplatesInsertRequest( instanceTemplate=client.messages.InstanceTemplate( properties=client.messages.InstanceProperties( machineType=machine_type, disks=self._CreateDiskMessages( holder, args, boot_disk_size_gb, image_uri, instance_template_ref.project), canIpForward=args.can_ip_forward, metadata=metadata, minCpuPlatform=args.min_cpu_platform, networkInterfaces=[network_interface], serviceAccounts=service_accounts, scheduling=scheduling, tags=containers_utils.CreateTagsMessage( client.messages, args.tags), ), description=args.description, name=instance_template_ref.Name(), ), project=instance_template_ref.project) return client.MakeRequests([(client.apitools_client.instanceTemplates, 'Insert', request)])