(ctx context.Context, dockerCLI command.Cli, services map[string]swarm.ServiceSpec, namespace convert.Namespace, sendAuth bool, resolveImage string)
| 200 | } |
| 201 | |
| 202 | func deployServices(ctx context.Context, dockerCLI command.Cli, services map[string]swarm.ServiceSpec, namespace convert.Namespace, sendAuth bool, resolveImage string) ([]string, error) { |
| 203 | apiClient := dockerCLI.Client() |
| 204 | out := dockerCLI.Out() |
| 205 | |
| 206 | existingServices, err := getStackServices(ctx, apiClient, namespace.Name()) |
| 207 | if err != nil { |
| 208 | return nil, err |
| 209 | } |
| 210 | |
| 211 | existingServiceMap := make(map[string]swarm.Service) |
| 212 | for _, svc := range existingServices.Items { |
| 213 | existingServiceMap[svc.Spec.Name] = svc |
| 214 | } |
| 215 | |
| 216 | var serviceIDs []string |
| 217 | |
| 218 | for internalName, serviceSpec := range services { |
| 219 | var ( |
| 220 | name = namespace.Scope(internalName) |
| 221 | image = serviceSpec.TaskTemplate.ContainerSpec.Image |
| 222 | encodedAuth string |
| 223 | ) |
| 224 | |
| 225 | if sendAuth { |
| 226 | // Retrieve encoded auth token from the image reference |
| 227 | encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), image) |
| 228 | if err != nil { |
| 229 | return nil, err |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if svc, exists := existingServiceMap[name]; exists { |
| 234 | _, _ = fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, svc.ID) |
| 235 | |
| 236 | updateOpts := client.ServiceUpdateOptions{ |
| 237 | Version: svc.Version, |
| 238 | EncodedRegistryAuth: encodedAuth, |
| 239 | } |
| 240 | |
| 241 | switch resolveImage { |
| 242 | case resolveImageAlways: |
| 243 | // image should be updated by the server using QueryRegistry |
| 244 | updateOpts.QueryRegistry = true |
| 245 | case resolveImageChanged: |
| 246 | if image != svc.Spec.Labels[convert.LabelImage] { |
| 247 | // Query the registry to resolve digest for the updated image |
| 248 | updateOpts.QueryRegistry = true |
| 249 | } else { |
| 250 | // image has not changed; update the serviceSpec with the |
| 251 | // existing information that was set by QueryRegistry on the |
| 252 | // previous deploy. Otherwise this will trigger an incorrect |
| 253 | // service update. |
| 254 | serviceSpec.TaskTemplate.ContainerSpec.Image = svc.Spec.TaskTemplate.ContainerSpec.Image |
| 255 | } |
| 256 | default: |
| 257 | if image == svc.Spec.Labels[convert.LabelImage] { |
| 258 | // image has not changed; update the serviceSpec with the |
| 259 | // existing information that was set by QueryRegistry on the |
searching dependent graphs…