Implement Swarm update handler using PUT

This commit implements an update handler for Docker Swarm, it queries the
current spec, updates values in-situ before calling ServiceUpdate.

The UpdateConfig FailureAction is set to rollback, so in the event of
supplying values to the update that would result in the service failing
then the update will be rolled back.

The UpdateConfig Parallelism param is set to an explicit value of 1 which
will result in functions being updated 1 by 1 rather than all at once.

It also moves the restartDelay declaration out of the create and update
handlers and into the main server function alongside maxRestarts.

And finally this commit uses the PUT HTTP verb for updates rather than
the non-HTTP UPDATE verb which was being used initially (also adding it
to the Swagger definition).

Signed-off-by: John McCabe <john@johnmccabe.net>
This commit is contained in:
John McCabe
2017-09-23 23:23:55 +01:00
committed by Alex Ellis
parent 9052a7d750
commit 40e1fac1c2
4 changed files with 145 additions and 18 deletions

View File

@ -21,8 +21,10 @@ import (
"github.com/docker/docker/registry"
)
var linuxOnlyConstraints = []string{"node.platform.os == linux"}
// MakeNewFunctionHandler creates a new function (service) inside the swarm network.
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client, maxRestarts uint64) http.HandlerFunc {
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client, maxRestarts uint64, restartDelay time.Duration) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
@ -51,7 +53,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
}
options.EncodedRegistryAuth = auth
}
spec := makeSpec(&request, maxRestarts)
spec := makeSpec(&request, maxRestarts, restartDelay)
response, err := c.ServiceCreate(context.Background(), spec, options)
if err != nil {
@ -64,8 +66,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
}
}
func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm.ServiceSpec {
linuxOnlyConstraints := []string{"node.platform.os == linux"}
func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64, restartDelay time.Duration) swarm.ServiceSpec {
constraints := []string{}
if request.Constraints != nil && len(request.Constraints) > 0 {
constraints = request.Constraints
@ -76,7 +77,6 @@ func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm
nets := []swarm.NetworkAttachmentConfig{
{Target: request.Network},
}
restartDelay := time.Second * 5
spec := swarm.ServiceSpec{
TaskTemplate: swarm.TaskSpec{
@ -100,13 +100,7 @@ func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm
}
// TODO: request.EnvProcess should only be set if it's not nil, otherwise we override anything in the Docker image already
var env []string
if len(request.EnvProcess) > 0 {
env = append(env, fmt.Sprintf("fprocess=%s", request.EnvProcess))
}
for k, v := range request.EnvVars {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
env := buildEnv(request.EnvProcess, request.EnvVars)
if len(env) > 0 {
spec.TaskTemplate.ContainerSpec.Env = env
@ -115,6 +109,17 @@ func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm
return spec
}
func buildEnv(envProcess string, envVars map[string]string) []string {
var env []string
if len(envProcess) > 0 {
env = append(env, fmt.Sprintf("fprocess=%s", envProcess))
}
for k, v := range envVars {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
return env
}
// BuildEncodedAuthConfig for private registry
func BuildEncodedAuthConfig(basicAuthB64 string, dockerImage string) (string, error) {
// extract registry server address