executor.go 6.1 KB

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