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 (
|
2017-03-29 18:55:21 -04:00
|
|
|
"errors"
|
2016-06-13 22:52:49 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-05-19 07:38:54 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-09-06 14:18:12 -04:00
|
|
|
mounttypes "github.com/docker/docker/api/types/mount"
|
|
|
|
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"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-13 22:52:49 -04:00
|
|
|
)
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
func containerSpecFromGRPC(c *swarmapi.ContainerSpec) *types.ContainerSpec {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
containerSpec := &types.ContainerSpec{
|
2017-02-06 00:22:57 -05:00
|
|
|
Image: c.Image,
|
|
|
|
Labels: c.Labels,
|
|
|
|
Command: c.Command,
|
|
|
|
Args: c.Args,
|
|
|
|
Hostname: c.Hostname,
|
|
|
|
Env: c.Env,
|
|
|
|
Dir: c.Dir,
|
|
|
|
User: c.User,
|
|
|
|
Groups: c.Groups,
|
|
|
|
StopSignal: c.StopSignal,
|
|
|
|
TTY: c.TTY,
|
|
|
|
OpenStdin: c.OpenStdin,
|
|
|
|
ReadOnly: c.ReadOnly,
|
|
|
|
Hosts: c.Hosts,
|
|
|
|
Secrets: secretReferencesFromGRPC(c.Secrets),
|
2017-03-15 17:52:17 -04:00
|
|
|
Configs: configReferencesFromGRPC(c.Configs),
|
2017-08-07 10:07:17 -04:00
|
|
|
Isolation: IsolationFromGRPC(c.Isolation),
|
2018-06-01 06:47:38 -04:00
|
|
|
Init: initFromGRPC(c.Init),
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:07:44 -04:00
|
|
|
if c.DNSConfig != nil {
|
|
|
|
containerSpec.DNSConfig = &types.DNSConfig{
|
|
|
|
Nameservers: c.DNSConfig.Nameservers,
|
|
|
|
Search: c.DNSConfig.Search,
|
|
|
|
Options: c.DNSConfig.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 18:55:21 -04:00
|
|
|
// Privileges
|
|
|
|
if c.Privileges != nil {
|
|
|
|
containerSpec.Privileges = &types.Privileges{}
|
|
|
|
|
|
|
|
if c.Privileges.CredentialSpec != nil {
|
|
|
|
containerSpec.Privileges.CredentialSpec = &types.CredentialSpec{}
|
|
|
|
switch c.Privileges.CredentialSpec.Source.(type) {
|
|
|
|
case *swarmapi.Privileges_CredentialSpec_File:
|
|
|
|
containerSpec.Privileges.CredentialSpec.File = c.Privileges.CredentialSpec.GetFile()
|
|
|
|
case *swarmapi.Privileges_CredentialSpec_Registry:
|
|
|
|
containerSpec.Privileges.CredentialSpec.Registry = c.Privileges.CredentialSpec.GetRegistry()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Privileges.SELinuxContext != nil {
|
|
|
|
containerSpec.Privileges.SELinuxContext = &types.SELinuxContext{
|
|
|
|
Disable: c.Privileges.SELinuxContext.Disable,
|
|
|
|
User: c.Privileges.SELinuxContext.User,
|
|
|
|
Type: c.Privileges.SELinuxContext.Type,
|
|
|
|
Role: c.Privileges.SELinuxContext.Role,
|
|
|
|
Level: c.Privileges.SELinuxContext.Level,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// Mounts
|
|
|
|
for _, m := range c.Mounts {
|
2016-08-15 12:13:18 -04:00
|
|
|
mount := mounttypes.Mount{
|
2016-06-13 22:52:49 -04:00
|
|
|
Target: m.Target,
|
|
|
|
Source: m.Source,
|
2016-08-15 12:13:18 -04:00
|
|
|
Type: mounttypes.Type(strings.ToLower(swarmapi.Mount_MountType_name[int32(m.Type)])),
|
2016-07-05 14:43:28 -04:00
|
|
|
ReadOnly: m.ReadOnly,
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.BindOptions != nil {
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.BindOptions = &mounttypes.BindOptions{
|
|
|
|
Propagation: mounttypes.Propagation(strings.ToLower(swarmapi.Mount_BindOptions_MountPropagation_name[int32(m.BindOptions.Propagation)])),
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.VolumeOptions != nil {
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.VolumeOptions = &mounttypes.VolumeOptions{
|
2016-07-05 14:43:28 -04:00
|
|
|
NoCopy: m.VolumeOptions.NoCopy,
|
|
|
|
Labels: m.VolumeOptions.Labels,
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
if m.VolumeOptions.DriverConfig != nil {
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.VolumeOptions.DriverConfig = &mounttypes.Driver{
|
2016-06-13 22:52:49 -04:00
|
|
|
Name: m.VolumeOptions.DriverConfig.Name,
|
|
|
|
Options: m.VolumeOptions.DriverConfig.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 11:43:47 -05:00
|
|
|
|
|
|
|
if m.TmpfsOptions != nil {
|
|
|
|
mount.TmpfsOptions = &mounttypes.TmpfsOptions{
|
|
|
|
SizeBytes: m.TmpfsOptions.SizeBytes,
|
|
|
|
Mode: m.TmpfsOptions.Mode,
|
|
|
|
}
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
containerSpec.Mounts = append(containerSpec.Mounts, mount)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.StopGracePeriod != nil {
|
2017-01-23 18:50:10 -05:00
|
|
|
grace, _ := gogotypes.DurationFromProto(c.StopGracePeriod)
|
2016-06-13 22:52:49 -04:00
|
|
|
containerSpec.StopGracePeriod = &grace
|
|
|
|
}
|
2016-10-13 14:28:32 -04:00
|
|
|
|
|
|
|
if c.Healthcheck != nil {
|
|
|
|
containerSpec.Healthcheck = healthConfigFromGRPC(c.Healthcheck)
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
return containerSpec
|
|
|
|
}
|
|
|
|
|
2018-06-01 06:47:38 -04:00
|
|
|
func initFromGRPC(v *gogotypes.BoolValue) *bool {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
value := v.GetValue()
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
|
|
|
func initToGRPC(v *bool) *gogotypes.BoolValue {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &gogotypes.BoolValue{Value: *v}
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:22:02 -04:00
|
|
|
func secretReferencesToGRPC(sr []*types.SecretReference) []*swarmapi.SecretReference {
|
2016-11-03 14:09:13 -04:00
|
|
|
refs := make([]*swarmapi.SecretReference, 0, len(sr))
|
2016-10-19 12:22:02 -04:00
|
|
|
for _, s := range sr {
|
2016-11-15 10:04:36 -05:00
|
|
|
ref := &swarmapi.SecretReference{
|
2016-10-19 12:22:02 -04:00
|
|
|
SecretID: s.SecretID,
|
|
|
|
SecretName: s.SecretName,
|
2016-11-15 10:04:36 -05:00
|
|
|
}
|
|
|
|
if s.File != nil {
|
|
|
|
ref.Target = &swarmapi.SecretReference_File{
|
2017-04-03 23:54:30 -04:00
|
|
|
File: &swarmapi.FileTarget{
|
2016-11-15 10:04:36 -05:00
|
|
|
Name: s.File.Name,
|
|
|
|
UID: s.File.UID,
|
|
|
|
GID: s.File.GID,
|
|
|
|
Mode: s.File.Mode,
|
2016-11-01 18:11:43 -04:00
|
|
|
},
|
2016-11-15 10:04:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refs = append(refs, ref)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return refs
|
|
|
|
}
|
2017-03-15 17:52:17 -04:00
|
|
|
|
2016-10-19 12:22:02 -04:00
|
|
|
func secretReferencesFromGRPC(sr []*swarmapi.SecretReference) []*types.SecretReference {
|
2016-11-03 14:09:13 -04:00
|
|
|
refs := make([]*types.SecretReference, 0, len(sr))
|
2016-10-19 12:22:02 -04:00
|
|
|
for _, s := range sr {
|
2016-11-01 18:11:43 -04:00
|
|
|
target := s.GetFile()
|
|
|
|
if target == nil {
|
|
|
|
// not a file target
|
|
|
|
logrus.Warnf("secret target not a file: secret=%s", s.SecretID)
|
|
|
|
continue
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
refs = append(refs, &types.SecretReference{
|
2016-11-15 10:04:36 -05:00
|
|
|
File: &types.SecretReferenceFileTarget{
|
2016-11-01 18:11:43 -04:00
|
|
|
Name: target.Name,
|
|
|
|
UID: target.UID,
|
|
|
|
GID: target.GID,
|
|
|
|
Mode: target.Mode,
|
|
|
|
},
|
2016-11-15 10:04:36 -05:00
|
|
|
SecretID: s.SecretID,
|
|
|
|
SecretName: s.SecretName,
|
2016-10-19 12:22:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2017-03-15 17:52:17 -04:00
|
|
|
func configReferencesToGRPC(sr []*types.ConfigReference) []*swarmapi.ConfigReference {
|
|
|
|
refs := make([]*swarmapi.ConfigReference, 0, len(sr))
|
|
|
|
for _, s := range sr {
|
|
|
|
ref := &swarmapi.ConfigReference{
|
|
|
|
ConfigID: s.ConfigID,
|
|
|
|
ConfigName: s.ConfigName,
|
|
|
|
}
|
|
|
|
if s.File != nil {
|
|
|
|
ref.Target = &swarmapi.ConfigReference_File{
|
|
|
|
File: &swarmapi.FileTarget{
|
|
|
|
Name: s.File.Name,
|
|
|
|
UID: s.File.UID,
|
|
|
|
GID: s.File.GID,
|
|
|
|
Mode: s.File.Mode,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refs = append(refs, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
|
|
|
func configReferencesFromGRPC(sr []*swarmapi.ConfigReference) []*types.ConfigReference {
|
|
|
|
refs := make([]*types.ConfigReference, 0, len(sr))
|
|
|
|
for _, s := range sr {
|
|
|
|
target := s.GetFile()
|
|
|
|
if target == nil {
|
|
|
|
// not a file target
|
|
|
|
logrus.Warnf("config target not a file: config=%s", s.ConfigID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
refs = append(refs, &types.ConfigReference{
|
|
|
|
File: &types.ConfigReferenceFileTarget{
|
|
|
|
Name: target.Name,
|
|
|
|
UID: target.UID,
|
|
|
|
GID: target.GID,
|
|
|
|
Mode: target.Mode,
|
|
|
|
},
|
|
|
|
ConfigID: s.ConfigID,
|
|
|
|
ConfigName: s.ConfigName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
func containerToGRPC(c *types.ContainerSpec) (*swarmapi.ContainerSpec, error) {
|
2016-06-13 22:52:49 -04:00
|
|
|
containerSpec := &swarmapi.ContainerSpec{
|
2017-02-06 00:22:57 -05:00
|
|
|
Image: c.Image,
|
|
|
|
Labels: c.Labels,
|
|
|
|
Command: c.Command,
|
|
|
|
Args: c.Args,
|
|
|
|
Hostname: c.Hostname,
|
|
|
|
Env: c.Env,
|
|
|
|
Dir: c.Dir,
|
|
|
|
User: c.User,
|
|
|
|
Groups: c.Groups,
|
|
|
|
StopSignal: c.StopSignal,
|
|
|
|
TTY: c.TTY,
|
|
|
|
OpenStdin: c.OpenStdin,
|
|
|
|
ReadOnly: c.ReadOnly,
|
|
|
|
Hosts: c.Hosts,
|
|
|
|
Secrets: secretReferencesToGRPC(c.Secrets),
|
2017-03-15 17:52:17 -04:00
|
|
|
Configs: configReferencesToGRPC(c.Configs),
|
2017-08-07 10:07:17 -04:00
|
|
|
Isolation: isolationToGRPC(c.Isolation),
|
2018-06-01 06:47:38 -04:00
|
|
|
Init: initToGRPC(c.Init),
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:07:44 -04:00
|
|
|
if c.DNSConfig != nil {
|
|
|
|
containerSpec.DNSConfig = &swarmapi.ContainerSpec_DNSConfig{
|
|
|
|
Nameservers: c.DNSConfig.Nameservers,
|
|
|
|
Search: c.DNSConfig.Search,
|
|
|
|
Options: c.DNSConfig.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
if c.StopGracePeriod != nil {
|
2017-01-23 18:50:10 -05:00
|
|
|
containerSpec.StopGracePeriod = gogotypes.DurationProto(*c.StopGracePeriod)
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2017-03-29 18:55:21 -04:00
|
|
|
// Privileges
|
|
|
|
if c.Privileges != nil {
|
|
|
|
containerSpec.Privileges = &swarmapi.Privileges{}
|
|
|
|
|
|
|
|
if c.Privileges.CredentialSpec != nil {
|
|
|
|
containerSpec.Privileges.CredentialSpec = &swarmapi.Privileges_CredentialSpec{}
|
|
|
|
|
|
|
|
if c.Privileges.CredentialSpec.File != "" && c.Privileges.CredentialSpec.Registry != "" {
|
|
|
|
return nil, errors.New("cannot specify both \"file\" and \"registry\" credential specs")
|
|
|
|
}
|
|
|
|
if c.Privileges.CredentialSpec.File != "" {
|
|
|
|
containerSpec.Privileges.CredentialSpec.Source = &swarmapi.Privileges_CredentialSpec_File{
|
|
|
|
File: c.Privileges.CredentialSpec.File,
|
|
|
|
}
|
|
|
|
} else if c.Privileges.CredentialSpec.Registry != "" {
|
|
|
|
containerSpec.Privileges.CredentialSpec.Source = &swarmapi.Privileges_CredentialSpec_Registry{
|
|
|
|
Registry: c.Privileges.CredentialSpec.Registry,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("must either provide \"file\" or \"registry\" for credential spec")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Privileges.SELinuxContext != nil {
|
|
|
|
containerSpec.Privileges.SELinuxContext = &swarmapi.Privileges_SELinuxContext{
|
|
|
|
Disable: c.Privileges.SELinuxContext.Disable,
|
|
|
|
User: c.Privileges.SELinuxContext.User,
|
|
|
|
Type: c.Privileges.SELinuxContext.Type,
|
|
|
|
Role: c.Privileges.SELinuxContext.Role,
|
|
|
|
Level: c.Privileges.SELinuxContext.Level,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// Mounts
|
|
|
|
for _, m := range c.Mounts {
|
|
|
|
mount := swarmapi.Mount{
|
|
|
|
Target: m.Target,
|
|
|
|
Source: m.Source,
|
2016-07-05 14:43:28 -04:00
|
|
|
ReadOnly: m.ReadOnly,
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if mountType, ok := swarmapi.Mount_MountType_value[strings.ToUpper(string(m.Type))]; ok {
|
|
|
|
mount.Type = swarmapi.Mount_MountType(mountType)
|
|
|
|
} else if string(m.Type) != "" {
|
|
|
|
return nil, fmt.Errorf("invalid MountType: %q", m.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.BindOptions != nil {
|
|
|
|
if mountPropagation, ok := swarmapi.Mount_BindOptions_MountPropagation_value[strings.ToUpper(string(m.BindOptions.Propagation))]; ok {
|
|
|
|
mount.BindOptions = &swarmapi.Mount_BindOptions{Propagation: swarmapi.Mount_BindOptions_MountPropagation(mountPropagation)}
|
|
|
|
} else if string(m.BindOptions.Propagation) != "" {
|
|
|
|
return nil, fmt.Errorf("invalid MountPropagation: %q", m.BindOptions.Propagation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.VolumeOptions != nil {
|
|
|
|
mount.VolumeOptions = &swarmapi.Mount_VolumeOptions{
|
2016-07-05 14:43:28 -04:00
|
|
|
NoCopy: m.VolumeOptions.NoCopy,
|
|
|
|
Labels: m.VolumeOptions.Labels,
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
if m.VolumeOptions.DriverConfig != nil {
|
|
|
|
mount.VolumeOptions.DriverConfig = &swarmapi.Driver{
|
|
|
|
Name: m.VolumeOptions.DriverConfig.Name,
|
|
|
|
Options: m.VolumeOptions.DriverConfig.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 11:43:47 -05:00
|
|
|
if m.TmpfsOptions != nil {
|
|
|
|
mount.TmpfsOptions = &swarmapi.Mount_TmpfsOptions{
|
|
|
|
SizeBytes: m.TmpfsOptions.SizeBytes,
|
|
|
|
Mode: m.TmpfsOptions.Mode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
containerSpec.Mounts = append(containerSpec.Mounts, mount)
|
|
|
|
}
|
|
|
|
|
2016-10-13 14:28:32 -04:00
|
|
|
if c.Healthcheck != nil {
|
|
|
|
containerSpec.Healthcheck = healthConfigToGRPC(c.Healthcheck)
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
return containerSpec, nil
|
|
|
|
}
|
2016-10-13 14:28:32 -04:00
|
|
|
|
|
|
|
func healthConfigFromGRPC(h *swarmapi.HealthConfig) *container.HealthConfig {
|
2017-01-23 18:50:10 -05:00
|
|
|
interval, _ := gogotypes.DurationFromProto(h.Interval)
|
|
|
|
timeout, _ := gogotypes.DurationFromProto(h.Timeout)
|
2017-04-10 20:02:58 -04:00
|
|
|
startPeriod, _ := gogotypes.DurationFromProto(h.StartPeriod)
|
2016-10-13 14:28:32 -04:00
|
|
|
return &container.HealthConfig{
|
2016-11-29 04:58:47 -05:00
|
|
|
Test: h.Test,
|
|
|
|
Interval: interval,
|
|
|
|
Timeout: timeout,
|
|
|
|
Retries: int(h.Retries),
|
|
|
|
StartPeriod: startPeriod,
|
2016-10-13 14:28:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func healthConfigToGRPC(h *container.HealthConfig) *swarmapi.HealthConfig {
|
|
|
|
return &swarmapi.HealthConfig{
|
2016-11-29 04:58:47 -05:00
|
|
|
Test: h.Test,
|
|
|
|
Interval: gogotypes.DurationProto(h.Interval),
|
|
|
|
Timeout: gogotypes.DurationProto(h.Timeout),
|
|
|
|
Retries: int32(h.Retries),
|
|
|
|
StartPeriod: gogotypes.DurationProto(h.StartPeriod),
|
2016-10-13 14:28:32 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 10:07:17 -04:00
|
|
|
|
|
|
|
// IsolationFromGRPC converts a swarm api container isolation to a moby isolation representation
|
|
|
|
func IsolationFromGRPC(i swarmapi.ContainerSpec_Isolation) container.Isolation {
|
|
|
|
switch i {
|
|
|
|
case swarmapi.ContainerIsolationHyperV:
|
|
|
|
return container.IsolationHyperV
|
|
|
|
case swarmapi.ContainerIsolationProcess:
|
|
|
|
return container.IsolationProcess
|
|
|
|
case swarmapi.ContainerIsolationDefault:
|
|
|
|
return container.IsolationDefault
|
|
|
|
}
|
|
|
|
return container.IsolationEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
func isolationToGRPC(i container.Isolation) swarmapi.ContainerSpec_Isolation {
|
|
|
|
if i.IsHyperV() {
|
|
|
|
return swarmapi.ContainerIsolationHyperV
|
|
|
|
}
|
|
|
|
if i.IsProcess() {
|
|
|
|
return swarmapi.ContainerIsolationProcess
|
|
|
|
}
|
|
|
|
return swarmapi.ContainerIsolationDefault
|
|
|
|
}
|