container.go 19 KB

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