container.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. package container
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/sirupsen/logrus"
  10. "github.com/docker/distribution/reference"
  11. "github.com/docker/docker/api/types"
  12. enginecontainer "github.com/docker/docker/api/types/container"
  13. "github.com/docker/docker/api/types/events"
  14. "github.com/docker/docker/api/types/filters"
  15. enginemount "github.com/docker/docker/api/types/mount"
  16. "github.com/docker/docker/api/types/network"
  17. volumetypes "github.com/docker/docker/api/types/volume"
  18. "github.com/docker/docker/daemon/cluster/convert"
  19. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  20. clustertypes "github.com/docker/docker/daemon/cluster/provider"
  21. "github.com/docker/go-connections/nat"
  22. netconst "github.com/docker/libnetwork/datastore"
  23. "github.com/docker/swarmkit/agent/exec"
  24. "github.com/docker/swarmkit/api"
  25. "github.com/docker/swarmkit/api/genericresource"
  26. "github.com/docker/swarmkit/template"
  27. gogotypes "github.com/gogo/protobuf/types"
  28. )
  29. const (
  30. // Explicitly use the kernel's default setting for CPU quota of 100ms.
  31. // https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt
  32. cpuQuotaPeriod = 100 * time.Millisecond
  33. // systemLabelPrefix represents the reserved namespace for system labels.
  34. systemLabelPrefix = "com.docker.swarm"
  35. )
  36. // containerConfig converts task properties into docker container compatible
  37. // components.
  38. type containerConfig struct {
  39. task *api.Task
  40. networksAttachments map[string]*api.NetworkAttachment
  41. }
  42. // newContainerConfig returns a validated container config. No methods should
  43. // return an error if this function returns without error.
  44. func newContainerConfig(t *api.Task, node *api.NodeDescription) (*containerConfig, error) {
  45. var c containerConfig
  46. return &c, c.setTask(t, node)
  47. }
  48. func (c *containerConfig) setTask(t *api.Task, node *api.NodeDescription) error {
  49. if t.Spec.GetContainer() == nil && t.Spec.GetAttachment() == nil {
  50. return exec.ErrRuntimeUnsupported
  51. }
  52. container := t.Spec.GetContainer()
  53. if container != nil {
  54. if container.Image == "" {
  55. return ErrImageRequired
  56. }
  57. if err := validateMounts(container.Mounts); err != nil {
  58. return err
  59. }
  60. }
  61. // index the networks by name
  62. c.networksAttachments = make(map[string]*api.NetworkAttachment, len(t.Networks))
  63. for _, attachment := range t.Networks {
  64. c.networksAttachments[attachment.Network.Spec.Annotations.Name] = attachment
  65. }
  66. c.task = t
  67. if t.Spec.GetContainer() != nil {
  68. preparedSpec, err := template.ExpandContainerSpec(node, t)
  69. if err != nil {
  70. return err
  71. }
  72. c.task.Spec.Runtime = &api.TaskSpec_Container{
  73. Container: preparedSpec,
  74. }
  75. }
  76. return nil
  77. }
  78. func (c *containerConfig) networkAttachmentContainerID() string {
  79. attachment := c.task.Spec.GetAttachment()
  80. if attachment == nil {
  81. return ""
  82. }
  83. return attachment.ContainerID
  84. }
  85. func (c *containerConfig) taskID() string {
  86. return c.task.ID
  87. }
  88. func (c *containerConfig) endpoint() *api.Endpoint {
  89. return c.task.Endpoint
  90. }
  91. func (c *containerConfig) spec() *api.ContainerSpec {
  92. return c.task.Spec.GetContainer()
  93. }
  94. func (c *containerConfig) nameOrID() string {
  95. if c.task.Spec.GetContainer() != nil {
  96. return c.name()
  97. }
  98. return c.networkAttachmentContainerID()
  99. }
  100. func (c *containerConfig) name() string {
  101. if c.task.Annotations.Name != "" {
  102. // if set, use the container Annotations.Name field, set in the orchestrator.
  103. return c.task.Annotations.Name
  104. }
  105. slot := fmt.Sprint(c.task.Slot)
  106. if slot == "" || c.task.Slot == 0 {
  107. slot = c.task.NodeID
  108. }
  109. // fallback to service.slot.id.
  110. return fmt.Sprintf("%s.%s.%s", c.task.ServiceAnnotations.Name, slot, c.task.ID)
  111. }
  112. func (c *containerConfig) image() string {
  113. raw := c.spec().Image
  114. ref, err := reference.ParseNormalizedNamed(raw)
  115. if err != nil {
  116. return raw
  117. }
  118. return reference.FamiliarString(reference.TagNameOnly(ref))
  119. }
  120. func (c *containerConfig) portBindings() nat.PortMap {
  121. portBindings := nat.PortMap{}
  122. if c.task.Endpoint == nil {
  123. return portBindings
  124. }
  125. for _, portConfig := range c.task.Endpoint.Ports {
  126. if portConfig.PublishMode != api.PublishModeHost {
  127. continue
  128. }
  129. port := nat.Port(fmt.Sprintf("%d/%s", portConfig.TargetPort, strings.ToLower(portConfig.Protocol.String())))
  130. binding := []nat.PortBinding{
  131. {},
  132. }
  133. if portConfig.PublishedPort != 0 {
  134. binding[0].HostPort = strconv.Itoa(int(portConfig.PublishedPort))
  135. }
  136. portBindings[port] = binding
  137. }
  138. return portBindings
  139. }
  140. func (c *containerConfig) exposedPorts() map[nat.Port]struct{} {
  141. exposedPorts := make(map[nat.Port]struct{})
  142. if c.task.Endpoint == nil {
  143. return exposedPorts
  144. }
  145. for _, portConfig := range c.task.Endpoint.Ports {
  146. if portConfig.PublishMode != api.PublishModeHost {
  147. continue
  148. }
  149. port := nat.Port(fmt.Sprintf("%d/%s", portConfig.TargetPort, strings.ToLower(portConfig.Protocol.String())))
  150. exposedPorts[port] = struct{}{}
  151. }
  152. return exposedPorts
  153. }
  154. func (c *containerConfig) config() *enginecontainer.Config {
  155. genericEnvs := genericresource.EnvFormat(c.task.AssignedGenericResources, "DOCKER_RESOURCE")
  156. env := append(c.spec().Env, genericEnvs...)
  157. config := &enginecontainer.Config{
  158. Labels: c.labels(),
  159. StopSignal: c.spec().StopSignal,
  160. Tty: c.spec().TTY,
  161. OpenStdin: c.spec().OpenStdin,
  162. User: c.spec().User,
  163. Env: env,
  164. Hostname: c.spec().Hostname,
  165. WorkingDir: c.spec().Dir,
  166. Image: c.image(),
  167. ExposedPorts: c.exposedPorts(),
  168. Healthcheck: c.healthcheck(),
  169. }
  170. if len(c.spec().Command) > 0 {
  171. // If Command is provided, we replace the whole invocation with Command
  172. // by replacing Entrypoint and specifying Cmd. Args is ignored in this
  173. // case.
  174. config.Entrypoint = append(config.Entrypoint, c.spec().Command...)
  175. config.Cmd = append(config.Cmd, c.spec().Args...)
  176. } else if len(c.spec().Args) > 0 {
  177. // In this case, we assume the image has an Entrypoint and Args
  178. // specifies the arguments for that entrypoint.
  179. config.Cmd = c.spec().Args
  180. }
  181. return config
  182. }
  183. func (c *containerConfig) labels() map[string]string {
  184. var (
  185. system = map[string]string{
  186. "task": "", // mark as cluster task
  187. "task.id": c.task.ID,
  188. "task.name": c.name(),
  189. "node.id": c.task.NodeID,
  190. "service.id": c.task.ServiceID,
  191. "service.name": c.task.ServiceAnnotations.Name,
  192. }
  193. labels = make(map[string]string)
  194. )
  195. // base labels are those defined in the spec.
  196. for k, v := range c.spec().Labels {
  197. labels[k] = v
  198. }
  199. // we then apply the overrides from the task, which may be set via the
  200. // orchestrator.
  201. for k, v := range c.task.Annotations.Labels {
  202. labels[k] = v
  203. }
  204. // finally, we apply the system labels, which override all labels.
  205. for k, v := range system {
  206. labels[strings.Join([]string{systemLabelPrefix, k}, ".")] = v
  207. }
  208. return labels
  209. }
  210. func (c *containerConfig) mounts() []enginemount.Mount {
  211. var r []enginemount.Mount
  212. for _, mount := range c.spec().Mounts {
  213. r = append(r, convertMount(mount))
  214. }
  215. return r
  216. }
  217. func convertMount(m api.Mount) enginemount.Mount {
  218. mount := enginemount.Mount{
  219. Source: m.Source,
  220. Target: m.Target,
  221. ReadOnly: m.ReadOnly,
  222. }
  223. switch m.Type {
  224. case api.MountTypeBind:
  225. mount.Type = enginemount.TypeBind
  226. case api.MountTypeVolume:
  227. mount.Type = enginemount.TypeVolume
  228. case api.MountTypeTmpfs:
  229. mount.Type = enginemount.TypeTmpfs
  230. }
  231. if m.BindOptions != nil {
  232. mount.BindOptions = &enginemount.BindOptions{}
  233. switch m.BindOptions.Propagation {
  234. case api.MountPropagationRPrivate:
  235. mount.BindOptions.Propagation = enginemount.PropagationRPrivate
  236. case api.MountPropagationPrivate:
  237. mount.BindOptions.Propagation = enginemount.PropagationPrivate
  238. case api.MountPropagationRSlave:
  239. mount.BindOptions.Propagation = enginemount.PropagationRSlave
  240. case api.MountPropagationSlave:
  241. mount.BindOptions.Propagation = enginemount.PropagationSlave
  242. case api.MountPropagationRShared:
  243. mount.BindOptions.Propagation = enginemount.PropagationRShared
  244. case api.MountPropagationShared:
  245. mount.BindOptions.Propagation = enginemount.PropagationShared
  246. }
  247. }
  248. if m.VolumeOptions != nil {
  249. mount.VolumeOptions = &enginemount.VolumeOptions{
  250. NoCopy: m.VolumeOptions.NoCopy,
  251. }
  252. if m.VolumeOptions.Labels != nil {
  253. mount.VolumeOptions.Labels = make(map[string]string, len(m.VolumeOptions.Labels))
  254. for k, v := range m.VolumeOptions.Labels {
  255. mount.VolumeOptions.Labels[k] = v
  256. }
  257. }
  258. if m.VolumeOptions.DriverConfig != nil {
  259. mount.VolumeOptions.DriverConfig = &enginemount.Driver{
  260. Name: m.VolumeOptions.DriverConfig.Name,
  261. }
  262. if m.VolumeOptions.DriverConfig.Options != nil {
  263. mount.VolumeOptions.DriverConfig.Options = make(map[string]string, len(m.VolumeOptions.DriverConfig.Options))
  264. for k, v := range m.VolumeOptions.DriverConfig.Options {
  265. mount.VolumeOptions.DriverConfig.Options[k] = v
  266. }
  267. }
  268. }
  269. }
  270. if m.TmpfsOptions != nil {
  271. mount.TmpfsOptions = &enginemount.TmpfsOptions{
  272. SizeBytes: m.TmpfsOptions.SizeBytes,
  273. Mode: m.TmpfsOptions.Mode,
  274. }
  275. }
  276. return mount
  277. }
  278. func (c *containerConfig) healthcheck() *enginecontainer.HealthConfig {
  279. hcSpec := c.spec().Healthcheck
  280. if hcSpec == nil {
  281. return nil
  282. }
  283. interval, _ := gogotypes.DurationFromProto(hcSpec.Interval)
  284. timeout, _ := gogotypes.DurationFromProto(hcSpec.Timeout)
  285. startPeriod, _ := gogotypes.DurationFromProto(hcSpec.StartPeriod)
  286. return &enginecontainer.HealthConfig{
  287. Test: hcSpec.Test,
  288. Interval: interval,
  289. Timeout: timeout,
  290. Retries: int(hcSpec.Retries),
  291. StartPeriod: startPeriod,
  292. }
  293. }
  294. func (c *containerConfig) hostConfig() *enginecontainer.HostConfig {
  295. hc := &enginecontainer.HostConfig{
  296. Resources: c.resources(),
  297. GroupAdd: c.spec().Groups,
  298. PortBindings: c.portBindings(),
  299. Mounts: c.mounts(),
  300. ReadonlyRootfs: c.spec().ReadOnly,
  301. }
  302. if c.spec().DNSConfig != nil {
  303. hc.DNS = c.spec().DNSConfig.Nameservers
  304. hc.DNSSearch = c.spec().DNSConfig.Search
  305. hc.DNSOptions = c.spec().DNSConfig.Options
  306. }
  307. c.applyPrivileges(hc)
  308. // The format of extra hosts on swarmkit is specified in:
  309. // http://man7.org/linux/man-pages/man5/hosts.5.html
  310. // IP_address canonical_hostname [aliases...]
  311. // However, the format of ExtraHosts in HostConfig is
  312. // <host>:<ip>
  313. // We need to do the conversion here
  314. // (Alias is ignored for now)
  315. for _, entry := range c.spec().Hosts {
  316. parts := strings.Fields(entry)
  317. if len(parts) > 1 {
  318. hc.ExtraHosts = append(hc.ExtraHosts, fmt.Sprintf("%s:%s", parts[1], parts[0]))
  319. }
  320. }
  321. if c.task.LogDriver != nil {
  322. hc.LogConfig = enginecontainer.LogConfig{
  323. Type: c.task.LogDriver.Name,
  324. Config: c.task.LogDriver.Options,
  325. }
  326. }
  327. if len(c.task.Networks) > 0 {
  328. labels := c.task.Networks[0].Network.Spec.Annotations.Labels
  329. name := c.task.Networks[0].Network.Spec.Annotations.Name
  330. if v, ok := labels["com.docker.swarm.predefined"]; ok && v == "true" {
  331. hc.NetworkMode = enginecontainer.NetworkMode(name)
  332. }
  333. }
  334. return hc
  335. }
  336. // This handles the case of volumes that are defined inside a service Mount
  337. func (c *containerConfig) volumeCreateRequest(mount *api.Mount) *volumetypes.VolumesCreateBody {
  338. var (
  339. driverName string
  340. driverOpts map[string]string
  341. labels map[string]string
  342. )
  343. if mount.VolumeOptions != nil && mount.VolumeOptions.DriverConfig != nil {
  344. driverName = mount.VolumeOptions.DriverConfig.Name
  345. driverOpts = mount.VolumeOptions.DriverConfig.Options
  346. labels = mount.VolumeOptions.Labels
  347. }
  348. if mount.VolumeOptions != nil {
  349. return &volumetypes.VolumesCreateBody{
  350. Name: mount.Source,
  351. Driver: driverName,
  352. DriverOpts: driverOpts,
  353. Labels: labels,
  354. }
  355. }
  356. return nil
  357. }
  358. func (c *containerConfig) resources() enginecontainer.Resources {
  359. resources := enginecontainer.Resources{}
  360. // If no limits are specified let the engine use its defaults.
  361. //
  362. // TODO(aluzzardi): We might want to set some limits anyway otherwise
  363. // "unlimited" tasks will step over the reservation of other tasks.
  364. r := c.task.Spec.Resources
  365. if r == nil || r.Limits == nil {
  366. return resources
  367. }
  368. if r.Limits.MemoryBytes > 0 {
  369. resources.Memory = r.Limits.MemoryBytes
  370. }
  371. if r.Limits.NanoCPUs > 0 {
  372. // CPU Period must be set in microseconds.
  373. resources.CPUPeriod = int64(cpuQuotaPeriod / time.Microsecond)
  374. resources.CPUQuota = r.Limits.NanoCPUs * resources.CPUPeriod / 1e9
  375. }
  376. return resources
  377. }
  378. // Docker daemon supports just 1 network during container create.
  379. func (c *containerConfig) createNetworkingConfig(b executorpkg.Backend) *network.NetworkingConfig {
  380. var networks []*api.NetworkAttachment
  381. if c.task.Spec.GetContainer() != nil || c.task.Spec.GetAttachment() != nil {
  382. networks = c.task.Networks
  383. }
  384. epConfig := make(map[string]*network.EndpointSettings)
  385. if len(networks) > 0 {
  386. epConfig[networks[0].Network.Spec.Annotations.Name] = getEndpointConfig(networks[0], b)
  387. }
  388. return &network.NetworkingConfig{EndpointsConfig: epConfig}
  389. }
  390. // TODO: Merge this function with createNetworkingConfig after daemon supports multiple networks in container create
  391. func (c *containerConfig) connectNetworkingConfig(b executorpkg.Backend) *network.NetworkingConfig {
  392. var networks []*api.NetworkAttachment
  393. if c.task.Spec.GetContainer() != nil {
  394. networks = c.task.Networks
  395. }
  396. // First network is used during container create. Other networks are used in "docker network connect"
  397. if len(networks) < 2 {
  398. return nil
  399. }
  400. epConfig := make(map[string]*network.EndpointSettings)
  401. for _, na := range networks[1:] {
  402. epConfig[na.Network.Spec.Annotations.Name] = getEndpointConfig(na, b)
  403. }
  404. return &network.NetworkingConfig{EndpointsConfig: epConfig}
  405. }
  406. func getEndpointConfig(na *api.NetworkAttachment, b executorpkg.Backend) *network.EndpointSettings {
  407. var ipv4, ipv6 string
  408. for _, addr := range na.Addresses {
  409. ip, _, err := net.ParseCIDR(addr)
  410. if err != nil {
  411. continue
  412. }
  413. if ip.To4() != nil {
  414. ipv4 = ip.String()
  415. continue
  416. }
  417. if ip.To16() != nil {
  418. ipv6 = ip.String()
  419. }
  420. }
  421. n := &network.EndpointSettings{
  422. NetworkID: na.Network.ID,
  423. IPAMConfig: &network.EndpointIPAMConfig{
  424. IPv4Address: ipv4,
  425. IPv6Address: ipv6,
  426. },
  427. DriverOpts: na.DriverAttachmentOpts,
  428. }
  429. if v, ok := na.Network.Spec.Annotations.Labels["com.docker.swarm.predefined"]; ok && v == "true" {
  430. if ln, err := b.FindNetwork(na.Network.Spec.Annotations.Name); err == nil {
  431. n.NetworkID = ln.ID()
  432. }
  433. }
  434. return n
  435. }
  436. func (c *containerConfig) virtualIP(networkID string) string {
  437. if c.task.Endpoint == nil {
  438. return ""
  439. }
  440. for _, eVip := range c.task.Endpoint.VirtualIPs {
  441. // We only support IPv4 VIPs for now.
  442. if eVip.NetworkID == networkID {
  443. vip, _, err := net.ParseCIDR(eVip.Addr)
  444. if err != nil {
  445. return ""
  446. }
  447. return vip.String()
  448. }
  449. }
  450. return ""
  451. }
  452. func (c *containerConfig) serviceConfig() *clustertypes.ServiceConfig {
  453. if len(c.task.Networks) == 0 {
  454. return nil
  455. }
  456. logrus.Debugf("Creating service config in agent for t = %+v", c.task)
  457. svcCfg := &clustertypes.ServiceConfig{
  458. Name: c.task.ServiceAnnotations.Name,
  459. Aliases: make(map[string][]string),
  460. ID: c.task.ServiceID,
  461. VirtualAddresses: make(map[string]*clustertypes.VirtualAddress),
  462. }
  463. for _, na := range c.task.Networks {
  464. svcCfg.VirtualAddresses[na.Network.ID] = &clustertypes.VirtualAddress{
  465. // We support only IPv4 virtual IP for now.
  466. IPv4: c.virtualIP(na.Network.ID),
  467. }
  468. if len(na.Aliases) > 0 {
  469. svcCfg.Aliases[na.Network.ID] = na.Aliases
  470. }
  471. }
  472. if c.task.Endpoint != nil {
  473. for _, ePort := range c.task.Endpoint.Ports {
  474. if ePort.PublishMode != api.PublishModeIngress {
  475. continue
  476. }
  477. svcCfg.ExposedPorts = append(svcCfg.ExposedPorts, &clustertypes.PortConfig{
  478. Name: ePort.Name,
  479. Protocol: int32(ePort.Protocol),
  480. TargetPort: ePort.TargetPort,
  481. PublishedPort: ePort.PublishedPort,
  482. })
  483. }
  484. }
  485. return svcCfg
  486. }
  487. // networks returns a list of network names attached to the container. The
  488. // returned name can be used to lookup the corresponding network create
  489. // options.
  490. func (c *containerConfig) networks() []string {
  491. var networks []string
  492. for name := range c.networksAttachments {
  493. networks = append(networks, name)
  494. }
  495. return networks
  496. }
  497. func (c *containerConfig) networkCreateRequest(name string) (clustertypes.NetworkCreateRequest, error) {
  498. na, ok := c.networksAttachments[name]
  499. if !ok {
  500. return clustertypes.NetworkCreateRequest{}, errors.New("container: unknown network referenced")
  501. }
  502. options := types.NetworkCreate{
  503. // ID: na.Network.ID,
  504. Labels: na.Network.Spec.Annotations.Labels,
  505. Internal: na.Network.Spec.Internal,
  506. Attachable: na.Network.Spec.Attachable,
  507. Ingress: convert.IsIngressNetwork(na.Network),
  508. EnableIPv6: na.Network.Spec.Ipv6Enabled,
  509. CheckDuplicate: true,
  510. Scope: netconst.SwarmScope,
  511. }
  512. if na.Network.Spec.GetNetwork() != "" {
  513. options.ConfigFrom = &network.ConfigReference{
  514. Network: na.Network.Spec.GetNetwork(),
  515. }
  516. }
  517. if na.Network.DriverState != nil {
  518. options.Driver = na.Network.DriverState.Name
  519. options.Options = na.Network.DriverState.Options
  520. }
  521. if na.Network.IPAM != nil {
  522. options.IPAM = &network.IPAM{
  523. Driver: na.Network.IPAM.Driver.Name,
  524. Options: na.Network.IPAM.Driver.Options,
  525. }
  526. for _, ic := range na.Network.IPAM.Configs {
  527. c := network.IPAMConfig{
  528. Subnet: ic.Subnet,
  529. IPRange: ic.Range,
  530. Gateway: ic.Gateway,
  531. }
  532. options.IPAM.Config = append(options.IPAM.Config, c)
  533. }
  534. }
  535. return clustertypes.NetworkCreateRequest{
  536. ID: na.Network.ID,
  537. NetworkCreateRequest: types.NetworkCreateRequest{
  538. Name: name,
  539. NetworkCreate: options,
  540. },
  541. }, nil
  542. }
  543. func (c *containerConfig) applyPrivileges(hc *enginecontainer.HostConfig) {
  544. privileges := c.spec().Privileges
  545. if privileges == nil {
  546. return
  547. }
  548. credentials := privileges.CredentialSpec
  549. if credentials != nil {
  550. switch credentials.Source.(type) {
  551. case *api.Privileges_CredentialSpec_File:
  552. hc.SecurityOpt = append(hc.SecurityOpt, "credentialspec=file://"+credentials.GetFile())
  553. case *api.Privileges_CredentialSpec_Registry:
  554. hc.SecurityOpt = append(hc.SecurityOpt, "credentialspec=registry://"+credentials.GetRegistry())
  555. }
  556. }
  557. selinux := privileges.SELinuxContext
  558. if selinux != nil {
  559. if selinux.Disable {
  560. hc.SecurityOpt = append(hc.SecurityOpt, "label=disable")
  561. }
  562. if selinux.User != "" {
  563. hc.SecurityOpt = append(hc.SecurityOpt, "label=user:"+selinux.User)
  564. }
  565. if selinux.Role != "" {
  566. hc.SecurityOpt = append(hc.SecurityOpt, "label=role:"+selinux.Role)
  567. }
  568. if selinux.Level != "" {
  569. hc.SecurityOpt = append(hc.SecurityOpt, "label=level:"+selinux.Level)
  570. }
  571. if selinux.Type != "" {
  572. hc.SecurityOpt = append(hc.SecurityOpt, "label=type:"+selinux.Type)
  573. }
  574. }
  575. }
  576. func (c containerConfig) eventFilter() filters.Args {
  577. filter := filters.NewArgs()
  578. filter.Add("type", events.ContainerEventType)
  579. filter.Add("name", c.name())
  580. filter.Add("label", fmt.Sprintf("%v.task.id=%v", systemLabelPrefix, c.task.ID))
  581. return filter
  582. }