Skip to content

Commit

Permalink
feat: adding woop scaling policy min/max for cpu and memory (#392)
Browse files Browse the repository at this point in the history
* WOOP-436 adding scaling policy min/max values

* WOOP-436 lookback type updated by accident

* WOOP-436 docs

* WOOP-436 skip default 0 values for policy max

* WOOP-436 added min value validation
  • Loading branch information
JolantaSk authored and sarvesh-cast committed Oct 15, 2024
1 parent 871af3e commit c4de9a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
29 changes: 25 additions & 4 deletions castai/resource_workload_scaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package castai
import (
"context"
"fmt"
"github.com/samber/lo"
"net/http"
"regexp"
"strings"
"time"

"github.com/samber/lo"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -67,13 +68,13 @@ func resourceWorkloadScalingPolicy() *schema.Resource {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: workloadScalingPolicyResourceSchema("QUANTILE", 0),
Elem: workloadScalingPolicyResourceSchema("QUANTILE", 0, 0.01),
},
"memory": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: workloadScalingPolicyResourceSchema("MAX", 0.1),
Elem: workloadScalingPolicyResourceSchema("MAX", 0.1, 10),
},
"startup": {
Type: schema.TypeList,
Expand Down Expand Up @@ -117,7 +118,7 @@ func resourceWorkloadScalingPolicy() *schema.Resource {
}
}

func workloadScalingPolicyResourceSchema(function string, overhead float64) *schema.Resource {
func workloadScalingPolicyResourceSchema(function string, overhead, minRecommended float64) *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"function": {
Expand Down Expand Up @@ -158,6 +159,18 @@ func workloadScalingPolicyResourceSchema(function string, overhead float64) *sch
Description: "The look back period in seconds for the recommendation.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(24*60*60, 7*24*60*60)),
},
"min": {
Type: schema.TypeFloat,
Default: minRecommended,
Optional: true,
Description: "Min values for the recommendation, applies to every container. For memory - this is in MiB, for CPU - this is in cores.",
ValidateDiagFunc: validation.ToDiagFunc(validation.FloatAtLeast(minRecommended)),
},
"max": {
Type: schema.TypeFloat,
Optional: true,
Description: "Max values for the recommendation, applies to every container. For memory - this is in MiB, for CPU - this is in cores.",
},
},
}
}
Expand Down Expand Up @@ -384,6 +397,12 @@ func toWorkloadScalingPolicies(obj map[string]interface{}) sdk.Workloadoptimizat
if v, ok := obj["look_back_period_seconds"].(int); ok && v > 0 {
out.LookBackPeriodSeconds = lo.ToPtr(int32(v))
}
if v, ok := obj["min"].(float64); ok {
out.Min = lo.ToPtr(v)
}
if v, ok := obj["max"].(float64); ok && v > 0 {
out.Max = lo.ToPtr(v)
}

return out
}
Expand All @@ -394,6 +413,8 @@ func toWorkloadScalingPoliciesMap(p sdk.WorkloadoptimizationV1ResourcePolicies)
"args": p.Args,
"overhead": p.Overhead,
"apply_threshold": p.ApplyThreshold,
"min": p.Min,
"max": p.Max,
}

if p.LookBackPeriodSeconds != nil {
Expand Down
12 changes: 12 additions & 0 deletions castai/resource_workload_scaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ func TestAccResourceWorkloadScalingPolicy(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "cpu.0.apply_threshold", "0.06"),
resource.TestCheckResourceAttr(resourceName, "cpu.0.args.0", "0.86"),
resource.TestCheckResourceAttr(resourceName, "cpu.0.look_back_period_seconds", "86401"),
resource.TestCheckResourceAttr(resourceName, "cpu.0.min", "0.1"),
resource.TestCheckResourceAttr(resourceName, "cpu.0.max", "1"),
resource.TestCheckResourceAttr(resourceName, "memory.0.function", "MAX"),
resource.TestCheckResourceAttr(resourceName, "memory.0.overhead", "0.25"),
resource.TestCheckResourceAttr(resourceName, "memory.0.apply_threshold", "0.1"),
resource.TestCheckResourceAttr(resourceName, "memory.0.args.#", "0"),
resource.TestCheckResourceAttr(resourceName, "memory.0.min", "100"),
),
},
{
Expand All @@ -63,10 +66,13 @@ func TestAccResourceWorkloadScalingPolicy(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "cpu.0.apply_threshold", "0.1"),
resource.TestCheckResourceAttr(resourceName, "cpu.0.args.0", "0.9"),
resource.TestCheckResourceAttr(resourceName, "cpu.0.look_back_period_seconds", "86402"),
resource.TestCheckResourceAttr(resourceName, "cpu.0.min", "0.1"),
resource.TestCheckResourceAttr(resourceName, "memory.0.function", "QUANTILE"),
resource.TestCheckResourceAttr(resourceName, "memory.0.overhead", "0.35"),
resource.TestCheckResourceAttr(resourceName, "memory.0.apply_threshold", "0.2"),
resource.TestCheckResourceAttr(resourceName, "memory.0.args.0", "0.9"),
resource.TestCheckResourceAttr(resourceName, "memory.0.min", "100"),
resource.TestCheckResourceAttr(resourceName, "memory.0.max", "512"),
resource.TestCheckResourceAttr(resourceName, "startup.0.period_seconds", "123"),
resource.TestCheckResourceAttr(resourceName, "downscaling.0.apply_type", "DEFERRED"),
),
Expand Down Expand Up @@ -97,12 +103,15 @@ func scalingPolicyConfig(clusterName, projectID, name string) string {
overhead = 0.05
apply_threshold = 0.06
args = ["0.86"]
min = 0.1
max = 1
look_back_period_seconds = 86401
}
memory {
function = "MAX"
overhead = 0.25
apply_threshold = 0.1
min = 100
}
}`, name)

Expand All @@ -123,12 +132,15 @@ func scalingPolicyConfigUpdated(clusterName, projectID, name string) string {
apply_threshold = 0.1
args = ["0.9"]
look_back_period_seconds = 86402
min = 0.1
}
memory {
function = "QUANTILE"
overhead = 0.35
apply_threshold = 0.2
args = ["0.9"]
min = 100
max = 512
}
startup {
period_seconds = 123
Expand Down
8 changes: 8 additions & 0 deletions castai/sdk/api.gen.go

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

6 changes: 6 additions & 0 deletions docs/resources/workload_scaling_policy.md

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

2 changes: 2 additions & 0 deletions examples/resources/castai_workload_scaling_policy/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ resource "castai_workload_scaling_policy" "services" {
apply_threshold = 0.1
args = ["0.9"]
look_back_period_seconds = 172800
min = 0.1
max = 1
}
memory {
function = "MAX"
Expand Down

0 comments on commit c4de9a5

Please sign in to comment.