executor.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/exec"
  16. "github.com/docker/swarmkit/agent/secrets"
  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. secrets exec.SecretsManager
  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. secrets: secrets.NewManager(),
  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. // add v2 plugins
  54. v2Plugins, err := e.backend.PluginManager().List(filters.NewArgs())
  55. if err == nil {
  56. for _, plgn := range v2Plugins {
  57. for _, typ := range plgn.Config.Interface.Types {
  58. if typ.Prefix != "docker" || !plgn.Enabled {
  59. continue
  60. }
  61. plgnTyp := typ.Capability
  62. if typ.Capability == "volumedriver" {
  63. plgnTyp = "Volume"
  64. } else if typ.Capability == "networkdriver" {
  65. plgnTyp = "Network"
  66. }
  67. plugins[api.PluginDescription{
  68. Type: plgnTyp,
  69. Name: plgn.Name,
  70. }] = struct{}{}
  71. }
  72. }
  73. }
  74. pluginFields := make([]api.PluginDescription, 0, len(plugins))
  75. for k := range plugins {
  76. pluginFields = append(pluginFields, k)
  77. }
  78. sort.Sort(sortedPlugins(pluginFields))
  79. // parse []string labels into a map[string]string
  80. labels := map[string]string{}
  81. for _, l := range info.Labels {
  82. stringSlice := strings.SplitN(l, "=", 2)
  83. // this will take the last value in the list for a given key
  84. // ideally, one shouldn't assign multiple values to the same key
  85. if len(stringSlice) > 1 {
  86. labels[stringSlice[0]] = stringSlice[1]
  87. }
  88. }
  89. description := &api.NodeDescription{
  90. Hostname: info.Name,
  91. Platform: &api.Platform{
  92. Architecture: info.Architecture,
  93. OS: info.OSType,
  94. },
  95. Engine: &api.EngineDescription{
  96. EngineVersion: info.ServerVersion,
  97. Labels: labels,
  98. Plugins: pluginFields,
  99. },
  100. Resources: &api.Resources{
  101. NanoCPUs: int64(info.NCPU) * 1e9,
  102. MemoryBytes: info.MemTotal,
  103. },
  104. }
  105. return description, nil
  106. }
  107. func (e *executor) Configure(ctx context.Context, node *api.Node) error {
  108. na := node.Attachment
  109. if na == nil {
  110. e.backend.ReleaseIngress()
  111. return nil
  112. }
  113. options := types.NetworkCreate{
  114. Driver: na.Network.DriverState.Name,
  115. IPAM: &network.IPAM{
  116. Driver: na.Network.IPAM.Driver.Name,
  117. },
  118. Options: na.Network.DriverState.Options,
  119. Ingress: true,
  120. CheckDuplicate: true,
  121. }
  122. for _, ic := range na.Network.IPAM.Configs {
  123. c := network.IPAMConfig{
  124. Subnet: ic.Subnet,
  125. IPRange: ic.Range,
  126. Gateway: ic.Gateway,
  127. }
  128. options.IPAM.Config = append(options.IPAM.Config, c)
  129. }
  130. _, err := e.backend.SetupIngress(clustertypes.NetworkCreateRequest{
  131. ID: na.Network.ID,
  132. NetworkCreateRequest: types.NetworkCreateRequest{
  133. Name: na.Network.Spec.Annotations.Name,
  134. NetworkCreate: options,
  135. },
  136. }, na.Addresses[0])
  137. return err
  138. }
  139. // Controller returns a docker container runner.
  140. func (e *executor) Controller(t *api.Task) (exec.Controller, error) {
  141. if t.Spec.GetAttachment() != nil {
  142. return newNetworkAttacherController(e.backend, t, e.secrets)
  143. }
  144. var ctlr exec.Controller
  145. switch r := t.Spec.GetRuntime().(type) {
  146. case *api.TaskSpec_Generic:
  147. logrus.WithFields(logrus.Fields{
  148. "kind": r.Generic.Kind,
  149. "type_url": r.Generic.Payload.TypeUrl,
  150. }).Debug("custom runtime requested")
  151. runtimeKind, err := naming.Runtime(t.Spec)
  152. if err != nil {
  153. return ctlr, err
  154. }
  155. switch runtimeKind {
  156. case string(swarmtypes.RuntimePlugin):
  157. c, err := plugin.NewController()
  158. if err != nil {
  159. return ctlr, err
  160. }
  161. ctlr = c
  162. default:
  163. return ctlr, fmt.Errorf("unsupported runtime type: %q", r.Generic.Kind)
  164. }
  165. case *api.TaskSpec_Container:
  166. c, err := newController(e.backend, t, secrets.Restrict(e.secrets, t))
  167. if err != nil {
  168. return ctlr, err
  169. }
  170. ctlr = c
  171. default:
  172. return ctlr, fmt.Errorf("unsupported runtime: %q", r)
  173. }
  174. return ctlr, nil
  175. }
  176. func (e *executor) SetNetworkBootstrapKeys(keys []*api.EncryptionKey) error {
  177. nwKeys := []*networktypes.EncryptionKey{}
  178. for _, key := range keys {
  179. nwKey := &networktypes.EncryptionKey{
  180. Subsystem: key.Subsystem,
  181. Algorithm: int32(key.Algorithm),
  182. Key: make([]byte, len(key.Key)),
  183. LamportTime: key.LamportTime,
  184. }
  185. copy(nwKey.Key, key.Key)
  186. nwKeys = append(nwKeys, nwKey)
  187. }
  188. e.backend.SetNetworkBootstrapKeys(nwKeys)
  189. return nil
  190. }
  191. func (e *executor) Secrets() exec.SecretsManager {
  192. return e.secrets
  193. }
  194. type sortedPlugins []api.PluginDescription
  195. func (sp sortedPlugins) Len() int { return len(sp) }
  196. func (sp sortedPlugins) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
  197. func (sp sortedPlugins) Less(i, j int) bool {
  198. if sp[i].Type != sp[j].Type {
  199. return sp[i].Type < sp[j].Type
  200. }
  201. return sp[i].Name < sp[j].Name
  202. }