Skip to content

Commit 1a0a61f

Browse files
mlsorensenMarcus SorensenDaanHooglandGutoVeronezi
authored
Allow option of exposing VM domain info via instance metadata (#6710)
This PR allows the cloud admin to set either a global or domain-specific value "metadata.allow.expose.domain", and when set this allows the VM to see the name and ID of the immediate domain that contains the VM in instance metadata. This can be useful or a variety of things such as bootstrapping VM configuration and access according to domain. This PR also deletes the CloudZonesNetworkElement because it isn't referred to anywhere, and there was initially some confusion as to whether this code needed to be updated when extending metadata. If it needs to be kept we can remove that delete from the PR. Signed-off-by: Marcus Sorensen <mls@apple.com> Co-authored-by: Marcus Sorensen <mls@apple.com> Co-authored-by: dahn <daan.hoogland@gmail.com> Co-authored-by: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com>
1 parent 434f15a commit 1a0a61f

File tree

9 files changed

+99
-282
lines changed

9 files changed

+99
-282
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ public interface NetworkModel {
7070
String PUBLIC_KEYS_FILE = "public-keys";
7171
String CLOUD_IDENTIFIER_FILE = "cloud-identifier";
7272
String HYPERVISOR_HOST_NAME_FILE = "hypervisor-host-name";
73+
String CLOUD_DOMAIN_FILE = "cloud-domain";
74+
String CLOUD_DOMAIN_ID_FILE = "cloud-domain-id";
7375
int CONFIGDATA_DIR = 0;
7476
int CONFIGDATA_FILE = 1;
7577
int CONFIGDATA_CONTENT = 2;
76-
ImmutableMap<String, String> openStackFileMapping = ImmutableMap.of(
77-
AVAILABILITY_ZONE_FILE, "availability_zone",
78-
LOCAL_HOSTNAME_FILE, "hostname",
79-
VM_ID_FILE, "uuid",
80-
PUBLIC_HOSTNAME_FILE, "name"
81-
);
78+
ImmutableMap<String, String> openStackFileMapping = ImmutableMap.<String, String>builder()
79+
.put(AVAILABILITY_ZONE_FILE, "availability_zone")
80+
.put(LOCAL_HOSTNAME_FILE, "hostname")
81+
.put(VM_ID_FILE, "uuid")
82+
.put(PUBLIC_HOSTNAME_FILE, "name")
83+
.put(CLOUD_DOMAIN_FILE, CLOUD_DOMAIN_FILE)
84+
.put(CLOUD_DOMAIN_ID_FILE, CLOUD_DOMAIN_ID_FILE)
85+
.put(HYPERVISOR_HOST_NAME_FILE, HYPERVISOR_HOST_NAME_FILE)
86+
.build();
8287

8388
static final ConfigKey<Integer> MACIdentifier = new ConfigKey<Integer>("Advanced",Integer.class, "mac.identifier", "0",
8489
"This value will be used while generating the mac addresses for isolated and shared networks. The hexadecimal equivalent value will be present at the 2nd octet of the mac address. Default value is null which means this feature is disabled.Its scope is global.", true, ConfigKey.Scope.Global);

engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public interface VirtualMachineManager extends Manager {
7979
ConfigKey<Boolean> AllowExposeHypervisorHostname = new ConfigKey<Boolean>("Advanced", Boolean.class, "global.allow.expose.host.hostname",
8080
"false", "If set to true, it allows the hypervisor host name on which the VM is spawned on to be exposed to the VM", true, ConfigKey.Scope.Global);
8181

82+
ConfigKey<Boolean> AllowExposeDomainInMetadata = new ConfigKey<>("Advanced", Boolean.class, "metadata.allow.expose.domain",
83+
"false", "If set to true, it allows the VM's domain to be seen in metadata.", true, ConfigKey.Scope.Domain);
84+
8285
interface Topics {
8386
String VM_POWER_STATE = "vm.powerstate";
8487
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4611,7 +4611,9 @@ public ConfigKey<?>[] getConfigKeys() {
46114611
return new ConfigKey<?>[] { ClusterDeltaSyncInterval, StartRetry, VmDestroyForcestop, VmOpCancelInterval, VmOpCleanupInterval, VmOpCleanupWait,
46124612
VmOpLockStateRetry, VmOpWaitInterval, ExecuteInSequence, VmJobCheckInterval, VmJobTimeout, VmJobStateReportInterval,
46134613
VmConfigDriveLabel, VmConfigDriveOnPrimaryPool, VmConfigDriveForceHostCacheUse, VmConfigDriveUseHostCacheOnUnsupportedPool,
4614-
HaVmRestartHostUp, ResourceCountRunningVMsonly, AllowExposeHypervisorHostname, AllowExposeHypervisorHostnameAccountLevel, SystemVmRootDiskSize };
4614+
HaVmRestartHostUp, ResourceCountRunningVMsonly, AllowExposeHypervisorHostname, AllowExposeHypervisorHostnameAccountLevel, SystemVmRootDiskSize,
4615+
AllowExposeDomainInMetadata
4616+
};
46154617
}
46164618

46174619
public List<StoragePoolAllocator> getStoragePoolAllocators() {

server/src/main/java/com/cloud/network/NetworkModelImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import javax.inject.Inject;
3535
import javax.naming.ConfigurationException;
3636

37+
import com.cloud.domain.Domain;
38+
import com.cloud.vm.VirtualMachineManager;
3739
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
3840
import org.apache.cloudstack.context.CallContext;
3941
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
@@ -2555,6 +2557,10 @@ public List<String[]> generateVmData(String userData, String serviceOffering, lo
25552557
final String zoneName = dcVo.getName();
25562558

25572559
IPAddressVO publicIp = _ipAddressDao.findByAssociatedVmId(vmId);
2560+
VirtualMachine vm = _vmDao.findById(vmId);
2561+
if (vm == null) {
2562+
throw new CloudRuntimeException(String.format("Cannot generate VM instance data, no VM exists by ID: %d", vmId));
2563+
}
25582564

25592565
final List<String[]> vmData = new ArrayList<String[]>();
25602566

@@ -2623,6 +2629,14 @@ public List<String[]> generateVmData(String userData, String serviceOffering, lo
26232629
vmData.add(new String[]{PASSWORD_DIR, PASSWORD_FILE, password});
26242630
}
26252631
vmData.add(new String[]{METATDATA_DIR, HYPERVISOR_HOST_NAME_FILE, hostname});
2632+
2633+
Domain domain = _domainDao.findById(vm.getDomainId());
2634+
if (domain != null && VirtualMachineManager.AllowExposeDomainInMetadata.valueIn(domain.getId())) {
2635+
s_logger.debug("Adding domain info to cloud metadata");
2636+
vmData.add(new String[]{METATDATA_DIR, CLOUD_DOMAIN_FILE, domain.getName()});
2637+
vmData.add(new String[]{METATDATA_DIR, CLOUD_DOMAIN_ID_FILE, domain.getUuid()});
2638+
}
2639+
26262640
return vmData;
26272641
}
26282642

server/src/main/java/com/cloud/network/element/CloudZonesNetworkElement.java

Lines changed: 0 additions & 270 deletions
This file was deleted.

server/src/main/java/com/cloud/network/router/CommandSetupHelper.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import javax.inject.Inject;
2828

29+
import com.cloud.domain.Domain;
30+
import com.cloud.domain.dao.DomainDao;
2931
import org.apache.cloudstack.api.ApiConstants;
3032
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
3133
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
@@ -149,6 +151,8 @@ public class CommandSetupHelper {
149151
@Inject
150152
private EntityManager _entityMgr;
151153

154+
@Inject
155+
private DomainDao domainDao;
152156
@Inject
153157
private NicDao _nicDao;
154158
@Inject
@@ -210,11 +214,18 @@ public void createVmDataCommand(final VirtualRouter router, final UserVm vm, fin
210214

211215
Host host = _hostDao.findById(vm.getHostId());
212216
String destHostname = VirtualMachineManager.getHypervisorHostname(host != null ? host.getName() : "");
213-
cmds.addCommand(
214-
"vmdata",
215-
generateVmDataCommand(router, nic.getIPv4Address(), vm.getUserData(), serviceOffering, zoneName,
216-
staticNatIp == null || staticNatIp.getState() != IpAddress.State.Allocated ? null : staticNatIp.getAddress().addr(), vm.getHostName(), vm.getInstanceName(),
217-
vm.getId(), vm.getUuid(), publicKey, nic.getNetworkId(), destHostname));
217+
VmDataCommand vmDataCommand = generateVmDataCommand(router, nic.getIPv4Address(), vm.getUserData(), serviceOffering, zoneName,
218+
staticNatIp == null || staticNatIp.getState() != IpAddress.State.Allocated ? null : staticNatIp.getAddress().addr(), vm.getHostName(), vm.getInstanceName(),
219+
vm.getId(), vm.getUuid(), publicKey, nic.getNetworkId(), destHostname);
220+
221+
Domain domain = domainDao.findById(vm.getDomainId());
222+
if (domain != null && VirtualMachineManager.AllowExposeDomainInMetadata.valueIn(domain.getId())) {
223+
s_logger.debug("Adding domain info to cloud metadata");
224+
vmDataCommand.addVmData(NetworkModel.METATDATA_DIR, NetworkModel.CLOUD_DOMAIN_FILE, domain.getName());
225+
vmDataCommand.addVmData(NetworkModel.METATDATA_DIR, NetworkModel.CLOUD_DOMAIN_ID_FILE, domain.getUuid());
226+
}
227+
228+
cmds.addCommand("vmdata", vmDataCommand);
218229
}
219230
}
220231

0 commit comments

Comments
 (0)