MCPcopy
hub / github.com/moby/moby / ServiceUpdate

Method ServiceUpdate

client/service_update.go:56–114  ·  view source on GitHub ↗

ServiceUpdate updates a Service. The version number is required to avoid conflicting writes. It must be the value as set *before* the update. You can find this value in the [swarm.Service.Meta] field, which can be found using [Client.ServiceInspectWithRaw].

(ctx context.Context, serviceID string, options ServiceUpdateOptions)

Source from the content-addressed store, hash-verified

54// You can find this value in the [swarm.Service.Meta] field, which can
55// be found using [Client.ServiceInspectWithRaw].
56func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, options ServiceUpdateOptions) (ServiceUpdateResult, error) {
57 serviceID, err := trimID("service", serviceID)
58 if err != nil {
59 return ServiceUpdateResult{}, err
60 }
61
62 if err := validateServiceSpec(options.Spec); err != nil {
63 return ServiceUpdateResult{}, err
64 }
65
66 query := url.Values{}
67 if options.RegistryAuthFrom != "" {
68 query.Set("registryAuthFrom", string(options.RegistryAuthFrom))
69 }
70
71 if options.Rollback != "" {
72 query.Set("rollback", options.Rollback)
73 }
74
75 query.Set("version", options.Version.String())
76
77 // ensure that the image is tagged
78 var warnings []string
79 switch {
80 case options.Spec.TaskTemplate.ContainerSpec != nil:
81 if taggedImg := imageWithTagString(options.Spec.TaskTemplate.ContainerSpec.Image); taggedImg != "" {
82 options.Spec.TaskTemplate.ContainerSpec.Image = taggedImg
83 }
84 if options.QueryRegistry {
85 if warning := resolveContainerSpecImage(ctx, cli, &options.Spec.TaskTemplate, options.EncodedRegistryAuth); warning != "" {
86 warnings = append(warnings, warning)
87 }
88 }
89 case options.Spec.TaskTemplate.PluginSpec != nil:
90 if taggedImg := imageWithTagString(options.Spec.TaskTemplate.PluginSpec.Remote); taggedImg != "" {
91 options.Spec.TaskTemplate.PluginSpec.Remote = taggedImg
92 }
93 if options.QueryRegistry {
94 if warning := resolvePluginSpecRemote(ctx, cli, &options.Spec.TaskTemplate, options.EncodedRegistryAuth); warning != "" {
95 warnings = append(warnings, warning)
96 }
97 }
98 }
99
100 headers := http.Header{}
101 if options.EncodedRegistryAuth != "" {
102 headers.Set(registry.AuthHeader, options.EncodedRegistryAuth)
103 }
104 resp, err := cli.post(ctx, "/services/"+serviceID+"/update", query, options.Spec, headers)
105 defer ensureReaderClosed(resp)
106 if err != nil {
107 return ServiceUpdateResult{}, err
108 }
109
110 var response swarm.ServiceUpdateResponse
111 err = json.NewDecoder(resp.Body).Decode(&response)
112 warnings = append(warnings, response.Warnings...)
113 return ServiceUpdateResult{Warnings: warnings}, err

Callers

nothing calls this directly

Calls 10

postMethod · 0.95
trimIDFunction · 0.85
validateServiceSpecFunction · 0.85
imageWithTagStringFunction · 0.85
resolvePluginSpecRemoteFunction · 0.85
ensureReaderClosedFunction · 0.85
SetMethod · 0.65
StringMethod · 0.65
DecodeMethod · 0.65

Tested by

no test coverage detected