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

Adding Resource Fields for api.Machine #203

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions pkg/api/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Machine struct {
type MachineSpec struct {
Power PowerState `json:"power"`

CpuMillis int64 `json:"cpuMillis"`
MemoryBytes int64 `json:"memoryBytes"`
Resources ResourceList `json:"resources"`
NUMAPreferences NUMAPreferences `json:"numaPreferences,omitempty"`

Image *string `json:"image"`
Ignition []byte `json:"ignition"`
Expand All @@ -31,6 +31,7 @@ type MachineSpec struct {
}

type MachineStatus struct {
NUMAPlacement NUMAPlacement `json:"numaPlacement,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

here maybe you will need to add ResourceList too

VolumeStatus []VolumeStatus `json:"volumeStatus"`
NetworkInterfaceStatus []NetworkInterfaceStatus `json:"networkInterfaceStatus"`
State MachineState `json:"state"`
Expand Down
57 changes: 57 additions & 0 deletions pkg/api/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package api

import "k8s.io/apimachinery/pkg/api/resource"

type ResourceName string

const (
ResourceCPU ResourceName = "cpu"
Copy link
Contributor

Choose a reason for hiding this comment

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

pls don't create own ResourceName or type. Ideally reuse core package: core "github.com/ironcore-dev/ironcore/api/core/v1alpha1"

ResourceMemory ResourceName = "memory"
ResourceSGX ResourceName = "sgx"
ResourceHugepages ResourceName = "hugepages"
)

type ResourceList map[ResourceName]resource.Quantity

type NUMAPreferences struct {
CPUNodes []int `json:"cpuNodes,omitempty"`
MemoryNodes []int `json:"memoryNodes,omitempty"`
IoNodes []int `json:"ioNodes,omitempty"`
}

type NUMAPlacement struct {
CPUNodes []int `json:"cpuNodes"`
MemoryNodes []int `json:"memoryNodes"`
IoNodes []int `json:"ioNodes"`
}

// get returns the resource with name if specified, otherwise it returns a nil quantity with default format
func (rl *ResourceList) get(name ResourceName, defaultFormat resource.Format) *resource.Quantity {
if val, ok := (*rl)[name]; ok {
return &val
}
return &resource.Quantity{Format: defaultFormat}
}

// CPU is a shorthand for getting the quantity associated with ResourceCPU.
func (rl *ResourceList) CPU() *resource.Quantity {
return rl.get(ResourceCPU, resource.DecimalSI)
}

// Memory is a shorthand for getting the quantity associated with ResourceMemory.
func (rl *ResourceList) Memory() *resource.Quantity {
return rl.get(ResourceMemory, resource.BinarySI)
}

// SGX is a shorthand for getting the quantity associated with ResourceSGX.
func (rl *ResourceList) SGX() *resource.Quantity {
return rl.get(ResourceSGX, resource.BinarySI)
}

// HugepagesEnabled is a shorthand for checking if hugepage is enabled.
func (rl *ResourceList) HugepagesEnabled() bool {
return rl.get(ResourceHugepages, resource.DecimalSI).Value() == 1
}
4 changes: 2 additions & 2 deletions pkg/controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func (r *MachineReconciler) setDomainMetadata(log logr.Logger, machine *api.Mach
func (r *MachineReconciler) setDomainResources(machine *api.Machine, domain *libvirtxml.Domain) error {
// TODO: check if there is better or check possible while conversion to uint
domain.Memory = &libvirtxml.DomainMemory{
Value: uint(machine.Spec.MemoryBytes),
Value: uint(machine.Spec.Resources.Memory().Value()),
Unit: "Byte",
}

Expand All @@ -765,7 +765,7 @@ func (r *MachineReconciler) setDomainResources(machine *api.Machine, domain *lib
}
}

cpu := uint(machine.Spec.CpuMillis / 1000)
cpu := uint(machine.Spec.Resources.CPU().Value() / 1000)
Copy link
Contributor

@lukas016 lukas016 Feb 23, 2024

Choose a reason for hiding this comment

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

this logic is wrong. 8.6 will be rounded down to 8 what doesn't make sense for me. I prefer do rounded up.

@afritzler do we need support millis unit? Cannot we support just whole cores? 1, 2, 3, 4, 5, 6, 7, 8, ...

domain.VCPU = &libvirtxml.DomainVCPU{
Value: cpu,
}
Expand Down
14 changes: 9 additions & 5 deletions provider/server/machine_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import (
"github.com/ironcore-dev/libvirt-provider/pkg/api"
machinev1alpha1 "github.com/ironcore-dev/libvirt-provider/provider/api/v1alpha1"
"github.com/ironcore-dev/libvirt-provider/provider/apiutils"
"k8s.io/apimachinery/pkg/api/resource"
)

func calcResources(class *iri.MachineClass) (int64, int64) {
func calcResources(class *iri.MachineClass) api.ResourceList {
//Todo do some magic
return class.Capabilities.CpuMillis, class.Capabilities.MemoryBytes

return api.ResourceList{
api.ResourceCPU: *resource.NewQuantity(class.Capabilities.CpuMillis, resource.DecimalSI),
api.ResourceMemory: *resource.NewQuantity(class.Capabilities.MemoryBytes, resource.BinarySI),
}
}

func (s *Server) createMachineFromIRIMachine(ctx context.Context, log logr.Logger, iriMachine *iri.Machine) (*api.Machine, error) {
Expand All @@ -37,7 +42,7 @@ func (s *Server) createMachineFromIRIMachine(ctx context.Context, log logr.Logge
}
log.V(2).Info("Validated class")

cpu, memory := calcResources(class)
resources := calcResources(class)

power, err := s.getPowerStateFromIRI(iriMachine.Spec.Power)
if err != nil {
Expand Down Expand Up @@ -71,8 +76,7 @@ func (s *Server) createMachineFromIRIMachine(ctx context.Context, log logr.Logge
},
Spec: api.MachineSpec{
Power: power,
CpuMillis: cpu,
MemoryBytes: memory,
Resources: resources,
Volumes: volumes,
Ignition: iriMachine.Spec.IgnitionData,
NetworkInterfaces: networkInterfaces,
Expand Down
Loading