Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add confidence threshold to workload scaling policy #459

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
.vscode/

# built binary
terraform-provider-castai
Expand Down
74 changes: 70 additions & 4 deletions castai/resource_workload_scaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ const (
minApplyThresholdValue = 0.01
maxApplyThresholdValue = 2.5
defaultApplyThresholdPercentage = 0.1
defaultConfidenceThreshold = 0.9
minNumeratorValue = 0.0
maxExponentValue = 1.
minExponentValue = 0.
)

const (
FieldLimitStrategy = "limit"
FieldLimitStrategyType = "type"
FieldLimitStrategyMultiplier = "multiplier"

FieldLimitStrategy = "limit"
FieldLimitStrategyType = "type"
FieldLimitStrategyMultiplier = "multiplier"
FieldConfidence = "confidence"
FieldConfidenceThreshold = "threshold"
DeprecatedFieldApplyThreshold = "apply_threshold"
FieldApplyThresholdStrategy = "apply_threshold_strategy"
FieldApplyThresholdStrategyType = "type"
Expand Down Expand Up @@ -119,6 +121,26 @@ func resourceWorkloadScalingPolicy() *schema.Resource {
},
},
},
FieldConfidence: {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Defines the confidence settings for applying recommendations.",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return suppressConfidenceThresholdDefaultValueDiff(FieldConfidence, old, new, d)
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
FieldConfidenceThreshold: {
Type: schema.TypeFloat,
Optional: true,
Default: 0.9,
Description: "Defines the confidence threshold for applying recommendations. The smaller number indicates that we require fewer metrics data points to apply recommendations.",
ValidateDiagFunc: validation.ToDiagFunc(validation.FloatBetween(0, 1)),
},
},
},
},
"downscaling": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -381,6 +403,8 @@ func resourceWorkloadScalingPolicyCreate(ctx context.Context, d *schema.Resource
}
req.RecommendationPolicies.Memory = memory
}

req.RecommendationPolicies.Confidence = toConfidence(toSection(d, FieldConfidence))

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

Expand Down Expand Up @@ -452,6 +476,9 @@ func resourceWorkloadScalingPolicyRead(ctx context.Context, d *schema.ResourceDa
if err := d.Set("startup", toStartupMap(sp.RecommendationPolicies.Startup)); err != nil {
return diag.FromErr(fmt.Errorf("setting startup: %w", err))
}
if err := d.Set(FieldConfidence, toConfidenceMap(sp.RecommendationPolicies.Confidence)); err != nil {
return diag.FromErr(fmt.Errorf("setting confidence: %w", err))
}
if err := d.Set("downscaling", toDownscalingMap(sp.RecommendationPolicies.Downscaling)); err != nil {
return diag.FromErr(fmt.Errorf("setting downscaling: %w", err))
}
Expand Down Expand Up @@ -483,6 +510,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
"downscaling",
"memory_event",
"anti_affinity",
FieldConfidence,
) {
tflog.Info(ctx, "scaling policy up to date")
return nil
Expand All @@ -509,6 +537,7 @@ func resourceWorkloadScalingPolicyUpdate(ctx context.Context, d *schema.Resource
Downscaling: toDownscaling(toSection(d, "downscaling")),
MemoryEvent: toMemoryEvent(toSection(d, "memory_event")),
AntiAffinity: toAntiAffinity(toSection(d, "anti_affinity")),
Confidence: toConfidence(toSection(d, FieldConfidence)),
},
}

Expand Down Expand Up @@ -740,6 +769,17 @@ func suppressThresholdStrategyDefaultValueDiff(resource, oldValue, newValue stri
return oldValue == newValue
}

func suppressConfidenceThresholdDefaultValueDiff(resource, oldValue, newValue string, d *schema.ResourceData) bool {
isConfidenceUnset := newValue == "0" || newValue == ""
if isConfidenceUnset {
confidenceThreshold := d.Get(fmt.Sprintf("%s.0.%s", resource, FieldConfidenceThreshold))
// Suppress diff if configuration saved from API equals to default
return confidenceThreshold == defaultConfidenceThreshold
}

return oldValue == newValue
}

func toWorkloadResourceLimit(obj map[string]any) (*sdk.WorkloadoptimizationV1ResourceLimitStrategy, error) {
if len(obj) == 0 {
return nil, nil
Expand Down Expand Up @@ -852,6 +892,32 @@ func applyThresholdStrategyToMap(s *sdk.WorkloadoptimizationV1ApplyThresholdStra
return []map[string]any{m}
}

func toConfidence(confidence map[string]any) *sdk.WorkloadoptimizationV1ConfidenceSettings {
if len(confidence) == 0 {
return nil
}

result := &sdk.WorkloadoptimizationV1ConfidenceSettings{}

if v, ok := confidence[FieldConfidenceThreshold].(float64); ok {
result.Threshold = &v
}

return result
}

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

m := map[string]any{
FieldConfidenceThreshold: s.Threshold,
}

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

func toStartup(startup map[string]any) *sdk.WorkloadoptimizationV1StartupSettings {
if len(startup) == 0 {
return nil
Expand Down
8 changes: 8 additions & 0 deletions castai/resource_workload_scaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestAccResourceWorkloadScalingPolicy(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "memory.0.limit.0.type", "MULTIPLIER"),
resource.TestCheckResourceAttr(resourceName, "memory.0.limit.0.multiplier", "1.8"),
resource.TestCheckResourceAttr(resourceName, "memory.0.management_option", "READ_ONLY"),
resource.TestCheckResourceAttr(resourceName, "confidence.0.threshold", "0.4"),
),
},
{
Expand Down Expand Up @@ -88,6 +89,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, "confidence.0.threshold", "0.6"),
resource.TestCheckResourceAttr(resourceName, "anti_affinity.0.consider_anti_affinity", "true"),
),
},
Expand All @@ -112,6 +114,9 @@ func scalingPolicyConfig(clusterName, projectID, name string) string {
cluster_id = castai_gke_cluster.test.id
apply_type = "IMMEDIATE"
management_option = "READ_ONLY"
confidence {
threshold = 0.4
}
cpu {
function = "QUANTILE"
overhead = 0.05
Expand Down Expand Up @@ -195,6 +200,9 @@ func scalingPolicyConfigUpdated(clusterName, projectID, name string) string {
anti_affinity {
consider_anti_affinity = true
}
confidence {
threshold = 0.6
}
}`, updatedName)

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

Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ resource "castai_workload_scaling_policy" "services" {
anti_affinity {
consider_anti_affinity = false
}
confidence {
threshold = 0.9
}
}
Loading