executor.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 {
  132. e.backend.ReleaseIngress()
  133. return e.backend.GetAttachmentStore().ResetAttachments(attachments)
  134. }
  135. options := types.NetworkCreate{
  136. Driver: ingressNA.Network.DriverState.Name,
  137. IPAM: &network.IPAM{
  138. Driver: ingressNA.Network.IPAM.Driver.Name,
  139. },
  140. Options: ingressNA.Network.DriverState.Options,
  141. Ingress: true,
  142. CheckDuplicate: true,
  143. }
  144. for _, ic := range ingressNA.Network.IPAM.Configs {
  145. c := network.IPAMConfig{
  146. Subnet: ic.Subnet,
  147. IPRange: ic.Range,
  148. Gateway: ic.Gateway,
  149. }
  150. options.IPAM.Config = append(options.IPAM.Config, c)
  151. }
  152. _, err := e.backend.SetupIngress(clustertypes.NetworkCreateRequest{
  153. ID: ingressNA.Network.ID,
  154. NetworkCreateRequest: types.NetworkCreateRequest{
  155. Name: ingressNA.Network.Spec.Annotations.Name,
  156. NetworkCreate: options,
  157. },
  158. }, ingressNA.Addresses[0])
  159. if err != nil {
  160. return err
  161. }
  162. return e.backend.GetAttachmentStore().ResetAttachments(attachments)
  163. }
  164. // Controller returns a docker container runner.
  165. func (e *executor) Controller(t *api.Task) (exec.Controller, error) {
  166. dependencyGetter := agent.Restrict(e.dependencies, t)
  167. // Get the node description from the executor field
  168. e.mutex.Lock()
  169. nodeDescription := e.node
  170. e.mutex.Unlock()
  171. if t.Spec.GetAttachment() != nil {
  172. return newNetworkAttacherController(e.backend, t, nodeDescription, dependencyGetter)
  173. }
  174. var ctlr exec.Controller
  175. switch r := t.Spec.GetRuntime().(type) {
  176. case *api.TaskSpec_Generic:
  177. logrus.WithFields(logrus.Fields{
  178. "kind": r.Generic.Kind,
  179. "type_url": r.Generic.Payload.TypeUrl,
  180. }).Debug("custom runtime requested")
  181. runtimeKind, err := naming.Runtime(t.Spec)
  182. if err != nil {
  183. return ctlr, err
  184. }
  185. switch runtimeKind {
  186. case string(swarmtypes.RuntimePlugin):
  187. info, _ := e.backend.SystemInfo()
  188. if !info.ExperimentalBuild {
  189. return ctlr, fmt.Errorf("runtime type %q only supported in experimental", swarmtypes.RuntimePlugin)
  190. }
  191. c, err := plugin.NewController(e.pluginBackend, t)
  192. if err != nil {
  193. return ctlr, err
  194. }
  195. ctlr = c
  196. default:
  197. return ctlr, fmt.Errorf("unsupported runtime type: %q", runtimeKind)
  198. }
  199. case *api.TaskSpec_Container:
  200. c, err := newController(e.backend, t, nodeDescription, dependencyGetter)
  201. if err != nil {
  202. return ctlr, err
  203. }
  204. ctlr = c
  205. default:
  206. return ctlr, fmt.Errorf("unsupported runtime: %q", r)
  207. }
  208. return ctlr, nil
  209. }
  210. func (e *executor) SetNetworkBootstrapKeys(keys []*api.EncryptionKey) error {
  211. nwKeys := []*networktypes.EncryptionKey{}
  212. for _, key := range keys {
  213. nwKey := &networktypes.EncryptionKey{
  214. Subsystem: key.Subsystem,
  215. Algorithm: int32(key.Algorithm),
  216. Key: make([]byte, len(key.Key)),
  217. LamportTime: key.LamportTime,
  218. }
  219. copy(nwKey.Key, key.Key)
  220. nwKeys = append(nwKeys, nwKey)
  221. }
  222. e.backend.SetNetworkBootstrapKeys(nwKeys)
  223. return nil
  224. }
  225. func (e *executor) Secrets() exec.SecretsManager {
  226. return e.dependencies.Secrets()
  227. }
  228. func (e *executor) Configs() exec.ConfigsManager {
  229. return e.dependencies.Configs()
  230. }
  231. type sortedPlugins []api.PluginDescription
  232. func (sp sortedPlugins) Len() int { return len(sp) }
  233. func (sp sortedPlugins) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
  234. func (sp sortedPlugins) Less(i, j int) bool {
  235. if sp[i].Type != sp[j].Type {
  236. return sp[i].Type < sp[j].Type
  237. }
  238. return sp[i].Name < sp[j].Name
  239. }