Skip to content

Commit

Permalink
feat: add startup period to woop policy (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
jansyk13 authored Sep 25, 2024
1 parent f1d8eca commit 7707f93
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
54 changes: 54 additions & 0 deletions castai/resource_workload_scaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func resourceWorkloadScalingPolicy() *schema.Resource {
MaxItems: 1,
Elem: workloadScalingPolicyResourceSchema("MAX", 0.1),
},
"startup": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"period_seconds": {
Type: schema.TypeInt,
Optional: true,
Description: "Defines the duration (in seconds) during which elevated resource usage is expected at startup.\nWhen set, recommendations will be adjusted to disregard resource spikes within this period.\nIf not specified, the workload will receive standard recommendations without startup considerations.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(120, 3600)),
},
},
},
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Second),
Expand Down Expand Up @@ -150,6 +165,8 @@ func resourceWorkloadScalingPolicyCreate(ctx context.Context, d *schema.Resource
req.RecommendationPolicies.Memory = toWorkloadScalingPolicies(v.([]interface{})[0].(map[string]interface{}))
}

req.RecommendationPolicies.Startup = toStartup(toSection(d, "startup"))

resp, err := client.WorkloadOptimizationAPICreateWorkloadScalingPolicyWithResponse(ctx, clusterID, req)
if checkErr := sdk.CheckOKResponse(resp, err); checkErr != nil {
return diag.FromErr(checkErr)
Expand Down Expand Up @@ -196,6 +213,10 @@ func resourceWorkloadScalingPolicyRead(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(fmt.Errorf("setting memory: %w", err))
}

if err := d.Set("startup", toStartupMap(sp.RecommendationPolicies.Startup)); err != nil {
return diag.FromErr(fmt.Errorf("setting startup: %w", err))
}

return nil
}

Expand All @@ -206,6 +227,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
"management_option",
"cpu",
"memory",
"startup",
) {
tflog.Info(ctx, "scaling policy up to date")
return nil
Expand All @@ -220,6 +242,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
ManagementOption: sdk.WorkloadoptimizationV1ManagementOption(d.Get("management_option").(string)),
Cpu: toWorkloadScalingPolicies(d.Get("cpu").([]interface{})[0].(map[string]interface{})),
Memory: toWorkloadScalingPolicies(d.Get("memory").([]interface{})[0].(map[string]interface{})),
Startup: toStartup(toSection(d, "startup")),
},
}

Expand Down Expand Up @@ -356,3 +379,34 @@ func toWorkloadScalingPoliciesMap(p sdk.WorkloadoptimizationV1ResourcePolicies)

return []map[string]interface{}{m}
}

func toStartup(startup map[string]interface{}) *sdk.WorkloadoptimizationV1StartupSettings {
if len(startup) == 0 {
return nil
}
result := &sdk.WorkloadoptimizationV1StartupSettings{}

if v, ok := startup["period_seconds"].(int); ok && v > 0 {
result.PeriodSeconds = lo.ToPtr(int32(v))
}

return result
}

func toStartupMap(s *sdk.WorkloadoptimizationV1StartupSettings) []map[string]interface{} {
if s == nil {
return nil
}

m := map[string]interface{}{}

if s.PeriodSeconds != nil {
m["period_seconds"] = int(*s.PeriodSeconds)
}

if len(m) == 0 {
return nil
}

return []map[string]interface{}{m}
}
4 changes: 4 additions & 0 deletions castai/resource_workload_scaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestAccResourceWorkloadScalingPolicy(t *testing.T) {
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, "startup.0.period_seconds", "123"),
),
},
},
Expand Down Expand Up @@ -128,6 +129,9 @@ func scalingPolicyConfigUpdated(clusterName, projectID, name string) string {
apply_threshold = 0.2
args = ["0.9"]
}
startup {
period_seconds = 123
}
}`, updatedName)

return ConfigCompose(testAccGKEClusterConfig(name, clusterName, projectID), cfg)
Expand Down
14 changes: 14 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.

3 changes: 3 additions & 0 deletions examples/resources/castai_workload_scaling_policy/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ resource "castai_workload_scaling_policy" "services" {
overhead = 0.35
apply_threshold = 0.2
}
startup {
period_seconds = 240
}
}

0 comments on commit 7707f93

Please sign in to comment.