Skip to content

Commit

Permalink
feat: Introduce activationThreshold/minMetricValue for Kubernetes Wor…
Browse files Browse the repository at this point in the history
…kload Scaler (#3408)
  • Loading branch information
Jorge Turrado Ferrero authored Jul 21, 2022
1 parent 70c45a4 commit c664037
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
23 changes: 17 additions & 6 deletions pkg/scalers/kubernetes_workload_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
kubernetesWorkloadMetricType = "External"
podSelectorKey = "podSelector"
valueKey = "value"
activationValueKey = "activationValue"
)

var phasesCountedAsTerminated = []corev1.PodPhase{
Expand All @@ -32,10 +33,11 @@ var phasesCountedAsTerminated = []corev1.PodPhase{
}

type kubernetesWorkloadMetadata struct {
podSelector labels.Selector
namespace string
value float64
scalerIndex int
podSelector labels.Selector
namespace string
value float64
activationValue float64
scalerIndex int
}

// NewKubernetesWorkloadScaler creates a new kubernetesWorkloadScaler
Expand Down Expand Up @@ -67,8 +69,17 @@ func parseWorkloadMetadata(config *ScalerConfig) (*kubernetesWorkloadMetadata, e
}
meta.value, err = strconv.ParseFloat(config.TriggerMetadata[valueKey], 64)
if err != nil || meta.value == 0 {
return nil, fmt.Errorf("value must be an integer greater than 0")
return nil, fmt.Errorf("value must be a float greater than 0")
}

meta.activationValue = 0
if val, ok := config.TriggerMetadata[activationValueKey]; ok {
meta.activationValue, err = strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("value must be a float")
}
}

meta.scalerIndex = config.ScalerIndex
return meta, nil
}
Expand All @@ -81,7 +92,7 @@ func (s *kubernetesWorkloadScaler) IsActive(ctx context.Context) (bool, error) {
return false, err
}

return pods > 0, nil
return float64(pods) > s.metadata.activationValue, nil
}

// Close no need for kubernetes workload scaler
Expand Down
1 change: 1 addition & 0 deletions pkg/scalers/kubernetes_workload_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var parseWorkloadMetadataTestDataset = []workloadMetadataTestData{
{map[string]string{"value": "a", "podSelector": "app=demo"}, "default", true},
{map[string]string{"value": "0", "podSelector": "app=demo"}, "test", true},
{map[string]string{"value": "0", "podSelector": "app=demo"}, "default", true},
{map[string]string{"value": "1", "activationValue": "aa", "podSelector": "app=demo"}, "test", true},
}

func TestParseWorkloadMetadata(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ spec:
- type: kubernetes-workload
metadata:
podSelector: 'pod=workload-test'
value: '1'`
value: '1'
activationValue: '3'`
)

func TestScaler(t *testing.T) {
Expand All @@ -115,13 +116,19 @@ func TestScaler(t *testing.T) {
"replica count should be 0 after 1 minute")

// test scaling
testActivation(t, kc)
testScaleUp(t, kc)
testScaleDown(t, kc)

// cleanup
DeleteKubernetesResources(t, kc, testNamespace, data, templates)
}

func testActivation(t *testing.T, kc *kubernetes.Clientset) {
KubernetesScaleDeployment(t, kc, monitoredDeploymentName, 2, testNamespace)
AssertReplicaCountNotChangeDuringTimePeriod(t, kc, sutDeploymentName, testNamespace, 0, 60)
}

func testScaleUp(t *testing.T, kc *kubernetes.Clientset) {
// scale monitored deployment to 5 replicas
KubernetesScaleDeployment(t, kc, monitoredDeploymentName, 5, testNamespace)
Expand Down

0 comments on commit c664037

Please sign in to comment.