executor.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package container // import "github.com/docker/docker/daemon/cluster/executor/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "sync"
  8. "github.com/containerd/log"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/filters"
  11. "github.com/docker/docker/api/types/network"
  12. swarmtypes "github.com/docker/docker/api/types/swarm"
  13. "github.com/docker/docker/daemon/cluster/controllers/plugin"
  14. "github.com/docker/docker/daemon/cluster/convert"
  15. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  16. clustertypes "github.com/docker/docker/daemon/cluster/provider"
  17. "github.com/docker/docker/libnetwork"
  18. networktypes "github.com/docker/docker/libnetwork/types"
  19. "github.com/moby/swarmkit/v2/agent"
  20. "github.com/moby/swarmkit/v2/agent/exec"
  21. "github.com/moby/swarmkit/v2/api"
  22. "github.com/moby/swarmkit/v2/api/naming"
  23. swarmlog "github.com/moby/swarmkit/v2/log"
  24. "github.com/moby/swarmkit/v2/template"
  25. "github.com/pkg/errors"
  26. )
  27. type executor struct {
  28. backend executorpkg.Backend
  29. imageBackend executorpkg.ImageBackend
  30. pluginBackend plugin.Backend
  31. volumeBackend executorpkg.VolumeBackend
  32. dependencies exec.DependencyManager
  33. mutex sync.Mutex // This mutex protects the following node field
  34. node *api.NodeDescription
  35. // nodeObj holds a copy of the swarmkit Node object from the time of the
  36. // last call to executor.Configure. This allows us to discover which
  37. // network attachments the node previously had, which further allows us to
  38. // determine which, if any, need to be removed. nodeObj is not protected by
  39. // a mutex, because it is only written to in the method (Configure) that it
  40. // is read from. If that changes, it may need to be guarded.
  41. nodeObj *api.Node
  42. }
  43. // NewExecutor returns an executor from the docker client.
  44. func NewExecutor(b executorpkg.Backend, p plugin.Backend, i executorpkg.ImageBackend, v executorpkg.VolumeBackend) exec.Executor {
  45. return &executor{
  46. backend: b,
  47. pluginBackend: p,
  48. imageBackend: i,
  49. volumeBackend: v,
  50. dependencies: agent.NewDependencyManager(b.PluginGetter()),
  51. }
  52. }
  53. // Describe returns the underlying node description from the docker client.
  54. func (e *executor) Describe(ctx context.Context) (*api.NodeDescription, error) {
  55. info, err := e.backend.SystemInfo(ctx)
  56. if err != nil {
  57. return nil, err
  58. }
  59. plugins := map[api.PluginDescription]struct{}{}
  60. addPlugins := func(typ string, names []string) {
  61. for _, name := range names {
  62. plugins[api.PluginDescription{
  63. Type: typ,
  64. Name: name,
  65. }] = struct{}{}
  66. }
  67. }
  68. // add v1 plugins
  69. addPlugins("Volume", info.Plugins.Volume)
  70. // Add builtin driver "overlay" (the only builtin multi-host driver) to
  71. // the plugin list by default.
  72. addPlugins("Network", append([]string{"overlay"}, info.Plugins.Network...))
  73. addPlugins("Authorization", info.Plugins.Authorization)
  74. addPlugins("Log", info.Plugins.Log)
  75. // add v2 plugins
  76. v2Plugins, err := e.backend.PluginManager().List(filters.NewArgs())
  77. if err == nil {
  78. for _, plgn := range v2Plugins {
  79. for _, typ := range plgn.Config.Interface.Types {
  80. if typ.Prefix != "docker" || !plgn.Enabled {
  81. continue
  82. }
  83. plgnTyp := typ.Capability
  84. switch typ.Capability {
  85. case "volumedriver":
  86. plgnTyp = "Volume"
  87. case "networkdriver":
  88. plgnTyp = "Network"
  89. case "logdriver":
  90. plgnTyp = "Log"
  91. }
  92. plugins[api.PluginDescription{
  93. Type: plgnTyp,
  94. Name: plgn.Name,
  95. }] = struct{}{}
  96. }
  97. }
  98. }
  99. pluginFields := make([]api.PluginDescription, 0, len(plugins))
  100. for k := range plugins {
  101. pluginFields = append(pluginFields, k)
  102. }
  103. sort.Sort(sortedPlugins(pluginFields))
  104. // parse []string labels into a map[string]string
  105. labels := map[string]string{}
  106. for _, l := range info.Labels {
  107. k, v, ok := strings.Cut(l, "=")
  108. // this will take the last value in the list for a given key
  109. // ideally, one shouldn't assign multiple values to the same key
  110. if ok {
  111. labels[k] = v
  112. }
  113. }
  114. // TODO(dperny): don't ignore the error here
  115. csiInfo, _ := e.Volumes().Plugins().NodeInfo(ctx)
  116. description := &api.NodeDescription{
  117. Hostname: info.Name,
  118. Platform: &api.Platform{
  119. Architecture: info.Architecture,
  120. OS: info.OSType,
  121. },
  122. Engine: &api.EngineDescription{
  123. EngineVersion: info.ServerVersion,
  124. Labels: labels,
  125. Plugins: pluginFields,
  126. },
  127. Resources: &api.Resources{
  128. NanoCPUs: int64(info.NCPU) * 1e9,
  129. MemoryBytes: info.MemTotal,
  130. Generic: convert.GenericResourcesToGRPC(info.GenericResources),
  131. },
  132. CSIInfo: csiInfo,
  133. }
  134. // Save the node information in the executor field
  135. e.mutex.Lock()
  136. e.node = description
  137. e.mutex.Unlock()
  138. return description, nil
  139. }
  140. func (e *executor) Configure(ctx context.Context, node *api.Node) error {
  141. var ingressNA *api.NetworkAttachment
  142. attachments := make(map[string]string)
  143. for _, na := range node.Attachments {
  144. if na == nil || na.Network == nil || len(na.Addresses) == 0 {
  145. // this should not happen, but we got a panic here and don't have a
  146. // good idea about what the underlying data structure looks like.
  147. swarmlog.G(ctx).WithField("NetworkAttachment", fmt.Sprintf("%#v", na)).Warn("skipping nil or malformed node network attachment entry")
  148. continue
  149. }
  150. if na.Network.Spec.Ingress {
  151. ingressNA = na
  152. }
  153. attachments[na.Network.ID] = na.Addresses[0]
  154. }
  155. // discover which, if any, attachments have been removed.
  156. //
  157. // we aren't responsible directly for creating these networks. that is
  158. // handled indirectly when a container using that network is created.
  159. // however, when it comes time to remove the network, none of the relevant
  160. // tasks may exist anymore. this means we should go ahead and try to remove
  161. // any network we know to no longer be in use.
  162. // removeAttachments maps the network ID to a boolean. This boolean
  163. // indicates whether the attachment in question is totally removed (true),
  164. // or has just had its IP changed (false)
  165. removeAttachments := make(map[string]bool)
  166. // the first time we Configure, nodeObj wil be nil, because it will not be
  167. // set yet. in that case, skip this check.
  168. if e.nodeObj != nil {
  169. for _, na := range e.nodeObj.Attachments {
  170. // same thing as above, check sanity of the attachments so we don't
  171. // get a panic.
  172. if na == nil || na.Network == nil || len(na.Addresses) == 0 {
  173. swarmlog.G(ctx).WithField("NetworkAttachment", fmt.Sprintf("%#v", na)).Warn("skipping nil or malformed node network attachment entry")
  174. continue
  175. }
  176. // now, check if the attachment exists and shares the same IP address.
  177. if ip, ok := attachments[na.Network.ID]; !ok || na.Addresses[0] != ip {
  178. // if the map entry exists, then the network still exists, and the
  179. // IP must be what has changed
  180. removeAttachments[na.Network.ID] = !ok
  181. }
  182. }
  183. }
  184. if (ingressNA == nil) && (node.Attachment != nil) && (len(node.Attachment.Addresses) > 0) {
  185. ingressNA = node.Attachment
  186. attachments[ingressNA.Network.ID] = ingressNA.Addresses[0]
  187. }
  188. if ingressNA == nil {
  189. e.backend.ReleaseIngress()
  190. return e.backend.GetAttachmentStore().ResetAttachments(attachments)
  191. }
  192. options := types.NetworkCreate{
  193. Driver: ingressNA.Network.DriverState.Name,
  194. IPAM: &network.IPAM{
  195. Driver: ingressNA.Network.IPAM.Driver.Name,
  196. },
  197. Options: ingressNA.Network.DriverState.Options,
  198. Ingress: true,
  199. }
  200. for _, ic := range ingressNA.Network.IPAM.Configs {
  201. c := network.IPAMConfig{
  202. Subnet: ic.Subnet,
  203. IPRange: ic.Range,
  204. Gateway: ic.Gateway,
  205. }
  206. options.IPAM.Config = append(options.IPAM.Config, c)
  207. }
  208. _, err := e.backend.SetupIngress(clustertypes.NetworkCreateRequest{
  209. ID: ingressNA.Network.ID,
  210. NetworkCreateRequest: types.NetworkCreateRequest{
  211. Name: ingressNA.Network.Spec.Annotations.Name,
  212. NetworkCreate: options,
  213. },
  214. }, ingressNA.Addresses[0])
  215. if err != nil {
  216. return err
  217. }
  218. var (
  219. activeEndpointsError *libnetwork.ActiveEndpointsError
  220. errNoSuchNetwork libnetwork.ErrNoSuchNetwork
  221. )
  222. // now, finally, remove any network LB attachments that we no longer have.
  223. for nw, gone := range removeAttachments {
  224. err := e.backend.DeleteManagedNetwork(nw)
  225. switch {
  226. case err == nil:
  227. continue
  228. case errors.As(err, &activeEndpointsError):
  229. // this is the purpose of the boolean in the map. it's literally
  230. // just to log an appropriate, informative error. i'm unsure if
  231. // this can ever actually occur, but we need to know if it does.
  232. if gone {
  233. swarmlog.G(ctx).Warnf("network %s should be removed, but still has active attachments", nw)
  234. } else {
  235. swarmlog.G(ctx).Warnf("network %s should have its node LB IP changed, but cannot be removed because of active attachments", nw)
  236. }
  237. continue
  238. case errors.As(err, &errNoSuchNetwork):
  239. // NoSuchNetworkError indicates the network is already gone.
  240. continue
  241. default:
  242. swarmlog.G(ctx).Errorf("network %s remove failed: %v", nw, err)
  243. }
  244. }
  245. // now update our copy of the node object, reset the attachment store, and
  246. // return
  247. e.nodeObj = node
  248. return e.backend.GetAttachmentStore().ResetAttachments(attachments)
  249. }
  250. // Controller returns a docker container runner.
  251. func (e *executor) Controller(t *api.Task) (exec.Controller, error) {
  252. dependencyGetter := template.NewTemplatedDependencyGetter(agent.Restrict(e.dependencies, t), t, nil)
  253. // Get the node description from the executor field
  254. e.mutex.Lock()
  255. nodeDescription := e.node
  256. e.mutex.Unlock()
  257. if t.Spec.GetAttachment() != nil {
  258. return newNetworkAttacherController(e.backend, e.imageBackend, e.volumeBackend, t, nodeDescription, dependencyGetter)
  259. }
  260. var ctlr exec.Controller
  261. switch r := t.Spec.GetRuntime().(type) {
  262. case *api.TaskSpec_Generic:
  263. swarmlog.G(context.TODO()).WithFields(log.Fields{
  264. "kind": r.Generic.Kind,
  265. "type_url": r.Generic.Payload.TypeUrl,
  266. }).Debug("custom runtime requested")
  267. runtimeKind, err := naming.Runtime(t.Spec)
  268. if err != nil {
  269. return ctlr, err
  270. }
  271. switch runtimeKind {
  272. case string(swarmtypes.RuntimePlugin):
  273. if !e.backend.HasExperimental() {
  274. return ctlr, fmt.Errorf("runtime type %q only supported in experimental", swarmtypes.RuntimePlugin)
  275. }
  276. c, err := plugin.NewController(e.pluginBackend, t)
  277. if err != nil {
  278. return ctlr, err
  279. }
  280. ctlr = c
  281. default:
  282. return ctlr, fmt.Errorf("unsupported runtime type: %q", runtimeKind)
  283. }
  284. case *api.TaskSpec_Container:
  285. c, err := newController(e.backend, e.imageBackend, e.volumeBackend, t, nodeDescription, dependencyGetter)
  286. if err != nil {
  287. return ctlr, err
  288. }
  289. ctlr = c
  290. default:
  291. return ctlr, fmt.Errorf("unsupported runtime: %q", r)
  292. }
  293. return ctlr, nil
  294. }
  295. func (e *executor) SetNetworkBootstrapKeys(keys []*api.EncryptionKey) error {
  296. nwKeys := []*networktypes.EncryptionKey{}
  297. for _, key := range keys {
  298. nwKey := &networktypes.EncryptionKey{
  299. Subsystem: key.Subsystem,
  300. Algorithm: int32(key.Algorithm),
  301. Key: make([]byte, len(key.Key)),
  302. LamportTime: key.LamportTime,
  303. }
  304. copy(nwKey.Key, key.Key)
  305. nwKeys = append(nwKeys, nwKey)
  306. }
  307. e.backend.SetNetworkBootstrapKeys(nwKeys)
  308. return nil
  309. }
  310. func (e *executor) Secrets() exec.SecretsManager {
  311. return e.dependencies.Secrets()
  312. }
  313. func (e *executor) Configs() exec.ConfigsManager {
  314. return e.dependencies.Configs()
  315. }
  316. func (e *executor) Volumes() exec.VolumesManager {
  317. return e.dependencies.Volumes()
  318. }
  319. type sortedPlugins []api.PluginDescription
  320. func (sp sortedPlugins) Len() int { return len(sp) }
  321. func (sp sortedPlugins) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
  322. func (sp sortedPlugins) Less(i, j int) bool {
  323. if sp[i].Type != sp[j].Type {
  324. return sp[i].Type < sp[j].Type
  325. }
  326. return sp[i].Name < sp[j].Name
  327. }