executor.go 6.9 KB

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