2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-11-14 21:08:24 -05:00
|
|
|
"encoding/json"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
)
|
|
|
|
|
2018-10-22 22:11:21 -04:00
|
|
|
// ServiceUpdate updates a Service. The version number is required to avoid conflicting writes.
|
|
|
|
// It should be the value as set *before* the update. You can find this value in the Meta field
|
|
|
|
// of swarm.Service, which can be found using ServiceInspectWithRaw.
|
2016-11-14 21:08:24 -05:00
|
|
|
func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
var (
|
2020-04-16 07:03:39 -04:00
|
|
|
query = url.Values{}
|
|
|
|
response = types.ServiceUpdateResponse{}
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
2017-05-12 16:51:52 -04:00
|
|
|
headers := map[string][]string{
|
|
|
|
"version": {cli.version},
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
if options.EncodedRegistryAuth != "" {
|
2017-05-12 16:51:52 -04:00
|
|
|
headers["X-Registry-Auth"] = []string{options.EncodedRegistryAuth}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 19:32:44 -04:00
|
|
|
if options.RegistryAuthFrom != "" {
|
|
|
|
query.Set("registryAuthFrom", options.RegistryAuthFrom)
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:27:01 -05:00
|
|
|
if options.Rollback != "" {
|
|
|
|
query.Set("rollback", options.Rollback)
|
|
|
|
}
|
|
|
|
|
2022-05-10 16:35:09 -04:00
|
|
|
query.Set("version", version.String())
|
2016-09-06 14:46:37 -04:00
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
if err := validateServiceSpec(service); err != nil {
|
2020-04-16 07:03:39 -04:00
|
|
|
return response, err
|
2017-05-18 18:00:25 -04:00
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
// ensure that the image is tagged
|
2020-04-16 07:03:39 -04:00
|
|
|
var resolveWarning string
|
|
|
|
switch {
|
|
|
|
case service.TaskTemplate.ContainerSpec != nil:
|
2017-06-07 13:07:01 -04:00
|
|
|
if taggedImg := imageWithTagString(service.TaskTemplate.ContainerSpec.Image); taggedImg != "" {
|
|
|
|
service.TaskTemplate.ContainerSpec.Image = taggedImg
|
|
|
|
}
|
|
|
|
if options.QueryRegistry {
|
2020-04-16 07:42:47 -04:00
|
|
|
resolveWarning = resolveContainerSpecImage(ctx, cli, &service.TaskTemplate, options.EncodedRegistryAuth)
|
2017-04-05 18:43:17 -04:00
|
|
|
}
|
2020-04-16 07:03:39 -04:00
|
|
|
case service.TaskTemplate.PluginSpec != nil:
|
2017-06-07 13:07:01 -04:00
|
|
|
if taggedImg := imageWithTagString(service.TaskTemplate.PluginSpec.Remote); taggedImg != "" {
|
|
|
|
service.TaskTemplate.PluginSpec.Remote = taggedImg
|
|
|
|
}
|
|
|
|
if options.QueryRegistry {
|
2020-04-16 07:42:47 -04:00
|
|
|
resolveWarning = resolvePluginSpecRemote(ctx, cli, &service.TaskTemplate, options.EncodedRegistryAuth)
|
2017-06-07 13:07:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
resp, err := cli.post(ctx, "/services/"+serviceID+"/update", query, service, headers)
|
2019-02-11 07:26:12 -05:00
|
|
|
defer ensureReaderClosed(resp)
|
2016-11-14 21:08:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.body).Decode(&response)
|
2020-04-16 07:03:39 -04:00
|
|
|
if resolveWarning != "" {
|
|
|
|
response.Warnings = append(response.Warnings, resolveWarning)
|
2017-04-05 18:43:17 -04:00
|
|
|
}
|
|
|
|
|
2016-11-14 21:08:24 -05:00
|
|
|
return response, err
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|