Skip to content

Commit 93239e0

Browse files
authored
Add conserve mode for VPC offerings (#12487)
1 parent 8c57953 commit 93239e0

File tree

44 files changed

+1040
-115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1040
-115
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ jobs:
146146
smoke/test_vm_snapshot_kvm
147147
smoke/test_vm_snapshots
148148
smoke/test_volumes
149+
smoke/test_vpc_conserve_mode
149150
smoke/test_vpc_ipv6
150151
smoke/test_vpc_redundant
151152
smoke/test_vpc_router_nics

api/src/main/java/com/cloud/network/NetworkService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,6 @@ Network createPrivateNetwork(String networkName, String displayText, long physic
279279
IpAddresses getIpAddressesFromIps(String ipAddress, String ip6Address, String macAddress);
280280

281281
String getNicVlanValueForExternalVm(NicTO nic);
282+
283+
Long getPreferredNetworkIdForPublicIpRuleAssignment(IpAddress ip, Long networkId);
282284
}

api/src/main/java/com/cloud/network/lb/LoadBalancingRulesService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ LoadBalancer createPublicLoadBalancerRule(String xId, String name, String descri
108108
/**
109109
* Assign a virtual machine or list of virtual machines, or Map of <vmId vmIp> to a load balancer.
110110
*/
111-
boolean assignToLoadBalancer(long lbRuleId, List<Long> vmIds, Map<Long, List<String>> vmIdIpMap, boolean isAutoScaleVM);
111+
boolean assignToLoadBalancer(long lbRuleId, List<Long> vmIds, Map<Long, List<String>> vmIdIpMap, Map<Long, Long> vmIdNetworkMap, boolean isAutoScaleVM);
112112

113113
boolean assignSSLCertToLoadBalancerRule(Long lbRuleId, String certName, String publicCert, String privateKey);
114114

api/src/main/java/com/cloud/network/vpc/VpcOffering.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ public enum State {
8484
NetworkOffering.RoutingMode getRoutingMode();
8585

8686
Boolean isSpecifyAsNumber();
87+
88+
boolean isConserveMode();
8789
}

api/src/main/java/com/cloud/network/vpc/VpcProvisioningService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ VpcOffering createVpcOffering(String name, String displayText, List<String> supp
3939
Map serviceCapabilitystList, NetUtils.InternetProtocol internetProtocol,
4040
Long serviceOfferingId, String externalProvider, NetworkOffering.NetworkMode networkMode,
4141
List<Long> domainIds, List<Long> zoneIds, VpcOffering.State state,
42-
NetworkOffering.RoutingMode routingMode, boolean specifyAsNumber);
42+
NetworkOffering.RoutingMode routingMode, boolean specifyAsNumber, boolean conserveMode);
4343

4444

4545
Pair<List<? extends VpcOffering>,Integer> listVpcOfferings(ListVPCOfferingsCmd cmd);

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ public class ApiConstants {
986986
public static final String REGION_ID = "regionid";
987987
public static final String VPC_OFF_ID = "vpcofferingid";
988988
public static final String VPC_OFF_NAME = "vpcofferingname";
989+
public static final String VPC_OFFERING_CONSERVE_MODE = "vpcofferingconservemode";
989990
public static final String NETWORK = "network";
990991
public static final String VPC_ID = "vpcid";
991992
public static final String VPC_NAME = "vpcname";

api/src/main/java/org/apache/cloudstack/api/command/admin/vpc/CreateVPCOfferingCmd.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ public class CreateVPCOfferingCmd extends BaseAsyncCreateCmd {
161161
description = "the routing mode for the VPC offering. Supported types are: Static or Dynamic.")
162162
private String routingMode;
163163

164+
@Parameter(name = ApiConstants.CONSERVE_MODE, type = CommandType.BOOLEAN,
165+
since = "4.23.0",
166+
description = "True if the VPC offering is IP conserve mode enabled, allowing public IPs to be used across multiple VPC tiers. Default value is false")
167+
private Boolean conserveMode;
168+
169+
164170
/////////////////////////////////////////////////////
165171
/////////////////// Accessors ///////////////////////
166172
/////////////////////////////////////////////////////
@@ -311,6 +317,10 @@ public String getRoutingMode() {
311317
return routingMode;
312318
}
313319

320+
public boolean isConserveMode() {
321+
return BooleanUtils.toBoolean(conserveMode);
322+
}
323+
314324
@Override
315325
public void create() throws ResourceAllocationException {
316326
VpcOffering vpcOff = _vpcProvSvc.createVpcOffering(this);

api/src/main/java/org/apache/cloudstack/api/command/user/firewall/CreatePortForwardingRuleCmd.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
5555
public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements PortForwardingRule {
5656

57-
5857
// ///////////////////////////////////////////////////
5958
// ////////////// API parameters /////////////////////
6059
// ///////////////////////////////////////////////////
@@ -278,13 +277,7 @@ public State getState() {
278277
@Override
279278
public long getNetworkId() {
280279
IpAddress ip = _entityMgr.findById(IpAddress.class, getIpAddressId());
281-
Long ntwkId = null;
282-
283-
if (ip.getAssociatedWithNetworkId() != null) {
284-
ntwkId = ip.getAssociatedWithNetworkId();
285-
} else {
286-
ntwkId = networkId;
287-
}
280+
Long ntwkId = _networkService.getPreferredNetworkIdForPublicIpRuleAssignment(ip, networkId);
288281
if (ntwkId == null) {
289282
throw new InvalidParameterValueException("Unable to create port forwarding rule for the ipAddress id=" + ipAddressId +
290283
" as ip is not associated with any network and no networkId is passed in");

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/AssignToLoadBalancerRuleCmd.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import com.cloud.network.Network;
27+
import com.cloud.utils.Pair;
2628
import com.cloud.utils.exception.CloudRuntimeException;
2729

2830
import org.apache.cloudstack.api.APICommand;
@@ -72,7 +74,7 @@ public class AssignToLoadBalancerRuleCmd extends BaseAsyncCmd {
7274

7375
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID_IP,
7476
type = CommandType.MAP,
75-
description = "VM ID and IP map, vmidipmap[0].vmid=1 vmidipmap[0].vmip=10.1.1.75",
77+
description = "VM ID and IP map, vmidipmap[0].vmid=1 vmidipmap[0].vmip=10.1.1.75. (Optional, for VPC Conserve Mode) Pass vmnetworkid. Example: vmidipmap[0].vmnetworkid=NETWORK_TIER_UUID",
7678
since = "4.4")
7779
private Map vmIdIpMap;
7880

@@ -116,15 +118,17 @@ public String getEventDescription() {
116118
}
117119

118120

119-
public Map<Long, List<String>> getVmIdIpListMap() {
120-
Map<Long, List<String>> vmIdIpsMap = new HashMap<Long, List<String>>();
121+
public Pair<Map<Long, List<String>>, Map<Long, Long>> getVmIdIpListMapAndVmIdNetworkMap() {
122+
Map<Long, List<String>> vmIdIpsMap = new HashMap<>();
123+
Map<Long, Long> vmIdNetworkMap = new HashMap<>();
121124
if (vmIdIpMap != null && !vmIdIpMap.isEmpty()) {
122125
Collection idIpsCollection = vmIdIpMap.values();
123126
Iterator iter = idIpsCollection.iterator();
124127
while (iter.hasNext()) {
125128
HashMap<String, String> idIpsMap = (HashMap<String, String>)iter.next();
126129
String vmId = idIpsMap.get("vmid");
127130
String vmIp = idIpsMap.get("vmip");
131+
String vmNetworkUuid = idIpsMap.get("vmnetworkid");
128132

129133
VirtualMachine lbvm = _entityMgr.findByUuid(VirtualMachine.class, vmId);
130134
if (lbvm == null) {
@@ -145,25 +149,35 @@ public Map<Long, List<String>> getVmIdIpListMap() {
145149
ipsList = new ArrayList<String>();
146150
}
147151
ipsList.add(vmIp);
152+
153+
if (vmNetworkUuid != null) {
154+
Network vmNetwork = _entityMgr.findByUuid(Network.class, vmNetworkUuid);
155+
if (vmNetwork == null) {
156+
throw new InvalidParameterValueException("Unable to find Network ID: " + vmNetworkUuid);
157+
}
158+
vmIdNetworkMap.put(longVmId, vmNetwork.getId());
159+
}
148160
vmIdIpsMap.put(longVmId, ipsList);
149161

150162
}
151163
}
152164

153-
return vmIdIpsMap;
165+
return new Pair<>(vmIdIpsMap, vmIdNetworkMap);
154166
}
155167

156168
@Override
157169
public void execute() {
158170
CallContext.current().setEventDetails("Load balancer ID: " + getResourceUuid(ApiConstants.ID) + " Instances IDs: " + StringUtils.join(getVirtualMachineIds(), ","));
159171

160-
Map<Long, List<String>> vmIdIpsMap = getVmIdIpListMap();
172+
Pair<Map<Long, List<String>>, Map<Long, Long>> mapsPair = getVmIdIpListMapAndVmIdNetworkMap();
173+
Map<Long, List<String>> vmIdIpsMap = mapsPair.first();
174+
Map<Long, Long> vmIdNetworkMap = mapsPair.second();
161175
boolean result = false;
162176

163177
try {
164-
result = _lbService.assignToLoadBalancer(getLoadBalancerId(), virtualMachineIds, vmIdIpsMap, false);
178+
result = _lbService.assignToLoadBalancer(getLoadBalancerId(), virtualMachineIds, vmIdIpsMap, vmIdNetworkMap, false);
165179
}catch (CloudRuntimeException ex) {
166-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign load balancer rule");
180+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign load balancer rule due to: " + ex.getMessage());
167181
}
168182

169183
if (result) {

api/src/main/java/org/apache/cloudstack/api/response/FirewallRuleResponse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public class FirewallRuleResponse extends BaseResponse {
9494
@Param(description = "The ID of the guest Network the port forwarding rule belongs to")
9595
private String networkId;
9696

97+
@SerializedName(ApiConstants.NETWORK_NAME)
98+
@Param(description = "The Name of the guest Network the port forwarding rule belongs to")
99+
private String networkName;
100+
97101
@SerializedName(ApiConstants.FOR_DISPLAY)
98102
@Param(description = "Is firewall for display to the regular user", since = "4.4", authorized = {RoleType.Admin})
99103
private Boolean forDisplay;
@@ -223,6 +227,10 @@ public void setNetworkId(String networkId) {
223227
this.networkId = networkId;
224228
}
225229

230+
public void setNetworkName(String networkName) {
231+
this.networkName = networkName;
232+
}
233+
226234
public void setForDisplay(Boolean forDisplay) {
227235
this.forDisplay = forDisplay;
228236
}

0 commit comments

Comments
 (0)