task.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package convert // import "github.com/docker/docker/daemon/cluster/convert"
  2. import (
  3. "strings"
  4. types "github.com/docker/docker/api/types/swarm"
  5. gogotypes "github.com/gogo/protobuf/types"
  6. swarmapi "github.com/moby/swarmkit/v2/api"
  7. )
  8. // TaskFromGRPC converts a grpc Task to a Task.
  9. func TaskFromGRPC(t swarmapi.Task) (types.Task, error) {
  10. containerStatus := t.Status.GetContainer()
  11. taskSpec, err := taskSpecFromGRPC(t.Spec)
  12. if err != nil {
  13. return types.Task{}, err
  14. }
  15. task := types.Task{
  16. ID: t.ID,
  17. Annotations: annotationsFromGRPC(t.Annotations),
  18. ServiceID: t.ServiceID,
  19. Slot: int(t.Slot),
  20. NodeID: t.NodeID,
  21. Spec: taskSpec,
  22. Status: types.TaskStatus{
  23. State: types.TaskState(strings.ToLower(t.Status.State.String())),
  24. Message: t.Status.Message,
  25. Err: t.Status.Err,
  26. },
  27. DesiredState: types.TaskState(strings.ToLower(t.DesiredState.String())),
  28. GenericResources: GenericResourcesFromGRPC(t.AssignedGenericResources),
  29. }
  30. // Meta
  31. task.Version.Index = t.Meta.Version.Index
  32. task.CreatedAt, _ = gogotypes.TimestampFromProto(t.Meta.CreatedAt)
  33. task.UpdatedAt, _ = gogotypes.TimestampFromProto(t.Meta.UpdatedAt)
  34. task.Status.Timestamp, _ = gogotypes.TimestampFromProto(t.Status.Timestamp)
  35. if containerStatus != nil {
  36. task.Status.ContainerStatus = &types.ContainerStatus{
  37. ContainerID: containerStatus.ContainerID,
  38. PID: int(containerStatus.PID),
  39. ExitCode: int(containerStatus.ExitCode),
  40. }
  41. }
  42. // NetworksAttachments
  43. for _, na := range t.Networks {
  44. task.NetworksAttachments = append(task.NetworksAttachments, networkAttachmentFromGRPC(na))
  45. }
  46. if t.JobIteration != nil {
  47. task.JobIteration = &types.Version{
  48. Index: t.JobIteration.Index,
  49. }
  50. }
  51. // appending to a nil slice is valid. if there are no items in t.Volumes,
  52. // then the task.Volumes will remain nil; otherwise, it will contain
  53. // converted entries.
  54. for _, v := range t.Volumes {
  55. task.Volumes = append(task.Volumes, types.VolumeAttachment{
  56. ID: v.ID,
  57. Source: v.Source,
  58. Target: v.Target,
  59. })
  60. }
  61. if t.Status.PortStatus == nil {
  62. return task, nil
  63. }
  64. for _, p := range t.Status.PortStatus.Ports {
  65. task.Status.PortStatus.Ports = append(task.Status.PortStatus.Ports, types.PortConfig{
  66. Name: p.Name,
  67. Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(p.Protocol)])),
  68. PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(p.PublishMode)])),
  69. TargetPort: p.TargetPort,
  70. PublishedPort: p.PublishedPort,
  71. })
  72. }
  73. return task, nil
  74. }