github.com/moby/docker@v26.1.3+incompatible/daemon/cluster/executor/container/container.go (about)

     1  package container // import "github.com/docker/docker/daemon/cluster/executor/container"
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/containerd/log"
    12  	"github.com/distribution/reference"
    13  	"github.com/docker/docker/api/types"
    14  	enginecontainer "github.com/docker/docker/api/types/container"
    15  	"github.com/docker/docker/api/types/events"
    16  	"github.com/docker/docker/api/types/filters"
    17  	enginemount "github.com/docker/docker/api/types/mount"
    18  	"github.com/docker/docker/api/types/network"
    19  	"github.com/docker/docker/api/types/volume"
    20  	"github.com/docker/docker/daemon/cluster/convert"
    21  	executorpkg "github.com/docker/docker/daemon/cluster/executor"
    22  	clustertypes "github.com/docker/docker/daemon/cluster/provider"
    23  	"github.com/docker/docker/libnetwork/scope"
    24  	"github.com/docker/go-connections/nat"
    25  	"github.com/docker/go-units"
    26  	gogotypes "github.com/gogo/protobuf/types"
    27  	"github.com/moby/swarmkit/v2/agent/exec"
    28  	"github.com/moby/swarmkit/v2/api"
    29  	"github.com/moby/swarmkit/v2/api/genericresource"
    30  	"github.com/moby/swarmkit/v2/template"
    31  )
    32  
    33  const (
    34  	// systemLabelPrefix represents the reserved namespace for system labels.
    35  	systemLabelPrefix = "com.docker.swarm"
    36  )
    37  
    38  // containerConfig converts task properties into docker container compatible
    39  // components.
    40  type containerConfig struct {
    41  	task                *api.Task
    42  	networksAttachments map[string]*api.NetworkAttachment
    43  }
    44  
    45  // newContainerConfig returns a validated container config. No methods should
    46  // return an error if this function returns without error.
    47  func newContainerConfig(t *api.Task, node *api.NodeDescription) (*containerConfig, error) {
    48  	var c containerConfig
    49  	return &c, c.setTask(t, node)
    50  }
    51  
    52  func (c *containerConfig) setTask(t *api.Task, node *api.NodeDescription) error {
    53  	if t.Spec.GetContainer() == nil && t.Spec.GetAttachment() == nil {
    54  		return exec.ErrRuntimeUnsupported
    55  	}
    56  
    57  	container := t.Spec.GetContainer()
    58  	if container != nil {
    59  		if container.Image == "" {
    60  			return ErrImageRequired
    61  		}
    62  
    63  		if err := validateMounts(container.Mounts); err != nil {
    64  			return err
    65  		}
    66  	}
    67  
    68  	// index the networks by name
    69  	c.networksAttachments = make(map[string]*api.NetworkAttachment, len(t.Networks))
    70  	for _, attachment := range t.Networks {
    71  		c.networksAttachments[attachment.Network.Spec.Annotations.Name] = attachment
    72  	}
    73  
    74  	c.task = t
    75  
    76  	if t.Spec.GetContainer() != nil {
    77  		preparedSpec, err := template.ExpandContainerSpec(node, t)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		c.task.Spec.Runtime = &api.TaskSpec_Container{
    82  			Container: preparedSpec,
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func (c *containerConfig) networkAttachmentContainerID() string {
    90  	attachment := c.task.Spec.GetAttachment()
    91  	if attachment == nil {
    92  		return ""
    93  	}
    94  
    95  	return attachment.ContainerID
    96  }
    97  
    98  func (c *containerConfig) taskID() string {
    99  	return c.task.ID
   100  }
   101  
   102  func (c *containerConfig) spec() *api.ContainerSpec {
   103  	return c.task.Spec.GetContainer()
   104  }
   105  
   106  func (c *containerConfig) nameOrID() string {
   107  	if c.task.Spec.GetContainer() != nil {
   108  		return c.name()
   109  	}
   110  
   111  	return c.networkAttachmentContainerID()
   112  }
   113  
   114  func (c *containerConfig) name() string {
   115  	if c.task.Annotations.Name != "" {
   116  		// if set, use the container Annotations.Name field, set in the orchestrator.
   117  		return c.task.Annotations.Name
   118  	}
   119  
   120  	slot := fmt.Sprint(c.task.Slot)
   121  	if slot == "" || c.task.Slot == 0 {
   122  		slot = c.task.NodeID
   123  	}
   124  
   125  	// fallback to service.slot.id.
   126  	return fmt.Sprintf("%s.%s.%s", c.task.ServiceAnnotations.Name, slot, c.task.ID)
   127  }
   128  
   129  func (c *containerConfig) image() string {
   130  	raw := c.spec().Image
   131  	ref, err := reference.ParseNormalizedNamed(raw)
   132  	if err != nil {
   133  		return raw
   134  	}
   135  	return reference.FamiliarString(reference.TagNameOnly(ref))
   136  }
   137  
   138  func (c *containerConfig) portBindings() nat.PortMap {
   139  	portBindings := nat.PortMap{}
   140  	if c.task.Endpoint == nil {
   141  		return portBindings
   142  	}
   143  
   144  	for _, portConfig := range c.task.Endpoint.Ports {
   145  		if portConfig.PublishMode != api.PublishModeHost {
   146  			continue
   147  		}
   148  
   149  		port := nat.Port(fmt.Sprintf("%d/%s", portConfig.TargetPort, strings.ToLower(portConfig.Protocol.String())))
   150  		binding := []nat.PortBinding{
   151  			{},
   152  		}
   153  
   154  		if portConfig.PublishedPort != 0 {
   155  			binding[0].HostPort = strconv.Itoa(int(portConfig.PublishedPort))
   156  		}
   157  		portBindings[port] = binding
   158  	}
   159  
   160  	return portBindings
   161  }
   162  
   163  func (c *containerConfig) isolation() enginecontainer.Isolation {
   164  	return convert.IsolationFromGRPC(c.spec().Isolation)
   165  }
   166  
   167  func (c *containerConfig) init() *bool {
   168  	if c.spec().Init == nil {
   169  		return nil
   170  	}
   171  	init := c.spec().Init.GetValue()
   172  	return &init
   173  }
   174  
   175  func (c *containerConfig) exposedPorts() map[nat.Port]struct{} {
   176  	exposedPorts := make(map[nat.Port]struct{})
   177  	if c.task.Endpoint == nil {
   178  		return exposedPorts
   179  	}
   180  
   181  	for _, portConfig := range c.task.Endpoint.Ports {
   182  		if portConfig.PublishMode != api.PublishModeHost {
   183  			continue
   184  		}
   185  
   186  		port := nat.Port(fmt.Sprintf("%d/%s", portConfig.TargetPort, strings.ToLower(portConfig.Protocol.String())))
   187  		exposedPorts[port] = struct{}{}
   188  	}
   189  
   190  	return exposedPorts
   191  }
   192  
   193  func (c *containerConfig) config() *enginecontainer.Config {
   194  	genericEnvs := genericresource.EnvFormat(c.task.AssignedGenericResources, "DOCKER_RESOURCE")
   195  	env := append(c.spec().Env, genericEnvs...)
   196  
   197  	config := &enginecontainer.Config{
   198  		Labels:       c.labels(),
   199  		StopSignal:   c.spec().StopSignal,
   200  		Tty:          c.spec().TTY,
   201  		OpenStdin:    c.spec().OpenStdin,
   202  		User:         c.spec().User,
   203  		Env:          env,
   204  		Hostname:     c.spec().Hostname,
   205  		WorkingDir:   c.spec().Dir,
   206  		Image:        c.image(),
   207  		ExposedPorts: c.exposedPorts(),
   208  		Healthcheck:  c.healthcheck(),
   209  	}
   210  
   211  	if len(c.spec().Command) > 0 {
   212  		// If Command is provided, we replace the whole invocation with Command
   213  		// by replacing Entrypoint and specifying Cmd. Args is ignored in this
   214  		// case.
   215  		config.Entrypoint = append(config.Entrypoint, c.spec().Command...)
   216  		config.Cmd = append(config.Cmd, c.spec().Args...)
   217  	} else if len(c.spec().Args) > 0 {
   218  		// In this case, we assume the image has an Entrypoint and Args
   219  		// specifies the arguments for that entrypoint.
   220  		config.Cmd = c.spec().Args
   221  	}
   222  
   223  	return config
   224  }
   225  
   226  func (c *containerConfig) labels() map[string]string {
   227  	var (
   228  		system = map[string]string{
   229  			"task":         "", // mark as cluster task
   230  			"task.id":      c.task.ID,
   231  			"task.name":    c.name(),
   232  			"node.id":      c.task.NodeID,
   233  			"service.id":   c.task.ServiceID,
   234  			"service.name": c.task.ServiceAnnotations.Name,
   235  		}
   236  		labels = make(map[string]string)
   237  	)
   238  
   239  	// base labels are those defined in the spec.
   240  	for k, v := range c.spec().Labels {
   241  		labels[k] = v
   242  	}
   243  
   244  	// we then apply the overrides from the task, which may be set via the
   245  	// orchestrator.
   246  	for k, v := range c.task.Annotations.Labels {
   247  		labels[k] = v
   248  	}
   249  
   250  	// finally, we apply the system labels, which override all labels.
   251  	for k, v := range system {
   252  		labels[strings.Join([]string{systemLabelPrefix, k}, ".")] = v
   253  	}
   254  
   255  	return labels
   256  }
   257  
   258  func (c *containerConfig) mounts(deps exec.VolumeGetter) []enginemount.Mount {
   259  	var r []enginemount.Mount
   260  	for _, mount := range c.spec().Mounts {
   261  		if mount.Type == api.MountTypeCluster {
   262  			r = append(r, c.convertCSIMount(mount, deps))
   263  		} else {
   264  			r = append(r, convertMount(mount))
   265  		}
   266  	}
   267  	return r
   268  }
   269  
   270  // convertCSIMount matches the CSI mount with the path of the CSI volume.
   271  //
   272  // technically quadratic with respect to the number of CSI mounts, but that
   273  // number shouldn't ever be large enough for quadratic to matter.
   274  //
   275  // TODO(dperny): figure out a scheme for errors? or maybe add code to
   276  // checkMounts?
   277  func (c *containerConfig) convertCSIMount(m api.Mount, deps exec.VolumeGetter) enginemount.Mount {
   278  	var mount enginemount.Mount
   279  
   280  	// these are actually bind mounts
   281  	mount.Type = enginemount.TypeBind
   282  
   283  	for _, attach := range c.task.Volumes {
   284  		if attach.Source == m.Source && attach.Target == m.Target {
   285  			// we should not get an error here, because we should have checked
   286  			// already that the volume is ready
   287  			path, _ := deps.Get(attach.ID)
   288  			mount.Source = path
   289  			mount.Target = m.Target
   290  		}
   291  	}
   292  
   293  	return mount
   294  }
   295  
   296  func convertMount(m api.Mount) enginemount.Mount {
   297  	mount := enginemount.Mount{
   298  		Source:   m.Source,
   299  		Target:   m.Target,
   300  		ReadOnly: m.ReadOnly,
   301  	}
   302  
   303  	switch m.Type {
   304  	case api.MountTypeBind:
   305  		mount.Type = enginemount.TypeBind
   306  	case api.MountTypeVolume:
   307  		mount.Type = enginemount.TypeVolume
   308  	case api.MountTypeTmpfs:
   309  		mount.Type = enginemount.TypeTmpfs
   310  	case api.MountTypeNamedPipe:
   311  		mount.Type = enginemount.TypeNamedPipe
   312  	case api.MountTypeCluster:
   313  		mount.Type = enginemount.TypeCluster
   314  	}
   315  
   316  	if m.BindOptions != nil {
   317  		mount.BindOptions = &enginemount.BindOptions{
   318  			NonRecursive:           m.BindOptions.NonRecursive,
   319  			CreateMountpoint:       m.BindOptions.CreateMountpoint,
   320  			ReadOnlyNonRecursive:   m.BindOptions.ReadOnlyNonRecursive,
   321  			ReadOnlyForceRecursive: m.BindOptions.ReadOnlyForceRecursive,
   322  		}
   323  		switch m.BindOptions.Propagation {
   324  		case api.MountPropagationRPrivate:
   325  			mount.BindOptions.Propagation = enginemount.PropagationRPrivate
   326  		case api.MountPropagationPrivate:
   327  			mount.BindOptions.Propagation = enginemount.PropagationPrivate
   328  		case api.MountPropagationRSlave:
   329  			mount.BindOptions.Propagation = enginemount.PropagationRSlave
   330  		case api.MountPropagationSlave:
   331  			mount.BindOptions.Propagation = enginemount.PropagationSlave
   332  		case api.MountPropagationRShared:
   333  			mount.BindOptions.Propagation = enginemount.PropagationRShared
   334  		case api.MountPropagationShared:
   335  			mount.BindOptions.Propagation = enginemount.PropagationShared
   336  		}
   337  	}
   338  
   339  	if m.VolumeOptions != nil {
   340  		mount.VolumeOptions = &enginemount.VolumeOptions{
   341  			NoCopy:  m.VolumeOptions.NoCopy,
   342  			Subpath: m.VolumeOptions.Subpath,
   343  		}
   344  		if m.VolumeOptions.Labels != nil {
   345  			mount.VolumeOptions.Labels = make(map[string]string, len(m.VolumeOptions.Labels))
   346  			for k, v := range m.VolumeOptions.Labels {
   347  				mount.VolumeOptions.Labels[k] = v
   348  			}
   349  		}
   350  		if m.VolumeOptions.DriverConfig != nil {
   351  			mount.VolumeOptions.DriverConfig = &enginemount.Driver{
   352  				Name: m.VolumeOptions.DriverConfig.Name,
   353  			}
   354  			if m.VolumeOptions.DriverConfig.Options != nil {
   355  				mount.VolumeOptions.DriverConfig.Options = make(map[string]string, len(m.VolumeOptions.DriverConfig.Options))
   356  				for k, v := range m.VolumeOptions.DriverConfig.Options {
   357  					mount.VolumeOptions.DriverConfig.Options[k] = v
   358  				}
   359  			}
   360  		}
   361  	}
   362  
   363  	if m.TmpfsOptions != nil {
   364  		mount.TmpfsOptions = &enginemount.TmpfsOptions{
   365  			SizeBytes: m.TmpfsOptions.SizeBytes,
   366  			Mode:      m.TmpfsOptions.Mode,
   367  		}
   368  	}
   369  
   370  	return mount
   371  }
   372  
   373  func (c *containerConfig) healthcheck() *enginecontainer.HealthConfig {
   374  	hcSpec := c.spec().Healthcheck
   375  	if hcSpec == nil {
   376  		return nil
   377  	}
   378  	interval, _ := gogotypes.DurationFromProto(hcSpec.Interval)
   379  	timeout, _ := gogotypes.DurationFromProto(hcSpec.Timeout)
   380  	startPeriod, _ := gogotypes.DurationFromProto(hcSpec.StartPeriod)
   381  	startInterval, _ := gogotypes.DurationFromProto(hcSpec.StartInterval)
   382  	return &enginecontainer.HealthConfig{
   383  		Test:          hcSpec.Test,
   384  		Interval:      interval,
   385  		Timeout:       timeout,
   386  		Retries:       int(hcSpec.Retries),
   387  		StartPeriod:   startPeriod,
   388  		StartInterval: startInterval,
   389  	}
   390  }
   391  
   392  func (c *containerConfig) hostConfig(deps exec.VolumeGetter) *enginecontainer.HostConfig {
   393  	hc := &enginecontainer.HostConfig{
   394  		Resources:      c.resources(),
   395  		GroupAdd:       c.spec().Groups,
   396  		PortBindings:   c.portBindings(),
   397  		Mounts:         c.mounts(deps),
   398  		ReadonlyRootfs: c.spec().ReadOnly,
   399  		Isolation:      c.isolation(),
   400  		Init:           c.init(),
   401  		Sysctls:        c.spec().Sysctls,
   402  		CapAdd:         c.spec().CapabilityAdd,
   403  		CapDrop:        c.spec().CapabilityDrop,
   404  	}
   405  
   406  	if c.spec().DNSConfig != nil {
   407  		hc.DNS = c.spec().DNSConfig.Nameservers
   408  		hc.DNSSearch = c.spec().DNSConfig.Search
   409  		hc.DNSOptions = c.spec().DNSConfig.Options
   410  	}
   411  
   412  	c.applyPrivileges(hc)
   413  
   414  	// The format of extra hosts on swarmkit is specified in:
   415  	// http://man7.org/linux/man-pages/man5/hosts.5.html
   416  	//    IP_address canonical_hostname [aliases...]
   417  	// However, the format of ExtraHosts in HostConfig is
   418  	//    <host>:<ip>
   419  	// We need to do the conversion here
   420  	// (Alias is ignored for now)
   421  	for _, entry := range c.spec().Hosts {
   422  		parts := strings.Fields(entry)
   423  		if len(parts) > 1 {
   424  			hc.ExtraHosts = append(hc.ExtraHosts, fmt.Sprintf("%s:%s", parts[1], parts[0]))
   425  		}
   426  	}
   427  
   428  	if c.task.LogDriver != nil {
   429  		hc.LogConfig = enginecontainer.LogConfig{
   430  			Type:   c.task.LogDriver.Name,
   431  			Config: c.task.LogDriver.Options,
   432  		}
   433  	}
   434  
   435  	if len(c.task.Networks) > 0 {
   436  		labels := c.task.Networks[0].Network.Spec.Annotations.Labels
   437  		name := c.task.Networks[0].Network.Spec.Annotations.Name
   438  		if v, ok := labels["com.docker.swarm.predefined"]; ok && v == "true" {
   439  			hc.NetworkMode = enginecontainer.NetworkMode(name)
   440  		}
   441  	}
   442  
   443  	return hc
   444  }
   445  
   446  // This handles the case of volumes that are defined inside a service Mount
   447  func (c *containerConfig) volumeCreateRequest(mount *api.Mount) *volume.CreateOptions {
   448  	var (
   449  		driverName string
   450  		driverOpts map[string]string
   451  		labels     map[string]string
   452  	)
   453  
   454  	if mount.VolumeOptions != nil && mount.VolumeOptions.DriverConfig != nil {
   455  		driverName = mount.VolumeOptions.DriverConfig.Name
   456  		driverOpts = mount.VolumeOptions.DriverConfig.Options
   457  		labels = mount.VolumeOptions.Labels
   458  	}
   459  
   460  	if mount.VolumeOptions != nil {
   461  		return &volume.CreateOptions{
   462  			Name:       mount.Source,
   463  			Driver:     driverName,
   464  			DriverOpts: driverOpts,
   465  			Labels:     labels,
   466  		}
   467  	}
   468  	return nil
   469  }
   470  
   471  func (c *containerConfig) resources() enginecontainer.Resources {
   472  	resources := enginecontainer.Resources{}
   473  
   474  	// set pids limit
   475  	pidsLimit := c.spec().PidsLimit
   476  	if pidsLimit > 0 {
   477  		resources.PidsLimit = &pidsLimit
   478  	}
   479  
   480  	resources.Ulimits = make([]*units.Ulimit, len(c.spec().Ulimits))
   481  	for i, ulimit := range c.spec().Ulimits {
   482  		resources.Ulimits[i] = &units.Ulimit{
   483  			Name: ulimit.Name,
   484  			Soft: ulimit.Soft,
   485  			Hard: ulimit.Hard,
   486  		}
   487  	}
   488  
   489  	// If no limits are specified let the engine use its defaults.
   490  	//
   491  	// TODO(aluzzardi): We might want to set some limits anyway otherwise
   492  	// "unlimited" tasks will step over the reservation of other tasks.
   493  	r := c.task.Spec.Resources
   494  	if r == nil || r.Limits == nil {
   495  		return resources
   496  	}
   497  
   498  	if r.Limits.MemoryBytes > 0 {
   499  		resources.Memory = r.Limits.MemoryBytes
   500  	}
   501  
   502  	if r.Limits.NanoCPUs > 0 {
   503  		resources.NanoCPUs = r.Limits.NanoCPUs
   504  	}
   505  
   506  	return resources
   507  }
   508  
   509  func (c *containerConfig) createNetworkingConfig(b executorpkg.Backend) *network.NetworkingConfig {
   510  	var networks []*api.NetworkAttachment
   511  	if c.task.Spec.GetContainer() != nil || c.task.Spec.GetAttachment() != nil {
   512  		networks = c.task.Networks
   513  	}
   514  
   515  	epConfig := make(map[string]*network.EndpointSettings)
   516  	for _, na := range networks {
   517  		epConfig[na.Network.Spec.Annotations.Name] = getEndpointConfig(na, b)
   518  	}
   519  
   520  	return &network.NetworkingConfig{EndpointsConfig: epConfig}
   521  }
   522  
   523  func getEndpointConfig(na *api.NetworkAttachment, b executorpkg.Backend) *network.EndpointSettings {
   524  	var ipv4, ipv6 string
   525  	for _, addr := range na.Addresses {
   526  		ip, _, err := net.ParseCIDR(addr)
   527  		if err != nil {
   528  			continue
   529  		}
   530  
   531  		if ip.To4() != nil {
   532  			ipv4 = ip.String()
   533  			continue
   534  		}
   535  
   536  		if ip.To16() != nil {
   537  			ipv6 = ip.String()
   538  		}
   539  	}
   540  
   541  	n := &network.EndpointSettings{
   542  		NetworkID: na.Network.ID,
   543  		IPAMConfig: &network.EndpointIPAMConfig{
   544  			IPv4Address: ipv4,
   545  			IPv6Address: ipv6,
   546  		},
   547  		DriverOpts: na.DriverAttachmentOpts,
   548  	}
   549  	if v, ok := na.Network.Spec.Annotations.Labels["com.docker.swarm.predefined"]; ok && v == "true" {
   550  		if ln, err := b.FindNetwork(na.Network.Spec.Annotations.Name); err == nil {
   551  			n.NetworkID = ln.ID()
   552  		}
   553  	}
   554  	return n
   555  }
   556  
   557  func (c *containerConfig) virtualIP(networkID string) string {
   558  	if c.task.Endpoint == nil {
   559  		return ""
   560  	}
   561  
   562  	for _, eVip := range c.task.Endpoint.VirtualIPs {
   563  		// We only support IPv4 VIPs for now.
   564  		if eVip.NetworkID == networkID {
   565  			vip, _, err := net.ParseCIDR(eVip.Addr)
   566  			if err != nil {
   567  				return ""
   568  			}
   569  
   570  			return vip.String()
   571  		}
   572  	}
   573  
   574  	return ""
   575  }
   576  
   577  func (c *containerConfig) serviceConfig() *clustertypes.ServiceConfig {
   578  	if len(c.task.Networks) == 0 {
   579  		return nil
   580  	}
   581  
   582  	log.G(context.TODO()).Debugf("Creating service config in agent for t = %+v", c.task)
   583  	svcCfg := &clustertypes.ServiceConfig{
   584  		Name:             c.task.ServiceAnnotations.Name,
   585  		Aliases:          make(map[string][]string),
   586  		ID:               c.task.ServiceID,
   587  		VirtualAddresses: make(map[string]*clustertypes.VirtualAddress),
   588  	}
   589  
   590  	for _, na := range c.task.Networks {
   591  		svcCfg.VirtualAddresses[na.Network.ID] = &clustertypes.VirtualAddress{
   592  			// We support only IPv4 virtual IP for now.
   593  			IPv4: c.virtualIP(na.Network.ID),
   594  		}
   595  		if len(na.Aliases) > 0 {
   596  			svcCfg.Aliases[na.Network.ID] = na.Aliases
   597  		}
   598  	}
   599  
   600  	if c.task.Endpoint != nil {
   601  		for _, ePort := range c.task.Endpoint.Ports {
   602  			if ePort.PublishMode != api.PublishModeIngress {
   603  				continue
   604  			}
   605  
   606  			svcCfg.ExposedPorts = append(svcCfg.ExposedPorts, &clustertypes.PortConfig{
   607  				Name:          ePort.Name,
   608  				Protocol:      int32(ePort.Protocol),
   609  				TargetPort:    ePort.TargetPort,
   610  				PublishedPort: ePort.PublishedPort,
   611  			})
   612  		}
   613  	}
   614  
   615  	return svcCfg
   616  }
   617  
   618  func (c *containerConfig) networkCreateRequest(name string) (clustertypes.NetworkCreateRequest, error) {
   619  	na, ok := c.networksAttachments[name]
   620  	if !ok {
   621  		return clustertypes.NetworkCreateRequest{}, errors.New("container: unknown network referenced")
   622  	}
   623  
   624  	options := types.NetworkCreate{
   625  		// ID:     na.Network.ID,
   626  		Labels:     na.Network.Spec.Annotations.Labels,
   627  		Internal:   na.Network.Spec.Internal,
   628  		Attachable: na.Network.Spec.Attachable,
   629  		Ingress:    convert.IsIngressNetwork(na.Network),
   630  		EnableIPv6: na.Network.Spec.Ipv6Enabled,
   631  		Scope:      scope.Swarm,
   632  	}
   633  
   634  	if na.Network.Spec.GetNetwork() != "" {
   635  		options.ConfigFrom = &network.ConfigReference{
   636  			Network: na.Network.Spec.GetNetwork(),
   637  		}
   638  	}
   639  
   640  	if na.Network.DriverState != nil {
   641  		options.Driver = na.Network.DriverState.Name
   642  		options.Options = na.Network.DriverState.Options
   643  	}
   644  	if na.Network.IPAM != nil {
   645  		options.IPAM = &network.IPAM{
   646  			Driver:  na.Network.IPAM.Driver.Name,
   647  			Options: na.Network.IPAM.Driver.Options,
   648  		}
   649  		for _, ic := range na.Network.IPAM.Configs {
   650  			c := network.IPAMConfig{
   651  				Subnet:  ic.Subnet,
   652  				IPRange: ic.Range,
   653  				Gateway: ic.Gateway,
   654  			}
   655  			options.IPAM.Config = append(options.IPAM.Config, c)
   656  		}
   657  	}
   658  
   659  	return clustertypes.NetworkCreateRequest{
   660  		ID: na.Network.ID,
   661  		NetworkCreateRequest: types.NetworkCreateRequest{
   662  			Name:          name,
   663  			NetworkCreate: options,
   664  		},
   665  	}, nil
   666  }
   667  
   668  func (c *containerConfig) applyPrivileges(hc *enginecontainer.HostConfig) {
   669  	privileges := c.spec().Privileges
   670  	if privileges == nil {
   671  		return
   672  	}
   673  
   674  	credentials := privileges.CredentialSpec
   675  	if credentials != nil {
   676  		switch credentials.Source.(type) {
   677  		case *api.Privileges_CredentialSpec_File:
   678  			hc.SecurityOpt = append(hc.SecurityOpt, "credentialspec=file://"+credentials.GetFile())
   679  		case *api.Privileges_CredentialSpec_Registry:
   680  			hc.SecurityOpt = append(hc.SecurityOpt, "credentialspec=registry://"+credentials.GetRegistry())
   681  		case *api.Privileges_CredentialSpec_Config:
   682  			hc.SecurityOpt = append(hc.SecurityOpt, "credentialspec=config://"+credentials.GetConfig())
   683  		}
   684  	}
   685  
   686  	selinux := privileges.SELinuxContext
   687  	if selinux != nil {
   688  		if selinux.Disable {
   689  			hc.SecurityOpt = append(hc.SecurityOpt, "label=disable")
   690  		}
   691  		if selinux.User != "" {
   692  			hc.SecurityOpt = append(hc.SecurityOpt, "label=user:"+selinux.User)
   693  		}
   694  		if selinux.Role != "" {
   695  			hc.SecurityOpt = append(hc.SecurityOpt, "label=role:"+selinux.Role)
   696  		}
   697  		if selinux.Level != "" {
   698  			hc.SecurityOpt = append(hc.SecurityOpt, "label=level:"+selinux.Level)
   699  		}
   700  		if selinux.Type != "" {
   701  			hc.SecurityOpt = append(hc.SecurityOpt, "label=type:"+selinux.Type)
   702  		}
   703  	}
   704  
   705  	// variable to make the lines shorter and easier to read
   706  	if seccomp := privileges.Seccomp; seccomp != nil {
   707  		switch seccomp.Mode {
   708  		// case api.Privileges_SeccompOpts_DEFAULT:
   709  		//   if the setting is default, nothing needs to be set here. we leave
   710  		//   the option empty.
   711  		case api.Privileges_SeccompOpts_UNCONFINED:
   712  			hc.SecurityOpt = append(hc.SecurityOpt, "seccomp=unconfined")
   713  		case api.Privileges_SeccompOpts_CUSTOM:
   714  			// Profile is bytes, but those bytes are actually a string. This is
   715  			// basically verbatim what happens in the cli after a file is read.
   716  			hc.SecurityOpt = append(hc.SecurityOpt, fmt.Sprintf("seccomp=%s", seccomp.Profile))
   717  		}
   718  	}
   719  
   720  	// if the setting is DEFAULT, then nothing to be done. If it's DISABLED,
   721  	// we set that. Custom not supported yet. When custom *is* supported, make
   722  	// it look like the above.
   723  	if apparmor := privileges.Apparmor; apparmor != nil && apparmor.Mode == api.Privileges_AppArmorOpts_DISABLED {
   724  		hc.SecurityOpt = append(hc.SecurityOpt, "apparmor=unconfined")
   725  	}
   726  
   727  	if privileges.NoNewPrivileges {
   728  		hc.SecurityOpt = append(hc.SecurityOpt, "no-new-privileges=true")
   729  	}
   730  }
   731  
   732  func (c *containerConfig) eventFilter() filters.Args {
   733  	return filters.NewArgs(
   734  		filters.Arg("type", string(events.ContainerEventType)),
   735  		filters.Arg("name", c.name()),
   736  		filters.Arg("label", fmt.Sprintf("%v.task.id=%v", systemLabelPrefix, c.task.ID)),
   737  	)
   738  }