Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
fix: add CPU field to resolvedOfferings and change QuotaRequests to u…
Browse files Browse the repository at this point in the history
…se resolvedOfferings

Signed-off-by: tylerslaton <[email protected]>
  • Loading branch information
tylerslaton committed Jan 26, 2024
1 parent ce008ae commit ed9bf05
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 48 deletions.
6 changes: 3 additions & 3 deletions pkg/apis/internal.acorn.io/v1/appinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ type VolumeResolvedOffering struct {
}

type ContainerResolvedOffering struct {
Class string `json:"class,omitempty"`
Memory *int64 `json:"memory,omitempty"`
CPUScaler *float64 `json:"cpuScaler,omitempty"`
Class string `json:"class,omitempty"`
Memory *int64 `json:"memory,omitempty"`
CPU *int64 `json:"cpu,omitempty"`
}

type Scheduling struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/apis/internal.acorn.io/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/computeclasses/computeclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func Validate(cc apiv1.ComputeClass, memory resource.Quantity, memDefault *int64
return nil
}

func CalculateCPU(cc internaladminv1.ProjectComputeClassInstance, memory resource.Quantity) (resource.Quantity, error) {
func CalculateCPU(cc internaladminv1.ProjectComputeClassInstance, memory resource.Quantity) resource.Quantity {
// The CPU scaler calculates the CPUs per Gi of memory so get the memory in a ratio of Gi
memoryInGi := memory.AsApproximateFloat64() / gi
// Since we're putting this in to mili-cpu's, multiply memoryInGi by the scaler and by 1000
value := cc.CPUScaler * memoryInGi * 1000

return *resource.NewMilliQuantity(int64(math.Ceil(value)), resource.DecimalSI), nil
return *resource.NewMilliQuantity(int64(math.Ceil(value)), resource.DecimalSI)
}

func GetComputeClassNameForWorkload(workload string, container internalv1.Container, computeClasses internalv1.ComputeClassMap) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ metadata:
description: Simple description for a simple ComputeClass
cpuScaler: 0.25
memory:
min: 1Mi
max: 2Mi
default: 1Mi
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: foo
operator: In
values:
- bar
min: 50Mi
max: 200Mi
default: 100Mi
requestScaler: 0.5
# affinity:
# nodeAffinity:
# requiredDuringSchedulingIgnoredDuringExecution:
# nodeSelectorTerms:
# - matchExpressions:
# - key: foo
# operator: In
# values:
# - bar
supportedRegions:
- local
17 changes: 9 additions & 8 deletions pkg/controller/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
adminv1 "github.com/acorn-io/runtime/pkg/apis/internal.admin.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/controller/appdefinition"
"github.com/acorn-io/runtime/pkg/labels"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/acorn-io/runtime/pkg/condition"
Expand Down Expand Up @@ -132,17 +131,19 @@ func addContainers(containers map[string]v1.Container, quotaRequest *adminv1.Quo
func addCompute(containers map[string]v1.Container, appInstance *v1.AppInstance, quotaRequest *adminv1.QuotaRequestInstance) {
// For each workload, add their memory/cpu requests to the quota request
for name, container := range containers {
var requirements corev1.ResourceRequirements
if specific, ok := appInstance.Status.Scheduling[name]; ok {
requirements = specific.Requirements
} else if all, ok := appInstance.Status.Scheduling[""]; ok {
requirements = all.Requirements
var cpu, memory resource.Quantity
if specific, ok := appInstance.Status.ResolvedOfferings.Containers[name]; ok {
cpu = *resource.NewMilliQuantity(*specific.CPU, resource.DecimalSI)
memory = *resource.NewQuantity(*specific.Memory, resource.BinarySI)
} else if all, ok := appInstance.Status.ResolvedOfferings.Containers[""]; ok {
cpu = *resource.NewMilliQuantity(*all.CPU, resource.DecimalSI)
memory = *resource.NewQuantity(*all.Memory, resource.BinarySI)
}

// Add the memory/cpu requests to the quota request for each container at the scale specified
for i := 0; i < replicas(container.Scale); i++ {
quotaRequest.Spec.Resources.CPU.Add(requirements.Requests["cpu"])
quotaRequest.Spec.Resources.Memory.Add(requirements.Requests["memory"])
quotaRequest.Spec.Resources.CPU.Add(cpu)
quotaRequest.Spec.Resources.Memory.Add(memory)
}

// Recurse over any sidecars. Since sidecars can't have sidecars, this is safe.
Expand Down
30 changes: 17 additions & 13 deletions pkg/controller/resolvedofferings/computeclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
adminv1 "github.com/acorn-io/runtime/pkg/apis/internal.admin.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/computeclasses"
"github.com/acorn-io/z"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
)

// resolveComputeClasses resolves the compute class information for each container in the AppInstance
Expand Down Expand Up @@ -44,19 +47,21 @@ func resolveComputeClasses(req router.Request, cfg *apiv1.Config, appInstance *v
return err
}
def := parsedMemory.Def.Value()
cpuQuantity := computeclasses.CalculateCPU(*cc, *parsedMemory.Def)
appInstance.Status.ResolvedOfferings.Containers[""] = v1.ContainerResolvedOffering{
Memory: &def,
CPUScaler: &cc.CPUScaler,
Class: appInstance.Status.ResolvedOfferings.Containers[""].Class,
Memory: &def,
CPU: z.Pointer(cpuQuantity.MilliValue()),
Class: appInstance.Status.ResolvedOfferings.Containers[""].Class,
}
}

// Check to see if the user overrode the memory for all containers
if appInstance.Spec.Memory[""] != nil {
if specificMemory := appInstance.Spec.Memory[""]; specificMemory != nil {
cpuQuantity := computeclasses.CalculateCPU(*cc, *resource.NewQuantity(*specificMemory, resource.BinarySI))
appInstance.Status.ResolvedOfferings.Containers[""] = v1.ContainerResolvedOffering{
Memory: appInstance.Spec.Memory[""],
CPUScaler: appInstance.Status.ResolvedOfferings.Containers[""].CPUScaler,
Class: appInstance.Status.ResolvedOfferings.Containers[""].Class,
Memory: appInstance.Spec.Memory[""],
CPU: z.Pointer(cpuQuantity.MilliValue()),
Class: appInstance.Status.ResolvedOfferings.Containers[""].Class,
}
}

Expand Down Expand Up @@ -94,8 +99,7 @@ func resolveComputeClass(req router.Request, appInstance *v1.AppInstance, config

func resolveComputeClassForContainer(req router.Request, appInstance *v1.AppInstance, configDefault *int64, defaultCC *adminv1.ProjectComputeClassInstance, defaultCCName, containerName string, container v1.Container) (v1.ContainerResolvedOffering, error) {
var (
cpuScaler *float64
ccName string
ccName string
)

// First, get the compute class for the workload
Expand All @@ -108,7 +112,6 @@ func resolveComputeClassForContainer(req router.Request, appInstance *v1.AppInst
}
if cc != nil {
ccName = cc.Name
cpuScaler = &cc.CPUScaler
} else {
ccName = defaultCCName
}
Expand Down Expand Up @@ -136,9 +139,10 @@ func resolveComputeClassForContainer(req router.Request, appInstance *v1.AppInst
memory = &def
}

cpuQuantity := computeclasses.CalculateCPU(*cc, *resource.NewQuantity(*memory, resource.BinarySI))
return v1.ContainerResolvedOffering{
Class: ccName,
Memory: memory,
CPUScaler: cpuScaler,
Class: ccName,
Memory: memory,
CPU: z.Pointer(cpuQuantity.MilliValue()),
}, nil
}
5 changes: 1 addition & 4 deletions pkg/controller/scheduling/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,7 @@ func ResourceRequirements(req router.Request, app *v1.AppInstance, containerName
}

if computeClass != nil {
cpuQuantity, err := computeclasses.CalculateCPU(*computeClass, memoryRequest)
if err != nil {
return nil, err
}
cpuQuantity := computeclasses.CalculateCPU(*computeClass, memoryRequest)
if cpuQuantity.Value() != 0 {
requirements.Requests[corev1.ResourceCPU] = cpuQuantity
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/openapi/generated/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed9bf05

Please sign in to comment.