Skip to content

Commit

Permalink
Merge pull request #484 from Icinga/split-redis-address
Browse files Browse the repository at this point in the history
Split Redis address into host and port
  • Loading branch information
julianbrost authored May 16, 2022
2 parents 6dc135d + 66e1954 commit 88736f7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
11 changes: 10 additions & 1 deletion config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ database:
user: icingadb
password: icingadb

# Connection configuration for the Redis where Icinga writes its configuration, state and history.
# This is the same connection as configured in the 'icingadb' feature of the corresponding Icinga node.
# High availability setups require a dedicated Redis server per Icinga node and
# therefore a dedicated Icinga DB instance that connects to it.
redis:
address: localhost:6380
# Redis host or absolute Unix socket path.
host: localhost

# Redis port. The Redis server provided by the icingadb-redis package listens on port '6380'.
# If not specified, the standard Redis port '6379' is used instead.
port: 6380

logging:
# Default logging level. Can be set to 'fatal', 'error', 'warn', 'info' or 'debug'.
Expand Down
3 changes: 2 additions & 1 deletion doc/03-Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Configuration of the Redis that Icinga writes to.

Option | Description
-------------------------|-----------------------------------------------
address | **Required.** Redis host:port address or absolute Unix socket path.
host | **Required.** Redis host or absolute Unix socket path.
port | **Optional.** Redis port. Defaults to `6379`. Specify `6380` if using the `icingadb-redis` package.
password | **Optional.** The password to use.
tls | **Optional.** Whether to use TLS.
cert | **Optional.** Path to TLS client certificate.
Expand Down
11 changes: 3 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"io/ioutil"
"net"
"os"
)

Expand Down Expand Up @@ -94,8 +93,8 @@ type TLS struct {
Insecure bool `yaml:"insecure"`
}

// MakeConfig assembles a tls.Config from t and address.
func (t *TLS) MakeConfig(address string) (*tls.Config, error) {
// MakeConfig assembles a tls.Config from t and serverName.
func (t *TLS) MakeConfig(serverName string) (*tls.Config, error) {
if !t.Enable {
return nil, nil
}
Expand Down Expand Up @@ -130,11 +129,7 @@ func (t *TLS) MakeConfig(address string) (*tls.Config, error) {
}
}

if host, _, err := net.SplitHostPort(address); err == nil {
tlsConfig.ServerName = host
} else {
tlsConfig.ServerName = address
}
tlsConfig.ServerName = serverName

return tlsConfig, nil
}
2 changes: 1 addition & 1 deletion pkg/config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (d *Database) Open(logger *logging.Logger) (*icingadb.DB, error) {
config.Timeout = time.Minute
config.Params = map[string]string{"sql_mode": "ANSI_QUOTES"}

tlsConfig, err := d.TlsOptions.MakeConfig(config.Addr)
tlsConfig, err := d.TlsOptions.MakeConfig(d.Host)
if err != nil {
return nil, err
}
Expand Down
26 changes: 19 additions & 7 deletions pkg/config/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"context"
"crypto/tls"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/icinga/icingadb/pkg/backoff"
"github.com/icinga/icingadb/pkg/icingaredis"
Expand All @@ -13,13 +14,15 @@ import (
"go.uber.org/zap"
"net"
"os"
"strings"
"syscall"
"time"
)

// Redis defines Redis client configuration.
type Redis struct {
Address string `yaml:"address"`
Host string `yaml:"host"`
Port int `yaml:"port" default:"6379"`
Password string `yaml:"password"`
TlsOptions TLS `yaml:",inline"`
Options icingaredis.Options `yaml:"options"`
Expand All @@ -30,7 +33,7 @@ type ctxDialerFunc = func(ctx context.Context, network, addr string) (net.Conn,
// NewClient prepares Redis client configuration,
// calls redis.NewClient, but returns *icingaredis.Client.
func (r *Redis) NewClient(logger *logging.Logger) (*icingaredis.Client, error) {
tlsConfig, err := r.TlsOptions.MakeConfig(r.Address)
tlsConfig, err := r.TlsOptions.MakeConfig(r.Host)
if err != nil {
return nil, err
}
Expand All @@ -44,14 +47,23 @@ func (r *Redis) NewClient(logger *logging.Logger) (*icingaredis.Client, error) {
dialer = (&tls.Dialer{NetDialer: dl, Config: tlsConfig}).DialContext
}

c := redis.NewClient(&redis.Options{
Addr: r.Address,
options := &redis.Options{
Dialer: dialWithLogging(dialer, logger),
Password: r.Password,
DB: 0, // Use default DB,
ReadTimeout: r.Options.Timeout,
TLSConfig: tlsConfig,
})
}

if strings.HasPrefix(r.Host, "/") {
options.Network = "unix"
options.Addr = r.Host
} else {
options.Network = "tcp"
options.Addr = net.JoinHostPort(r.Host, fmt.Sprint(r.Port))
}

c := redis.NewClient(options)

opts := c.Options()
opts.MaxRetries = opts.PoolSize + 1 // https://github.com/go-redis/redis/issues/1737
Expand Down Expand Up @@ -103,8 +115,8 @@ func dialWithLogging(dialer ctxDialerFunc, logger *logging.Logger) ctxDialerFunc

// Validate checks constraints in the supplied Redis configuration and returns an error if they are violated.
func (r *Redis) Validate() error {
if r.Address == "" {
return errors.New("Redis address missing")
if r.Host == "" {
return errors.New("Redis host missing")
}

return r.Options.Validate()
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-yaml v1.9.5
github.com/google/uuid v1.3.0
github.com/icinga/icinga-testing v0.0.0-20220513095329-9c98d3145b01
github.com/icinga/icinga-testing v0.0.0-20220516144008-9600081b7a69
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.10.5
github.com/stretchr/testify v1.7.0
Expand Down
6 changes: 4 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,10 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/icinga/icinga-testing v0.0.0-20220513095329-9c98d3145b01 h1:0dwlZFGWPnmmhvHr2P7chxMwzbW7+R3iX6SyeFBd+WM=
github.com/icinga/icinga-testing v0.0.0-20220513095329-9c98d3145b01/go.mod h1:ZP0pyqhmrRwwQ6FpAfz7UZMgmH7i3vOjEOm9JcFwOw0=
github.com/icinga/icinga-testing v0.0.0-20220513144038-4aef51dce82a h1:d0pnssSfJwMsEDB1CIQSkQ3XACVhVIzkVgcirMfLfkM=
github.com/icinga/icinga-testing v0.0.0-20220513144038-4aef51dce82a/go.mod h1:ZP0pyqhmrRwwQ6FpAfz7UZMgmH7i3vOjEOm9JcFwOw0=
github.com/icinga/icinga-testing v0.0.0-20220516144008-9600081b7a69 h1:M5KN3s3TuHpGPnP78h5cFogtQrywapFIaYfvohQHc7I=
github.com/icinga/icinga-testing v0.0.0-20220516144008-9600081b7a69/go.mod h1:ZP0pyqhmrRwwQ6FpAfz7UZMgmH7i3vOjEOm9JcFwOw0=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
Expand Down

0 comments on commit 88736f7

Please sign in to comment.