Use duration type for options for Docker auto-scaling delays (#524)
This commit is contained in:
+8
-8
@@ -8,11 +8,11 @@ type WebhookConfig struct {
|
||||
}
|
||||
|
||||
type AutoScale struct {
|
||||
Up bool `usage:"Scale from zero on access. For Kubernetes, increases StatefulSet replicas from 0 to 1. For Docker, starts or unpauses the container when accessed"`
|
||||
Down bool `default:"false" usage:"Scale to zero after idle. For Kubernetes, decreases StatefulSet replicas from 1 to 0. For Docker, gracefully stops the container when there are no connections"`
|
||||
DownAfter string `default:"10m" usage:"Server scale down delay after there are no connections"`
|
||||
AllowDeny string `usage:"Path to config for server allowlists and denylists. If a global/server entry is specified, only players allowed to connect to the server will be able to trigger a scale up when -auto-scale-up is enabled or cancel active down scalers when -auto-scale-down is enabled"`
|
||||
AsleepMOTD string `usage:"MOTD to display when auto-scaled down servers are accessed; if empty, no status will be served"`
|
||||
Up bool `usage:"Scale from zero on access. For Kubernetes, increases StatefulSet replicas from 0 to 1. For Docker, starts or unpauses the container when accessed"`
|
||||
Down bool `default:"false" usage:"Scale to zero after idle. For Kubernetes, decreases StatefulSet replicas from 1 to 0. For Docker, gracefully stops the container when there are no connections"`
|
||||
DownAfter time.Duration `default:"10m" usage:"Server scale down delay after there are no connections"`
|
||||
AllowDeny string `usage:"Path to config for server allowlists and denylists. If a global/server entry is specified, only players allowed to connect to the server will be able to trigger a scale up when -auto-scale-up is enabled or cancel active down scalers when -auto-scale-down is enabled"`
|
||||
AsleepMOTD string `usage:"MOTD to display when auto-scaled down servers are accessed; if empty, no status will be served"`
|
||||
}
|
||||
|
||||
type RoutesConfig struct {
|
||||
@@ -37,9 +37,9 @@ type Config struct {
|
||||
KubeNamespace string `usage:"The namespace to watch or blank for all, which is the default"`
|
||||
InDocker bool `usage:"Use Docker service discovery"`
|
||||
InDockerSwarm bool `usage:"Use Docker Swarm service discovery"`
|
||||
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"`
|
||||
DockerSocket string `usage:"Path to Docker socket to use"`
|
||||
DockerTimeout time.Duration `usage:"Timeout (as duration) for the Docker integrations"`
|
||||
DockerRefreshInterval time.Duration `default:"15s" usage:"Refresh interval (as duration) 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
|
||||
|
||||
+20
-19
@@ -29,12 +29,12 @@ const (
|
||||
)
|
||||
|
||||
type dockerWatcherConfig struct {
|
||||
autoScaleUp bool
|
||||
autoScaleDown bool
|
||||
socket string
|
||||
timeoutSeconds int
|
||||
refreshIntervalSeconds int
|
||||
apiVersion string
|
||||
autoScaleUp bool
|
||||
autoScaleDown bool
|
||||
socket string
|
||||
timeout time.Duration
|
||||
refreshInterval time.Duration
|
||||
apiVersion string
|
||||
}
|
||||
|
||||
func (c *dockerWatcherConfig) apiVersionOpt() client.Opt {
|
||||
@@ -47,15 +47,15 @@ func (c *dockerWatcherConfig) apiVersionOpt() client.Opt {
|
||||
}
|
||||
}
|
||||
|
||||
func NewDockerWatcher(socket string, timeoutSeconds int, refreshIntervalSeconds int, autoScaleUp bool, autoScaleDown bool, dockerApiVersion string) IDockerWatcher {
|
||||
func NewDockerWatcher(socket string, timeout time.Duration, refreshInterval time.Duration, autoScaleUp bool, autoScaleDown bool, dockerApiVersion string) IDockerWatcher {
|
||||
return &dockerWatcherImpl{
|
||||
config: dockerWatcherConfig{
|
||||
socket: socket,
|
||||
timeoutSeconds: timeoutSeconds,
|
||||
refreshIntervalSeconds: refreshIntervalSeconds,
|
||||
autoScaleUp: autoScaleUp,
|
||||
autoScaleDown: autoScaleDown,
|
||||
apiVersion: dockerApiVersion,
|
||||
socket: socket,
|
||||
timeout: timeout,
|
||||
refreshInterval: refreshInterval,
|
||||
autoScaleUp: autoScaleUp,
|
||||
autoScaleDown: autoScaleDown,
|
||||
apiVersion: dockerApiVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -225,24 +225,25 @@ func (w *dockerWatcherImpl) monitorContainers(ctx context.Context) error {
|
||||
func (w *dockerWatcherImpl) Start(ctx context.Context) error {
|
||||
var err error
|
||||
|
||||
timeout := time.Duration(w.config.timeoutSeconds) * time.Second
|
||||
refreshInterval := time.Duration(w.config.refreshIntervalSeconds) * time.Second
|
||||
|
||||
opts := []client.Opt{
|
||||
client.WithHost(w.config.socket),
|
||||
client.WithTimeout(timeout),
|
||||
client.FromEnv,
|
||||
client.WithTimeout(w.config.timeout),
|
||||
client.WithHTTPHeaders(map[string]string{
|
||||
"User-Agent": "mc-router ",
|
||||
}),
|
||||
w.config.apiVersionOpt(),
|
||||
}
|
||||
if w.config.socket != "" {
|
||||
opts = append(opts, client.WithHost(w.config.socket))
|
||||
}
|
||||
|
||||
w.client, err = client.NewClientWithOpts(opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(refreshInterval)
|
||||
// TODO: replace all this with events listening
|
||||
ticker := time.NewTicker(w.config.refreshInterval)
|
||||
|
||||
logrus.Trace("Performing initial listing of Docker containers")
|
||||
initialContainers, err := w.listContainers(ctx)
|
||||
|
||||
+9
-12
@@ -19,15 +19,15 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func NewDockerSwarmWatcher(socket string, timeoutSeconds int, refreshIntervalSeconds int, autoScaleUp bool, autoScaleDown bool, dockerApiVersion string) IDockerWatcher {
|
||||
func NewDockerSwarmWatcher(socket string, timeout time.Duration, refreshInterval time.Duration, autoScaleUp bool, autoScaleDown bool, dockerApiVersion string) IDockerWatcher {
|
||||
return &dockerSwarmWatcherImpl{
|
||||
config: dockerWatcherConfig{
|
||||
socket: socket,
|
||||
timeoutSeconds: timeoutSeconds,
|
||||
refreshIntervalSeconds: refreshIntervalSeconds,
|
||||
autoScaleUp: autoScaleUp,
|
||||
autoScaleDown: autoScaleDown,
|
||||
apiVersion: dockerApiVersion,
|
||||
socket: socket,
|
||||
timeout: timeout,
|
||||
refreshInterval: refreshInterval,
|
||||
autoScaleUp: autoScaleUp,
|
||||
autoScaleDown: autoScaleDown,
|
||||
apiVersion: dockerApiVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -61,12 +61,9 @@ func (w *dockerSwarmWatcherImpl) makeSleeperFunc(_ *routableService) SleeperFunc
|
||||
func (w *dockerSwarmWatcherImpl) Start(ctx context.Context) error {
|
||||
var err error
|
||||
|
||||
timeout := time.Duration(w.config.timeoutSeconds) * time.Second
|
||||
refreshInterval := time.Duration(w.config.refreshIntervalSeconds) * time.Second
|
||||
|
||||
opts := []client.Opt{
|
||||
client.WithHost(w.config.socket),
|
||||
client.WithTimeout(timeout),
|
||||
client.WithTimeout(w.config.timeout),
|
||||
client.WithHTTPHeaders(map[string]string{
|
||||
"User-Agent": "mc-router ",
|
||||
}),
|
||||
@@ -78,7 +75,7 @@ func (w *dockerSwarmWatcherImpl) Start(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(refreshInterval)
|
||||
ticker := time.NewTicker(w.config.refreshInterval)
|
||||
serviceMap := map[string]*routableService{}
|
||||
|
||||
logrus.Trace("Performing initial listing of Docker containers")
|
||||
|
||||
@@ -56,7 +56,6 @@ func (ds *downScalerImpl) Begin(backendEndpoint string) {
|
||||
scaleDownCancel()
|
||||
}
|
||||
|
||||
logrus.WithField("backendEndpoint", backendEndpoint).Debug("Beginning scale down")
|
||||
scaleDownContext, scaleDownContextCancellation := context.WithCancel(ds.parentContext)
|
||||
ds.contextCancellations[backendEndpoint] = scaleDownContextCancellation
|
||||
go ds.scaleDown(scaleDownContext, backendEndpoint)
|
||||
@@ -78,12 +77,18 @@ func (ds *downScalerImpl) Cancel(backendEndpoint string) {
|
||||
}
|
||||
|
||||
func (ds *downScalerImpl) scaleDown(ctx context.Context, backendEndpoint string) {
|
||||
logrus.WithField("backendEndpoint", backendEndpoint).
|
||||
WithField("delay", ds.delay).
|
||||
Debug("Starting scale-down timer")
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(ds.delay):
|
||||
sleepers := Routes.GetSleepers(backendEndpoint)
|
||||
logrus.WithField("backendEndpoint", backendEndpoint).
|
||||
WithField("sleepers", len(sleepers)).
|
||||
Debug("Found sleepers to use")
|
||||
if len(sleepers) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
+1
-5
@@ -7,7 +7,6 @@ import (
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -50,10 +49,7 @@ func NewServer(ctx context.Context, config *Config) (*Server, error) {
|
||||
metricsBuilder := NewMetricsBuilder(config.MetricsBackend, &config.MetricsBackendConfig)
|
||||
|
||||
downScalerEnabled := config.AutoScale.Down && (config.InKubeCluster || config.KubeConfig != "" || config.InDocker)
|
||||
downScalerDelay, err := time.ParseDuration(config.AutoScale.DownAfter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not parse auto-scale-down-after duration: %w", err)
|
||||
}
|
||||
downScalerDelay := config.AutoScale.DownAfter
|
||||
// Only one instance should be created
|
||||
DownScaler = NewDownScaler(ctx, downScalerEnabled, downScalerDelay)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user