Skip to content

Commit f46b788

Browse files
authored
[NSX] Allow NSX isolated networks (#8132)
* Add network offerings for NSX on isolated networks * Fix offerings creation * In progress NSX isolated network * Fixes * Fix NIC allocation to router
1 parent c135fa1 commit f46b788

File tree

13 files changed

+201
-111
lines changed

13 files changed

+201
-111
lines changed

api/src/main/java/com/cloud/offering/NetworkOffering.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ public enum NsxMode {
5757

5858
public final static String DefaultSharedNetworkOfferingWithSGService = "DefaultSharedNetworkOfferingWithSGService";
5959
public static final String DEFAULT_TUNGSTEN_SHARED_NETWORK_OFFERING_WITH_SGSERVICE = "DefaultTungstenSharedNetworkOfferingWithSGService";
60+
public static final String DEFAULT_NAT_NSX_OFFERING_FOR_VPC = "DefaultNATNSXNetworkOfferingForVpc";
61+
public static final String DEFAULT_ROUTED_NSX_OFFERING_FOR_VPC = "DefaultRoutedNSXNetworkOfferingForVpc";
6062
public static final String DEFAULT_NAT_NSX_OFFERING = "DefaultNATNSXNetworkOffering";
61-
public static final String DEFAULT_ROUTER_NSX_OFFERING = "DefaultRouteNSXNetworkOffering";
63+
public static final String DEFAULT_ROUTED_NSX_OFFERING = "DefaultRoutedNSXNetworkOffering";
6264
public final static String QuickCloudNoServices = "QuickCloudNoServices";
6365
public final static String DefaultIsolatedNetworkOfferingWithSourceNatService = "DefaultIsolatedNetworkOfferingWithSourceNatService";
6466
public final static String OvsIsolatedNetworkOfferingWithSourceNatService = "OvsIsolatedNetworkOfferingWithSourceNatService";

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,39 +1475,52 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
14751475
}
14761476

14771477
public void setVmNetworkDetails(VMInstanceVO vm, VirtualMachineTO vmTO) {
1478+
Map<Long, String> networkToNetworkNameMap = new HashMap<>();
14781479
if (VirtualMachine.Type.User.equals(vm.getType())) {
14791480
List<UserVmJoinVO> userVmJoinVOs = userVmJoinDao.searchByIds(vm.getId());
1480-
Map<Long, String> networkToNetworkNameMap = new HashMap<>();
14811481
if (userVmJoinVOs != null && !userVmJoinVOs.isEmpty()) {
14821482
for (UserVmJoinVO userVmJoinVO : userVmJoinVOs) {
1483-
NetworkVO networkVO = _networkDao.findById(userVmJoinVO.getNetworkId());
1484-
Account acc = accountDao.findById(networkVO.getAccountId());
1485-
Domain domain = domainDao.findById(networkVO.getDomainId());
1486-
DataCenter zone = _dcDao.findById(vm.getDataCenterId());
1487-
if (Objects.isNull(zone)) {
1488-
throw new CloudRuntimeException(String.format("Failed to find zone with ID: %s", vm.getDataCenterId()));
1489-
}
1490-
if (Objects.isNull(acc)) {
1491-
throw new CloudRuntimeException(String.format("Failed to find account with ID: %s", networkVO.getAccountId()));
1492-
}
1493-
if (Objects.isNull(domain)) {
1494-
throw new CloudRuntimeException(String.format("Failed to find domain with ID: %s", networkVO.getDomainId()));
1495-
}
1496-
String networkName = String.format("D%s-A%s-Z%s", domain.getId(), acc.getId(), zone.getId());
1497-
if (Objects.isNull(networkVO.getVpcId())) {
1498-
networkName += "-S"+networkVO.getId();
1499-
} else {
1500-
VpcVO vpc = vpcDao.findById(networkVO.getVpcId());
1501-
if (Objects.isNull(vpc)) {
1502-
throw new CloudRuntimeException(String.format("Failed to find VPC with ID: %s", networkVO.getVpcId()));
1503-
}
1504-
networkName = String.format("%s-V%s-S%s", networkName, vpc.getId(), networkVO.getId());
1505-
}
1506-
networkToNetworkNameMap.put(networkVO.getId(), networkName);
1483+
addToNetworkNameMap(userVmJoinVO.getNetworkId(), vm.getDataCenterId(), networkToNetworkNameMap);
15071484
}
15081485
vmTO.setNetworkIdToNetworkNameMap(networkToNetworkNameMap);
15091486
}
1487+
} else if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) {
1488+
List<DomainRouterJoinVO> routerJoinVO = domainRouterJoinDao.getRouterByIdAndTrafficType(vm.getId(), Networks.TrafficType.Guest);
1489+
for (DomainRouterJoinVO router : routerJoinVO) {
1490+
NetworkVO guestNetwork = _networkDao.findById(router.getNetworkId());
1491+
if (guestNetwork.getVpcId() == null && guestNetwork.getBroadcastDomainType() == Networks.BroadcastDomainType.NSX) {
1492+
addToNetworkNameMap(router.getNetworkId(), vm.getDataCenterId(), networkToNetworkNameMap);
1493+
}
1494+
}
1495+
vmTO.setNetworkIdToNetworkNameMap(networkToNetworkNameMap);
1496+
}
1497+
}
1498+
1499+
private void addToNetworkNameMap(long networkId, long dataCenterId, Map<Long, String> networkToNetworkNameMap) {
1500+
NetworkVO networkVO = _networkDao.findById(networkId);
1501+
Account acc = accountDao.findById(networkVO.getAccountId());
1502+
Domain domain = domainDao.findById(networkVO.getDomainId());
1503+
DataCenter zone = _dcDao.findById(dataCenterId);
1504+
if (Objects.isNull(zone)) {
1505+
throw new CloudRuntimeException(String.format("Failed to find zone with ID: %s", dataCenterId));
1506+
}
1507+
if (Objects.isNull(acc)) {
1508+
throw new CloudRuntimeException(String.format("Failed to find account with ID: %s", networkVO.getAccountId()));
1509+
}
1510+
if (Objects.isNull(domain)) {
1511+
throw new CloudRuntimeException(String.format("Failed to find domain with ID: %s", networkVO.getDomainId()));
1512+
}
1513+
String networkName = String.format("D%s-A%s-Z%s", domain.getId(), acc.getId(), zone.getId());
1514+
if (Objects.isNull(networkVO.getVpcId())) {
1515+
networkName += "-S"+networkVO.getId();
1516+
} else {
1517+
VpcVO vpc = vpcDao.findById(networkVO.getVpcId());
1518+
if (Objects.isNull(vpc)) {
1519+
throw new CloudRuntimeException(String.format("Failed to find VPC with ID: %s", networkVO.getVpcId()));
1520+
}
1521+
networkName = String.format("%s-V%s-S%s", networkName, vpc.getId(), networkVO.getId());
15101522
}
1523+
networkToNetworkNameMap.put(networkVO.getId(), networkName);
15111524
}
15121525

