Skip to content

Commit

Permalink
Add redis host and port parameter to the scaler with tests (#719)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Gualandri <ivan.gualandri@gmail.com>
  • Loading branch information
inuyasha82 authored Apr 2, 2020
1 parent bfb2ec9 commit fac8478
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/scalers/redis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
defaultRedisPassword = ""
defaultDbIdx = 0
defaultEnableTLS = false
defaultHost = ""
defaultPort = ""
)

type redisScaler struct {
Expand All @@ -33,6 +35,8 @@ type redisMetadata struct {
listName string
address string
password string
host string
port string
databaseIndex int
enableTLS bool
}
Expand Down Expand Up @@ -70,14 +74,38 @@ func parseRedisMetadata(metadata, resolvedEnv, authParams map[string]string) (*r
}

address := defaultRedisAddress
host := defaultHost
port := defaultPort
if val, ok := metadata["address"]; ok && val != "" {
address = val
} else {
if val, ok := metadata["host"]; ok && val != "" {
host = val
} else {
return nil, fmt.Errorf("no address or host given. address should be in the format of host:port or you should set the host/port values")
}
if val, ok := metadata["port"]; ok && val != "" {
port = val
} else {
return nil, fmt.Errorf("no address or port given. address should be in the format of host:port or you should set the host/port values")
}
}

if val, ok := resolvedEnv[address]; ok {
meta.address = val
} else {
return nil, fmt.Errorf("no address given. Address should be in the format of host:port")
if val, ok := resolvedEnv[host]; ok {
meta.host = val
} else {
return nil, fmt.Errorf("no address given or host given. Address should be in the format of host:port or you should provide both host and port")
}

if val, ok := resolvedEnv[port]; ok {
meta.port = val
} else {
return nil, fmt.Errorf("no address or port given. Address should be in the format of host:port or you should provide both host and port")
}
meta.address = fmt.Sprintf("%s:%s", meta.host, meta.port)
}

meta.password = defaultRedisPassword
Expand Down
7 changes: 7 additions & 0 deletions pkg/scalers/redis_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

var testRedisResolvedEnv = map[string]string{
"REDIS_HOST": "none",
"REDIS_PORT": "6379",
"REDIS_PASSWORD": "none",
}

Expand All @@ -20,6 +21,12 @@ var testRedisMetadata = []parseRedisMetadataTestData{
{map[string]string{}, true, map[string]string{}},
// properly formed listName
{map[string]string{"listName": "mylist", "listLength": "10", "address": "REDIS_HOST", "password": "REDIS_PASSWORD"}, false, map[string]string{}},
// properly formed hostPort
{map[string]string{"listName": "mylist", "listLength": "10", "host": "REDIS_HOST", "port": "REDIS_PORT", "password": "REDIS_PASSWORD"}, false, map[string]string{}},
// properly formed hostPort
{map[string]string{"listName": "mylist", "listLength": "10", "address": "REDIS_HOST", "host": "REDIS_HOST", "port": "REDIS_PORT", "password": "REDIS_PASSWORD"}, false, map[string]string{}},
// improperly formed hostPort
{map[string]string{"listName": "mylist", "listLength": "10", "host": "REDIS_HOST", "password": "REDIS_PASSWORD"}, true, map[string]string{}},
// properly formed listName, empty address
{map[string]string{"listName": "mylist", "listLength": "10", "address": "", "password": ""}, true, map[string]string{}},
// improperly formed listLength
Expand Down

0 comments on commit fac8478

Please sign in to comment.