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

2.8 sentinel #4183

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- name: Build
working-directory: ./src/github.com/docker/distribution
run: |
go env
DCO_VERBOSITY=-q script/validate/dco
GO111MODULE=on script/setup/install-dev-tools
script/validate/vendor
Expand Down
3 changes: 3 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ type Configuration struct {
// Addr specifies the the redis instance available to the application.
Addr string `yaml:"addr,omitempty"`

// SentinelMasterSet specifies the redis sentinel master set name.
SentinelMasterSet string `yaml:"sentinelMasterSet,omitempty"`

// Password string to use when making a connection.
Password string `yaml:"password,omitempty"`

Expand Down
42 changes: 41 additions & 1 deletion registry/handlers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"context"
"crypto/rand"
"errors"
"expvar"
"fmt"
"math"
Expand All @@ -16,6 +17,7 @@ import (
"strings"
"time"

"github.com/FZambia/sentinel"
"github.com/distribution/reference"
"github.com/docker/distribution"
"github.com/docker/distribution/configuration"
Expand All @@ -37,7 +39,6 @@ import (
"github.com/docker/distribution/registry/storage/driver/factory"
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware"
"github.com/docker/distribution/version"
"github.com/docker/go-metrics"
"github.com/docker/libtrust"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -499,6 +500,45 @@ func (app *App) configureRedis(configuration *configuration.Configuration) {
return
}

var getRedisAddr func() (string, error)
var testOnBorrow func(c redis.Conn, t time.Time) error
if configuration.Redis.SentinelMasterSet != "" {
sntnl := &sentinel.Sentinel{
Addrs: strings.Split(configuration.Redis.Addr, ","),
MasterName: configuration.Redis.SentinelMasterSet,
Dial: func(addr string) (redis.Conn, error) {
c, err := redis.DialTimeout("tcp", addr,
configuration.Redis.DialTimeout,
configuration.Redis.ReadTimeout,
configuration.Redis.WriteTimeout)
if err != nil {
return nil, err
}
return c, nil
},
}
getRedisAddr = func() (string, error) {
return sntnl.MasterAddr()
}
testOnBorrow = func(c redis.Conn, t time.Time) error {
if !sentinel.TestRole(c, "master") {
return errors.New("role check failed")
}
return nil
}

} else {
getRedisAddr = func() (string, error) {
return configuration.Redis.Addr, nil
}
testOnBorrow = func(c redis.Conn, t time.Time) error {
// TODO(stevvooe): We can probably do something more interesting
// here with the health package.
_, err := c.Do("PING")
return err
}
}

pool := &redis.Pool{
Dial: func() (redis.Conn, error) {
// TODO(stevvooe): Yet another use case for contextual timing.
Expand Down
55 changes: 54 additions & 1 deletion registry/handlers/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,29 @@ func TestAppDispatcher(t *testing.T) {
// TestNewApp covers the creation of an application via NewApp with a
// configuration.
func TestNewApp(t *testing.T) {
ctx := context.Background()

config := configuration.Configuration{
Storage: configuration.Storage{
"testdriver": nil,
"maintenance": configuration.Parameters{"uploadpurging": map[interface{}]interface{}{
"enabled": false,
}},
},
Auth: configuration.Auth{
// For now, we simply test that new auth results in a viable
// application.
"silly": {
"realm": "realm-test",
"service": "service-test",
},
},
}
runAppWithConfig(t, config)
}

// TestNewApp covers the creation of an application via NewApp with a
// configuration(with redis).
func TestNewAppWithRedis(t *testing.T) {
config := configuration.Configuration{
Storage: configuration.Storage{
"testdriver": nil,
Expand All @@ -157,7 +179,38 @@ func TestNewApp(t *testing.T) {
},
},
}
config.Redis.Addr = "127.0.0.1:6379"
config.Redis.DB = 0
runAppWithConfig(t, config)
}

// TestNewApp covers the creation of an application via NewApp with a
// configuration(with redis sentinel cluster).
func TestNewAppWithRedisSentinelCluster(t *testing.T) {
config := configuration.Configuration{
Storage: configuration.Storage{
"testdriver": nil,
"maintenance": configuration.Parameters{"uploadpurging": map[interface{}]interface{}{
"enabled": false,
}},
},
Auth: configuration.Auth{
// For now, we simply test that new auth results in a viable
// application.
"silly": {
"realm": "realm-test",
"service": "service-test",
},
},
}
config.Redis.Addr = "192.168.0.11:26379,192.168.0.12:26379"
config.Redis.DB = 0
config.Redis.SentinelMasterSet = "mymaster"
runAppWithConfig(t, config)
}

func runAppWithConfig(t *testing.T, config configuration.Configuration) {
ctx := context.Background()
// Mostly, with this test, given a sane configuration, we are simply
// ensuring that NewApp doesn't panic. We might want to tweak this
// behavior.
Expand Down