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