2018-02-05 16:05:59 -05:00
|
|
|
package convert // import "github.com/docker/docker/daemon/cluster/convert"
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
2017-01-23 18:50:10 -05:00
|
|
|
gogotypes "github.com/gogo/protobuf/types"
|
2022-04-21 17:33:07 -04:00
|
|
|
swarmapi "github.com/moby/swarmkit/v2/api"
|
2016-06-13 22:52:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TaskFromGRPC converts a grpc Task to a Task.
|
2017-06-07 13:07:01 -04:00
|
|
|
func TaskFromGRPC(t swarmapi.Task) (types.Task, error) {
|
2016-06-13 22:52:49 -04:00
|
|
|
containerStatus := t.Status.GetContainer()
|
2017-06-07 13:07:01 -04:00
|
|
|
taskSpec, err := taskSpecFromGRPC(t.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return types.Task{}, err
|
|
|
|
}
|
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-06-07 13:07:01 -04:00
|
|
|
Spec: taskSpec,
|
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,
|
|
|
|
},
|
2017-05-30 20:02:11 -04:00
|
|
|
DesiredState: types.TaskState(strings.ToLower(t.DesiredState.String())),
|
|
|
|
GenericResources: GenericResourcesFromGRPC(t.AssignedGenericResources),
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-01-30 12:26:56 -05:00
|
|
|
task.Status.ContainerStatus = &types.ContainerStatus{
|
|
|
|
ContainerID: containerStatus.ContainerID,
|
|
|
|
PID: int(containerStatus.PID),
|
|
|
|
ExitCode: int(containerStatus.ExitCode),
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-12-02 13:27:23 -05:00
|
|
|
if t.JobIteration != nil {
|
|
|
|
task.JobIteration = &types.Version{
|
|
|
|
Index: t.JobIteration.Index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 15:13:26 -05:00
|
|
|
if t.Status.PortStatus == nil {
|
2017-06-07 13:07:01 -04:00
|
|
|
return task, nil
|
2016-11-10 15:13:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
return task, nil
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|