This commit is contained in:
Alexis Couvreur
2020-10-21 22:31:29 +02:00
parent eb0ca0b44f
commit 3dc0a19bd2
3 changed files with 27 additions and 22 deletions

View File

@@ -11,15 +11,15 @@ It will scale back to 1 when there is a user requesting the service.
## Configuration
- `serviceUrl` the traefik-ondemand-service url
- `dockerServiceName` the service to sclae on demand name (docker service ls)
- `timeoutSeconds` timeout in seconds for the service to be scaled down to zero after the last request
- `serviceUrl` the traefik-ondemand-service url (e.g. http://ondemand:1000)
- `name` the service to sclae on demand name (docker service ls)
- *`timeout` (default: 60)* timeout in seconds for the service to be scaled down to zero after the last request
See `config.yml` and docker-compose.yml for full configuration.
See `config.yml` and `docker-compose.yml` for full configuration.
## Demo
The service whoami is scaled to 0. We configured a timeout of 10 seconds.
The service **whoami** is scaled to 0. We configured a **timeout of 10** seconds.
![Demo](./img/demo.gif)
@@ -39,7 +39,7 @@ Otherwise when scaling to 0 the specification would not be found because there i
### The need of "traefik-ondemand-service"
This is a small project developped to interact freely with the docker deamon and manage an independant lifecycle.
We are running "traefik-ondemand-service" to interact freely with the docker deamon and manage an independant lifecycle from traefik.
*We may try to update this plugin to embed the scaling behavior in a future.*

View File

@@ -1,10 +1,11 @@
http:
middlewares:
ondemand:
ondemand-60:
plugin:
dev:
serviceUrl: http://ondemand:10000
dockerServiceName: TRAEFIK_HACKATHON_whoami
name: TRAEFIK_HACKATHON_whoami
timeout: 60
services:
whoami:
@@ -18,5 +19,5 @@ http:
entryPoints:
- "http"
middlewares:
- ondemand
- ondemand-60
service: "whoami"

View File

@@ -7,45 +7,49 @@ import (
"net/http"
)
const defaultTimeoutSeconds = 10
const defaultTimeoutSeconds = 60
// Config the plugin configuration
type Config struct {
DockerServiceName string
ServiceUrl string
TimeoutSeconds uint64
Name string
ServiceUrl string
Timeout uint64
}
func CreateConfig() *Config {
return &Config{
TimeoutSeconds: defaultTimeoutSeconds,
Timeout: defaultTimeoutSeconds,
}
}
type Ondemand struct {
next http.Handler
name string
ServiceUrl string
TimeoutSeconds uint64
DockerServiceName string
serviceUrl string
timeoutSeconds uint64
dockerServiceName string
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.ServiceUrl) == 0 {
return nil, fmt.Errorf("ServiceUrl cannot be null")
return nil, fmt.Errorf("serviceUrl cannot be null")
}
if len(config.Name) == 0 {
return nil, fmt.Errorf("name cannot be null")
}
return &Ondemand{
next: next,
name: name,
ServiceUrl: config.ServiceUrl,
DockerServiceName: config.DockerServiceName,
TimeoutSeconds: config.TimeoutSeconds,
serviceUrl: config.ServiceUrl,
dockerServiceName: config.Name,
timeoutSeconds: config.Timeout,
}, nil
}
func (e *Ondemand) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
url := fmt.Sprintf("%s/?name=%s&timeout=%d", e.ServiceUrl, e.DockerServiceName, e.TimeoutSeconds)
url := fmt.Sprintf("%s/?name=%s&timeout=%d", e.serviceUrl, e.dockerServiceName, e.timeoutSeconds)
resp, err := http.Get(url)
if err != nil {
println("Could not contact", url)