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