executor.go 7.4 KB

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