Skip to content

Commit

Permalink
libvirt: Add podvm instance cpu and mem size support for libvirt
Browse files Browse the repository at this point in the history
Use io.katacontainers.config.hypervisor.default_vcpus and io.katacontainers.config.hypervisor.default_memory annotations to set the libvirt podvm instance. Use the default values if no annotations are provided.

Fixes: #1650

Signed-off-by : savitrilh <[email protected]>
  • Loading branch information
savitrilh committed Oct 14, 2024
1 parent d3ad603 commit 628f293
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/cloud-providers/libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions src/cloud-providers/libvirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 628f293

Please sign in to comment.