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