(ctx context.Context, apiClient client.NetworkAPIClient, flags *pflag.FlagSet, spec *swarm.ServiceSpec)
| 1307 | } |
| 1308 | |
| 1309 | func updateNetworks(ctx context.Context, apiClient client.NetworkAPIClient, flags *pflag.FlagSet, spec *swarm.ServiceSpec) error { |
| 1310 | // spec.TaskTemplate.Networks takes precedence over the deprecated |
| 1311 | // spec.Networks field. If spec.Network is in use, we'll migrate those |
| 1312 | // values to spec.TaskTemplate.Networks. |
| 1313 | specNetworks := spec.TaskTemplate.Networks |
| 1314 | |
| 1315 | toRemove := buildToRemoveSet(flags, flagNetworkRemove) |
| 1316 | idsToRemove := make(map[string]struct{}) |
| 1317 | for networkIDOrName := range toRemove { |
| 1318 | nw, err := apiClient.NetworkInspect(ctx, networkIDOrName, client.NetworkInspectOptions{Scope: "swarm"}) |
| 1319 | if err != nil { |
| 1320 | return err |
| 1321 | } |
| 1322 | idsToRemove[nw.Network.ID] = struct{}{} |
| 1323 | } |
| 1324 | |
| 1325 | existingNetworks := make(map[string]struct{}) |
| 1326 | var newNetworks []swarm.NetworkAttachmentConfig |
| 1327 | for _, nw := range specNetworks { |
| 1328 | if _, exists := idsToRemove[nw.Target]; exists { |
| 1329 | continue |
| 1330 | } |
| 1331 | |
| 1332 | newNetworks = append(newNetworks, nw) |
| 1333 | existingNetworks[nw.Target] = struct{}{} |
| 1334 | } |
| 1335 | |
| 1336 | if flags.Changed(flagNetworkAdd) { |
| 1337 | values := flags.Lookup(flagNetworkAdd).Value.(*opts.NetworkOpt) |
| 1338 | networks := convertNetworks(*values) |
| 1339 | for _, nw := range networks { |
| 1340 | nwID, err := resolveNetworkID(ctx, apiClient, nw.Target) |
| 1341 | if err != nil { |
| 1342 | return err |
| 1343 | } |
| 1344 | if _, exists := existingNetworks[nwID]; exists { |
| 1345 | return fmt.Errorf("service is already attached to network %s", nw.Target) |
| 1346 | } |
| 1347 | nw.Target = nwID |
| 1348 | newNetworks = append(newNetworks, nw) |
| 1349 | existingNetworks[nw.Target] = struct{}{} |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | sort.Slice(newNetworks, func(i, j int) bool { |
| 1354 | return newNetworks[i].Target < newNetworks[j].Target |
| 1355 | }) |
| 1356 | |
| 1357 | spec.TaskTemplate.Networks = newNetworks |
| 1358 | return nil |
| 1359 | } |
| 1360 | |
| 1361 | // updateCredSpecConfig updates the value of the credential spec Config field |
| 1362 | // to the config ID if the credential spec has changed. it mutates the passed |
no test coverage detected
searching dependent graphs…