Skip to content

Commit

Permalink
feat: add memory event setting to WOOP policy (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
jansyk13 authored Oct 7, 2024
1 parent 8dc5255 commit 782c71c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 7 deletions.
56 changes: 56 additions & 0 deletions castai/resource_workload_scaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ func resourceWorkloadScalingPolicy() *schema.Resource {
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)),
},
},
},
},
"memory_event": {
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 applying recommendation for memory related event.
- 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)),
},
Expand Down Expand Up @@ -199,6 +216,8 @@ func resourceWorkloadScalingPolicyCreate(ctx context.Context, d *schema.Resource

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

req.RecommendationPolicies.MemoryEvent = toMemoryEvent(toSection(d, "memory_event"))

resp, err := client.WorkloadOptimizationAPICreateWorkloadScalingPolicyWithResponse(ctx, clusterID, req)
if checkErr := sdk.CheckOKResponse(resp, err); checkErr != nil {
return diag.FromErr(checkErr)
Expand Down Expand Up @@ -250,6 +269,9 @@ func resourceWorkloadScalingPolicyRead(ctx context.Context, d *schema.ResourceDa
if err := d.Set("downscaling", toDownscalingMap(sp.RecommendationPolicies.Downscaling)); err != nil {
return diag.FromErr(fmt.Errorf("setting downscaling: %w", err))
}
if err := d.Set("memory_event", toMemoryEventMap(sp.RecommendationPolicies.MemoryEvent)); err != nil {
return diag.FromErr(fmt.Errorf("setting memory event: %w", err))
}

return nil
}
Expand All @@ -263,6 +285,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
"memory",
"startup",
"downscaling",
"memory_event",
) {
tflog.Info(ctx, "scaling policy up to date")
return nil
Expand All @@ -279,6 +302,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
Memory: toWorkloadScalingPolicies(d.Get("memory").([]interface{})[0].(map[string]interface{})),
Startup: toStartup(toSection(d, "startup")),
Downscaling: toDownscaling(toSection(d, "downscaling")),
MemoryEvent: toMemoryEvent(toSection(d, "memory_event")),
},
}

Expand Down Expand Up @@ -486,3 +510,35 @@ func toDownscalingMap(s *sdk.WorkloadoptimizationV1DownscalingSettings) []map[st

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

func toMemoryEvent(memoryEvent map[string]any) *sdk.WorkloadoptimizationV1MemoryEventSettings {
if len(memoryEvent) == 0 {
return nil
}

result := &sdk.WorkloadoptimizationV1MemoryEventSettings{}

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

return result
}

func toMemoryEventMap(s *sdk.WorkloadoptimizationV1MemoryEventSettings) []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 @@ -75,6 +75,7 @@ func TestAccResourceWorkloadScalingPolicy(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "memory.0.max", "512"),
resource.TestCheckResourceAttr(resourceName, "startup.0.period_seconds", "123"),
resource.TestCheckResourceAttr(resourceName, "downscaling.0.apply_type", "DEFERRED"),
resource.TestCheckResourceAttr(resourceName, "memory_event.0.apply_type", "DEFERRED"),
),
},
},
Expand Down Expand Up @@ -148,6 +149,9 @@ func scalingPolicyConfigUpdated(clusterName, projectID, name string) string {
downscaling {
apply_type = "DEFERRED"
}
memory_event {
apply_type = "DEFERRED"
}
}`, updatedName)

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

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 @@ -23,4 +23,7 @@ resource "castai_workload_scaling_policy" "services" {
downscaling {
apply_type = "DEFERRED"
}
downscaling {
apply_type = "IMMEDIATE"
}
}

0 comments on commit 782c71c

Please sign in to comment.