From aa6386184af26531cdb0ca042704395392857942 Mon Sep 17 00:00:00 2001 From: Ning Wu Date: Mon, 7 Feb 2022 00:20:10 -0800 Subject: [PATCH 1/3] Use net.JoinHostPort() to support connection to redis pod in IPv6 k8s cluster --- service/redis/client.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/service/redis/client.go b/service/redis/client.go index 1b416e8a7..13aab39b9 100644 --- a/service/redis/client.go +++ b/service/redis/client.go @@ -6,6 +6,7 @@ import ( "regexp" "strconv" "strings" + "net" rediscli "github.com/go-redis/redis" ) @@ -60,7 +61,7 @@ var ( // GetNumberSentinelsInMemory return the number of sentinels that the requested sentinel has func (c *client) GetNumberSentinelsInMemory(ip string) (int32, error) { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, sentinelPort), + Addr: net.JoinHostPort(ip, sentinelPort), Password: "", DB: 0, } @@ -87,7 +88,7 @@ func (c *client) GetNumberSentinelsInMemory(ip string) (int32, error) { // GetNumberSentinelsInMemory return the number of sentinels that the requested sentinel has func (c *client) GetNumberSentinelSlavesInMemory(ip string) (int32, error) { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, sentinelPort), + Addr: net.JoinHostPort(ip, sentinelPort), Password: "", DB: 0, } @@ -122,7 +123,7 @@ func isSentinelReady(info string) error { // ResetSentinel sends a sentinel reset * for the given sentinel func (c *client) ResetSentinel(ip string) error { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, sentinelPort), + Addr: net.JoinHostPort(ip, sentinelPort), Password: "", DB: 0, } @@ -143,7 +144,7 @@ func (c *client) ResetSentinel(ip string) error { // GetSlaveOf returns the master of the given redis, or nil if it's master func (c *client) GetSlaveOf(ip, password string) (string, error) { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, redisPort), + Addr: net.JoinHostPort(ip, redisPort), Password: password, DB: 0, } @@ -162,7 +163,7 @@ func (c *client) GetSlaveOf(ip, password string) (string, error) { func (c *client) IsMaster(ip, password string) (bool, error) { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, redisPort), + Addr: net.JoinHostPort(ip, redisPort), Password: password, DB: 0, } @@ -181,7 +182,7 @@ func (c *client) MonitorRedis(ip, monitor, quorum, password string) error { func (c *client) MonitorRedisWithPort(ip, monitor, port, quorum, password string) error { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, sentinelPort), + Addr: net.JoinHostPort(ip, sentinelPort), Password: "", DB: 0, } @@ -216,7 +217,7 @@ func (c *client) MonitorRedisWithPort(ip, monitor, port, quorum, password string func (c *client) MakeMaster(ip string, password string) error { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, redisPort), + Addr: net.JoinHostPort(ip, redisPort), Password: password, DB: 0, } @@ -234,7 +235,7 @@ func (c *client) MakeSlaveOf(ip, masterIP, password string) error { func (c *client) MakeSlaveOfWithPort(ip, masterIP, masterPort, password string) error { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, redisPort), // this is IP and Port for the RedisFailover redis + Addr: net.JoinHostPort(ip, redisPort), // this is IP and Port for the RedisFailover redis Password: password, DB: 0, } @@ -248,7 +249,7 @@ func (c *client) MakeSlaveOfWithPort(ip, masterIP, masterPort, password string) func (c *client) GetSentinelMonitor(ip string) (string, string, error) { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, sentinelPort), + Addr: net.JoinHostPort(ip, sentinelPort), Password: "", DB: 0, } @@ -270,7 +271,7 @@ func (c *client) GetSentinelMonitor(ip string) (string, string, error) { func (c *client) SetCustomSentinelConfig(ip string, configs []string) error { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, sentinelPort), + Addr: net.JoinHostPort(ip, sentinelPort), Password: "", DB: 0, } @@ -291,7 +292,7 @@ func (c *client) SetCustomSentinelConfig(ip string, configs []string) error { func (c *client) SetCustomRedisConfig(ip string, configs []string, password string) error { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, redisPort), + Addr: net.JoinHostPort(ip, redisPort), Password: password, DB: 0, } @@ -334,7 +335,7 @@ func (c *client) getConfigParameters(config string) (parameter string, value str func (c *client) SlaveIsReady(ip, password string) (bool, error) { options := &rediscli.Options{ - Addr: fmt.Sprintf("%s:%s", ip, redisPort), + Addr: net.JoinHostPort(ip, redisPort), Password: password, DB: 0, } From e8b7b8a9e3bae8d7cb27b8646e38889a815df70d Mon Sep 17 00:00:00 2001 From: Ning Wu Date: Mon, 7 Feb 2022 00:59:07 -0800 Subject: [PATCH 2/3] Fix golang check --- service/redis/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/redis/client.go b/service/redis/client.go index 13aab39b9..db5f3166b 100644 --- a/service/redis/client.go +++ b/service/redis/client.go @@ -3,10 +3,10 @@ package redis import ( "errors" "fmt" + "net" "regexp" "strconv" "strings" - "net" rediscli "github.com/go-redis/redis" ) From 52358c00ca4de52d1919f1d7a65ff1d4082e7e02 Mon Sep 17 00:00:00 2001 From: Ning Wu Date: Mon, 7 Feb 2022 05:27:39 -0800 Subject: [PATCH 3/3] Fixed formatting issue with gofmt -w service/redis/client.go --- service/redis/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/redis/client.go b/service/redis/client.go index db5f3166b..036bfebe3 100644 --- a/service/redis/client.go +++ b/service/redis/client.go @@ -3,7 +3,7 @@ package redis import ( "errors" "fmt" - "net" + "net" "regexp" "strconv" "strings"