2016-06-13 22:52:49 -04:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:52:49 -04:00
|
|
|
swarmapi "github.com/docker/swarmkit/api"
|
2017-01-23 18:50:10 -05:00
|
|
|
gogotypes "github.com/gogo/protobuf/types"
|
2016-06-13 22:52:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TaskFromGRPC converts a grpc Task to a Task.
|
|
|
|
func TaskFromGRPC(t swarmapi.Task) types.Task {
|
2016-09-13 20:44:06 -04:00
|
|
|
if t.Spec.GetAttachment() != nil {
|
|
|
|
return types.Task{}
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
containerStatus := t.Status.GetContainer()
|
2016-08-23 19:50:15 -04:00
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
task := types.Task{
|
2017-02-13 03:07:03 -05:00
|
|
|
ID: t.ID,
|
|
|
|
Annotations: annotationsFromGRPC(t.Annotations),
|
|
|
|
ServiceID: t.ServiceID,
|
|
|
|
Slot: int(t.Slot),
|
|
|
|
NodeID: t.NodeID,
|
2017-02-24 18:20:18 -05:00
|
|
|
Spec: taskSpecFromGRPC(t.Spec),
|
2016-06-13 22:52:49 -04:00
|
|
|
Status: types.TaskStatus{
|
|
|
|
State: types.TaskState(strings.ToLower(t.Status.State.String())),
|
|
|
|
Message: t.Status.Message,
|
|
|
|
Err: t.Status.Err,
|
|
|
|
},
|
|
|
|
DesiredState: types.TaskState(strings.ToLower(t.DesiredState.String())),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meta
|
|
|
|
task.Version.Index = t.Meta.Version.Index
|
2017-01-23 18:50:10 -05:00
|
|
|
task.CreatedAt, _ = gogotypes.TimestampFromProto(t.Meta.CreatedAt)
|
|
|
|
task.UpdatedAt, _ = gogotypes.TimestampFromProto(t.Meta.UpdatedAt)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2017-01-23 18:50:10 -05:00
|
|
|
task.Status.Timestamp, _ = gogotypes.TimestampFromProto(t.Status.Timestamp)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
if containerStatus != nil {
|
|
|
|
task.Status.ContainerStatus.ContainerID = containerStatus.ContainerID
|
|
|
|
task.Status.ContainerStatus.PID = int(containerStatus.PID)
|
|
|
|
task.Status.ContainerStatus.ExitCode = int(containerStatus.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworksAttachments
|
|
|
|
for _, na := range t.Networks {
|
2017-02-16 07:08:57 -05:00
|
|
|
task.NetworksAttachments = append(task.NetworksAttachments, networkAttachmentFromGRPC(na))
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2016-11-10 15:13:26 -05:00
|
|
|
if t.Status.PortStatus == nil {
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range t.Status.PortStatus.Ports {
|
|
|
|
task.Status.PortStatus.Ports = append(task.Status.PortStatus.Ports, types.PortConfig{
|
|
|
|
Name: p.Name,
|
|
|
|
Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(p.Protocol)])),
|
|
|
|
PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(p.PublishMode)])),
|
|
|
|
TargetPort: p.TargetPort,
|
|
|
|
PublishedPort: p.PublishedPort,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
return task
|
|
|
|
}
|