Exemple #1
0
 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)
Exemple #2
0
    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)
Exemple #3
0
    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)