MCPcopy Create free account
hub / github.com/docker/cli / generateTasks

Function generateTasks

cli/command/service/list_test.go:217–271  ·  view source on GitHub ↗
(t *testing.T, services client.ServiceListResult, nodes client.NodeListResult, opts clusterOpts)

Source from the content-addressed store, hash-verified

215}
216
217func generateTasks(t *testing.T, services client.ServiceListResult, nodes client.NodeListResult, opts clusterOpts) client.TaskListResult {
218 t.Helper()
219 tasks := client.TaskListResult{}
220
221 for _, s := range services.Items {
222 if s.Spec.Mode.Replicated == nil && s.Spec.Mode.Global == nil {
223 continue
224 }
225 var runningTasks, failedTasks, desiredTasks uint64
226
227 // Set the number of desired tasks to generate, based on the service's mode
228 if s.Spec.Mode.Replicated != nil {
229 desiredTasks = *s.Spec.Mode.Replicated.Replicas
230 } else if s.Spec.Mode.Global != nil {
231 desiredTasks = opts.activeNodes
232 }
233
234 for _, n := range nodes.Items {
235 if runningTasks < opts.runningTasks && n.Status.State != swarm.NodeStateDown {
236 tasks.Items = append(tasks.Items, swarm.Task{
237 NodeID: n.ID,
238 ServiceID: s.ID,
239 Status: swarm.TaskStatus{State: swarm.TaskStateRunning},
240 DesiredState: swarm.TaskStateRunning,
241 })
242 runningTasks++
243 }
244
245 // If the number of "running" tasks is lower than the desired number
246 // of tasks of the service, fill in the remaining number of tasks
247 // with failed tasks. These tasks have a desired "running" state,
248 // and thus will be included when calculating the "desired" tasks
249 // for services.
250 if failedTasks < (desiredTasks - opts.runningTasks) {
251 tasks.Items = append(tasks.Items, swarm.Task{
252 NodeID: n.ID,
253 ServiceID: s.ID,
254 Status: swarm.TaskStatus{State: swarm.TaskStateFailed},
255 DesiredState: swarm.TaskStateRunning,
256 })
257 failedTasks++
258 }
259
260 // Also add tasks with DesiredState: Shutdown. These should not be
261 // counted as running or desired tasks.
262 tasks.Items = append(tasks.Items, swarm.Task{
263 NodeID: n.ID,
264 ServiceID: s.ID,
265 Status: swarm.TaskStatus{State: swarm.TaskStateShutdown},
266 DesiredState: swarm.TaskStateShutdown,
267 })
268 }
269 }
270 return tasks
271}
272
273// generateNodes generates a "nodes" endpoint API response with the requested
274// number of "ready" nodes. In addition, a "down" node is generated.

Callers 1

generateClusterFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…