From a8700bff7f713cdb60ed96552a4cedaf1872dee6 Mon Sep 17 00:00:00 2001 From: sato03 Date: Fri, 20 Oct 2023 06:40:10 -0300 Subject: [PATCH] server: set Default NIC when VM has no default NIC (#7859) Co-authored-by: Henrique Sato --- .../engine/orchestration/NetworkOrchestrator.java | 14 +++++++++++++- .../main/java/com/cloud/vm/UserVmManagerImpl.java | 14 ++++++++++++++ ui/public/locales/en.json | 1 + ui/public/locales/pt_BR.json | 1 + ui/src/views/tools/ImportUnmanagedInstance.vue | 7 +++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java b/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java index 11e27de8e997..ba9ef03ede6d 100644 --- a/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java +++ b/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java @@ -4331,7 +4331,9 @@ public NicProfile createNicForVm(final Network network, final NicProfile request if (nic == null || vmProfile.getType() == VirtualMachine.Type.User) { final int deviceId = _nicDao.getFreeDeviceId(vm.getId()); - nic = allocateNic(requested, network, false, deviceId, vmProfile).first(); + boolean isDefaultNic = getNicProfileDefaultNic(requested); + + nic = allocateNic(requested, network, isDefaultNic, deviceId, vmProfile).first(); if (nic == null) { throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network); @@ -4359,6 +4361,16 @@ public NicProfile createNicForVm(final Network network, final NicProfile request return nic; } + private boolean getNicProfileDefaultNic(NicProfile nicProfile) { + if (nicProfile != null) { + s_logger.debug(String.format("Using requested nic profile isDefaultNic value [%s].", nicProfile.isDefaultNic())); + return nicProfile.isDefaultNic(); + } + + s_logger.debug("Using isDefaultNic default value [false] as requested nic profile is null."); + return false; + } + @Override public List getNicProfiles(final VirtualMachine vm) { final List nics = _nicDao.listByVmId(vm.getId()); diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index c9085b385f5f..d2314198d0fa 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -1425,6 +1425,8 @@ public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterV } } + setNicAsDefaultIfNeeded(vmInstance, profile); + NicProfile guestNic = null; boolean cleanUp = true; @@ -1453,6 +1455,18 @@ public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterV return _vmDao.findById(vmInstance.getId()); } + /** + * Set NIC as default if VM has no default NIC + * @param vmInstance VM instance to be checked + * @param nicProfile NIC profile to be updated + */ + public void setNicAsDefaultIfNeeded(UserVmVO vmInstance, NicProfile nicProfile) { + if (_networkModel.getDefaultNic(vmInstance.getId()) == null) { + s_logger.debug(String.format("Setting NIC %s as default as VM %s has no default NIC.", nicProfile.getName(), vmInstance.getName())); + nicProfile.setDefaultNic(true); + } + } + /** * duplicated in {@see VirtualMachineManagerImpl} for a {@see VMInstanceVO} */ diff --git a/ui/public/locales/en.json b/ui/public/locales/en.json index 824638efa4d8..55f07357de97 100644 --- a/ui/public/locales/en.json +++ b/ui/public/locales/en.json @@ -3079,6 +3079,7 @@ "message.vr.alert.upon.network.offering.creation.l2": "As virtual routers are not created for L2 networks, the compute offering will not be used.", "message.vr.alert.upon.network.offering.creation.others": "As none of the obligatory services for creating a virtual router (VPN, DHCP, DNS, Firewall, LB, UserData, SourceNat, StaticNat, PortForwarding) are enabled, the virtual router will not be created and the compute offering will not be used.", "message.warn.filetype": "jpg, jpeg, png, bmp and svg are the only supported image formats.", +"message.warn.importing.instance.without.nic": "WARNING: this instance is being imported without NICs and many network resources will not be available. Consider creating a NIC via VCenter before importing or as soon as the instance is imported.", "message.warn.zone.mtu.update": "Please note that this limit won't affect pre-existing network’s MTU settings", "message.zone.creation.complete": "Zone creation complete.", "message.zone.detail.description": "Populate zone details.", diff --git a/ui/public/locales/pt_BR.json b/ui/public/locales/pt_BR.json index 712d9f136232..c95bb5b90e14 100644 --- a/ui/public/locales/pt_BR.json +++ b/ui/public/locales/pt_BR.json @@ -2475,6 +2475,7 @@ "message.vr.alert.upon.network.offering.creation.l2": "Como VRs não são criados para redes do tipo L2, a oferta de computação não será utilizada.", "message.vr.alert.upon.network.offering.creation.others": "Como nenhum dos serviços obrigatórios para criação do VR (VPN, DHCP, DNS, Firewall, LB, UserData, SourceNat, StaticNat, PortForwarding) foram habilitados, o VR não será criado e a oferta de computação não será usada.", "message.warn.filetype": "jpg, jpeg, png, bmp e svg s\u00e3o os \u00fanicos formatos de imagem suportados", +"message.warn.importing.instance.without.nic": "AVISO: essa instância está sendo importada sem NICs e muitos recursos de rede não estarão disponíveis. Considere criar uma NIC antes de importar via VCenter ou assim que a instância for importada.", "message.zone.creation.complete": "Cria\u00e7\u00e3o de zona completa", "message.zone.detail.description": "Preencha os detalhes da zona", "message.zone.detail.hint": "Uma zona \u00e9 a maior unidade organizacional no CloudStack, e normalmente corresponde a um \u00fanico datacenter. As zonas proporcionam isolamento f\u00edsico e redund\u00e2ncia. Uma zona consiste em um ou mais pods (cada um contendo hosts e servidores de armazenamento prim\u00e1rio) e um servidor de armazenamento secund\u00e1rio que \u00e9 compartilhado por todos os pods da zona.", diff --git a/ui/src/views/tools/ImportUnmanagedInstance.vue b/ui/src/views/tools/ImportUnmanagedInstance.vue index 1e5629ba51af..6d90f72c5ee0 100644 --- a/ui/src/views/tools/ImportUnmanagedInstance.vue +++ b/ui/src/views/tools/ImportUnmanagedInstance.vue @@ -227,6 +227,13 @@ filterMatchKey="broadcasturi" @select-multi-network="updateMultiNetworkOffering" /> + + + + +