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 (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
2017-06-07 13:07:01 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm/runtime"
|
2016-06-13 22:52:49 -04:00
|
|
|
"github.com/docker/docker/pkg/namesgenerator"
|
|
|
|
swarmapi "github.com/docker/swarmkit/api"
|
2017-05-30 20:02:11 -04:00
|
|
|
"github.com/docker/swarmkit/api/genericresource"
|
2017-06-07 13:07:01 -04:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2017-01-23 18:50:10 -05:00
|
|
|
gogotypes "github.com/gogo/protobuf/types"
|
2017-06-07 13:07:01 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-06-13 22:52:49 -04:00
|
|
|
)
|
|
|
|
|
2017-03-27 22:28:35 -04:00
|
|
|
var (
|
|
|
|
// ErrUnsupportedRuntime returns an error if the runtime is not supported by the daemon
|
|
|
|
ErrUnsupportedRuntime = errors.New("unsupported runtime")
|
2017-10-18 17:41:59 -04:00
|
|
|
// ErrMismatchedRuntime returns an error if the runtime does not match the provided spec
|
|
|
|
ErrMismatchedRuntime = errors.New("mismatched Runtime and *Spec fields")
|
2017-03-27 22:28:35 -04:00
|
|
|
)
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// ServiceFromGRPC converts a grpc Service to a Service.
|
2017-03-01 15:52:55 -05:00
|
|
|
func ServiceFromGRPC(s swarmapi.Service) (types.Service, error) {
|
|
|
|
curSpec, err := serviceSpecFromGRPC(&s.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return types.Service{}, err
|
|
|
|
}
|
|
|
|
prevSpec, err := serviceSpecFromGRPC(s.PreviousSpec)
|
|
|
|
if err != nil {
|
|
|
|
return types.Service{}, err
|
|
|
|
}
|
2016-09-02 17:12:05 -04:00
|
|
|
service := types.Service{
|
|
|
|
ID: s.ID,
|
2017-03-01 15:52:55 -05:00
|
|
|
Spec: *curSpec,
|
|
|
|
PreviousSpec: prevSpec,
|
2016-09-02 17:12:05 -04:00
|
|
|
|
|
|
|
Endpoint: endpointFromGRPC(s.Endpoint),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meta
|
|
|
|
service.Version.Index = s.Meta.Version.Index
|
2017-01-23 18:50:10 -05:00
|
|
|
service.CreatedAt, _ = gogotypes.TimestampFromProto(s.Meta.CreatedAt)
|
|
|
|
service.UpdatedAt, _ = gogotypes.TimestampFromProto(s.Meta.UpdatedAt)
|
2016-09-02 17:12:05 -04:00
|
|
|
|
|
|
|
// UpdateStatus
|
|
|
|
if s.UpdateStatus != nil {
|
2016-12-01 17:08:06 -05:00
|
|
|
service.UpdateStatus = &types.UpdateStatus{}
|
2016-09-02 17:12:05 -04:00
|
|
|
switch s.UpdateStatus.State {
|
|
|
|
case swarmapi.UpdateStatus_UPDATING:
|
|
|
|
service.UpdateStatus.State = types.UpdateStateUpdating
|
|
|
|
case swarmapi.UpdateStatus_PAUSED:
|
|
|
|
service.UpdateStatus.State = types.UpdateStatePaused
|
|
|
|
case swarmapi.UpdateStatus_COMPLETED:
|
|
|
|
service.UpdateStatus.State = types.UpdateStateCompleted
|
2017-02-15 17:53:58 -05:00
|
|
|
case swarmapi.UpdateStatus_ROLLBACK_STARTED:
|
|
|
|
service.UpdateStatus.State = types.UpdateStateRollbackStarted
|
|
|
|
case swarmapi.UpdateStatus_ROLLBACK_PAUSED:
|
|
|
|
service.UpdateStatus.State = types.UpdateStateRollbackPaused
|
|
|
|
case swarmapi.UpdateStatus_ROLLBACK_COMPLETED:
|
|
|
|
service.UpdateStatus.State = types.UpdateStateRollbackCompleted
|
2016-09-02 17:12:05 -04:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:50:10 -05:00
|
|
|
startedAt, _ := gogotypes.TimestampFromProto(s.UpdateStatus.StartedAt)
|
2017-04-14 13:44:24 -04:00
|
|
|
if !startedAt.IsZero() && startedAt.Unix() != 0 {
|
2016-12-01 17:08:06 -05:00
|
|
|
service.UpdateStatus.StartedAt = &startedAt
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:50:10 -05:00
|
|
|
completedAt, _ := gogotypes.TimestampFromProto(s.UpdateStatus.CompletedAt)
|
2017-04-14 13:44:24 -04:00
|
|
|
if !completedAt.IsZero() && completedAt.Unix() != 0 {
|
2016-12-01 17:08:06 -05:00
|
|
|
service.UpdateStatus.CompletedAt = &completedAt
|
|
|
|
}
|
|
|
|
|
2016-09-02 17:12:05 -04:00
|
|
|
service.UpdateStatus.Message = s.UpdateStatus.Message
|
|
|
|
}
|
|
|
|
|
2017-03-01 15:52:55 -05:00
|
|
|
return service, nil
|
2016-09-02 17:12:05 -04:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:52:55 -05:00
|
|
|
func serviceSpecFromGRPC(spec *swarmapi.ServiceSpec) (*types.ServiceSpec, error) {
|
2016-09-02 17:12:05 -04:00
|
|
|
if spec == nil {
|
2017-03-01 15:52:55 -05:00
|
|
|
return nil, nil
|
2016-09-02 17:12:05 -04:00
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2016-08-23 19:50:15 -04:00
|
|
|
serviceNetworks := make([]types.NetworkAttachmentConfig, 0, len(spec.Networks))
|
2016-06-13 22:52:49 -04:00
|
|
|
for _, n := range spec.Networks {
|
2017-05-09 19:10:08 -04:00
|
|
|
netConfig := types.NetworkAttachmentConfig{Target: n.Target, Aliases: n.Aliases, DriverOpts: n.DriverAttachmentOpts}
|
|
|
|
serviceNetworks = append(serviceNetworks, netConfig)
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
2016-08-23 19:50:15 -04:00
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
taskTemplate, err := taskSpecFromGRPC(spec.Task)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-01 15:52:55 -05:00
|
|
|
|
2017-03-27 10:51:42 -04:00
|
|
|
switch t := spec.Task.GetRuntime().(type) {
|
2017-03-01 15:52:55 -05:00
|
|
|
case *swarmapi.TaskSpec_Container:
|
|
|
|
containerConfig := t.Container
|
|
|
|
taskTemplate.ContainerSpec = containerSpecFromGRPC(containerConfig)
|
|
|
|
taskTemplate.Runtime = types.RuntimeContainer
|
|
|
|
case *swarmapi.TaskSpec_Generic:
|
2017-03-24 15:19:59 -04:00
|
|
|
switch t.Generic.Kind {
|
|
|
|
case string(types.RuntimePlugin):
|
2017-03-01 15:52:55 -05:00
|
|
|
taskTemplate.Runtime = types.RuntimePlugin
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown task runtime type: %s", t.Generic.Payload.TypeUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("error creating service; unsupported runtime %T", t)
|
|
|
|
}
|
|
|
|
|
2016-09-02 17:12:05 -04:00
|
|
|
convertedSpec := &types.ServiceSpec{
|
2017-02-24 18:20:18 -05:00
|
|
|
Annotations: annotationsFromGRPC(spec.Annotations),
|
2017-03-01 15:52:55 -05:00
|
|
|
TaskTemplate: taskTemplate,
|
2016-09-02 17:12:05 -04:00
|
|
|
Networks: serviceNetworks,
|
|
|
|
EndpointSpec: endpointSpecFromGRPC(spec.Endpoint),
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
// UpdateConfig
|
2017-02-15 19:04:30 -05:00
|
|
|
convertedSpec.UpdateConfig = updateConfigFromGRPC(spec.Update)
|
|
|
|
convertedSpec.RollbackConfig = updateConfigFromGRPC(spec.Rollback)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2016-07-22 14:35:51 -04:00
|
|
|
// Mode
|
2016-09-02 17:12:05 -04:00
|
|
|
switch t := spec.GetMode().(type) {
|
2016-06-13 22:52:49 -04:00
|
|
|
case *swarmapi.ServiceSpec_Global:
|
2016-09-02 17:12:05 -04:00
|
|
|
convertedSpec.Mode.Global = &types.GlobalService{}
|
2016-06-13 22:52:49 -04:00
|
|
|
case *swarmapi.ServiceSpec_Replicated:
|
2016-09-02 17:12:05 -04:00
|
|
|
convertedSpec.Mode.Replicated = &types.ReplicatedService{
|
2016-06-13 22:52:49 -04:00
|
|
|
Replicas: &t.Replicated.Replicas,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 15:52:55 -05:00
|
|
|
return convertedSpec, nil
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceSpecToGRPC converts a ServiceSpec to a grpc ServiceSpec.
|
|
|
|
func ServiceSpecToGRPC(s types.ServiceSpec) (swarmapi.ServiceSpec, error) {
|
|
|
|
name := s.Name
|
|
|
|
if name == "" {
|
|
|
|
name = namesgenerator.GetRandomName(0)
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:50:15 -04:00
|
|
|
serviceNetworks := make([]*swarmapi.NetworkAttachmentConfig, 0, len(s.Networks))
|
2016-06-13 22:52:49 -04:00
|
|
|
for _, n := range s.Networks {
|
2017-05-09 19:10:08 -04:00
|
|
|
netConfig := &swarmapi.NetworkAttachmentConfig{Target: n.Target, Aliases: n.Aliases, DriverAttachmentOpts: n.DriverOpts}
|
|
|
|
serviceNetworks = append(serviceNetworks, netConfig)
|
2016-08-23 19:50:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
taskNetworks := make([]*swarmapi.NetworkAttachmentConfig, 0, len(s.TaskTemplate.Networks))
|
|
|
|
for _, n := range s.TaskTemplate.Networks {
|
2017-05-09 19:10:08 -04:00
|
|
|
netConfig := &swarmapi.NetworkAttachmentConfig{Target: n.Target, Aliases: n.Aliases, DriverAttachmentOpts: n.DriverOpts}
|
|
|
|
taskNetworks = append(taskNetworks, netConfig)
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
spec := swarmapi.ServiceSpec{
|
|
|
|
Annotations: swarmapi.Annotations{
|
|
|
|
Name: name,
|
|
|
|
Labels: s.Labels,
|
|
|
|
},
|
|
|
|
Task: swarmapi.TaskSpec{
|
2016-10-20 15:04:01 -04:00
|
|
|
Resources: resourcesToGRPC(s.TaskTemplate.Resources),
|
|
|
|
LogDriver: driverToGRPC(s.TaskTemplate.LogDriver),
|
|
|
|
Networks: taskNetworks,
|
|
|
|
ForceUpdate: s.TaskTemplate.ForceUpdate,
|
2016-06-13 22:52:49 -04:00
|
|
|
},
|
2016-08-23 19:50:15 -04:00
|
|
|
Networks: serviceNetworks,
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:52:55 -05:00
|
|
|
switch s.TaskTemplate.Runtime {
|
|
|
|
case types.RuntimeContainer, "": // if empty runtime default to container
|
2017-06-07 13:07:01 -04:00
|
|
|
if s.TaskTemplate.ContainerSpec != nil {
|
|
|
|
containerSpec, err := containerToGRPC(s.TaskTemplate.ContainerSpec)
|
|
|
|
if err != nil {
|
|
|
|
return swarmapi.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
spec.Task.Runtime = &swarmapi.TaskSpec_Container{Container: containerSpec}
|
2017-10-18 17:41:59 -04:00
|
|
|
} else {
|
|
|
|
// If the ContainerSpec is nil, we can't set the task runtime
|
|
|
|
return swarmapi.ServiceSpec{}, ErrMismatchedRuntime
|
2017-03-01 15:52:55 -05:00
|
|
|
}
|
|
|
|
case types.RuntimePlugin:
|
2017-10-18 17:41:59 -04:00
|
|
|
if s.TaskTemplate.PluginSpec != nil {
|
|
|
|
if s.Mode.Replicated != nil {
|
|
|
|
return swarmapi.ServiceSpec{}, errors.New("plugins must not use replicated mode")
|
|
|
|
}
|
2017-06-07 13:07:01 -04:00
|
|
|
|
2017-10-18 17:41:59 -04:00
|
|
|
s.Mode.Global = &types.GlobalService{} // must always be global
|
2017-06-07 13:07:01 -04:00
|
|
|
|
|
|
|
pluginSpec, err := proto.Marshal(s.TaskTemplate.PluginSpec)
|
|
|
|
if err != nil {
|
|
|
|
return swarmapi.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
spec.Task.Runtime = &swarmapi.TaskSpec_Generic{
|
|
|
|
Generic: &swarmapi.GenericRuntimeSpec{
|
|
|
|
Kind: string(types.RuntimePlugin),
|
|
|
|
Payload: &gogotypes.Any{
|
|
|
|
TypeUrl: string(types.RuntimeURLPlugin),
|
|
|
|
Value: pluginSpec,
|
|
|
|
},
|
2017-03-01 15:52:55 -05:00
|
|
|
},
|
2017-06-07 13:07:01 -04:00
|
|
|
}
|
2017-10-18 17:41:59 -04:00
|
|
|
} else {
|
|
|
|
return swarmapi.ServiceSpec{}, ErrMismatchedRuntime
|
|
|
|
}
|
|
|
|
case types.RuntimeNetworkAttachment:
|
|
|
|
// NOTE(dperny) I'm leaving this case here for completeness. The actual
|
2018-09-16 01:18:24 -04:00
|
|
|
// code is left out deliberately, as we should refuse to parse a
|
2017-10-18 17:41:59 -04:00
|
|
|
// Network Attachment runtime; it will cause weird behavior all over
|
|
|
|
// the system if we do. Instead, fallthrough and return
|
|
|
|
// ErrUnsupportedRuntime if we get one.
|
|
|
|
fallthrough
|
2017-03-01 15:52:55 -05:00
|
|
|
default:
|
2017-03-27 22:28:35 -04:00
|
|
|
return swarmapi.ServiceSpec{}, ErrUnsupportedRuntime
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
restartPolicy, err := restartPolicyToGRPC(s.TaskTemplate.RestartPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return swarmapi.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
spec.Task.Restart = restartPolicy
|
|
|
|
|
|
|
|
if s.TaskTemplate.Placement != nil {
|
2017-01-19 18:27:37 -05:00
|
|
|
var preferences []*swarmapi.PlacementPreference
|
|
|
|
for _, pref := range s.TaskTemplate.Placement.Preferences {
|
|
|
|
if pref.Spread != nil {
|
|
|
|
preferences = append(preferences, &swarmapi.PlacementPreference{
|
|
|
|
Preference: &swarmapi.PlacementPreference_Spread{
|
|
|
|
Spread: &swarmapi.SpreadOver{
|
|
|
|
SpreadDescriptor: pref.Spread.SpreadDescriptor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 14:47:29 -04:00
|
|
|
var platforms []*swarmapi.Platform
|
|
|
|
for _, plat := range s.TaskTemplate.Placement.Platforms {
|
|
|
|
platforms = append(platforms, &swarmapi.Platform{
|
|
|
|
Architecture: plat.Architecture,
|
|
|
|
OS: plat.OS,
|
|
|
|
})
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
spec.Task.Placement = &swarmapi.Placement{
|
|
|
|
Constraints: s.TaskTemplate.Placement.Constraints,
|
2017-01-19 18:27:37 -05:00
|
|
|
Preferences: preferences,
|
2018-09-30 08:28:37 -04:00
|
|
|
MaxReplicas: s.TaskTemplate.Placement.MaxReplicas,
|
2017-05-10 14:47:29 -04:00
|
|
|
Platforms: platforms,
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:04:30 -05:00
|
|
|
spec.Update, err = updateConfigToGRPC(s.UpdateConfig)
|
|
|
|
if err != nil {
|
|
|
|
return swarmapi.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
spec.Rollback, err = updateConfigToGRPC(s.RollbackConfig)
|
|
|
|
if err != nil {
|
2017-02-16 12:27:01 -05:00
|
|
|
return swarmapi.ServiceSpec{}, err
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.EndpointSpec != nil {
|
|
|
|
if s.EndpointSpec.Mode != "" &&
|
|
|
|
s.EndpointSpec.Mode != types.ResolutionModeVIP &&
|
|
|
|
s.EndpointSpec.Mode != types.ResolutionModeDNSRR {
|
|
|
|
return swarmapi.ServiceSpec{}, fmt.Errorf("invalid resolution mode: %q", s.EndpointSpec.Mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec.Endpoint = &swarmapi.EndpointSpec{}
|
|
|
|
|
|
|
|
spec.Endpoint.Mode = swarmapi.EndpointSpec_ResolutionMode(swarmapi.EndpointSpec_ResolutionMode_value[strings.ToUpper(string(s.EndpointSpec.Mode))])
|
|
|
|
|
|
|
|
for _, portConfig := range s.EndpointSpec.Ports {
|
|
|
|
spec.Endpoint.Ports = append(spec.Endpoint.Ports, &swarmapi.PortConfig{
|
|
|
|
Name: portConfig.Name,
|
|
|
|
Protocol: swarmapi.PortConfig_Protocol(swarmapi.PortConfig_Protocol_value[strings.ToUpper(string(portConfig.Protocol))]),
|
2016-11-10 15:13:26 -05:00
|
|
|
PublishMode: swarmapi.PortConfig_PublishMode(swarmapi.PortConfig_PublishMode_value[strings.ToUpper(string(portConfig.PublishMode))]),
|
2016-06-13 22:52:49 -04:00
|
|
|
TargetPort: portConfig.TargetPort,
|
|
|
|
PublishedPort: portConfig.PublishedPort,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 14:00:16 -04:00
|
|
|
// Mode
|
|
|
|
if s.Mode.Global != nil && s.Mode.Replicated != nil {
|
|
|
|
return swarmapi.ServiceSpec{}, fmt.Errorf("cannot specify both replicated mode and global mode")
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
if s.Mode.Global != nil {
|
|
|
|
spec.Mode = &swarmapi.ServiceSpec_Global{
|
|
|
|
Global: &swarmapi.GlobalService{},
|
|
|
|
}
|
|
|
|
} else if s.Mode.Replicated != nil && s.Mode.Replicated.Replicas != nil {
|
|
|
|
spec.Mode = &swarmapi.ServiceSpec_Replicated{
|
|
|
|
Replicated: &swarmapi.ReplicatedService{Replicas: *s.Mode.Replicated.Replicas},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
spec.Mode = &swarmapi.ServiceSpec_Replicated{
|
|
|
|
Replicated: &swarmapi.ReplicatedService{Replicas: 1},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec, nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 03:07:03 -05:00
|
|
|
func annotationsFromGRPC(ann swarmapi.Annotations) types.Annotations {
|
|
|
|
a := types.Annotations{
|
|
|
|
Name: ann.Name,
|
|
|
|
Labels: ann.Labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Labels == nil {
|
|
|
|
a.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:02:11 -04:00
|
|
|
// GenericResourcesFromGRPC converts a GRPC GenericResource to a GenericResource
|
|
|
|
func GenericResourcesFromGRPC(genericRes []*swarmapi.GenericResource) []types.GenericResource {
|
|
|
|
var generic []types.GenericResource
|
|
|
|
for _, res := range genericRes {
|
|
|
|
var current types.GenericResource
|
|
|
|
|
|
|
|
switch r := res.Resource.(type) {
|
|
|
|
case *swarmapi.GenericResource_DiscreteResourceSpec:
|
|
|
|
current.DiscreteResourceSpec = &types.DiscreteGenericResource{
|
|
|
|
Kind: r.DiscreteResourceSpec.Kind,
|
|
|
|
Value: r.DiscreteResourceSpec.Value,
|
|
|
|
}
|
|
|
|
case *swarmapi.GenericResource_NamedResourceSpec:
|
|
|
|
current.NamedResourceSpec = &types.NamedGenericResource{
|
|
|
|
Kind: r.NamedResourceSpec.Kind,
|
|
|
|
Value: r.NamedResourceSpec.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generic = append(generic, current)
|
|
|
|
}
|
|
|
|
|
|
|
|
return generic
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
func resourcesFromGRPC(res *swarmapi.ResourceRequirements) *types.ResourceRequirements {
|
|
|
|
var resources *types.ResourceRequirements
|
|
|
|
if res != nil {
|
|
|
|
resources = &types.ResourceRequirements{}
|
|
|
|
if res.Limits != nil {
|
|
|
|
resources.Limits = &types.Resources{
|
|
|
|
NanoCPUs: res.Limits.NanoCPUs,
|
|
|
|
MemoryBytes: res.Limits.MemoryBytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if res.Reservations != nil {
|
|
|
|
resources.Reservations = &types.Resources{
|
2017-05-30 20:02:11 -04:00
|
|
|
NanoCPUs: res.Reservations.NanoCPUs,
|
|
|
|
MemoryBytes: res.Reservations.MemoryBytes,
|
|
|
|
GenericResources: GenericResourcesFromGRPC(res.Reservations.Generic),
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:02:11 -04:00
|
|
|
// GenericResourcesToGRPC converts a GenericResource to a GRPC GenericResource
|
|
|
|
func GenericResourcesToGRPC(genericRes []types.GenericResource) []*swarmapi.GenericResource {
|
|
|
|
var generic []*swarmapi.GenericResource
|
|
|
|
for _, res := range genericRes {
|
|
|
|
var r *swarmapi.GenericResource
|
|
|
|
|
|
|
|
if res.DiscreteResourceSpec != nil {
|
|
|
|
r = genericresource.NewDiscrete(res.DiscreteResourceSpec.Kind, res.DiscreteResourceSpec.Value)
|
|
|
|
} else if res.NamedResourceSpec != nil {
|
|
|
|
r = genericresource.NewString(res.NamedResourceSpec.Kind, res.NamedResourceSpec.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
generic = append(generic, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return generic
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
func resourcesToGRPC(res *types.ResourceRequirements) *swarmapi.ResourceRequirements {
|
|
|
|
var reqs *swarmapi.ResourceRequirements
|
|
|
|
if res != nil {
|
|
|
|
reqs = &swarmapi.ResourceRequirements{}
|
|
|
|
if res.Limits != nil {
|
|
|
|
reqs.Limits = &swarmapi.Resources{
|
|
|
|
NanoCPUs: res.Limits.NanoCPUs,
|
|
|
|
MemoryBytes: res.Limits.MemoryBytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if res.Reservations != nil {
|
|
|
|
reqs.Reservations = &swarmapi.Resources{
|
|
|
|
NanoCPUs: res.Reservations.NanoCPUs,
|
|
|
|
MemoryBytes: res.Reservations.MemoryBytes,
|
2017-05-30 20:02:11 -04:00
|
|
|
Generic: GenericResourcesToGRPC(res.Reservations.GenericResources),
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reqs
|
|
|
|
}
|
|
|
|
|
|
|
|
func restartPolicyFromGRPC(p *swarmapi.RestartPolicy) *types.RestartPolicy {
|
|
|
|
var rp *types.RestartPolicy
|
|
|
|
if p != nil {
|
|
|
|
rp = &types.RestartPolicy{}
|
2016-09-30 03:54:47 -04:00
|
|
|
|
|
|
|
switch p.Condition {
|
|
|
|
case swarmapi.RestartOnNone:
|
|
|
|
rp.Condition = types.RestartPolicyConditionNone
|
|
|
|
case swarmapi.RestartOnFailure:
|
|
|
|
rp.Condition = types.RestartPolicyConditionOnFailure
|
|
|
|
case swarmapi.RestartOnAny:
|
|
|
|
rp.Condition = types.RestartPolicyConditionAny
|
|
|
|
default:
|
|
|
|
rp.Condition = types.RestartPolicyConditionAny
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
if p.Delay != nil {
|
2017-01-23 18:50:10 -05:00
|
|
|
delay, _ := gogotypes.DurationFromProto(p.Delay)
|
2016-06-13 22:52:49 -04:00
|
|
|
rp.Delay = &delay
|
|
|
|
}
|
|
|
|
if p.Window != nil {
|
2017-01-23 18:50:10 -05:00
|
|
|
window, _ := gogotypes.DurationFromProto(p.Window)
|
2016-06-13 22:52:49 -04:00
|
|
|
rp.Window = &window
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.MaxAttempts = &p.MaxAttempts
|
|
|
|
}
|
|
|
|
return rp
|
|
|
|
}
|
|
|
|
|
|
|
|
func restartPolicyToGRPC(p *types.RestartPolicy) (*swarmapi.RestartPolicy, error) {
|
|
|
|
var rp *swarmapi.RestartPolicy
|
|
|
|
if p != nil {
|
|
|
|
rp = &swarmapi.RestartPolicy{}
|
2016-09-30 03:54:47 -04:00
|
|
|
|
|
|
|
switch p.Condition {
|
|
|
|
case types.RestartPolicyConditionNone:
|
|
|
|
rp.Condition = swarmapi.RestartOnNone
|
|
|
|
case types.RestartPolicyConditionOnFailure:
|
|
|
|
rp.Condition = swarmapi.RestartOnFailure
|
|
|
|
case types.RestartPolicyConditionAny:
|
|
|
|
rp.Condition = swarmapi.RestartOnAny
|
|
|
|
default:
|
|
|
|
if string(p.Condition) != "" {
|
|
|
|
return nil, fmt.Errorf("invalid RestartCondition: %q", p.Condition)
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
rp.Condition = swarmapi.RestartOnAny
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Delay != nil {
|
2017-01-23 18:50:10 -05:00
|
|
|
rp.Delay = gogotypes.DurationProto(*p.Delay)
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
if p.Window != nil {
|
2017-01-23 18:50:10 -05:00
|
|
|
rp.Window = gogotypes.DurationProto(*p.Window)
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
if p.MaxAttempts != nil {
|
|
|
|
rp.MaxAttempts = *p.MaxAttempts
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func placementFromGRPC(p *swarmapi.Placement) *types.Placement {
|
2017-01-19 18:27:37 -05:00
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r := &types.Placement{
|
|
|
|
Constraints: p.Constraints,
|
2018-09-30 08:28:37 -04:00
|
|
|
MaxReplicas: p.MaxReplicas,
|
2017-01-19 18:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pref := range p.Preferences {
|
|
|
|
if spread := pref.GetSpread(); spread != nil {
|
|
|
|
r.Preferences = append(r.Preferences, types.PlacementPreference{
|
|
|
|
Spread: &types.SpreadOver{
|
|
|
|
SpreadDescriptor: spread.SpreadDescriptor,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2017-05-10 14:47:29 -04:00
|
|
|
for _, plat := range p.Platforms {
|
|
|
|
r.Platforms = append(r.Platforms, types.Platform{
|
|
|
|
Architecture: plat.Architecture,
|
|
|
|
OS: plat.OS,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
return r
|
|
|
|
}
|
2016-07-08 21:44:18 -04:00
|
|
|
|
|
|
|
func driverFromGRPC(p *swarmapi.Driver) *types.Driver {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Driver{
|
|
|
|
Name: p.Name,
|
|
|
|
Options: p.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func driverToGRPC(p *types.Driver) *swarmapi.Driver {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &swarmapi.Driver{
|
|
|
|
Name: p.Name,
|
|
|
|
Options: p.Options,
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 19:04:30 -05:00
|
|
|
|
|
|
|
func updateConfigFromGRPC(updateConfig *swarmapi.UpdateConfig) *types.UpdateConfig {
|
|
|
|
if updateConfig == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
converted := &types.UpdateConfig{
|
|
|
|
Parallelism: updateConfig.Parallelism,
|
|
|
|
MaxFailureRatio: updateConfig.MaxFailureRatio,
|
|
|
|
}
|
|
|
|
|
|
|
|
converted.Delay = updateConfig.Delay
|
|
|
|
if updateConfig.Monitor != nil {
|
|
|
|
converted.Monitor, _ = gogotypes.DurationFromProto(updateConfig.Monitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch updateConfig.FailureAction {
|
|
|
|
case swarmapi.UpdateConfig_PAUSE:
|
|
|
|
converted.FailureAction = types.UpdateFailureActionPause
|
|
|
|
case swarmapi.UpdateConfig_CONTINUE:
|
|
|
|
converted.FailureAction = types.UpdateFailureActionContinue
|
|
|
|
case swarmapi.UpdateConfig_ROLLBACK:
|
|
|
|
converted.FailureAction = types.UpdateFailureActionRollback
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:38:19 -05:00
|
|
|
switch updateConfig.Order {
|
|
|
|
case swarmapi.UpdateConfig_STOP_FIRST:
|
|
|
|
converted.Order = types.UpdateOrderStopFirst
|
|
|
|
case swarmapi.UpdateConfig_START_FIRST:
|
|
|
|
converted.Order = types.UpdateOrderStartFirst
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:04:30 -05:00
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateConfigToGRPC(updateConfig *types.UpdateConfig) (*swarmapi.UpdateConfig, error) {
|
|
|
|
if updateConfig == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
converted := &swarmapi.UpdateConfig{
|
|
|
|
Parallelism: updateConfig.Parallelism,
|
|
|
|
Delay: updateConfig.Delay,
|
|
|
|
MaxFailureRatio: updateConfig.MaxFailureRatio,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch updateConfig.FailureAction {
|
|
|
|
case types.UpdateFailureActionPause, "":
|
|
|
|
converted.FailureAction = swarmapi.UpdateConfig_PAUSE
|
|
|
|
case types.UpdateFailureActionContinue:
|
|
|
|
converted.FailureAction = swarmapi.UpdateConfig_CONTINUE
|
|
|
|
case types.UpdateFailureActionRollback:
|
|
|
|
converted.FailureAction = swarmapi.UpdateConfig_ROLLBACK
|
|
|
|
default:
|
2017-01-18 14:38:19 -05:00
|
|
|
return nil, fmt.Errorf("unrecognized update failure action %s", updateConfig.FailureAction)
|
2017-02-15 19:04:30 -05:00
|
|
|
}
|
|
|
|
if updateConfig.Monitor != 0 {
|
|
|
|
converted.Monitor = gogotypes.DurationProto(updateConfig.Monitor)
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:38:19 -05:00
|
|
|
switch updateConfig.Order {
|
|
|
|
case types.UpdateOrderStopFirst, "":
|
|
|
|
converted.Order = swarmapi.UpdateConfig_STOP_FIRST
|
|
|
|
case types.UpdateOrderStartFirst:
|
|
|
|
converted.Order = swarmapi.UpdateConfig_START_FIRST
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unrecognized update order %s", updateConfig.Order)
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:04:30 -05:00
|
|
|
return converted, nil
|
|
|
|
}
|
2017-02-24 18:20:18 -05:00
|
|
|
|
2017-10-18 17:41:59 -04:00
|
|
|
func networkAttachmentSpecFromGRPC(attachment swarmapi.NetworkAttachmentSpec) *types.NetworkAttachmentSpec {
|
|
|
|
return &types.NetworkAttachmentSpec{
|
|
|
|
ContainerID: attachment.ContainerID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
func taskSpecFromGRPC(taskSpec swarmapi.TaskSpec) (types.TaskSpec, error) {
|
2017-02-24 18:20:18 -05:00
|
|
|
taskNetworks := make([]types.NetworkAttachmentConfig, 0, len(taskSpec.Networks))
|
|
|
|
for _, n := range taskSpec.Networks {
|
2017-05-09 19:10:08 -04:00
|
|
|
netConfig := types.NetworkAttachmentConfig{Target: n.Target, Aliases: n.Aliases, DriverOpts: n.DriverAttachmentOpts}
|
|
|
|
taskNetworks = append(taskNetworks, netConfig)
|
2017-02-24 18:20:18 -05:00
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
t := types.TaskSpec{
|
2017-02-24 18:20:18 -05:00
|
|
|
Resources: resourcesFromGRPC(taskSpec.Resources),
|
|
|
|
RestartPolicy: restartPolicyFromGRPC(taskSpec.Restart),
|
|
|
|
Placement: placementFromGRPC(taskSpec.Placement),
|
|
|
|
LogDriver: driverFromGRPC(taskSpec.LogDriver),
|
|
|
|
Networks: taskNetworks,
|
|
|
|
ForceUpdate: taskSpec.ForceUpdate,
|
|
|
|
}
|
2017-06-07 13:07:01 -04:00
|
|
|
|
|
|
|
switch taskSpec.GetRuntime().(type) {
|
|
|
|
case *swarmapi.TaskSpec_Container, nil:
|
|
|
|
c := taskSpec.GetContainer()
|
|
|
|
if c != nil {
|
|
|
|
t.ContainerSpec = containerSpecFromGRPC(c)
|
|
|
|
}
|
|
|
|
case *swarmapi.TaskSpec_Generic:
|
|
|
|
g := taskSpec.GetGeneric()
|
|
|
|
if g != nil {
|
|
|
|
switch g.Kind {
|
|
|
|
case string(types.RuntimePlugin):
|
|
|
|
var p runtime.PluginSpec
|
|
|
|
if err := proto.Unmarshal(g.Payload.Value, &p); err != nil {
|
|
|
|
return t, errors.Wrap(err, "error unmarshalling plugin spec")
|
|
|
|
}
|
|
|
|
t.PluginSpec = &p
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:41:59 -04:00
|
|
|
case *swarmapi.TaskSpec_Attachment:
|
|
|
|
a := taskSpec.GetAttachment()
|
|
|
|
if a != nil {
|
|
|
|
t.NetworkAttachmentSpec = networkAttachmentSpecFromGRPC(*a)
|
|
|
|
}
|
|
|
|
t.Runtime = types.RuntimeNetworkAttachment
|
2017-06-07 13:07:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
2017-02-24 18:20:18 -05:00
|
|
|
}
|