Use Docker API version auto negotiation (#486)
This commit is contained in:
@@ -37,6 +37,7 @@ type Config struct {
|
||||
DockerSocket string `default:"unix:///var/run/docker.sock" usage:"Path to Docker socket to use"`
|
||||
DockerTimeout int `default:"0" usage:"Timeout configuration in seconds for the Docker integrations"`
|
||||
DockerRefreshInterval int `default:"15" usage:"Refresh interval in seconds for the Docker integrations"`
|
||||
DockerApiVersion string `usage:"Instead of auto-negotiating, use specific Docker API version"`
|
||||
MetricsBackend string `default:"discard" usage:"Backend to use for metrics exposure/publishing: discard,expvar,influxdb,prometheus"`
|
||||
MetricsBackendConfig MetricsBackendConfig
|
||||
UseProxyProtocol bool `default:"false" usage:"Send PROXY protocol to backend servers"`
|
||||
|
||||
+41
-16
@@ -15,28 +15,56 @@ import (
|
||||
)
|
||||
|
||||
type IDockerWatcher interface {
|
||||
Start(ctx context.Context, socket string, timeoutSeconds int, refreshIntervalSeconds int, autoScaleUp bool, autoScaleDown bool) error
|
||||
Start(ctx context.Context) error
|
||||
}
|
||||
|
||||
const (
|
||||
DockerAPIVersion = "1.24"
|
||||
DockerRouterLabelHost = "mc-router.host"
|
||||
DockerRouterLabelPort = "mc-router.port"
|
||||
DockerRouterLabelDefault = "mc-router.default"
|
||||
DockerRouterLabelNetwork = "mc-router.network"
|
||||
)
|
||||
|
||||
var DockerWatcher IDockerWatcher = &dockerWatcherImpl{}
|
||||
type dockerWatcherConfig struct {
|
||||
autoScaleUp bool
|
||||
autoScaleDown bool
|
||||
socket string
|
||||
timeoutSeconds int
|
||||
refreshIntervalSeconds int
|
||||
apiVersion string
|
||||
}
|
||||
|
||||
func (c *dockerWatcherConfig) apiVersionOpt() client.Opt {
|
||||
if c.apiVersion != "" {
|
||||
logrus.WithField("apiVersion", c.apiVersion).Debug("Using specific Docker API version")
|
||||
return client.WithVersion(c.apiVersion)
|
||||
} else {
|
||||
logrus.Debug("Using Docker API version negotiation")
|
||||
return client.WithAPIVersionNegotiation()
|
||||
}
|
||||
}
|
||||
|
||||
func NewDockerWatcher(socket string, timeoutSeconds int, refreshIntervalSeconds int, autoScaleUp bool, autoScaleDown bool, dockerApiVersion string) IDockerWatcher {
|
||||
return &dockerWatcherImpl{
|
||||
config: dockerWatcherConfig{
|
||||
socket: socket,
|
||||
timeoutSeconds: timeoutSeconds,
|
||||
refreshIntervalSeconds: refreshIntervalSeconds,
|
||||
autoScaleUp: autoScaleUp,
|
||||
autoScaleDown: autoScaleDown,
|
||||
apiVersion: dockerApiVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type dockerWatcherImpl struct {
|
||||
sync.RWMutex
|
||||
autoScaleUp bool
|
||||
autoScaleDown bool
|
||||
client *client.Client
|
||||
config dockerWatcherConfig
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
func (w *dockerWatcherImpl) makeWakerFunc(_ *routableContainer) ScalerFunc {
|
||||
if !w.autoScaleUp {
|
||||
if !w.config.autoScaleUp {
|
||||
return nil
|
||||
}
|
||||
return func(ctx context.Context) error {
|
||||
@@ -46,7 +74,7 @@ func (w *dockerWatcherImpl) makeWakerFunc(_ *routableContainer) ScalerFunc {
|
||||
}
|
||||
|
||||
func (w *dockerWatcherImpl) makeSleeperFunc(_ *routableContainer) ScalerFunc {
|
||||
if !w.autoScaleDown {
|
||||
if !w.config.autoScaleDown {
|
||||
return nil
|
||||
}
|
||||
return func(ctx context.Context) error {
|
||||
@@ -55,22 +83,19 @@ func (w *dockerWatcherImpl) makeSleeperFunc(_ *routableContainer) ScalerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (w *dockerWatcherImpl) Start(ctx context.Context, socket string, timeoutSeconds int, refreshIntervalSeconds int, autoScaleUp bool, autoScaleDown bool) error {
|
||||
func (w *dockerWatcherImpl) Start(ctx context.Context) error {
|
||||
var err error
|
||||
|
||||
w.autoScaleUp = autoScaleUp
|
||||
w.autoScaleDown = autoScaleDown
|
||||
|
||||
timeout := time.Duration(timeoutSeconds) * time.Second
|
||||
refreshInterval := time.Duration(refreshIntervalSeconds) * time.Second
|
||||
timeout := time.Duration(w.config.timeoutSeconds) * time.Second
|
||||
refreshInterval := time.Duration(w.config.refreshIntervalSeconds) * time.Second
|
||||
|
||||
opts := []client.Opt{
|
||||
client.WithHost(socket),
|
||||
client.WithHost(w.config.socket),
|
||||
client.WithTimeout(timeout),
|
||||
client.WithHTTPHeaders(map[string]string{
|
||||
"User-Agent": "mc-router ",
|
||||
}),
|
||||
client.WithVersion(DockerAPIVersion),
|
||||
w.config.apiVersionOpt(),
|
||||
}
|
||||
|
||||
w.client, err = client.NewClientWithOpts(opts...)
|
||||
|
||||
+21
-14
@@ -19,17 +19,27 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var DockerSwarmWatcher IDockerWatcher = &dockerSwarmWatcherImpl{}
|
||||
func NewDockerSwarmWatcher(socket string, timeoutSeconds int, refreshIntervalSeconds int, autoScaleUp bool, autoScaleDown bool, dockerApiVersion string) IDockerWatcher {
|
||||
return &dockerSwarmWatcherImpl{
|
||||
config: dockerWatcherConfig{
|
||||
socket: socket,
|
||||
timeoutSeconds: timeoutSeconds,
|
||||
refreshIntervalSeconds: refreshIntervalSeconds,
|
||||
autoScaleUp: autoScaleUp,
|
||||
autoScaleDown: autoScaleDown,
|
||||
apiVersion: dockerApiVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type dockerSwarmWatcherImpl struct {
|
||||
sync.RWMutex
|
||||
autoScaleUp bool
|
||||
autoScaleDown bool
|
||||
client *client.Client
|
||||
config dockerWatcherConfig
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
func (w *dockerSwarmWatcherImpl) makeWakerFunc(_ *routableService) ScalerFunc {
|
||||
if !w.autoScaleUp {
|
||||
if !w.config.autoScaleUp {
|
||||
return nil
|
||||
}
|
||||
return func(ctx context.Context) error {
|
||||
@@ -39,7 +49,7 @@ func (w *dockerSwarmWatcherImpl) makeWakerFunc(_ *routableService) ScalerFunc {
|
||||
}
|
||||
|
||||
func (w *dockerSwarmWatcherImpl) makeSleeperFunc(_ *routableService) ScalerFunc {
|
||||
if !w.autoScaleDown {
|
||||
if !w.config.autoScaleDown {
|
||||
return nil
|
||||
}
|
||||
return func(ctx context.Context) error {
|
||||
@@ -48,22 +58,19 @@ func (w *dockerSwarmWatcherImpl) makeSleeperFunc(_ *routableService) ScalerFunc
|
||||
}
|
||||
}
|
||||
|
||||
func (w *dockerSwarmWatcherImpl) Start(ctx context.Context, socket string, timeoutSeconds int, refreshIntervalSeconds int, autoScaleUp bool, autoScaleDown bool) error {
|
||||
func (w *dockerSwarmWatcherImpl) Start(ctx context.Context) error {
|
||||
var err error
|
||||
|
||||
w.autoScaleUp = autoScaleUp
|
||||
w.autoScaleDown = autoScaleDown
|
||||
|
||||
timeout := time.Duration(timeoutSeconds) * time.Second
|
||||
refreshInterval := time.Duration(refreshIntervalSeconds) * time.Second
|
||||
timeout := time.Duration(w.config.timeoutSeconds) * time.Second
|
||||
refreshInterval := time.Duration(w.config.refreshIntervalSeconds) * time.Second
|
||||
|
||||
opts := []client.Opt{
|
||||
client.WithHost(socket),
|
||||
client.WithHost(w.config.socket),
|
||||
client.WithTimeout(timeout),
|
||||
client.WithHTTPHeaders(map[string]string{
|
||||
"User-Agent": "mc-router ",
|
||||
}),
|
||||
client.WithVersion(DockerAPIVersion),
|
||||
client.WithAPIVersionNegotiation(),
|
||||
}
|
||||
|
||||
w.client, err = client.NewClientWithOpts(opts...)
|
||||
|
||||
+4
-2
@@ -143,7 +143,8 @@ func NewServer(ctx context.Context, config *Config) (*Server, error) {
|
||||
|
||||
// TODO convert to RouteFinder
|
||||
if config.InDocker {
|
||||
err = DockerWatcher.Start(ctx, config.DockerSocket, config.DockerTimeout, config.DockerRefreshInterval, config.AutoScale.Up, config.AutoScale.Down)
|
||||
watcher := NewDockerWatcher(config.DockerSocket, config.DockerTimeout, config.DockerRefreshInterval, config.AutoScale.Up, config.AutoScale.Down, config.DockerApiVersion)
|
||||
err = watcher.Start(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not start docker integration: %w", err)
|
||||
}
|
||||
@@ -151,7 +152,8 @@ func NewServer(ctx context.Context, config *Config) (*Server, error) {
|
||||
|
||||
// TODO convert to RouteFinder
|
||||
if config.InDockerSwarm {
|
||||
err = DockerSwarmWatcher.Start(ctx, config.DockerSocket, config.DockerTimeout, config.DockerRefreshInterval, config.AutoScale.Up, config.AutoScale.Down)
|
||||
watcher := NewDockerSwarmWatcher(config.DockerSocket, config.DockerTimeout, config.DockerRefreshInterval, config.AutoScale.Up, config.AutoScale.Down, config.DockerApiVersion)
|
||||
err = watcher.Start(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not start docker swarm integration: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user