Skip to content

Commit 6491a69

Browse files
ustcweizhouDaanHoogland
authored andcommitted
bugfix #2 vpc: Fix remove first public ip will remove all ips on the nic
1 parent 81ac9f9 commit 6491a69

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

server/src/main/java/com/cloud/network/rules/NicPlugInOutRules.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import com.cloud.network.Networks.BroadcastDomainType;
3939
import com.cloud.network.Networks.IsolationType;
4040
import com.cloud.network.PublicIpAddress;
41+
import com.cloud.network.dao.IPAddressDao;
42+
import com.cloud.network.dao.IPAddressVO;
4143
import com.cloud.network.router.VirtualRouter;
4244
import com.cloud.network.vpc.VpcManager;
4345
import com.cloud.network.vpc.VpcVO;
@@ -159,6 +161,8 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
159161

160162
VpcManager vpcMgr = visitor.getVirtualNetworkApplianceFactory().getVpcMgr();
161163
NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
164+
IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
165+
162166
// find out nics to unplug
163167
for (PublicIpAddress ip : _ipAddresses) {
164168
long publicNtwkId = ip.getNetworkId();
@@ -170,10 +174,23 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
170174
}
171175

172176
if (ip.getState() == IpAddress.State.Releasing) {
173-
Nic nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
177+
NicVO nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
174178
if (nic != null) {
175-
nicsToUnplug.put(ip.getVlanTag(), ip);
176-
s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
179+
final List<IPAddressVO> allIps = ipAddressDao.listByAssociatedVpc(ip.getVpcId(), null);
180+
boolean ipUpdated = false;
181+
for (IPAddressVO allIp : allIps) {
182+
if (allIp.getId() != ip.getId() && allIp.getVlanId() == ip.getVlanId() && allIp.getVmIp() != null) {
183+
s_logger.debug("Updating the nic " + nic + " with new ip address " + allIp.getAddress().addr());
184+
nic.setIPv4Address(allIp.getAddress().addr());
185+
nicDao.update(nic.getId(), nic);
186+
ipUpdated = true;
187+
break;
188+
}
189+
}
190+
if (!ipUpdated) {
191+
nicsToUnplug.put(ip.getVlanTag(), ip);
192+
s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
193+
}
177194
}
178195
}
179196
}
@@ -215,4 +232,4 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
215232

216233
return nicsToChange;
217234
}
218-
}
235+
}

0 commit comments

Comments
 (0)