executor.go 4.9 KB

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