github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/daemon/cluster/convert/container.go (about)

     1  package convert
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/Sirupsen/logrus"
     8  	container "github.com/docker/docker/api/types/container"
     9  	mounttypes "github.com/docker/docker/api/types/mount"
    10  	types "github.com/docker/docker/api/types/swarm"
    11  	swarmapi "github.com/docker/swarmkit/api"
    12  	gogotypes "github.com/gogo/protobuf/types"
    13  )
    14  
    15  func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec {
    16  	containerSpec := types.ContainerSpec{
    17  		Image:     c.Image,
    18  		Labels:    c.Labels,
    19  		Command:   c.Command,
    20  		Args:      c.Args,
    21  		Hostname:  c.Hostname,
    22  		Env:       c.Env,
    23  		Dir:       c.Dir,
    24  		User:      c.User,
    25  		Groups:    c.Groups,
    26  		TTY:       c.TTY,
    27  		OpenStdin: c.OpenStdin,
    28  		ReadOnly:  c.ReadOnly,
    29  		Hosts:     c.Hosts,
    30  		Secrets:   secretReferencesFromGRPC(c.Secrets),
    31  	}
    32  
    33  	if c.DNSConfig != nil {
    34  		containerSpec.DNSConfig = &types.DNSConfig{
    35  			Nameservers: c.DNSConfig.Nameservers,
    36  			Search:      c.DNSConfig.Search,
    37  			Options:     c.DNSConfig.Options,
    38  		}
    39  	}
    40  
    41  	// Mounts
    42  	for _, m := range c.Mounts {
    43  		mount := mounttypes.Mount{
    44  			Target:   m.Target,
    45  			Source:   m.Source,
    46  			Type:     mounttypes.Type(strings.ToLower(swarmapi.Mount_MountType_name[int32(m.Type)])),
    47  			ReadOnly: m.ReadOnly,
    48  		}
    49  
    50  		if m.BindOptions != nil {
    51  			mount.BindOptions = &mounttypes.BindOptions{
    52  				Propagation: mounttypes.Propagation(strings.ToLower(swarmapi.Mount_BindOptions_MountPropagation_name[int32(m.BindOptions.Propagation)])),
    53  			}
    54  		}
    55  
    56  		if m.VolumeOptions != nil {
    57  			mount.VolumeOptions = &mounttypes.VolumeOptions{
    58  				NoCopy: m.VolumeOptions.NoCopy,
    59  				Labels: m.VolumeOptions.Labels,
    60  			}
    61  			if m.VolumeOptions.DriverConfig != nil {
    62  				mount.VolumeOptions.DriverConfig = &mounttypes.Driver{
    63  					Name:    m.VolumeOptions.DriverConfig.Name,
    64  					Options: m.VolumeOptions.DriverConfig.Options,
    65  				}
    66  			}
    67  		}
    68  
    69  		if m.TmpfsOptions != nil {
    70  			mount.TmpfsOptions = &mounttypes.TmpfsOptions{
    71  				SizeBytes: m.TmpfsOptions.SizeBytes,
    72  				Mode:      m.TmpfsOptions.Mode,
    73  			}
    74  		}
    75  		containerSpec.Mounts = append(containerSpec.Mounts, mount)
    76  	}
    77  
    78  	if c.StopGracePeriod != nil {
    79  		grace, _ := gogotypes.DurationFromProto(c.StopGracePeriod)
    80  		containerSpec.StopGracePeriod = &grace
    81  	}
    82  
    83  	if c.Healthcheck != nil {
    84  		containerSpec.Healthcheck = healthConfigFromGRPC(c.Healthcheck)
    85  	}
    86  
    87  	return containerSpec
    88  }
    89  
    90  func secretReferencesToGRPC(sr []*types.SecretReference) []*swarmapi.SecretReference {
    91  	refs := make([]*swarmapi.SecretReference, 0, len(sr))
    92  	for _, s := range sr {
    93  		ref := &swarmapi.SecretReference{
    94  			SecretID:   s.SecretID,
    95  			SecretName: s.SecretName,
    96  		}
    97  		if s.File != nil {
    98  			ref.Target = &swarmapi.SecretReference_File{
    99  				File: &swarmapi.SecretReference_FileTarget{
   100  					Name: s.File.Name,
   101  					UID:  s.File.UID,
   102  					GID:  s.File.GID,
   103  					Mode: s.File.Mode,
   104  				},
   105  			}
   106  		}
   107  
   108  		refs = append(refs, ref)
   109  	}
   110  
   111  	return refs
   112  }
   113  func secretReferencesFromGRPC(sr []*swarmapi.SecretReference) []*types.SecretReference {
   114  	refs := make([]*types.SecretReference, 0, len(sr))
   115  	for _, s := range sr {
   116  		target := s.GetFile()
   117  		if target == nil {
   118  			// not a file target
   119  			logrus.Warnf("secret target not a file: secret=%s", s.SecretID)
   120  			continue
   121  		}
   122  		refs = append(refs, &types.SecretReference{
   123  			File: &types.SecretReferenceFileTarget{
   124  				Name: target.Name,
   125  				UID:  target.UID,
   126  				GID:  target.GID,
   127  				Mode: target.Mode,
   128  			},
   129  			SecretID:   s.SecretID,
   130  			SecretName: s.SecretName,
   131  		})
   132  	}
   133  
   134  	return refs
   135  }
   136  
   137  func containerToGRPC(c types.ContainerSpec) (*swarmapi.ContainerSpec, error) {
   138  	containerSpec := &swarmapi.ContainerSpec{
   139  		Image:     c.Image,
   140  		Labels:    c.Labels,
   141  		Command:   c.Command,
   142  		Args:      c.Args,
   143  		Hostname:  c.Hostname,
   144  		Env:       c.Env,
   145  		Dir:       c.Dir,
   146  		User:      c.User,
   147  		Groups:    c.Groups,
   148  		TTY:       c.TTY,
   149  		OpenStdin: c.OpenStdin,
   150  		ReadOnly:  c.ReadOnly,
   151  		Hosts:     c.Hosts,
   152  		Secrets:   secretReferencesToGRPC(c.Secrets),
   153  	}
   154  
   155  	if c.DNSConfig != nil {
   156  		containerSpec.DNSConfig = &swarmapi.ContainerSpec_DNSConfig{
   157  			Nameservers: c.DNSConfig.Nameservers,
   158  			Search:      c.DNSConfig.Search,
   159  			Options:     c.DNSConfig.Options,
   160  		}
   161  	}
   162  
   163  	if c.StopGracePeriod != nil {
   164  		containerSpec.StopGracePeriod = gogotypes.DurationProto(*c.StopGracePeriod)
   165  	}
   166  
   167  	// Mounts
   168  	for _, m := range c.Mounts {
   169  		mount := swarmapi.Mount{
   170  			Target:   m.Target,
   171  			Source:   m.Source,
   172  			ReadOnly: m.ReadOnly,
   173  		}
   174  
   175  		if mountType, ok := swarmapi.Mount_MountType_value[strings.ToUpper(string(m.Type))]; ok {
   176  			mount.Type = swarmapi.Mount_MountType(mountType)
   177  		} else if string(m.Type) != "" {
   178  			return nil, fmt.Errorf("invalid MountType: %q", m.Type)
   179  		}
   180  
   181  		if m.BindOptions != nil {
   182  			if mountPropagation, ok := swarmapi.Mount_BindOptions_MountPropagation_value[strings.ToUpper(string(m.BindOptions.Propagation))]; ok {
   183  				mount.BindOptions = &swarmapi.Mount_BindOptions{Propagation: swarmapi.Mount_BindOptions_MountPropagation(mountPropagation)}
   184  			} else if string(m.BindOptions.Propagation) != "" {
   185  				return nil, fmt.Errorf("invalid MountPropagation: %q", m.BindOptions.Propagation)
   186  			}
   187  		}
   188  
   189  		if m.VolumeOptions != nil {
   190  			mount.VolumeOptions = &swarmapi.Mount_VolumeOptions{
   191  				NoCopy: m.VolumeOptions.NoCopy,
   192  				Labels: m.VolumeOptions.Labels,
   193  			}
   194  			if m.VolumeOptions.DriverConfig != nil {
   195  				mount.VolumeOptions.DriverConfig = &swarmapi.Driver{
   196  					Name:    m.VolumeOptions.DriverConfig.Name,
   197  					Options: m.VolumeOptions.DriverConfig.Options,
   198  				}
   199  			}
   200  		}
   201  
   202  		if m.TmpfsOptions != nil {
   203  			mount.TmpfsOptions = &swarmapi.Mount_TmpfsOptions{
   204  				SizeBytes: m.TmpfsOptions.SizeBytes,
   205  				Mode:      m.TmpfsOptions.Mode,
   206  			}
   207  		}
   208  
   209  		containerSpec.Mounts = append(containerSpec.Mounts, mount)
   210  	}
   211  
   212  	if c.Healthcheck != nil {
   213  		containerSpec.Healthcheck = healthConfigToGRPC(c.Healthcheck)
   214  	}
   215  
   216  	return containerSpec, nil
   217  }
   218  
   219  func healthConfigFromGRPC(h *swarmapi.HealthConfig) *container.HealthConfig {
   220  	interval, _ := gogotypes.DurationFromProto(h.Interval)
   221  	timeout, _ := gogotypes.DurationFromProto(h.Timeout)
   222  	return &container.HealthConfig{
   223  		Test:     h.Test,
   224  		Interval: interval,
   225  		Timeout:  timeout,
   226  		Retries:  int(h.Retries),
   227  	}
   228  }
   229  
   230  func healthConfigToGRPC(h *container.HealthConfig) *swarmapi.HealthConfig {
   231  	return &swarmapi.HealthConfig{
   232  		Test:     h.Test,
   233  		Interval: gogotypes.DurationProto(h.Interval),
   234  		Timeout:  gogotypes.DurationProto(h.Timeout),
   235  		Retries:  int32(h.Retries),
   236  	}
   237  }