From 951f56083abadee4018289877f995f12f8f93e6b Mon Sep 17 00:00:00 2001 From: SAVITRI HUNASHEEKATTI Date: Mon, 14 Oct 2024 15:22:15 +0530 Subject: [PATCH] libvirt: Add podvm instance cpu and mem size support for libvirt Add podvm instance cpu and mem size support for libvirt Fixes: #1650 Signed-off-by : SAVITRI HUNASHEEKATTI --- src/cloud-providers/libvirt/libvirt.go | 16 ++++++++++++++-- src/cloud-providers/libvirt/provider.go | 7 +++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/cloud-providers/libvirt/libvirt.go b/src/cloud-providers/libvirt/libvirt.go index a68fae40d..7a237a954 100644 --- a/src/cloud-providers/libvirt/libvirt.go +++ b/src/cloud-providers/libvirt/libvirt.go @@ -27,6 +27,12 @@ const ( GetDomainIPsRetries = 20 // The sleep time between retries to get the domain IP addresses GetDomainIPsSleep = time.Second * 3 + // Default Memory size + LIBVIRT_DEF_MEM_SIZE = uint(8) + // Default CPU size + LIBVIRT_DEF_CPU_SIZE = uint(2) + // Convert Memory size from MegaBytes to GigaBytes + LIBVIRT_CONV_MEM = 1000 ) type domainConfig struct { @@ -529,8 +535,14 @@ func getDomainIPs(dom *libvirt.Domain) ([]netip.Addr, error) { func CreateDomain(ctx context.Context, libvirtClient *libvirtClient, v *vmConfig) (result *createDomainOutput, err error) { - v.cpu = uint(2) - v.mem = uint(8) + // Assign the default CPU size and memory when no default memory and + // CPU are provided through annotations + if v.cpu == 0 { + v.cpu = LIBVIRT_DEF_CPU_SIZE + } + if v.mem == 0 { + v.mem = LIBVIRT_DEF_MEM_SIZE + } v.rootDiskSize = uint64(10) exists, err := checkDomainExistsByName(v.name, libvirtClient) diff --git a/src/cloud-providers/libvirt/provider.go b/src/cloud-providers/libvirt/provider.go index dce89a148..e98c33100 100644 --- a/src/cloud-providers/libvirt/provider.go +++ b/src/cloud-providers/libvirt/provider.go @@ -56,9 +56,12 @@ func (p *libvirtProvider) CreateInstance(ctx context.Context, podName, sandboxID return nil, err } - // TODO: Specify the maximum instance name length in Libvirt - vm := &vmConfig{name: instanceName, userData: userData, firmware: p.serviceConfig.Firmware} + // Convert the memory units in gigabytes + instanceMemory := uint(spec.Memory / LIBVIRT_CONV_MEM) + instanceVCPUs := uint(spec.VCPUs) + // TODO: Specify the maximum instance name length in Libvirt + vm := &vmConfig{name: instanceName, cpu: instanceVCPUs, mem: instanceMemory, userData: userData, firmware: p.serviceConfig.Firmware} if p.serviceConfig.DisableCVM { vm.launchSecurityType = NoLaunchSecurity } else if p.serviceConfig.LaunchSecurity != "" {