executor.go 4.0 KB

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