Skip to content

Commit c9ce3e2

Browse files
nvazquezyadvr
authored andcommitted
router: Persistent DHCP leases file on VRs and cleanup /etc/hosts on VM deletion (apache#3351)
Since the CloudStack virtual router was redesigned on version 4.6 it has been observed that the DHCP leases file is not persistent across network operations. This causes conflicts on guest VMs static IPs, causing these static IPs to not be renewed by the DHCP server running on isolated and VPC networks' virtual routers (dnsmasq). On stopping or destroying a VM, its dhcp/dns records are not removed from the virtual router causing ghost effects. Fixes apache#3272 Fixes apache#3354 Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 2484527 commit c9ce3e2

23 files changed

Lines changed: 205 additions & 17 deletions

File tree

api/src/com/cloud/network/element/DhcpServiceProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ boolean configDhcpSupportForSubnet(Network network, NicProfile nic, VirtualMachi
3737
boolean removeDhcpSupportForSubnet(Network network) throws ResourceUnavailableException;
3838

3939
boolean setExtraDhcpOptions(Network network, long nicId, Map<Integer, String> dhcpOptions);
40+
41+
boolean removeDhcpEntry(Network network, NicProfile nic, VirtualMachineProfile vmProfile) throws ResourceUnavailableException;
4042
}

core/src/com/cloud/agent/api/routing/DhcpEntryCommand.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public class DhcpEntryCommand extends NetworkElementCommand {
3535
String duid;
3636
private boolean isDefault;
3737
boolean executeInSequence = false;
38+
boolean remove;
39+
40+
public boolean isRemove() {
41+
return remove;
42+
}
43+
44+
public void setRemove(boolean remove) {
45+
this.remove = remove;
46+
}
3847

3948
protected DhcpEntryCommand() {
4049

core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,4 @@ public class VRScripts {
6868
public static final String UPDATE_HOST_PASSWD = "update_host_passwd.sh";
6969

7070
public static final String VR_CFG = "vr_cfg.sh";
71-
7271
}

core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.IOException;
2323
import java.net.InetSocketAddress;
2424
import java.nio.channels.SocketChannel;
25-
import org.joda.time.Duration;
2625
import java.util.ArrayList;
2726
import java.util.HashMap;
2827
import java.util.List;
@@ -41,6 +40,7 @@
4140
import org.apache.cloudstack.ca.SetupKeystoreAnswer;
4241
import org.apache.cloudstack.utils.security.KeyStoreUtils;
4342
import org.apache.log4j.Logger;
43+
import org.joda.time.Duration;
4444

4545
import com.cloud.agent.api.Answer;
4646
import com.cloud.agent.api.CheckRouterAnswer;

core/src/com/cloud/agent/resource/virtualnetwork/facade/DhcpEntryConfigItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
3535
final DhcpEntryCommand command = (DhcpEntryCommand) cmd;
3636

3737
final VmDhcpConfig vmDhcpConfig = new VmDhcpConfig(command.getVmName(), command.getVmMac(), command.getVmIpAddress(), command.getVmIp6Address(), command.getDuid(), command.getDefaultDns(),
38-
command.getDefaultRouter(), command.getStaticRoutes(), command.isDefault());
38+
command.getDefaultRouter(), command.getStaticRoutes(), command.isDefault(), command.isRemove());
3939

4040
return generateConfigItems(vmDhcpConfig);
4141
}

core/src/com/cloud/agent/resource/virtualnetwork/model/VmDhcpConfig.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ public class VmDhcpConfig extends ConfigBase {
3030
private String staticRoutes;
3131
private boolean defaultEntry;
3232

33+
// Indicate if the entry should be removed when set to true
34+
private boolean remove;
35+
3336
public VmDhcpConfig() {
3437
super(VM_DHCP);
3538
}
3639

3740
public VmDhcpConfig(String hostName, String macAddress, String ipv4Address, String ipv6Address, String ipv6Duid, String dnsAddresses, String defaultGateway,
38-
String staticRoutes, boolean defaultEntry) {
41+
String staticRoutes, boolean defaultEntry, boolean remove) {
3942
super(VM_DHCP);
4043
this.hostName = hostName;
4144
this.macAddress = macAddress;
@@ -46,6 +49,7 @@ public VmDhcpConfig(String hostName, String macAddress, String ipv4Address, Stri
4649
this.defaultGateway = defaultGateway;
4750
this.staticRoutes = staticRoutes;
4851
this.defaultEntry = defaultEntry;
52+
this.remove = remove;
4953
}
5054

5155
public String getHostName() {
@@ -64,6 +68,14 @@ public void setMacAddress(String macAddress) {
6468
this.macAddress = macAddress;
6569
}
6670

71+
public boolean isRemove() {
72+
return remove;
73+
}
74+
75+
public void setRemove(boolean remove) {
76+
this.remove = remove;
77+
}
78+
6779
public String getIpv4Address() {
6880
return ipv4Address;
6981
}

engine/api/src/org/apache/cloudstack/engine/orchestration/service/NetworkOrchestrationService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,8 @@ void implementNetworkElementsAndResources(DeployDestination dest, ReservationCon
309309
*/
310310
boolean areRoutersRunning(final List<? extends VirtualRouter> routers);
311311

312+
/**
313+
* Remove entry from /etc/dhcphosts and /etc/hosts on virtual routers
314+
*/
315+
void cleanupNicDhcpDnsEntry(Network network, VirtualMachineProfile vmProfile, NicProfile nicProfile);
312316
}

engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,34 @@ public boolean areRoutersRunning(final List<? extends VirtualRouter> routers) {
29122912
return true;
29132913
}
29142914

2915+
/**
2916+
* Cleanup entry on VR file specified by type
2917+
*/
2918+
@Override
2919+
public void cleanupNicDhcpDnsEntry(Network network, VirtualMachineProfile vmProfile, NicProfile nicProfile) {
2920+
2921+
final List<Provider> networkProviders = getNetworkProviders(network.getId());
2922+
for (final NetworkElement element : networkElements) {
2923+
if (networkProviders.contains(element.getProvider())) {
2924+
if (!_networkModel.isProviderEnabledInPhysicalNetwork(_networkModel.getPhysicalNetworkId(network), element.getProvider().getName())) {
2925+
throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + " either doesn't exist or is not enabled in physical network id: "
2926+
+ network.getPhysicalNetworkId());
2927+
}
2928+
if (vmProfile.getType() == Type.User && element.getProvider() != null) {
2929+
if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.Dhcp)
2930+
&& _networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.Dhcp, element.getProvider()) && element instanceof DhcpServiceProvider) {
2931+
final DhcpServiceProvider sp = (DhcpServiceProvider) element;
2932+
try {
2933+
sp.removeDhcpEntry(network, nicProfile, vmProfile);
2934+
} catch (ResourceUnavailableException e) {
2935+
s_logger.error("Failed to remove dhcp-dns entry due to: ", e);
2936+
}
2937+
}
2938+
}
2939+
}
2940+
}
2941+
}
2942+
29152943
/**
29162944
* rollingRestartRouters performs restart of routers of a network by first
29172945
* deploying a new VR and then destroying old VRs in rolling fashion. For

plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpElement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,9 @@ public boolean setExtraDhcpOptions(Network network, long nicId, Map<Integer, Str
185185
return false;
186186
}
187187

188+
@Override
189+
public boolean removeDhcpEntry(Network network, NicProfile nic, VirtualMachineProfile vmProfile) throws ResourceUnavailableException {
190+
return false;
191+
}
192+
188193
}

plugins/network-elements/juniper-contrail/src/org/apache/cloudstack/network/contrail/management/ContrailElementImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,9 @@ public boolean removeDhcpSupportForSubnet(Network network)
379379
public boolean setExtraDhcpOptions(Network network, long nicId, Map<Integer, String> dhcpOptions) {
380380
return false;
381381
}
382+
383+
@Override
384+
public boolean removeDhcpEntry(Network network, NicProfile nic, VirtualMachineProfile vmProfile) {
385+
return false;
386+
}
382387
}

0 commit comments

Comments
 (0)