github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/daemon/cluster/convert/task.go (about) 1 package convert 2 3 import ( 4 "strings" 5 6 types "github.com/docker/docker/api/types/swarm" 7 swarmapi "github.com/docker/swarmkit/api" 8 gogotypes "github.com/gogo/protobuf/types" 9 ) 10 11 // TaskFromGRPC converts a grpc Task to a Task. 12 func TaskFromGRPC(t swarmapi.Task) (types.Task, error) { 13 if t.Spec.GetAttachment() != nil { 14 return types.Task{}, nil 15 } 16 containerStatus := t.Status.GetContainer() 17 taskSpec, err := taskSpecFromGRPC(t.Spec) 18 if err != nil { 19 return types.Task{}, err 20 } 21 task := types.Task{ 22 ID: t.ID, 23 Annotations: annotationsFromGRPC(t.Annotations), 24 ServiceID: t.ServiceID, 25 Slot: int(t.Slot), 26 NodeID: t.NodeID, 27 Spec: taskSpec, 28 Status: types.TaskStatus{ 29 State: types.TaskState(strings.ToLower(t.Status.State.String())), 30 Message: t.Status.Message, 31 Err: t.Status.Err, 32 }, 33 DesiredState: types.TaskState(strings.ToLower(t.DesiredState.String())), 34 GenericResources: GenericResourcesFromGRPC(t.AssignedGenericResources), 35 } 36 37 // Meta 38 task.Version.Index = t.Meta.Version.Index 39 task.CreatedAt, _ = gogotypes.TimestampFromProto(t.Meta.CreatedAt) 40 task.UpdatedAt, _ = gogotypes.TimestampFromProto(t.Meta.UpdatedAt) 41 42 task.Status.Timestamp, _ = gogotypes.TimestampFromProto(t.Status.Timestamp) 43 44 if containerStatus != nil { 45 task.Status.ContainerStatus.ContainerID = containerStatus.ContainerID 46 task.Status.ContainerStatus.PID = int(containerStatus.PID) 47 task.Status.ContainerStatus.ExitCode = int(containerStatus.ExitCode) 48 } 49 50 // NetworksAttachments 51 for _, na := range t.Networks { 52 task.NetworksAttachments = append(task.NetworksAttachments, networkAttachmentFromGRPC(na)) 53 } 54 55 if t.Status.PortStatus == nil { 56 return task, nil 57 } 58 59 for _, p := range t.Status.PortStatus.Ports { 60 task.Status.PortStatus.Ports = append(task.Status.PortStatus.Ports, types.PortConfig{ 61 Name: p.Name, 62 Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(p.Protocol)])), 63 PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(p.PublishMode)])), 64 TargetPort: p.TargetPort, 65 PublishedPort: p.PublishedPort, 66 }) 67 } 68 69 return task, nil 70 }