executor.go 8.1 KB

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