github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/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  	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  	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.Status.PortStatus == nil {
    55  		return task, nil
    56  	}
    57  
    58  	for _, p := range t.Status.PortStatus.Ports {
    59  		task.Status.PortStatus.Ports = append(task.Status.PortStatus.Ports, types.PortConfig{
    60  			Name:          p.Name,
    61  			Protocol:      types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(p.Protocol)])),
    62  			PublishMode:   types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(p.PublishMode)])),
    63  			TargetPort:    p.TargetPort,
    64  			PublishedPort: p.PublishedPort,
    65  		})
    66  	}
    67  
    68  	if t.JobIteration != nil {
    69  		task.JobIteration = &types.Version{
    70  			Index: t.JobIteration.Index,
    71  		}
    72  	}
    73  
    74  	return task, nil
    75  }