Skip to content

Commit

Permalink
feat: limit apply target (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo authored Jul 1, 2024
2 parents f83ebd5 + e5abb81 commit c755198
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 59 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ The operator uses **annotations** on VPA objects to configure its behavior. Belo
- **`oblik.socialgouv.io/memory-limit-from-cpu-algo`**: Algorithm to calculate the memory limit based on cpu limit. Options: `ratio` (default), `margin`.
- **`oblik.socialgouv.io/memory-request-from-cpu-value`**: Value used to calculate the memory request based on cpu request. Default is `2`.
- **`oblik.socialgouv.io/memory-limit-from-cpu-value`**: Value used to calculate the memory limit based on cpu limit. Default is `2`.
- **`oblik.socialgouv.io/request-apply-target`**: Select which recommendation to apply by default (overridable for cpu and memory specifically with the following annotations). Options: `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/request-cpu-apply-target`**: Select which recommendation to apply for cpu. Options: `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/request-memory-apply-target`**: Select which recommendation to apply for memory. Options: `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/request-apply-target`**: Select which recommendation to apply by default on request (overridable for cpu and memory specifically with the following annotations). Options: `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/request-cpu-apply-target`**: Select which recommendation to apply for cpu request. Options: `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/request-memory-apply-target`**: Select which recommendation to apply for memory request. Options: `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/limit-apply-target`**: Select which recommendation to apply by default on limit(overridable for cpu and memory specifically with the following annotations). Options: `auto` (default, the limit will be calculated from request), `frugal` (aka lowerBound), `balanced` (aka target), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/limit-cpu-apply-target`**: Select which recommendation to apply for cpu. Options: `auto` (default, the limit will be calculated from request), `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/limit-memory-apply-target`**: Select which recommendation to apply for memory. Options: `auto` (default, the limit will be calculated from request), `frugal` (aka lowerBound), `balanced` (aka target, default), `peak` (aka `upperBound`).
- **`oblik.socialgouv.io/request-cpu-scale-direction`**: Select scaling allowed direction. Options: `both` (default), `up`, `down`.
- **`oblik.socialgouv.io/request-memory-scale-direction`**: Select scaling allowed direction. Options: `both` (default), `up`, `down`.
- **`oblik.socialgouv.io/limit-cpu-scale-direction`**: Select scaling allowed direction. Options: `both` (default), `up`, `down`.
Expand Down
17 changes: 13 additions & 4 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ const (
UnprovidedApplyDefaultModeValue
)

type ApplyTarget int
type RequestApplyTarget int

const (
ApplyTargetFrugal ApplyTarget = iota
ApplyTargetBalanced
ApplyTargetPeak
RequestApplyTargetFrugal RequestApplyTarget = iota
RequestApplyTargetBalanced
RequestApplyTargetPeak
)

type LimitApplyTarget int

const (
LimitApplyTargetAuto LimitApplyTarget = iota
LimitApplyTargetFrugal
LimitApplyTargetBalanced
LimitApplyTargetPeak
)

type ScaleDirection int
Expand Down
117 changes: 95 additions & 22 deletions pkg/config/load-cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ type LoadCfg struct {
MemoryLimitFromCpuAlgo *calculator.CalculatorAlgo
MemoryLimitFromCpuValue *string

RequestApplyTarget *ApplyTarget
RequestCpuApplyTarget *ApplyTarget
RequestMemoryApplyTarget *ApplyTarget
RequestApplyTarget *RequestApplyTarget
RequestCpuApplyTarget *RequestApplyTarget
RequestMemoryApplyTarget *RequestApplyTarget

LimitApplyTarget *LimitApplyTarget
LimitCpuApplyTarget *LimitApplyTarget
LimitMemoryApplyTarget *LimitApplyTarget

RequestCpuScaleDirection *ScaleDirection
RequestMemoryScaleDirection *ScaleDirection
Expand Down Expand Up @@ -415,43 +419,43 @@ func loadVpaCommonCfg(cfg *LoadCfg, vpaResource *vpa.VerticalPodAutoscaler, anno
case "lowerBound":
fallthrough
case "frugal":
applyTarget := ApplyTargetFrugal
cfg.RequestMemoryApplyTarget = &applyTarget
applyTarget := RequestApplyTargetFrugal
cfg.RequestApplyTarget = &applyTarget
case "target":
fallthrough
case "balanced":
applyTarget := ApplyTargetBalanced
cfg.RequestMemoryApplyTarget = &applyTarget
applyTarget := RequestApplyTargetBalanced
cfg.RequestApplyTarget = &applyTarget
case "upperBound":
fallthrough
case "peak":
applyTarget := ApplyTargetPeak
cfg.RequestMemoryApplyTarget = &applyTarget
applyTarget := RequestApplyTargetPeak
cfg.RequestApplyTarget = &applyTarget
default:
klog.Warningf("Unknown apply-target: %s", requestApplyTarget)
klog.Warningf("Unknown request-apply-target: %s", requestApplyTarget)
}
}

requestCpuApplyTarget := getAnnotation("request-apply-target")
requestCpuApplyTarget := getAnnotation("request-cpu-apply-target")
if requestCpuApplyTarget != "" {
switch requestCpuApplyTarget {
case "lowerBound":
fallthrough
case "frugal":
applyTarget := ApplyTargetFrugal
cfg.RequestMemoryApplyTarget = &applyTarget
applyTarget := RequestApplyTargetFrugal
cfg.RequestCpuApplyTarget = &applyTarget
case "target":
fallthrough
case "balanced":
applyTarget := ApplyTargetBalanced
cfg.RequestMemoryApplyTarget = &applyTarget
applyTarget := RequestApplyTargetBalanced
cfg.RequestCpuApplyTarget = &applyTarget
case "upperBound":
fallthrough
case "peak":
applyTarget := ApplyTargetPeak
cfg.RequestMemoryApplyTarget = &applyTarget
applyTarget := RequestApplyTargetPeak
cfg.RequestCpuApplyTarget = &applyTarget
default:
klog.Warningf("Unknown apply-target: %s", requestCpuApplyTarget)
klog.Warningf("Unknown request-apply-target: %s", requestCpuApplyTarget)
}
}

Expand All @@ -461,20 +465,89 @@ func loadVpaCommonCfg(cfg *LoadCfg, vpaResource *vpa.VerticalPodAutoscaler, anno
case "lowerBound":
fallthrough
case "frugal":
applyTarget := ApplyTargetFrugal
applyTarget := RequestApplyTargetFrugal
cfg.RequestMemoryApplyTarget = &applyTarget
case "target":
fallthrough
case "balanced":
applyTarget := ApplyTargetBalanced
applyTarget := RequestApplyTargetBalanced
cfg.RequestMemoryApplyTarget = &applyTarget
case "upperBound":
fallthrough
case "peak":
applyTarget := ApplyTargetPeak
applyTarget := RequestApplyTargetPeak
cfg.RequestMemoryApplyTarget = &applyTarget
default:
klog.Warningf("Unknown apply-target: %s", requestMemoryApplyTarget)
klog.Warningf("Unknown request-apply-target: %s", requestMemoryApplyTarget)
}
}

limitApplyTarget := getAnnotation("limit-apply-target")
if limitApplyTarget != "" {
switch limitApplyTarget {
case "lowerBound":
fallthrough
case "frugal":
applyTarget := LimitApplyTargetFrugal
cfg.LimitApplyTarget = &applyTarget
case "target":
fallthrough
case "balanced":
applyTarget := LimitApplyTargetBalanced
cfg.LimitApplyTarget = &applyTarget
case "upperBound":
fallthrough
case "peak":
applyTarget := LimitApplyTargetPeak
cfg.LimitApplyTarget = &applyTarget
default:
klog.Warningf("Unknown apply-target: %s", limitApplyTarget)
}
}

limitCpuApplyTarget := getAnnotation("limit-cpu-apply-target")
if limitCpuApplyTarget != "" {
switch limitCpuApplyTarget {
case "lowerBound":
fallthrough
case "frugal":
applyTarget := LimitApplyTargetFrugal
cfg.LimitCpuApplyTarget = &applyTarget
case "target":
fallthrough
case "balanced":
applyTarget := LimitApplyTargetBalanced
cfg.LimitCpuApplyTarget = &applyTarget
case "upperBound":
fallthrough
case "peak":
applyTarget := LimitApplyTargetPeak
cfg.LimitCpuApplyTarget = &applyTarget
default:
klog.Warningf("Unknown apply-target: %s", limitCpuApplyTarget)
}
}

limitMemoryApplyTarget := getAnnotation("limit-memory-apply-target")
if limitMemoryApplyTarget != "" {
switch limitMemoryApplyTarget {
case "lowerBound":
fallthrough
case "frugal":
applyTarget := LimitApplyTargetFrugal
cfg.LimitMemoryApplyTarget = &applyTarget
case "target":
fallthrough
case "balanced":
applyTarget := LimitApplyTargetBalanced
cfg.LimitMemoryApplyTarget = &applyTarget
case "upperBound":
fallthrough
case "peak":
applyTarget := LimitApplyTargetPeak
cfg.LimitMemoryApplyTarget = &applyTarget
default:
klog.Warningf("Unknown apply-target: %s", limitMemoryApplyTarget)
}
}

Expand Down
125 changes: 109 additions & 16 deletions pkg/config/vpa-workload-cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func (v *VpaWorkloadCfg) GetMemoryLimitFromCpuValue(containerName string) string
return utils.GetEnv("OBLIK_DEFAULT_MEMORY_LIMIT_FROM_CPU_VALUE", "2")
}

func (v *VpaWorkloadCfg) GetRequestApplyTarget(containerName string) ApplyTarget {
func (v *VpaWorkloadCfg) GetRequestApplyTarget(containerName string) RequestApplyTarget {
if v.Containers[containerName] != nil && v.Containers[containerName].RequestApplyTarget != nil {
return *v.Containers[containerName].RequestApplyTarget
}
Expand All @@ -674,23 +674,23 @@ func (v *VpaWorkloadCfg) GetRequestApplyTarget(containerName string) ApplyTarget
case "lowerBound":
fallthrough
case "frugal":
return ApplyTargetFrugal
return RequestApplyTargetFrugal
case "target":
fallthrough
case "balanced":
return ApplyTargetBalanced
return RequestApplyTargetBalanced
case "upperBound":
fallthrough
case "peak":
return ApplyTargetPeak
return RequestApplyTargetPeak
default:
klog.Warningf("Unknown apply-target: %s", requestApplyTarget)
klog.Warningf("Unknown request-apply-target: %s", requestApplyTarget)
}
}
return ApplyTargetBalanced
return RequestApplyTargetBalanced
}

func (v *VpaWorkloadCfg) GetRequestCpuApplyTarget(containerName string) ApplyTarget {
func (v *VpaWorkloadCfg) GetRequestCpuApplyTarget(containerName string) RequestApplyTarget {
if v.Containers[containerName] != nil && v.Containers[containerName].RequestCpuApplyTarget != nil {
return *v.Containers[containerName].RequestCpuApplyTarget
}
Expand All @@ -703,23 +703,23 @@ func (v *VpaWorkloadCfg) GetRequestCpuApplyTarget(containerName string) ApplyTar
case "lowerBound":
fallthrough
case "frugal":
return ApplyTargetFrugal
return RequestApplyTargetFrugal
case "target":
fallthrough
case "balanced":
return ApplyTargetBalanced
return RequestApplyTargetBalanced
case "upperBound":
fallthrough
case "peak":
return ApplyTargetPeak
return RequestApplyTargetPeak
default:
klog.Warningf("Unknown apply-target: %s", requestCpuApplyTarget)
klog.Warningf("Unknown request-apply-target: %s", requestCpuApplyTarget)
}
}
return v.GetRequestApplyTarget(containerName)
}

func (v *VpaWorkloadCfg) GetRequestMemoryApplyTarget(containerName string) ApplyTarget {
func (v *VpaWorkloadCfg) GetRequestMemoryApplyTarget(containerName string) RequestApplyTarget {
if v.Containers[containerName] != nil && v.Containers[containerName].RequestMemoryApplyTarget != nil {
return *v.Containers[containerName].RequestMemoryApplyTarget
}
Expand All @@ -732,22 +732,115 @@ func (v *VpaWorkloadCfg) GetRequestMemoryApplyTarget(containerName string) Apply
case "lowerBound":
fallthrough
case "frugal":
return ApplyTargetFrugal
return RequestApplyTargetFrugal
case "target":
fallthrough
case "balanced":
return ApplyTargetBalanced
return RequestApplyTargetBalanced
case "upperBound":
fallthrough
case "peak":
return ApplyTargetPeak
return RequestApplyTargetPeak
default:
klog.Warningf("Unknown apply-target: %s", requestMemoryApplyTarget)
klog.Warningf("Unknown request-apply-target: %s", requestMemoryApplyTarget)
}
}
return v.GetRequestApplyTarget(containerName)
}

func (v *VpaWorkloadCfg) GetLimitApplyTarget(containerName string) LimitApplyTarget {
if v.Containers[containerName] != nil && v.Containers[containerName].LimitApplyTarget != nil {
return *v.Containers[containerName].LimitApplyTarget
}
if v.LimitApplyTarget != nil {
return *v.LimitApplyTarget
}
limitApplyTarget := utils.GetEnv("OBLIK_DEFAULT_LIMIT_APPLY_TARGET", "")
if limitApplyTarget != "" {
switch limitApplyTarget {
case "auto":
return LimitApplyTargetAuto
case "lowerBound":
fallthrough
case "frugal":
return LimitApplyTargetFrugal
case "target":
fallthrough
case "balanced":
return LimitApplyTargetBalanced
case "upperBound":
fallthrough
case "peak":
return LimitApplyTargetPeak
default:
klog.Warningf("Unknown limit-apply-target: %s", limitApplyTarget)
}
}
return LimitApplyTargetAuto
}

func (v *VpaWorkloadCfg) GetLimitCpuApplyTarget(containerName string) LimitApplyTarget {
if v.Containers[containerName] != nil && v.Containers[containerName].LimitCpuApplyTarget != nil {
return *v.Containers[containerName].LimitCpuApplyTarget
}
if v.LimitCpuApplyTarget != nil {
return *v.LimitCpuApplyTarget
}
limitCpuApplyTarget := utils.GetEnv("OBLIK_DEFAULT_LIMIT_CPU_APPLY_TARGET", "")
if limitCpuApplyTarget != "" {
switch limitCpuApplyTarget {
case "auto":
return LimitApplyTargetAuto
case "lowerBound":
fallthrough
case "frugal":
return LimitApplyTargetFrugal
case "target":
fallthrough
case "balanced":
return LimitApplyTargetBalanced
case "upperBound":
fallthrough
case "peak":
return LimitApplyTargetPeak
default:
klog.Warningf("Unknown limit-apply-target: %s", limitCpuApplyTarget)
}
}
return LimitApplyTargetAuto
}

func (v *VpaWorkloadCfg) GetLimitMemoryApplyTarget(containerName string) LimitApplyTarget {
if v.Containers[containerName] != nil && v.Containers[containerName].LimitMemoryApplyTarget != nil {
return *v.Containers[containerName].LimitMemoryApplyTarget
}
if v.LimitMemoryApplyTarget != nil {
return *v.LimitMemoryApplyTarget
}
limitMemoryApplyTarget := utils.GetEnv("OBLIK_DEFAULT_LIMIT_MEMORY_APPLY_TARGET", "")
if limitMemoryApplyTarget != "" {
switch limitMemoryApplyTarget {
case "auto":
return LimitApplyTargetAuto
case "lowerBound":
fallthrough
case "frugal":
return LimitApplyTargetFrugal
case "target":
fallthrough
case "balanced":
return LimitApplyTargetBalanced
case "upperBound":
fallthrough
case "peak":
return LimitApplyTargetPeak
default:
klog.Warningf("Unknown limit-apply-target: %s", limitMemoryApplyTarget)
}
}
return LimitApplyTargetAuto
}

func (v *VpaWorkloadCfg) GetRequestCpuScaleDirection(containerName string) ScaleDirection {
if v.Containers[containerName] != nil && v.Containers[containerName].RequestCpuScaleDirection != nil {
return *v.Containers[containerName].RequestCpuScaleDirection
Expand Down
Loading

0 comments on commit c755198

Please sign in to comment.