executor.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package container // import "github.com/docker/docker/daemon/cluster/executor/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "sync"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/filters"
  10. "github.com/docker/docker/api/types/network"
  11. swarmtypes "github.com/docker/docker/api/types/swarm"
  12. "github.com/docker/docker/daemon/cluster/controllers/plugin"
  13. "github.com/docker/docker/daemon/cluster/convert"
  14. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  15. clustertypes "github.com/docker/docker/daemon/cluster/provider"
  16. networktypes "github.com/docker/libnetwork/types"
  17. "github.com/docker/swarmkit/agent"
  18. "github.com/docker/swarmkit/agent/exec"
  19. "github.com/docker/swarmkit/api"
  20. "github.com/docker/swarmkit/api/naming"
  21. "github.com/docker/swarmkit/template"
  22. "github.com/sirupsen/logrus"
  23. )
  24. type executor struct {
  25. backend executorpkg.Backend
  26. imageBackend executorpkg.ImageBackend
  27. pluginBackend plugin.Backend
  28. volumeBackend executorpkg.VolumeBackend
  29. dependencies exec.DependencyManager
  30. mutex sync.Mutex // This mutex protects the following node field
  31. node *api.NodeDescription
  32. }
  33. // NewExecutor returns an executor from the docker client.
  34. func NewExecutor(b executorpkg.Backend, p plugin.Backend, i executorpkg.ImageBackend, v executorpkg.VolumeBackend) exec.Executor {
  35. return &executor{
  36. backend: b,
  37. pluginBackend: p,
  38. imageBackend: i,
  39. volumeBackend: v,
  40. dependencies: agent.NewDependencyManager(),
  41. }
  42. }
  43. // Describe returns the underlying node description from the docker client.
  44. func (e *executor) Describe(ctx context.Context) (*api.NodeDescription, error) {
  45. info, err := e.backend.SystemInfo()
  46. if err != nil {
  47. return nil, err
  48. }
  49. plugins := map[api.PluginDescription]struct{}{}
  50. addPlugins := func(typ string, names []string) {
  51. for _, name := range names {
  52. plugins[api.PluginDescription{
  53. Type: typ,
  54. Name: name,
  55. }] = struct{}{}
  56. }
  57. }
  58. // add v1 plugins
  59. addPlugins("Volume", info.Plugins.Volume)
  60. // Add builtin driver "overlay" (the only builtin multi-host driver) to
  61. // the plugin list by default.
  62. addPlugins("Network", append([]string{"overlay"}, info.Plugins.Network...))
  63. addPlugins("Authorization", info.Plugins.Authorization)
  64. addPlugins("Log", info.Plugins.Log)
  65. // add v2 plugins
  66. v2Plugins, err := e.backend.PluginManager().List(filters.NewArgs())
  67. if err == nil {
  68. for _, plgn := range v2Plugins {
  69. for _, typ := range plgn.Config.Interface.Types {
  70. if typ.Prefix != "docker" || !plgn.Enabled {
  71. continue
  72. }
  73. plgnTyp := typ.Capability
  74. switch typ.Capability {
  75. case "volumedriver":
  76. plgnTyp = "Volume"
  77. case "networkdriver":
  78. plgnTyp = "Network"
  79. case "logdriver":
  80. plgnTyp = "Log"
  81. }
  82. plugins[api.PluginDescription{
  83. Type: plgnTyp,
  84. Name: plgn.Name,
  85. }] = struct{}{}
  86. }
  87. }
  88. }
  89. pluginFields := make([]api.PluginDescription, 0, len(plugins))
  90. for k := range plugins {
  91. pluginFields = append(pluginFields, k)
  92. }
  93. sort.Sort(sortedPlugins(pluginFields))
  94. // parse []string labels into a map[string]string
  95. labels := map[string]string{}
  96. for _, l := range info.Labels {
  97. stringSlice := strings.SplitN(l, "=", 2)
  98. // this will take the last value in the list for a given key
  99. // ideally, one shouldn't assign multiple values to the same key
  100. if len(stringSlice) > 1 {
  101. labels[stringSlice[0]] = stringSlice[1]
  102. }
  103. }
  104. description := &api.NodeDescription{
  105. Hostname: info.Name,
  106. Platform: &api.Platform{
  107. Architecture: info.Architecture,
  108. OS: info.OSType,
  109. },
  110. Engine: &api.EngineDescription{
  111. EngineVersion: info.ServerVersion,
  112. Labels: labels,
  113. Plugins: pluginFields,
  114. },
  115. Resources: &api.Resources{
  116. NanoCPUs: int64(info.NCPU) * 1e9,
  117. MemoryBytes: info.MemTotal,
  118. Generic: convert.GenericResourcesToGRPC(info.GenericResources),
  119. },
  120. }
  121. // Save the node information in the executor field
  122. e.mutex.Lock()
  123. e.node = description
  124. e.mutex.Unlock()
  125. return description, nil
  126. }
  127. func (e *executor) Configure(ctx context.Context, node *api.Node) error {
  128. var ingressNA *api.NetworkAttachment
  129. attachments := make(map[string]string)
  130. for _, na := range node.Attachments {
  131. if na == nil || na.Network == nil || len(na.Addresses) == 0 {
  132. // this should not happen, but we got a panic here and don't have a
  133. // good idea about what the underlying data structure looks like.
  134. logrus.WithField("NetworkAttachment", fmt.Sprintf("%#v", na)).
  135. Warnf("skipping nil or malformed node network attachment entry")
  136. continue
  137. }
  138. if na.Network.Spec.Ingress {
  139. ingressNA = na
  140. }
  141. attachments[na.Network.ID] = na.Addresses[0]
  142. }
  143. if (ingressNA == nil) && (node.Attachment != nil) && (len(node.Attachment.Addresses) > 0) {
  144. ingressNA = node.Attachment
  145. attachments[ingressNA.Network.ID] = ingressNA.Addresses[0]
  146. }
  147. if ingressNA == nil {
  148. e.backend.ReleaseIngress()
  149. return e.backend.GetAttachmentStore().ResetAttachments(attachments)
  150. }
  151. options := types.NetworkCreate{
  152. Driver: ingressNA.Network.DriverState.Name,
  153. IPAM: &network.IPAM{
  154. Driver: ingressNA.Network.IPAM.Driver.Name,
  155. },
  156. Options: ingressNA.Network.DriverState.Options,
  157. Ingress: true,
  158. CheckDuplicate: true,
  159. }
  160. for _, ic := range ingressNA.Network.IPAM.Configs {
  161. c := network.IPAMConfig{
  162. Subnet: ic.Subnet,
  163. IPRange: ic.Range,
  164. Gateway: ic.Gateway,
  165. }
  166. options.IPAM.Config = append(options.IPAM.Config, c)
  167. }
  168. _, err := e.backend.SetupIngress(clustertypes.NetworkCreateRequest{
  169. ID: ingressNA.Network.ID,
  170. NetworkCreateRequest: types.NetworkCreateRequest{
  171. Name: ingressNA.Network.Spec.Annotations.Name,
  172. NetworkCreate: options,
  173. },
  174. }, ingressNA.Addresses[0])
  175. if err != nil {
  176. return err
  177. }
  178. return e.backend.GetAttachmentStore().ResetAttachments(attachments)
  179. }
  180. // Controller returns a docker container runner.
  181. func (e *executor) Controller(t *api.Task) (exec.Controller, error) {
  182. dependencyGetter := template.NewTemplatedDependencyGetter(agent.Restrict(e.dependencies, t), t, nil)
  183. // Get the node description from the executor field
  184. e.mutex.Lock()
  185. nodeDescription := e.node
  186. e.mutex.Unlock()
  187. if t.Spec.GetAttachment() != nil {
  188. return newNetworkAttacherController(e.backend, e.imageBackend, e.volumeBackend, t, nodeDescription, dependencyGetter)
  189. }
  190. var ctlr exec.Controller
  191. switch r := t.Spec.GetRuntime().(type) {
  192. case *api.TaskSpec_Generic:
  193. logrus.WithFields(logrus.Fields{
  194. "kind": r.Generic.Kind,
  195. "type_url": r.Generic.Payload.TypeUrl,
  196. }).Debug("custom runtime requested")
  197. runtimeKind, err := naming.Runtime(t.Spec)
  198. if err != nil {
  199. return ctlr, err
  200. }
  201. switch runtimeKind {
  202. case string(swarmtypes.RuntimePlugin):
  203. if !e.backend.HasExperimental() {
  204. return ctlr, fmt.Errorf("runtime type %q only supported in experimental", swarmtypes.RuntimePlugin)
  205. }
  206. c, err := plugin.NewController(e.pluginBackend, t)
  207. if err != nil {
  208. return ctlr, err
  209. }
  210. ctlr = c
  211. default:
  212. return ctlr, fmt.Errorf("unsupported runtime type: %q", runtimeKind)
  213. }
  214. case *api.TaskSpec_Container:
  215. c, err := newController(e.backend, e.imageBackend, e.volumeBackend, t, nodeDescription, dependencyGetter)
  216. if err != nil {
  217. return ctlr, err
  218. }
  219. ctlr = c
  220. default:
  221. return ctlr, fmt.Errorf("unsupported runtime: %q", r)
  222. }
  223. return ctlr, nil
  224. }
  225. func (e *executor) SetNetworkBootstrapKeys(keys []*api.EncryptionKey) error {
  226. nwKeys := []*networktypes.EncryptionKey{}
  227. for _, key := range keys {
  228. nwKey := &networktypes.EncryptionKey{
  229. Subsystem: key.Subsystem,
  230. Algorithm: int32(key.Algorithm),
  231. Key: make([]byte, len(key.Key)),
  232. LamportTime: key.LamportTime,
  233. }
  234. copy(nwKey.Key, key.Key)
  235. nwKeys = append(nwKeys, nwKey)
  236. }
  237. e.backend.SetNetworkBootstrapKeys(nwKeys)
  238. return nil
  239. }
  240. func (e *executor) Secrets() exec.SecretsManager {
  241. return e.dependencies.Secrets()
  242. }
  243. func (e *executor) Configs() exec.ConfigsManager {
  244. return e.dependencies.Configs()
  245. }
  246. type sortedPlugins []api.PluginDescription
  247. func (sp sortedPlugins) Len() int { return len(sp) }
  248. func (sp sortedPlugins) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
  249. func (sp sortedPlugins) Less(i, j int) bool {
  250. if sp[i].Type != sp[j].Type {
  251. return sp[i].Type < sp[j].Type
  252. }
  253. return sp[i].Name < sp[j].Name
  254. }