def _CreateGcePlacementGroupSpec(group_style=_STRATEGY):
    FLAGS.placement_group_style = group_style
    return gce_placement_group.GcePlacementGroupSpec('GcePlacementGroupSpec',
                                                     zone=_ZONE,
                                                     project=_PROJECT,
                                                     num_vms=2,
                                                     flag_values=FLAGS)
Пример #2
0
    def __init__(self, network_spec: GceNetworkSpec):
        super(GceNetwork, self).__init__(network_spec)
        self.project: Optional[str] = network_spec.project
        self.vpn_gateway: Dict[str, GceVpnGateway] = {}

        #  Figuring out the type of network here.
        #  Precedence: User Managed > MULTI > SINGLE > DEFAULT
        self.net_type = network.NetType.DEFAULT.value
        self.cidr = NETWORK_RANGE
        if FLAGS.gce_subnet_region:
            self.net_type = network.NetType.SINGLE.value
            self.cidr = FLAGS.gce_subnet_addr
        if network_spec.cidr:
            self.net_type = network.NetType.MULTI.value
            self.cidr = network_spec.cidr
        self.mtu = network_spec.mtu

        name = self._MakeGceNetworkName()

        subnet_region = (FLAGS.gce_subnet_region if not network_spec.cidr else
                         util.GetRegionFromZone(network_spec.zone))
        mode = 'auto' if subnet_region is None else 'custom'
        self.network_resource = GceNetworkResource(name, mode, self.project,
                                                   self.mtu)
        if subnet_region is None:
            self.subnet_resource = None
        else:
            self.subnet_resource = GceSubnetResource(
                FLAGS.gce_subnet_name or name, name, subnet_region, self.cidr,
                self.project)

        # Stage FW rules.
        self.all_nets = self._GetNetworksFromSpec(
            network_spec)  # Holds the different networks in this run.
        # Holds FW rules for any external subnets.
        self.external_nets_rules: Dict[str, GceFirewallRule] = {}

        #  Set the default rule to allow all traffic within this network's subnet.
        firewall_name = self._MakeGceFWRuleName()
        self.default_firewall_rule = GceFirewallRule(firewall_name,
                                                     self.project, ALLOW_ALL,
                                                     name, self.cidr)

        # Set external rules to allow traffic from other subnets in this benchmark.
        for ext_net in self.all_nets:
            if ext_net == self.cidr:
                continue  # We've already added our own network to the default rule.
            rule_name = self._MakeGceFWRuleName(dst_cidr=ext_net)
            self.external_nets_rules[rule_name] = GceFirewallRule(
                rule_name, self.project, ALLOW_ALL, name, ext_net)

        # Add VpnGateways to the network.
        if FLAGS.use_vpn:
            for gatewaynum in range(0, FLAGS.vpn_service_gateway_count):
                vpn_gateway_name = 'vpngw-%s-%s-%s' % (util.GetRegionFromZone(
                    network_spec.zone), gatewaynum, FLAGS.run_uri)
                self.vpn_gateway[vpn_gateway_name] = GceVpnGateway(
                    vpn_gateway_name, name,
                    util.GetRegionFromZone(network_spec.zone),
                    network_spec.cidr, self.project)

        # Add GCE Placement Group
        no_placement_group = (not FLAGS.placement_group_style
                              or FLAGS.placement_group_style
                              == placement_group.PLACEMENT_GROUP_NONE)
        if no_placement_group:
            self.placement_group = None
        else:
            placement_group_spec = gce_placement_group.GcePlacementGroupSpec(
                'GcePlacementGroupSpec',
                flag_values=FLAGS,
                zone=network_spec.zone,
                project=self.project,
                num_vms=self._GetNumberVms())
            self.placement_group = gce_placement_group.GcePlacementGroup(
                placement_group_spec)