@@ -274,6 +274,7 @@ func (l *LibVirt) Process(hv v1.Hypervisor) (v1.Hypervisor, error) {
274274 l .addCapabilities ,
275275 l .addDomainCapabilities ,
276276 l .addAllocationCapacity ,
277+ l .addEffectiveCapacity ,
277278 }
278279 var err error
279280 for _ , processor := range processors {
@@ -458,14 +459,14 @@ func (l *LibVirt) addAllocationCapacity(old v1.Hypervisor) (v1.Hypervisor, error
458459
459460 cellsById [cell .ID ] = v1.Cell {
460461 CellID : cell .ID ,
461- Allocation : map [string ]resource.Quantity {
462+ Allocation : map [v1. ResourceName ]resource.Quantity {
462463 // Will be updated below when we look at the domain infos.
463- "memory" : * resource .NewQuantity (0 , resource .BinarySI ),
464- "cpu" : * resource .NewQuantity (0 , resource .DecimalSI ),
464+ v1 . ResourceMemory : * resource .NewQuantity (0 , resource .BinarySI ),
465+ v1 . ResourceCPU : * resource .NewQuantity (0 , resource .DecimalSI ),
465466 },
466- Capacity : map [string ]resource.Quantity {
467- "memory" : memoryCapacity ,
468- "cpu" : cpuCapacity ,
467+ Capacity : map [v1. ResourceName ]resource.Quantity {
468+ v1 . ResourceMemory : memoryCapacity ,
469+ v1 . ResourceCPU : cpuCapacity ,
469470 },
470471 }
471472 }
@@ -505,14 +506,14 @@ func (l *LibVirt) addAllocationCapacity(old v1.Hypervisor) (v1.Hypervisor, error
505506 domInfo .Name , memoryNode .CellID ,
506507 )
507508 }
508- memAllocCell := cell .Allocation ["memory" ]
509+ memAllocCell := cell .Allocation [v1 . ResourceMemory ]
509510 // If a domain is using multiple memory cells, assume the
510511 // distribution across cells is even.
511512 nCells := int64 (len (domInfo .NumaTune .MemNodes )) // is non-zero
512513 memAllocPerCell := * resource .
513514 NewQuantity (memAlloc .Value ()/ nCells , resource .BinarySI )
514515 memAllocCell .Add (memAllocPerCell )
515- cell .Allocation ["memory" ] = memAllocCell
516+ cell .Allocation [v1 . ResourceMemory ] = memAllocCell
516517 cellsById [memoryNode .CellID ] = cell
517518 }
518519
@@ -528,14 +529,14 @@ func (l *LibVirt) addAllocationCapacity(old v1.Hypervisor) (v1.Hypervisor, error
528529 domInfo .Name , cpuCell .ID ,
529530 )
530531 }
531- cpuAllocCell := cell .Allocation ["cpu" ]
532+ cpuAllocCell := cell .Allocation [v1 . ResourceCPU ]
532533 // If a domain is using multiple cpu cells, assume the distribution
533534 // across cells is even.
534535 nCells := int64 (len (domInfo .CPU .Numa .Cells )) // is non-zero
535536 cpuAllocPerCell := * resource .
536537 NewQuantity (cpuAlloc .Value ()/ nCells , resource .DecimalSI )
537538 cpuAllocCell .Add (cpuAllocPerCell )
538- cell .Allocation ["cpu" ] = cpuAllocCell
539+ cell .Allocation [v1 . ResourceCPU ] = cpuAllocCell
539540 cellsById [cpuCell .ID ] = cell
540541 }
541542 }
@@ -544,12 +545,48 @@ func (l *LibVirt) addAllocationCapacity(old v1.Hypervisor) (v1.Hypervisor, error
544545 cellsAsSlice = append (cellsAsSlice , cell )
545546 }
546547
547- newHv .Status .Capacity = make (map [string ]resource.Quantity )
548- newHv .Status .Capacity ["memory" ] = * totalMemoryCapacity
549- newHv .Status .Capacity ["cpu" ] = * totalCpuCapacity
550- newHv .Status .Allocation = make (map [string ]resource.Quantity )
551- newHv .Status .Allocation ["memory" ] = * totalMemoryAlloc
552- newHv .Status .Allocation ["cpu" ] = * totalCpuAlloc
548+ newHv .Status .Capacity = make (map [v1. ResourceName ]resource.Quantity )
549+ newHv .Status .Capacity [v1 . ResourceMemory ] = * totalMemoryCapacity
550+ newHv .Status .Capacity [v1 . ResourceCPU ] = * totalCpuCapacity
551+ newHv .Status .Allocation = make (map [v1. ResourceName ]resource.Quantity )
552+ newHv .Status .Allocation [v1 . ResourceMemory ] = * totalMemoryAlloc
553+ newHv .Status .Allocation [v1 . ResourceCPU ] = * totalCpuAlloc
553554 newHv .Status .Cells = cellsAsSlice
554555 return newHv , nil
555556}
557+
558+ // Add the effective capacity to the hypervisor instance.
559+ //
560+ // The effective capacity is calculated as the physical capacity times the
561+ // applied overcommit ratio, or 1.0 by default. In case the resulting values
562+ // are fractional, they are floored.
563+ func (l * LibVirt ) addEffectiveCapacity (old v1.Hypervisor ) (v1.Hypervisor , error ) {
564+ newHv := * old .DeepCopy ()
565+ // Always recreate the EffectiveCapacity map to remove stale entries
566+ newHv .Status .EffectiveCapacity = make (map [v1.ResourceName ]resource.Quantity )
567+ for resourceName , capacity := range newHv .Status .Capacity {
568+ overcommit , ok := newHv .Spec .Overcommit [resourceName ]
569+ if ! ok {
570+ overcommit = 1.0
571+ }
572+ flooredValue := int64 (float64 (capacity .Value ()) * overcommit )
573+ effectiveCapacity := resource .NewQuantity (flooredValue , capacity .Format )
574+ newHv .Status .EffectiveCapacity [resourceName ] = * effectiveCapacity
575+ }
576+ // Also apply this to each cell.
577+ for i , cell := range newHv .Status .Cells {
578+ // Always recreate the cell's EffectiveCapacity map to remove stale entries
579+ cell .EffectiveCapacity = make (map [v1.ResourceName ]resource.Quantity )
580+ for resourceName , capacity := range cell .Capacity {
581+ overcommit , ok := newHv .Spec .Overcommit [resourceName ]
582+ if ! ok {
583+ overcommit = 1.0
584+ }
585+ flooredValue := int64 (float64 (capacity .Value ()) * overcommit )
586+ effectiveCapacity := resource .NewQuantity (flooredValue , capacity .Format )
587+ cell .EffectiveCapacity [resourceName ] = * effectiveCapacity
588+ }
589+ newHv .Status .Cells [i ] = cell
590+ }
591+ return newHv , nil
592+ }
0 commit comments