From 57ddefadec69fb0b6243574dc84671e47e73bd83 Mon Sep 17 00:00:00 2001 From: Arsalan Khan Date: Fri, 20 Dec 2024 10:50:39 +0100 Subject: [PATCH] wip: custom metrics with binding secret --- src/acceptance/app/custom_metric_test.go | 10 +++++--- src/acceptance/helpers/helpers.go | 30 ++++++++++++++++-------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/acceptance/app/custom_metric_test.go b/src/acceptance/app/custom_metric_test.go index 3b9410e5c1..8e22415187 100644 --- a/src/acceptance/app/custom_metric_test.go +++ b/src/acceptance/app/custom_metric_test.go @@ -27,12 +27,16 @@ var _ = Describe("AutoScaler custom metrics", func() { Describe("custom metrics policy for same app", func() { BeforeEach(func() { - policy = GenerateDynamicScaleOutAndInPolicy(1, 2, "test_metric", 500, 500) + policy = GeneratePolicyWithCredentialType( + 1, 2, "test_metric", 500, 500, + "binding-secret") instanceName = CreatePolicy(cfg, appToScaleName, appToScaleGUID, policy) StartApp(appToScaleName, cfg.CfPushTimeoutDuration()) }) - // This test will fail if credential-type is set to X509 in autoscaler broker. - // Therefore, only mtls connection will be supported for custom metrics in future + /* + Going forward, custom metrics submission should be possible via mTLS route only.This test can be removed in future if credential-type is set to X509. + Added test for rollback cases where custom metrics are still sent via basic auth route. + */ Context("when scaling by custom metrics", func() { It("should scale out and scale in", Label(acceptance.LabelSmokeTests), func() { By("Scale out to 2 instances") diff --git a/src/acceptance/helpers/helpers.go b/src/acceptance/helpers/helpers.go index ee3c2bc9f4..335dc337f7 100644 --- a/src/acceptance/helpers/helpers.go +++ b/src/acceptance/helpers/helpers.go @@ -59,10 +59,11 @@ func (b *BindingConfig) SetCustomMetricsStrategy(allowFrom string) { } type ScalingPolicy struct { - InstanceMin int `json:"instance_min_count"` - InstanceMax int `json:"instance_max_count"` - ScalingRules []*ScalingRule `json:"scaling_rules,omitempty"` - Schedules *ScalingSchedules `json:"schedules,omitempty"` + InstanceMin int `json:"instance_min_count"` + InstanceMax int `json:"instance_max_count"` + ScalingRules []*ScalingRule `json:"scaling_rules,omitempty"` + Schedules *ScalingSchedules `json:"schedules,omitempty"` + CredentialType *string `json:"credential_type,omitempty"` } type ScalingPolicyWithExtraFields struct { @@ -191,7 +192,7 @@ func GenerateBindingsWithScalingPolicy(allowFrom string, instanceMin, instanceMa Configuration: Configuration{CustomMetrics: CustomMetricsConfig{ MetricSubmissionStrategy: MetricsSubmissionStrategy{AllowFrom: allowFrom}, }}, - ScalingPolicy: buildScaleOutScaleInPolicy(instanceMin, instanceMax, metricName, scaleInThreshold, scaleOutThreshold), + ScalingPolicy: buildScaleOutScaleInPolicy(instanceMin, instanceMax, metricName, scaleInThreshold, scaleOutThreshold, ""), } marshalledBinding, err := MarshalWithoutHTMLEscape(bindingConfig) Expect(err).NotTo(HaveOccurred()) @@ -265,14 +266,22 @@ func GenerateDynamicScaleOutPolicyWithExtraFields(instanceMin, instanceMax int, } func GenerateDynamicScaleOutAndInPolicy(instanceMin, instanceMax int, metricName string, scaleInWhenBelowThreshold int64, scaleOutWhenGreaterOrEqualThreshold int64) string { - policy := buildScaleOutScaleInPolicy(instanceMin, instanceMax, metricName, scaleInWhenBelowThreshold, scaleOutWhenGreaterOrEqualThreshold) + policy := buildScaleOutScaleInPolicy(instanceMin, instanceMax, metricName, scaleInWhenBelowThreshold, scaleOutWhenGreaterOrEqualThreshold, "") marshaled, err := MarshalWithoutHTMLEscape(policy) Expect(err).NotTo(HaveOccurred()) return string(marshaled) } -func buildScaleOutScaleInPolicy(instanceMin int, instanceMax int, metricName string, scaleInWhenBelowThreshold int64, scaleOutWhenGreaterOrEqualThreshold int64) ScalingPolicy { +func GeneratePolicyWithCredentialType(instanceMin, instanceMax int, metricName string, scaleInWhenBelowThreshold int64, scaleOutWhenGreaterOrEqualThreshold int64, credentialType string) string { + policyWithCredentialType := buildScaleOutScaleInPolicy(instanceMin, instanceMax, metricName, scaleInWhenBelowThreshold, scaleOutWhenGreaterOrEqualThreshold, credentialType) + marshaled, err := MarshalWithoutHTMLEscape(policyWithCredentialType) + Expect(err).NotTo(HaveOccurred()) + + return string(marshaled) +} + +func buildScaleOutScaleInPolicy(instanceMin int, instanceMax int, metricName string, scaleInWhenBelowThreshold int64, scaleOutWhenGreaterOrEqualThreshold int64, credentialType string) ScalingPolicy { scalingOutRule := ScalingRule{ MetricType: metricName, BreachDurationSeconds: TestBreachDurationSeconds, @@ -290,9 +299,10 @@ func buildScaleOutScaleInPolicy(instanceMin int, instanceMax int, metricName str Adjustment: "-1", } policy := ScalingPolicy{ - InstanceMin: instanceMin, - InstanceMax: instanceMax, - ScalingRules: []*ScalingRule{&scalingOutRule, &scalingInRule}, + InstanceMin: instanceMin, + InstanceMax: instanceMax, + ScalingRules: []*ScalingRule{&scalingOutRule, &scalingInRule}, + CredentialType: &credentialType, } return policy }