Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Redis DB option #2527

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const (
StatsNamespace = "stats-namespace"
AllowDraftPRs = "allow-draft-prs"
PortFlag = "port"
RedisDB = "redis-db"
RedisHost = "redis-host"
RedisPassword = "redis-password"
RedisPort = "redis-port"
Expand Down Expand Up @@ -134,6 +135,7 @@ const (
DefaultParallelPoolSize = 15
DefaultStatsNamespace = "atlantis"
DefaultPort = 4141
DefaultRedisDB = 0
DefaultRedisPort = 6379
DefaultRedisTLSEnabled = false
DefaultRedisInsecureSkipVerify = false
Expand Down Expand Up @@ -478,6 +480,10 @@ var intFlags = map[string]intFlag{
description: "Port to bind to.",
defaultValue: DefaultPort,
},
RedisDB: {
description: "The Redis Database to use when using a Locking DB type of 'redis'.",
defaultValue: DefaultRedisDB,
},
RedisPort: {
description: "The Redis Port for when using a Locking DB type of 'redis'.",
defaultValue: DefaultRedisPort,
Expand Down Expand Up @@ -719,6 +725,9 @@ func (s *ServerCmd) setDefaults(c *server.UserConfig) {
if c.Port == 0 {
c.Port = DefaultPort
}
if c.RedisDB == 0 {
c.RedisDB = DefaultRedisDB
}
if c.RedisPort == 0 {
c.RedisPort = DefaultRedisPort
}
Expand Down
6 changes: 6 additions & 0 deletions runatlantis.io/docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ Values are chosen in this order:
```
The Redis Port for when using a Locking DB type of `redis`. Defaults to `6379`.

### `--redis-db`
```bash
atlantis server --redis-db=0
```
The Redis Database to use when using a Locking DB type of `redis`. Defaults to `0`.

### `--redis-tls-enabled`
```bash
atlantis server --redis-tls-enabled=false
Expand Down
4 changes: 2 additions & 2 deletions server/core/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
pullKeySeparator = "::"
)

func New(hostname string, port int, password string, tlsEnabled bool, insecureSkipVerify bool) (*RedisDB, error) {
func New(hostname string, port int, password string, tlsEnabled bool, insecureSkipVerify bool, db int) (*RedisDB, error) {
var rdb *redis.Client

var tlsConfig *tls.Config
Expand All @@ -40,7 +40,7 @@ func New(hostname string, port int, password string, tlsEnabled bool, insecureSk
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", hostname, port),
Password: password,
DB: 0, // use default DB
DB: db,
TLSConfig: tlsConfig,
})

Expand Down
4 changes: 2 additions & 2 deletions server/core/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,15 +803,15 @@ func TestPullStatus_UpdateMerge(t *testing.T) {
}

func newTestRedis(mr *miniredis.Miniredis) *redis.RedisDB {
r, err := redis.New(mr.Host(), mr.Server().Addr().Port, "", false, false)
r, err := redis.New(mr.Host(), mr.Server().Addr().Port, "", false, false, 0)
if err != nil {
panic(errors.Wrap(err, "failed to create test redis client"))
}
return r
}

func newTestRedisTLS(mr *miniredis.Miniredis) *redis.RedisDB {
r, err := redis.New(mr.Host(), mr.Server().Addr().Port, "", true, true)
r, err := redis.New(mr.Host(), mr.Server().Addr().Port, "", true, true, 0)
if err != nil {
panic(errors.Wrap(err, "failed to create test redis client"))
}
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
switch dbtype := userConfig.LockingDBType; dbtype {
case "redis":
logger.Info("Utilizing Redis DB")
backend, err = redis.New(userConfig.RedisHost, userConfig.RedisPort, userConfig.RedisPassword, userConfig.RedisTLSEnabled, userConfig.RedisInsecureSkipVerify)
backend, err = redis.New(userConfig.RedisHost, userConfig.RedisPort, userConfig.RedisPassword, userConfig.RedisTLSEnabled, userConfig.RedisInsecureSkipVerify, userConfig.RedisDB)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions server/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type UserConfig struct {
StatsNamespace string `mapstructure:"stats-namespace"`
PlanDrafts bool `mapstructure:"allow-draft-prs"`
Port int `mapstructure:"port"`
RedisDB int `mapstructure:"redis-db"`
RedisHost string `mapstructure:"redis-host"`
RedisPassword string `mapstructure:"redis-password"`
RedisPort int `mapstructure:"redis-port"`
Expand Down