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"
|
|
|
|
"github.com/docker/swarmkit/protobuf/ptypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
containerConfig := t.Spec.Runtime.(*swarmapi.TaskSpec_Container).Container
|
|
|
|
containerStatus := t.Status.GetContainer()
|
2016-08-23 19:50:15 -04:00
|
|
|
networks := make([]types.NetworkAttachmentConfig, 0, len(t.Spec.Networks))
|
|
|
|
for _, n := range t.Spec.Networks {
|
|
|
|
networks = append(networks, types.NetworkAttachmentConfig{Target: n.Target, Aliases: n.Aliases})
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
task := types.Task{
|
2016-07-23 12:58:58 -04:00
|
|
|
ID: t.ID,
|
|
|
|
Annotations: types.Annotations{
|
|
|
|
Name: t.Annotations.Name,
|
|
|
|
Labels: t.Annotations.Labels,
|
|
|
|
},
|
2016-06-13 22:52:49 -04:00
|
|
|
ServiceID: t.ServiceID,
|
|
|
|
Slot: int(t.Slot),
|
|
|
|
NodeID: t.NodeID,
|
|
|
|
Spec: types.TaskSpec{
|
|
|
|
ContainerSpec: containerSpecFromGRPC(containerConfig),
|
|
|
|
Resources: resourcesFromGRPC(t.Spec.Resources),
|
|
|
|
RestartPolicy: restartPolicyFromGRPC(t.Spec.Restart),
|
|
|
|
Placement: placementFromGRPC(t.Spec.Placement),
|
2016-07-08 21:44:18 -04:00
|
|
|
LogDriver: driverFromGRPC(t.Spec.LogDriver),
|
2016-08-23 19:50:15 -04:00
|
|
|
Networks: networks,
|
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
|
|
|
|
task.CreatedAt, _ = ptypes.Timestamp(t.Meta.CreatedAt)
|
|
|
|
task.UpdatedAt, _ = ptypes.Timestamp(t.Meta.UpdatedAt)
|
|
|
|
|
|
|
|
task.Status.Timestamp, _ = ptypes.Timestamp(t.Status.Timestamp)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
task.NetworksAttachments = append(task.NetworksAttachments, networkAttachementFromGRPC(na))
|
|
|
|
}
|
|
|
|
|
|
|
|
return task
|
|
|
|
}
|