github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/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  	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 = &types.ContainerStatus{
    46  			ContainerID: containerStatus.ContainerID,
    47  			PID:         int(containerStatus.PID),
    48  			ExitCode:    int(containerStatus.ExitCode),
    49  		}
    50  	}
    51  
    52  	// NetworksAttachments
    53  	for _, na := range t.Networks {
    54  		task.NetworksAttachments = append(task.NetworksAttachments, networkAttachmentFromGRPC(na))
    55  	}
    56  
    57  	if t.Status.PortStatus == nil {
    58  		return task, nil
    59  	}
    60  
    61  	for _, p := range t.Status.PortStatus.Ports {
    62  		task.Status.PortStatus.Ports = append(task.Status.PortStatus.Ports, types.PortConfig{
    63  			Name:          p.Name,
    64  			Protocol:      types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(p.Protocol)])),
    65  			PublishMode:   types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(p.PublishMode)])),
    66  			TargetPort:    p.TargetPort,
    67  			PublishedPort: p.PublishedPort,
    68  		})
    69  	}
    70  
    71  	return task, nil
    72  }