Skip to content

Commit

Permalink
storage/redis: Add option to connect to database via tls
Browse files Browse the repository at this point in the history
Signed-off-by: Heathcliff <[email protected]>
  • Loading branch information
heathcliff26 committed May 2, 2024
1 parent 92fb33b commit b7adda5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions configs/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ storage:
password: ""
# (Optional) Database to use
db: 0
# (Optional) Use TLS when connecting to database
tls: false
# (Optional) Sentinel optiones
sentinel:
# Enable sentinel
Expand Down
25 changes: 18 additions & 7 deletions pkg/lock-manager/storage/redis/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redis

import (
"context"
"crypto/tls"
"fmt"
"time"

Expand All @@ -22,6 +23,7 @@ type RedisConfig struct {
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
DB int `yaml:"db,omitempty"`
TLS bool `yaml:"tls,omitempty"`
Sentinel RedisSentinelConfig `yaml:"sentinel,omitempty"`
}

Expand All @@ -36,6 +38,12 @@ type RedisSentinelConfig struct {
func NewRedisBackend(cfg *RedisConfig) (*RedisBackend, error) {
var client *redis.Client
var lb *loadbalancer
var tlsConfig *tls.Config

if cfg.TLS {
tlsConfig = &tls.Config{}
}

switch {
case cfg.Sentinel.Enabled:
client = redis.NewFailoverClient(&redis.FailoverOptions{
Expand All @@ -46,20 +54,23 @@ func NewRedisBackend(cfg *RedisConfig) (*RedisBackend, error) {
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
TLSConfig: tlsConfig,
})
case len(cfg.Addrs) > 0:
opt := redis.Options{
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
TLSConfig: tlsConfig,
}
client, lb = NewRedisClientWithLoadbalancer(cfg.Addrs, &opt)
default:
client = redis.NewClient(&redis.Options{
Addr: cfg.Addr,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
Addr: cfg.Addr,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
TLSConfig: tlsConfig,
})
}

Expand Down

0 comments on commit b7adda5

Please sign in to comment.