Skip to content

Commit

Permalink
feat: Introduce activationThreshold/minMetricValue for MongoDB Scaler (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Turrado Ferrero authored Jul 20, 2022
1 parent b616ae0 commit 70c45a4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
14 changes: 13 additions & 1 deletion pkg/scalers/mongo_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type mongoDBMetadata struct {
// A threshold that is used as targetAverageValue in HPA
// +required
queryValue int64
// A threshold that is used to check if scaler is active
// +optional
activationQueryValue int64
// The name of the metric to use in the Horizontal Pod Autoscaler. This value will be prefixed with "mongodb-".
// +optional
metricName string
Expand Down Expand Up @@ -134,6 +137,15 @@ func parseMongoDBMetadata(config *ScalerConfig) (*mongoDBMetadata, string, error
return nil, "", fmt.Errorf("no queryValue given")
}

meta.activationQueryValue = 0
if val, ok := config.TriggerMetadata["activationQueryValue"]; ok {
activationQueryValue, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, "", fmt.Errorf("failed to convert %v to int, because of %v", activationQueryValue, err.Error())
}
meta.activationQueryValue = activationQueryValue
}

meta.dbName, err = GetFromAuthOrMeta(config, "dbName")
if err != nil {
return nil, "", err
Expand Down Expand Up @@ -196,7 +208,7 @@ func (s *mongoDBScaler) IsActive(ctx context.Context) (bool, error) {
mongoDBLog.Error(err, fmt.Sprintf("failed to get query result by mongoDB, because of %v", err))
return false, err
}
return result > 0, nil
return result > s.metadata.activationQueryValue, nil
}

// Close disposes of mongoDB connections
Expand Down
7 changes: 7 additions & 0 deletions pkg/scalers/mongo_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ var testMONGODBMetadata = []parseMongoDBMetadataTestData{
resolvedEnv: testMongoDBResolvedEnv,
raisesError: false,
},
// wrong activationQueryValue
{
metadata: map[string]string{"query": `{"name":"John"}`, "metricName": "hpa", "collection": "demo", "queryValue": "12", "activationQueryValue": "aa", "connectionStringFromEnv": "Mongo_CONN_STR", "dbName": "test"},
authParams: map[string]string{},
resolvedEnv: testMongoDBResolvedEnv,
raisesError: true,
},
}

var mongoDBMetricIdentifiers = []mongoDBMetricIdentifier{
Expand Down
20 changes: 18 additions & 2 deletions tests/scalers_go/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/base64"
"fmt"
"testing"
"time"

"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -144,6 +145,7 @@ spec:
collection: {{.Collection}}
query: '{"region":"eu-1","state":"running","plan":"planA"}'
queryValue: "1"
activationQueryValue: "4"
authenticationRef:
name: {{.TriggerAuthName}}
`
Expand All @@ -164,6 +166,7 @@ func TestScaler(t *testing.T) {
"job count should be 0 after 1 minute")

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

// cleanup
Expand Down Expand Up @@ -221,12 +224,25 @@ func setupMongo(t *testing.T, kc *kubernetes.Clientset) string {
return mongoPod
}

func testActivation(t *testing.T, kc *kubernetes.Clientset, mongoPod string) {
t.Log("--- testing activation ---")

insertCmd := fmt.Sprintf(`db.%s.insert([
{"region":"eu-1","state":"running","plan":"planA","goods":"apple"},
{"region":"eu-1","state":"running","plan":"planA","goods":"orange"}
])`, mongoCollection)

_, err := ExecuteCommand(fmt.Sprintf("kubectl exec %s -n %s -- mongo --eval '%s'", mongoPod, mongoNamespace, insertCmd))
assert.NoErrorf(t, err, "cannot insert mongo records - %s", err)
time.Sleep(time.Second * 60)
assert.True(t, WaitForJobCount(t, kc, testNamespace, 0, 60, 1),
"job count should be 0 after 1 minute")
}

func testScaleUp(t *testing.T, kc *kubernetes.Clientset, mongoPod string) {
t.Log("--- testing scale up ---")

insertCmd := fmt.Sprintf(`db.%s.insert([
{"region":"eu-1","state":"running","plan":"planA","goods":"apple"},
{"region":"eu-1","state":"running","plan":"planA","goods":"orange"},
{"region":"eu-1","state":"running","plan":"planA","goods":"strawberry"},
{"region":"eu-1","state":"running","plan":"planA","goods":"cherry"},
{"region":"eu-1","state":"running","plan":"planA","goods":"pineapple"}
Expand Down

0 comments on commit 70c45a4

Please sign in to comment.