15131526
private void logBootModeParameters(Map<VirtualMachineProfile.Param, Object> params) {

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/CreateNsxTier1GatewayCommand.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@
1919
import java.util.Objects;
2020

2121
public class CreateNsxTier1GatewayCommand extends NsxCommand {
22-
private long vpcId;
23-
private String vpcName;
22+
private Long networkResourceId;
23+
private String networkResourceName;
24+
private boolean isResourceVpc;
2425

25-
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, long vpcId, String vpcName) {
26+
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId,
27+
Long networkResourceId, String networkResourceName, boolean isResourceVpc) {
2628
super(domainId, accountId, zoneId);
27-
this.vpcId = vpcId;
28-
this.vpcName = vpcName;
29+
this.networkResourceId = networkResourceId;
30+
this.networkResourceName = networkResourceName;
31+
this.isResourceVpc = isResourceVpc;
2932
}
3033

31-
public long getVpcId() {
32-
return vpcId;
34+
public Long getNetworkResourceId() {
35+
return networkResourceId;
3336
}
3437

35-
public String getVpcName() {
36-
return vpcName;
38+
public boolean isResourceVpc() {
39+
return isResourceVpc;
40+
}
41+
42+
public String getNetworkResourceName() {
43+
return networkResourceName;
3744
}
3845

3946
@Override
@@ -42,11 +49,11 @@ public boolean equals(Object o) {
4249
if (o == null || getClass() != o.getClass()) return false;
4350
if (!super.equals(o)) return false;
4451
CreateNsxTier1GatewayCommand that = (CreateNsxTier1GatewayCommand) o;
45-
return Objects.equals(vpcName, that.vpcName);
52+
return Objects.equals(networkResourceName, that.networkResourceName);
4653
}
4754

4855
@Override
4956
public int hashCode() {
50-
return Objects.hash(super.hashCode(), vpcName);
57+
return Objects.hash(super.hashCode(), networkResourceName);
5158
}
5259
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/DeleteNsxTier1GatewayCommand.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@
1818

1919
public class DeleteNsxTier1GatewayCommand extends NsxCommand {
2020

21-
private Long vpcId;
22-
private String vpcName;
21+
private Long networkResourceId;
22+
private String networkResourceName;
23+
private boolean isResourceVpc;
2324

24-
public DeleteNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, Long vpcId, String vpcName) {
25+
public DeleteNsxTier1GatewayCommand(long domainId, long accountId, long zoneId,
26+
Long networkResourceId, String networkResourceName, boolean isResourceVpc) {
2527
super(domainId, accountId, zoneId);
26-
this.vpcId = vpcId;
27-
this.vpcName = vpcName;
28+
this.networkResourceId = networkResourceId;
29+
this.networkResourceName = networkResourceName;
30+
this.isResourceVpc = isResourceVpc;
2831
}
2932

30-
public Long getVpcId() {
31-
return vpcId;
33+
public Long getNetworkResourceId() {
34+
return networkResourceId;
3235
}
3336

34-
public String getVpcName() {
35-
return vpcName;
37+
public String getNetworkResourceName() {
38+
return networkResourceName;
39+
}
40+
41+
public boolean isResourceVpc() {
42+
return isResourceVpc;
3643
}
3744
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/resource/NsxResource.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.HashMap;
4949
import java.util.List;
5050
import java.util.Map;
51+
import java.util.Objects;
5152
import java.util.stream.Collectors;
5253

5354
public class NsxResource implements ServerResource {
@@ -213,7 +214,7 @@ private Answer executeRequest(CreateNsxDhcpRelayConfigCommand cmd) {
213214
long zoneId = cmd.getZoneId();
214215
long domainId = cmd.getDomainId();
215216
long accountId = cmd.getAccountId();
216-
long vpcId = cmd.getVpcId();
217+
Long vpcId = cmd.getVpcId();
217218
long networkId = cmd.getNetworkId();
218219
String vpcName = cmd.getVpcName();
219220
String networkName = cmd.getNetworkName();
@@ -253,18 +254,20 @@ private Answer executeRequest(ReadyCommand cmd) {
253254
}
254255

255256
private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
256-
String name = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId());
257+
String name = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getNetworkResourceId(), cmd.isResourceVpc());
257258
try {
258259
nsxApiClient.createTier1Gateway(name, tier0Gateway, edgeCluster);
259260
return new NsxAnswer(cmd, true, "");
260261
} catch (CloudRuntimeException e) {
261-
LOGGER.error(String.format("Cannot create tier 1 gateway %s (VPC: %s): %s", name, cmd.getVpcName(), e.getMessage()));
262+
String msg = String.format("Cannot create tier 1 gateway %s (%s: %s): %s", name,
263+
(cmd.isResourceVpc() ? "VPC" : "NETWORK"), cmd.getNetworkResourceName(), e.getMessage());
264+
LOGGER.error(msg);
262265
return new NsxAnswer(cmd, e);
263266
}
264267
}
265268

266269
private Answer executeRequest(DeleteNsxTier1GatewayCommand cmd) {
267-
String tier1Id = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId());
270+
String tier1Id = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getNetworkResourceId(), cmd.isResourceVpc());
268271
try {
269272
nsxApiClient.deleteTier1Gateway(tier1Id);
270273
} catch (Exception e) {
@@ -309,8 +312,11 @@ private Answer executeRequest(CreateNsxSegmentCommand cmd) {
309312
String segmentName = NsxControllerUtils.getNsxSegmentId(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId(), cmd.getNetworkId());
310313
String gatewayAddress = cmd.getNetworkGateway() + "/" + cmd.getNetworkCidr().split("/")[1];
311314

312-
nsxApiClient.createSegment(cmd.getZoneId(), cmd.getDomainId(), cmd.getAccountId(), cmd.getVpcId(),
313-
segmentName, gatewayAddress, tier0Gateway, enforcementPointPath, transportZones);
315+
Long networkResourceId = Objects.isNull(cmd.getVpcId()) ? cmd.getNetworkId() : cmd.getVpcId();
316+
boolean isResourceVpc = !Objects.isNull(cmd.getVpcId());
317+
String tier1GatewayName = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(),
318+
cmd.getZoneId(), networkResourceId, isResourceVpc);
319+
nsxApiClient.createSegment(segmentName, tier1GatewayName, gatewayAddress, enforcementPointPath, transportZones);
314320
} catch (Exception e) {
315321
LOGGER.error(String.format("Failed to create network: %s", cmd.getNetworkName()));
316322
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxApiClient.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
import java.util.List;
5252
import java.util.function.Function;
5353

54-
import static java.util.Objects.isNull;
55-
5654
public class NsxApiClient {
5755

5856
private final Function<Class<? extends Service>, Service> nsxService;
@@ -249,7 +247,8 @@ public TransportZoneListResult getTransportZones() {
249247
}
250248
}
251249

252-
public void createSegment(long zoneId, long domainId, long accountId, Long vpcId, String segmentName, String gatewayAddress, String tier0Gateway, String enforcementPointPath, List<TransportZone> transportZones) {
250+
public void createSegment(String segmentName, String tier1GatewayName, String gatewayAddress, String enforcementPointPath,
251+
List<TransportZone> transportZones) {
253252
try {
254253
Segments segmentService = (Segments) nsxService.apply(Segments.class);
255254
SegmentSubnet subnet = new SegmentSubnet.Builder()
@@ -259,8 +258,7 @@ public void createSegment(long zoneId, long domainId, long accountId, Long vpcId
259258
.setResourceType(SEGMENT_RESOURCE_TYPE)
260259
.setId(segmentName)
261260
.setDisplayName(segmentName)
262-
.setConnectivityPath(isNull(vpcId) ? TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway
263-
: TIER_1_GATEWAY_PATH_PREFIX + NsxControllerUtils.getTier1GatewayName(domainId, accountId, zoneId, vpcId))
261+
.setConnectivityPath(TIER_1_GATEWAY_PATH_PREFIX + tier1GatewayName)
264262
.setAdminState(AdminState.UP.name())
265263
.setSubnets(List.of(subnet))
266264
.setTransportZonePath(enforcementPointPath + "/transport-zones/" + transportZones.get(0).getId())

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxElement.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ public Network.Provider getProvider() {
179179

180180
@Override
181181
public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
182+
// Account account = accountMgr.getAccount(network.getAccountId());
183+
// DomainVO domain = domainDao.findById(network.getDomainId());
184+
// return nsxService.createNetwork(network.getDataCenterId(), account.getId(), domain.getId(), network.getId(), network.getName());
185+
// TODO: Check if the network is NSX based (was already implemented as part of the guru.setup()
182186
return true;
183187
}
184188

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxGuestNetworkGuru.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.apache.cloudstack.NsxAnswer;
5050
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
5151
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
52+
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
5253
import org.apache.cloudstack.utils.NsxControllerUtils;
5354

5455
import org.apache.cloudstack.utils.NsxHelper;
@@ -206,9 +207,8 @@ public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfil
206207
}
207208
VpcVO vpc = _vpcDao.findById(network.getVpcId());
208209
if (Objects.isNull(vpc)) {
209-
String msg = String.format("Unable to find VPC with id: %s", network.getVpcId());
210-
LOGGER.error(msg);
211-
throw new CloudRuntimeException(msg);
210+
String msg = String.format("Unable to find VPC with id: %s, allocating for network %s", network.getVpcId(), network.getName());
211+
LOGGER.debug(msg);
212212
}
213213

214214
DomainVO domain = domainDao.findById(account.getDomainId());
@@ -270,14 +270,6 @@ public void commitMigration(NicProfile nic, Network network, VirtualMachineProfi
270270
}
271271

272272
private void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
273-
String vpcName = null;
274-
if (nonNull(networkVO.getVpcId())) {
275-
VpcVO vpc = _vpcDao.findById(networkVO.getVpcId());
276-
if (isNull(vpc)) {
277-
throw new CloudRuntimeException(String.format("Failed to find VPC network with id: %s", networkVO.getVpcId()));
278-
}
279-
vpcName = vpc.getName();
280-
}
281273
Account account = accountDao.findById(networkVO.getAccountId());
282274
if (isNull(account)) {
283275
throw new CloudRuntimeException(String.format("Unable to find account with id: %s", networkVO.getAccountId()));
@@ -288,6 +280,23 @@ private void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
288280
LOGGER.error(msg);
289281
throw new CloudRuntimeException(msg);
290282
}
283+
String vpcName = null;
284+
if (nonNull(networkVO.getVpcId())) {
285+
VpcVO vpc = _vpcDao.findById(networkVO.getVpcId());
286+
if (isNull(vpc)) {
287+
throw new CloudRuntimeException(String.format("Failed to find VPC network with id: %s", networkVO.getVpcId()));
288+
}
289+
vpcName = vpc.getName();
290+
} else {
291+
LOGGER.debug(String.format("Creating a Tier 1 Gateway for the network %s before creating the NSX segment", networkVO.getName()));
292+
CreateNsxTier1GatewayCommand nsxTier1GatewayCommand = NsxHelper.createNsxTier1GatewayCommand(domain, account, zone, networkVO.getId(), networkVO.getName(), false);
293+
NsxAnswer nsxAnswer = nsxControllerUtils.sendNsxCommand(nsxTier1GatewayCommand, zone.getId());
294+
if (!nsxAnswer.getResult()) {
295+
String msg = String.format("Could not create a Tier 1 Gateway for network %s: %s", networkVO.getName(), nsxAnswer.getDetails());
296+
LOGGER.error(msg);
297+
throw new CloudRuntimeException(msg);
298+
}
299+
}
291300
CreateNsxSegmentCommand command = NsxHelper.createNsxSegmentCommand(domain, account, zone, vpcName, networkVO);
292301
NsxAnswer answer = nsxControllerUtils.sendNsxCommand(command, zone.getId());
293302
if (!answer.getResult()) {

0 commit comments

Comments
 (0)