Skip to content

Commit

Permalink
feat: add woop anti-affinity settings to tf scaling policy (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
UOndro authored Oct 9, 2024
1 parent 782c71c commit 50e0579
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 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 @@ -125,6 +125,21 @@ func resourceWorkloadScalingPolicy() *schema.Resource {
},
},
},
"anti_affinity": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"consider_anti_affinity": {
Type: schema.TypeBool,
Optional: true,
Description: `Defines if anti-affinity should be considered when scaling the workload.
If enabled, requiring host ports, or having anti-affinity on hostname will force all recommendations to be deferred.`,
},
},
},
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Second),
Expand Down Expand Up @@ -218,6 +233,8 @@ func resourceWorkloadScalingPolicyCreate(ctx context.Context, d *schema.Resource

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

req.RecommendationPolicies.AntiAffinity = toAntiAffinity(toSection(d, "anti_affinity"))

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

return nil
}
Expand All @@ -286,6 +306,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
"startup",
"downscaling",
"memory_event",
"anti_affinity",
) {
tflog.Info(ctx, "scaling policy up to date")
return nil
Expand All @@ -303,6 +324,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
Startup: toStartup(toSection(d, "startup")),
Downscaling: toDownscaling(toSection(d, "downscaling")),
MemoryEvent: toMemoryEvent(toSection(d, "memory_event")),
AntiAffinity: toAntiAffinity(toSection(d, "anti_affinity")),
},
}

Expand Down Expand Up @@ -542,3 +564,35 @@ func toMemoryEventMap(s *sdk.WorkloadoptimizationV1MemoryEventSettings) []map[st

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

func toAntiAffinity(antiAffinity map[string]any) *sdk.WorkloadoptimizationV1AntiAffinitySettings {
if len(antiAffinity) == 0 {
return nil
}

result := &sdk.WorkloadoptimizationV1AntiAffinitySettings{}

if v, ok := antiAffinity["consider_anti_affinity"].(bool); ok {
result.ConsiderAntiAffinity = lo.ToPtr(v)
}

return result
}

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

m := map[string]any{}

if s.ConsiderAntiAffinity != nil {
m["consider_anti_affinity"] = *s.ConsiderAntiAffinity
}

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 @@ -76,6 +76,7 @@ func TestAccResourceWorkloadScalingPolicy(t *testing.T) {
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"),
resource.TestCheckResourceAttr(resourceName, "anti_affinity.0.consider_anti_affinity", "true"),
),
},
},
Expand Down Expand Up @@ -151,6 +152,9 @@ func scalingPolicyConfigUpdated(clusterName, projectID, name string) string {
}
memory_event {
apply_type = "DEFERRED"
}
anti_affinity {
consider_anti_affinity = true
}
}`, updatedName)

Expand Down
20 changes: 17 additions & 3 deletions castai/sdk/api.gen.go

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

15 changes: 14 additions & 1 deletion docs/resources/workload_scaling_policy.md

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

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ resource "castai_workload_scaling_policy" "services" {
downscaling {
apply_type = "DEFERRED"
}
downscaling {
memory_event {
apply_type = "IMMEDIATE"
}
anti_affinity {
consider_anti_affinity = false
}
}

0 comments on commit 50e0579

Please sign in to comment.