Skip to content

enhancement: add instance info as Libvirt metadata #11061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.20
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions api/src/main/java/com/cloud/agent/api/to/VirtualMachineMetadataTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.agent.api.to;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VirtualMachineMetadataTO {
// VM details
private final String name;
private final String internalName;
private final String displayName;
private final String instanceUuid;
private final Integer cpuCores;
private final Integer memory;
private final Long created;
private final Long started;

// Owner details
private final String ownerDomainUuid;
private final String ownerDomainName;
private final String ownerAccountUuid;
private final String ownerAccountName;
private final String ownerProjectUuid;
private final String ownerProjectName;

// Host and service offering
private final String serviceOfferingName;
private final List<String> serviceOfferingHostTags;

// zone, pod, and cluster details
private final String zoneName;
private final String zoneUuid;
private final String podName;
private final String podUuid;
private final String clusterName;
private final String clusterUuid;

// resource tags
private final Map<String, String> resourceTags;

public VirtualMachineMetadataTO(
String name, String internalName, String displayName, String instanceUuid, Integer cpuCores, Integer memory, Long created, Long started,
String ownerDomainUuid, String ownerDomainName, String ownerAccountUuid, String ownerAccountName, String ownerProjectUuid, String ownerProjectName,
String serviceOfferingName, List<String> serviceOfferingHostTags,
String zoneName, String zoneUuid, String podName, String podUuid, String clusterName, String clusterUuid, Map<String, String> resourceTags) {
/*
* Something failed in the metadata shall not be a fatal error, the VM can still be started
* Thus, the unknown fields just get an explicit "unknown" value so it can be fixed in case
* there are bugs on some execution paths.
* */

this.name = (name != null) ? name : "unknown";
this.internalName = (internalName != null) ? internalName : "unknown";
this.displayName = (displayName != null) ? displayName : "unknown";
this.instanceUuid = (instanceUuid != null) ? instanceUuid : "unknown";
this.cpuCores = (cpuCores != null) ? cpuCores : -1;
this.memory = (memory != null) ? memory : -1;
this.created = (created != null) ? created : 0;
this.started = (started != null) ? started : 0;
this.ownerDomainUuid = (ownerDomainUuid != null) ? ownerDomainUuid : "unknown";
this.ownerDomainName = (ownerDomainName != null) ? ownerDomainName : "unknown";
this.ownerAccountUuid = (ownerAccountUuid != null) ? ownerAccountUuid : "unknown";
this.ownerAccountName = (ownerAccountName != null) ? ownerAccountName : "unknown";
this.ownerProjectUuid = (ownerProjectUuid != null) ? ownerProjectUuid : "unknown";
this.ownerProjectName = (ownerProjectName != null) ? ownerProjectName : "unknown";
this.serviceOfferingName = (serviceOfferingName != null) ? serviceOfferingName : "unknown";
this.serviceOfferingHostTags = (serviceOfferingHostTags != null) ? serviceOfferingHostTags : new ArrayList<>();
this.zoneName = (zoneName != null) ? zoneName : "unknown";
this.zoneUuid = (zoneUuid != null) ? zoneUuid : "unknown";
this.podName = (podName != null) ? podName : "unknown";
this.podUuid = (podUuid != null) ? podUuid : "unknown";
this.clusterName = (clusterName != null) ? clusterName : "unknown";
this.clusterUuid = (clusterUuid != null) ? clusterUuid : "unknown";

this.resourceTags = (resourceTags != null) ? resourceTags : new HashMap<>();
}

public String getName() {
return name;
}

public String getInternalName() {
return internalName;
}

public String getDisplayName() {
return displayName;
}

public String getInstanceUuid() {
return instanceUuid;
}

public Integer getCpuCores() {
return cpuCores;
}

public Integer getMemory() {
return memory;
}

public Long getCreated() { return created; }

public Long getStarted() {
return started;
}

public String getOwnerDomainUuid() {
return ownerDomainUuid;
}

public String getOwnerDomainName() {
return ownerDomainName;
}

public String getOwnerAccountUuid() {
return ownerAccountUuid;
}

public String getOwnerAccountName() {
return ownerAccountName;
}

public String getOwnerProjectUuid() {
return ownerProjectUuid;
}

public String getOwnerProjectName() {
return ownerProjectName;
}

public String getserviceOfferingName() {
return serviceOfferingName;
}

public List<String> getserviceOfferingHostTags() {
return serviceOfferingHostTags;
}

public String getZoneName() {
return zoneName;
}

public String getZoneUuid() {
return zoneUuid;
}

public String getPodName() {
return podName;
}

public String getPodUuid() {
return podUuid;
}

public String getClusterName() {
return clusterName;
}

public String getClusterUuid() {
return clusterUuid;
}

public Map<String, String> getResourceTags() { return resourceTags; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class VirtualMachineTO {
DeployAsIsInfoTO deployAsIsInfo;
String metadataManufacturer;
String metadataProductName;
VirtualMachineMetadataTO metadata;

public VirtualMachineTO(long id, String instanceName, VirtualMachine.Type type, int cpus, Integer speed, long minRam, long maxRam, BootloaderType bootloader,
String os, boolean enableHA, boolean limitCpuUse, String vncPassword) {
Expand Down Expand Up @@ -447,6 +448,14 @@ public void setMetadataProductName(String metadataProductName) {
this.metadataProductName = metadataProductName;
}

public VirtualMachineMetadataTO getMetadata() {
return metadata;
}

public void setMetadata(VirtualMachineMetadataTO metadata) {
this.metadata = metadata;
}

@Override
public String toString() {
return String.format("VM {id: \"%s\", name: \"%s\", uuid: \"%s\", type: \"%s\"}", id, name, uuid, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import com.cloud.agent.api.to.VirtualMachineMetadataTO;
import org.apache.cloudstack.api.ApiConstants.IoDriverPolicy;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.storage.command.browser.ListDataStoreObjectsCommand;
Expand Down Expand Up @@ -171,6 +172,7 @@
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef.WatchDogAction;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef.WatchDogModel;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.MetadataDef;
import com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceAgentExecutor;
import com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceExecutor;
import com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceServiceExecutor;
Expand Down Expand Up @@ -2628,9 +2630,19 @@ private void configureVM(VirtualMachineTO vmTO, LibvirtVMDef vm, Map<String, Str
vm.addComp(createClockDef(vmTO));
vm.addComp(createDevicesDef(vmTO, guest, vcpus, isUefiEnabled));

MetadataDef metaDef;
if ((metaDef = createMetadataDef(vmTO)) != null) {
vm.addComp(metaDef);
}

addExtraConfigsToVM(vmTO, vm, extraConfig);
}

protected MetadataDef createMetadataDef(VirtualMachineTO vmTO) {
VirtualMachineMetadataTO metadata = vmTO.getMetadata();
return (metadata != null) ? new MetadataDef(metadata) : null;
}

/**
* Adds extra configuration to User VM Domain XML before starting.
*/
Expand Down
Loading
Loading