Skip to content

Commit

Permalink
Set the instance type on the VM
Browse files Browse the repository at this point in the history
Set the selected instance type if exists on the VM. First we will try to
find the namespaced instance type. If not found, we will lookup for the
cluster wide one. Once the instance type is set, we skip setting the CPU
and memory requested resources.

Signed-off-by: Liran Rotenberg <[email protected]>
  • Loading branch information
liranr23 committed Mar 24, 2024
1 parent c533185 commit f253c83
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/plan/adapter/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ func (r *Builder) mapResources(vm *model.Workload, object *cnv.VirtualMachineSpe
object.Template.Spec.Domain.CPU.IsolateEmulatorThread = true
}
}
if object.Instancetype != nil {
return
}

Check warning on line 434 in pkg/controller/plan/adapter/openstack/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/openstack/builder.go#L432-L434

Added lines #L432 - L434 were not covered by tests

// Set CPU Sockets/Cores/Threads and Memory requests
// TODO support NUMA, CPU pinning
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/plan/adapter/ova/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ func (r *Builder) mapInput(object *cnv.VirtualMachineSpec) {
}

func (r *Builder) mapMemory(vm *model.VM, object *cnv.VirtualMachineSpec) error {
if object.Instancetype != nil {
return nil
}

Check warning on line 312 in pkg/controller/plan/adapter/ova/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/ova/builder.go#L310-L312

Added lines #L310 - L312 were not covered by tests
var memoryBytes int64
memoryBytes, err := getResourceCapacity(int64(vm.MemoryMB), vm.MemoryUnits)
if err != nil {
Expand All @@ -326,6 +329,9 @@ func (r *Builder) mapCPU(vm *model.VM, object *cnv.VirtualMachineSpec) {
vm.CoresPerSocket = 1
}
object.Template.Spec.Domain.Machine = &cnv.Machine{Type: "q35"}
if object.Instancetype != nil {
return
}

Check warning on line 334 in pkg/controller/plan/adapter/ova/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/ova/builder.go#L332-L334

Added lines #L332 - L334 were not covered by tests
object.Template.Spec.Domain.CPU = &cnv.CPU{
Sockets: uint32(vm.CpuCount / vm.CoresPerSocket),
Cores: uint32(vm.CoresPerSocket),
Expand Down
16 changes: 11 additions & 5 deletions pkg/controller/plan/adapter/ovirt/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ func (r *Builder) mapClock(vm *model.Workload, object *cnv.VirtualMachineSpec) {
}

func (r *Builder) mapMemory(vm *model.Workload, object *cnv.VirtualMachineSpec) {
if object.Instancetype != nil {
return
}

Check warning on line 367 in pkg/controller/plan/adapter/ovirt/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/ovirt/builder.go#L365-L367

Added lines #L365 - L367 were not covered by tests
reservation := resource.NewQuantity(vm.Memory, resource.BinarySI)
object.Template.Spec.Domain.Resources = cnv.ResourceRequirements{
Requests: map[core.ResourceName]resource.Quantity{
Expand All @@ -371,11 +374,6 @@ func (r *Builder) mapMemory(vm *model.Workload, object *cnv.VirtualMachineSpec)
}
func (r *Builder) mapCPU(vm *model.Workload, object *cnv.VirtualMachineSpec) {
object.Template.Spec.Domain.Machine = &cnv.Machine{Type: "q35"}
object.Template.Spec.Domain.CPU = &cnv.CPU{
Sockets: uint32(vm.CpuSockets),
Cores: uint32(vm.CpuCores),
Threads: uint32(vm.CpuThreads),
}
if vm.CpuPinningPolicy == model.Dedicated {
object.Template.Spec.Domain.CPU.DedicatedCPUPlacement = true
}
Expand All @@ -384,6 +382,14 @@ func (r *Builder) mapCPU(vm *model.Workload, object *cnv.VirtualMachineSpec) {
} else if r.Plan.Spec.PreserveClusterCPUModel {
r.setCpuFlags(r.getClusterCpu(vm), object)
}
if object.Instancetype != nil {
return
}
object.Template.Spec.Domain.CPU = &cnv.CPU{
Sockets: uint32(vm.CpuSockets),
Cores: uint32(vm.CpuCores),
Threads: uint32(vm.CpuThreads),
}

Check warning on line 392 in pkg/controller/plan/adapter/ovirt/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/adapter/ovirt/builder.go#L385-L392

Added lines #L385 - L392 were not covered by tests
}

func (r *Builder) getClusterCpu(vm *model.Workload) string {
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/plan/adapter/vsphere/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ func (r *Builder) mapClock(host *model.Host, object *cnv.VirtualMachineSpec) {
}

func (r *Builder) mapMemory(vm *model.VM, object *cnv.VirtualMachineSpec) {
if object.Instancetype != nil {
return
}
memoryBytes := int64(vm.MemoryMB) * 1024 * 1024
reservation := resource.NewQuantity(memoryBytes, resource.BinarySI)
object.Template.Spec.Domain.Resources = cnv.ResourceRequirements{
Expand All @@ -568,6 +571,9 @@ func (r *Builder) mapMemory(vm *model.VM, object *cnv.VirtualMachineSpec) {

func (r *Builder) mapCPU(vm *model.VM, object *cnv.VirtualMachineSpec) {
object.Template.Spec.Domain.Machine = &cnv.Machine{Type: "q35"}
if object.Instancetype != nil {
return
}
object.Template.Spec.Domain.CPU = &cnv.CPU{
Sockets: uint32(vm.CpuCount / vm.CoresPerSocket),
Cores: uint32(vm.CoresPerSocket),
Expand Down
66 changes: 66 additions & 0 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,11 @@ func (r *KubeVirt) virtualMachine(vm *plan.VMStatus) (object *cnv.VirtualMachine
}
}

object, err = r.setIntanceType(vm, object)
if err != nil {
return
}

Check warning on line 1238 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1235-L1238

Added lines #L1235 - L1238 were not covered by tests

if object.Spec.Template.ObjectMeta.Labels == nil {
object.Spec.Template.ObjectMeta.Labels = map[string]string{}
}
Expand Down Expand Up @@ -1279,6 +1284,67 @@ func (r *KubeVirt) vmPreference(vm *plan.VMStatus) (virtualMachine *cnv.VirtualM
return
}

func (r *KubeVirt) setIntanceType(vm *plan.VMStatus, object *cnv.VirtualMachine) (virtualMachine *cnv.VirtualMachine, err error) {
if r.Plan.Spec.SelectedInstanceType == "" {
return object, nil
}
intancetypeName, kind, err := r.getInstancetype(vm, r.Plan.Spec.SelectedInstanceType)
if err != nil {
return
}
object.Spec.Instancetype = &cnv.InstancetypeMatcher{Name: intancetypeName, Kind: kind}
virtualMachine = object
return

Check warning on line 1297 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1287-L1297

Added lines #L1287 - L1297 were not covered by tests
}

// Attempt to find a suitable instance type
func (r *KubeVirt) getInstancetype(vm *plan.VMStatus, instancetypeName string) (name, kind string, err error) {
name, kind, err = r.getVirtualMachineInstancetype(instancetypeName)
if err != nil {
if k8serr.IsNotFound(err) {
r.Log.Info("could not find a local instance type for destination VM. trying cluster wide",
"vm",
vm.String())
} else {
return
}

Check warning on line 1310 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1301-L1310

Added lines #L1301 - L1310 were not covered by tests
}

name, kind, err = r.getVirtualMachineClusterInstancetype(vm, instancetypeName)
return

Check warning on line 1314 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1313-L1314

Added lines #L1313 - L1314 were not covered by tests
}

func (r *KubeVirt) getVirtualMachineInstancetype(instancetypeName string) (name, kind string, err error) {
virtualMachineClusterInstancetype := &instancetype.VirtualMachineInstancetype{}
err = r.Destination.Client.Get(
context.TODO(),
client.ObjectKey{Name: instancetypeName, Namespace: r.Plan.Spec.TargetNamespace},
virtualMachineClusterInstancetype)
if err != nil {
return
}
return instancetypeName, "VirtualMachineInstancetype", nil

Check warning on line 1326 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1317-L1326

Added lines #L1317 - L1326 were not covered by tests
}

func (r *KubeVirt) getVirtualMachineClusterInstancetype(vm *plan.VMStatus, instancetypeName string) (name, kind string, err error) {
virtualMachineClusterInstancetype := &instancetype.VirtualMachineClusterInstancetype{}
err = r.Destination.Client.Get(
context.TODO(),
client.ObjectKey{Name: instancetypeName},
virtualMachineClusterInstancetype)
if err != nil {
if k8serr.IsNotFound(err) {
r.Log.Info("could not find instance type for destination VM.",
"vm",
vm.String(),
"error",
err)
}
return

Check warning on line 1343 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1329-L1343

Added lines #L1329 - L1343 were not covered by tests
}
return instancetypeName, "VirtualMachineClusterInstancetype", nil

Check warning on line 1345 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1345

Added line #L1345 was not covered by tests
}

func (r *KubeVirt) getOsMapConfig(providerType v1beta1.ProviderType) (configMap *core.ConfigMap, err error) {
configMap = &core.ConfigMap{}
var configMapName string
Expand Down

0 comments on commit f253c83

Please sign in to comment.