diff --git a/pkg/scalers/redis_scaler.go b/pkg/scalers/redis_scaler.go index 784dfa40e72..5324bd7b345 100644 --- a/pkg/scalers/redis_scaler.go +++ b/pkg/scalers/redis_scaler.go @@ -22,6 +22,8 @@ const ( defaultRedisPassword = "" defaultDbIdx = 0 defaultEnableTLS = false + defaultHost = "" + defaultPort = "" ) type redisScaler struct { @@ -33,6 +35,8 @@ type redisMetadata struct { listName string address string password string + host string + port string databaseIndex int enableTLS bool } @@ -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 diff --git a/pkg/scalers/redis_scaler_test.go b/pkg/scalers/redis_scaler_test.go index ec00f814d29..a19e9686091 100644 --- a/pkg/scalers/redis_scaler_test.go +++ b/pkg/scalers/redis_scaler_test.go @@ -6,6 +6,7 @@ import ( var testRedisResolvedEnv = map[string]string{ "REDIS_HOST": "none", + "REDIS_PORT": "6379", "REDIS_PASSWORD": "none", } @@ -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