Skip to content
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

libvirt: Add podvm instance cpu and mem size support for libvirt #2116

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/cloud-api-adaptor/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,17 @@ libvirt() {
test_vars LIBVIRT_URI

[[ "${DISABLECVM}" = "true" ]] && optionals+="-disable-cvm "
[[ "${LIBVIRT_CPU}" ]] && optionals+="-CPU ${LIBVIRT_CPU} "
[[ "${LIBVIRT_MEMORY}" ]] && optionals+="-Memory ${LIBVIRT_MEMORY} "
Comment on lines +155 to +156
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like convention is for the flags to be lowercase


set -x
exec cloud-api-adaptor libvirt \
-uri "${LIBVIRT_URI}" \
-data-dir /opt/data-dir \
-pods-dir /run/peerpod/pods \
-network-name "${LIBVIRT_NET:-default}" \
-pool-name "${LIBVIRT_POOL:-default}" \

${optionals} \
-socket /run/peerpod/hypervisor.sock
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ configMapGenerator:
- LIBVIRT_EFI_FIRMWARE="/usr/share/OVMF/OVMF_CODE_4M.fd" # Edit to change the EFI firmware path, or comment to unset, if not using EFI.
#- LIBVIRT_LAUNCH_SECURITY="" #sev or s390-pv
#- LIBVIRT_VOL_NAME="" # Uncomment and set if you want to use a specific volume name. Defaults to podvm-base.qcow2
#- LIBVIRT_CPU="2"
#- LIBVIRT_Memory="800
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean 8196 here?

#- PAUSE_IMAGE="" # Uncomment and set if you want to use a specific pause image
#- TUNNEL_TYPE="" # Uncomment and set if you want to use a specific tunnel type. Defaults to vxlan
#- VXLAN_PORT="" # Uncomment and set if you want to use a specific vxlan port. Defaults to 4789
Expand Down
14 changes: 0 additions & 14 deletions src/cloud-providers/libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ 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 @@ -666,14 +660,6 @@ func getDomainIPs(dom *libvirt.Domain) ([]netip.Addr, error) {

func CreateDomain(ctx context.Context, libvirtClient *libvirtClient, v *vmConfig) (result *createDomainOutput, err error) {

// 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
20 changes: 19 additions & 1 deletion src/cloud-providers/libvirt/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package libvirt

import (
"flag"
"strconv"

provider "github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers"
)
Expand All @@ -23,6 +24,8 @@ const (
defaultVolName = "podvm-base.qcow2"
defaultLaunchSecurity = ""
defaultFirmware = ""
defaultCPU = "2"
defaultMemory = "8000"
)

func init() {
Expand All @@ -38,7 +41,8 @@ func (_ *Manager) ParseCmd(flags *flag.FlagSet) {
flags.BoolVar(&libvirtcfg.DisableCVM, "disable-cvm", false, "Use non-CVMs for peer pods")
flags.StringVar(&libvirtcfg.LaunchSecurity, "launch-security", defaultLaunchSecurity, "Libvirt's LaunchSecurity element for Confidential VMs. SEV or s390-pv. If omitted, will automatically determine.")
flags.StringVar(&libvirtcfg.Firmware, "firmware", defaultFirmware, "Path to OVMF")

flags.UintVar(&libvirtcfg.CPU, "CPU", 2, "Number of processors allocated")
flags.UintVar(&libvirtcfg.Memory, "Memory", 8, "Amount of memory in GB")
}

func (_ *Manager) LoadEnv() {
Expand All @@ -48,6 +52,20 @@ func (_ *Manager) LoadEnv() {
provider.DefaultToEnv(&libvirtcfg.VolName, "LIBVIRT_VOL_NAME", defaultVolName)
provider.DefaultToEnv(&libvirtcfg.LaunchSecurity, "LIBVIRT_LAUNCH_SECURITY", defaultLaunchSecurity)
provider.DefaultToEnv(&libvirtcfg.Firmware, "LIBVIRT_EFI_FIRMWARE", defaultFirmware)

var memoryStr, processorsStr string
provider.DefaultToEnv(&memoryStr, "LIBVIRT_MEMORY", defaultMemory)
if memoryStr != "" {
memory, _ := strconv.ParseUint(memoryStr, 10, 64)
libvirtcfg.Memory = uint(memory)
}

provider.DefaultToEnv(&processorsStr, "LIBVIRT_CPU", defaultCPU)
if processorsStr != "" {
cpu, _ := strconv.ParseUint(processorsStr, 10, 64)
libvirtcfg.CPU = uint(cpu)
}

}

func (_ *Manager) NewProvider() (provider.Provider, error) {
Expand Down
10 changes: 10 additions & 0 deletions src/cloud-providers/libvirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

const maxInstanceNameLen = 63

// Convert Memory size from MegaBytes to GigaBytes
LIBVIRT_CONV_MEM = 1000

Check failure on line 24 in src/cloud-providers/libvirt/provider.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected declaration, found LIBVIRT_CONV_MEM

Check failure on line 24 in src/cloud-providers/libvirt/provider.go

View workflow job for this annotation

GitHub Actions / vet and fmt

expected declaration, found LIBVIRT_CONV_MEM

Check failure on line 24 in src/cloud-providers/libvirt/provider.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected declaration, found LIBVIRT_CONV_MEM (typecheck)

type libvirtProvider struct {
libvirtClient *libvirtClient
serviceConfig *Config
Expand Down Expand Up @@ -57,8 +60,15 @@
}

// Convert the memory units in gigabytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment need updating too.

instanceMemory := uint(spec.Memory / LIBVIRT_CONV_MEM)

Check failure on line 63 in src/cloud-providers/libvirt/provider.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: LIBVIRT_CONV_MEM
if instanceMemory == 0 {
instanceMemory = uint(p.serviceConfig.Memory / LIBVIRT_CONV_MEM)

Check failure on line 65 in src/cloud-providers/libvirt/provider.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: LIBVIRT_CONV_MEM
}

instanceVCPUs := uint(spec.VCPUs)
if instanceVCPUs == 0 {
instanceVCPUs = uint(p.serviceConfig.CPU)
}

// TODO: Specify the maximum instance name length in Libvirt
vm := &vmConfig{name: instanceName, cpu: instanceVCPUs, mem: instanceMemory, userData: userData, firmware: p.serviceConfig.Firmware}
Expand Down
2 changes: 2 additions & 0 deletions src/cloud-providers/libvirt/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Config struct {
VolName string
LaunchSecurity string
Firmware string
CPU uint
Memory uint
}

type vmConfig struct {
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.