executor.go 7.7 KB

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