executor.go 7.6 KB

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