executor.go 3.5 KB

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