Skip to content

Commit

Permalink
feat: add woop downscaling behaviour customization (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
UndeadRat22 authored and sarvesh-cast committed Oct 15, 2024
1 parent d032e26 commit 871af3e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
57 changes: 56 additions & 1 deletion castai/resource_workload_scaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ func resourceWorkloadScalingPolicy() *schema.Resource {
},
},
},
"downscaling": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"apply_type": {
Type: schema.TypeString,
Optional: true,
Description: `Defines the apply type to be used when downscaling.
- IMMEDIATE - pods are restarted immediately when new recommendation is generated.
- DEFERRED - pods are not restarted and recommendation values are applied during natural restarts only (new deployment, etc.)`,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"IMMEDIATE", "DEFERRED"}, false)),
},
},
},
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Second),
Expand Down Expand Up @@ -167,6 +184,8 @@ func resourceWorkloadScalingPolicyCreate(ctx context.Context, d *schema.Resource

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

req.RecommendationPolicies.Downscaling = toDownscaling(toSection(d, "downscaling"))

resp, err := client.WorkloadOptimizationAPICreateWorkloadScalingPolicyWithResponse(ctx, clusterID, req)
if checkErr := sdk.CheckOKResponse(resp, err); checkErr != nil {
return diag.FromErr(checkErr)
Expand Down Expand Up @@ -212,10 +231,12 @@ func resourceWorkloadScalingPolicyRead(ctx context.Context, d *schema.ResourceDa
if err := d.Set("memory", toWorkloadScalingPoliciesMap(sp.RecommendationPolicies.Memory)); err != nil {
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))
}
if err := d.Set("downscaling", toDownscalingMap(sp.RecommendationPolicies.Downscaling)); err != nil {
return diag.FromErr(fmt.Errorf("setting downscaling: %w", err))
}

return nil
}
Expand All @@ -228,6 +249,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
"cpu",
"memory",
"startup",
"downscaling",
) {
tflog.Info(ctx, "scaling policy up to date")
return nil
Expand All @@ -243,6 +265,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
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")),
Downscaling: toDownscaling(toSection(d, "downscaling")),
},
}

Expand Down Expand Up @@ -410,3 +433,35 @@ func toStartupMap(s *sdk.WorkloadoptimizationV1StartupSettings) []map[string]int

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

func toDownscaling(downscaling map[string]any) *sdk.WorkloadoptimizationV1DownscalingSettings {
if len(downscaling) == 0 {
return nil
}

result := &sdk.WorkloadoptimizationV1DownscalingSettings{}

if v, ok := downscaling["apply_type"].(string); ok && v != "" {
result.ApplyType = lo.ToPtr(sdk.WorkloadoptimizationV1ApplyType(v))
}

return result
}

func toDownscalingMap(s *sdk.WorkloadoptimizationV1DownscalingSettings) []map[string]any {
if s == nil {
return nil
}

m := map[string]any{}

if s.ApplyType != nil {
m["apply_type"] = string(*s.ApplyType)
}

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

return []map[string]any{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 @@ -68,6 +68,7 @@ func TestAccResourceWorkloadScalingPolicy(t *testing.T) {
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"),
resource.TestCheckResourceAttr(resourceName, "downscaling.0.apply_type", "DEFERRED"),
),
},
},
Expand Down Expand Up @@ -132,6 +133,9 @@ func scalingPolicyConfigUpdated(clusterName, projectID, name string) string {
startup {
period_seconds = 123
}
downscaling {
apply_type = "DEFERRED"
}
}`, updatedName)

return ConfigCompose(testAccGKEClusterConfig(name, clusterName, projectID), cfg)
Expand Down
35 changes: 33 additions & 2 deletions castai/sdk/api.gen.go

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

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.

1 change: 1 addition & 0 deletions examples/aks/aks_cluster/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ provider "azurerm" {
}

provider "azuread" {
version = "2.53.1"
tenant_id = data.azurerm_subscription.current.tenant_id
}
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 @@ -18,4 +18,7 @@ resource "castai_workload_scaling_policy" "services" {
startup {
period_seconds = 240
}
downscaling {
apply_type = "DEFERRED"
}
}

0 comments on commit 871af3e

Please sign in to comment.