github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/daemon/cluster/executor/container/executor.go (about)

     1  package container // import "github.com/Prakhar-Agarwal-byte/moby/daemon/cluster/executor/container"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  	"strings"
     8  	"sync"
     9  
    10  	"github.com/containerd/log"
    11  	"github.com/Prakhar-Agarwal-byte/moby/api/types"
    12  	"github.com/Prakhar-Agarwal-byte/moby/api/types/filters"
    13  	"github.com/Prakhar-Agarwal-byte/moby/api/types/network"
    14  	swarmtypes "github.com/Prakhar-Agarwal-byte/moby/api/types/swarm"
    15  	"github.com/Prakhar-Agarwal-byte/moby/daemon/cluster/controllers/plugin"
    16  	"github.com/Prakhar-Agarwal-byte/moby/daemon/cluster/convert"
    17  	executorpkg "github.com/Prakhar-Agarwal-byte/moby/daemon/cluster/executor"
    18  	clustertypes "github.com/Prakhar-Agarwal-byte/moby/daemon/cluster/provider"
    19  	"github.com/Prakhar-Agarwal-byte/moby/libnetwork"
    20  	networktypes "github.com/Prakhar-Agarwal-byte/moby/libnetwork/types"
    21  	"github.com/moby/swarmkit/v2/agent"
    22  	"github.com/moby/swarmkit/v2/agent/exec"
    23  	"github.com/moby/swarmkit/v2/api"
    24  	"github.com/moby/swarmkit/v2/api/naming"
    25  	swarmlog "github.com/moby/swarmkit/v2/log"
    26  	"github.com/moby/swarmkit/v2/template"
    27  	"github.com/pkg/errors"
    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  		k, v, ok := strings.Cut(l, "=")
   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 ok {
   121  			labels[k] = v
   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  			swarmlog.G(ctx).WithField("NetworkAttachment", fmt.Sprintf("%#v", na)).Warn("skipping nil or malformed node network attachment entry")
   164  			continue
   165  		}
   166  
   167  		if na.Network.Spec.Ingress {
   168  			ingressNA = na
   169  		}
   170  
   171  		attachments[na.Network.ID] = na.Addresses[0]
   172  	}
   173  
   174  	// discover which, if any, attachments have been removed.
   175  	//
   176  	// we aren't responsible directly for creating these networks. that is
   177  	// handled indirectly when a container using that network is created.
   178  	// however, when it comes time to remove the network, none of the relevant
   179  	// tasks may exist anymore. this means we should go ahead and try to remove
   180  	// any network we know to no longer be in use.
   181  
   182  	// removeAttachments maps the network ID to a boolean. This boolean
   183  	// indicates whether the attachment in question is totally removed (true),
   184  	// or has just had its IP changed (false)
   185  	removeAttachments := make(map[string]bool)
   186  
   187  	// the first time we Configure, nodeObj wil be nil, because it will not be
   188  	// set yet. in that case, skip this check.
   189  	if e.nodeObj != nil {
   190  		for _, na := range e.nodeObj.Attachments {
   191  			// same thing as above, check sanity of the attachments so we don't
   192  			// get a panic.
   193  			if na == nil || na.Network == nil || len(na.Addresses) == 0 {
   194  				swarmlog.G(ctx).WithField("NetworkAttachment", fmt.Sprintf("%#v", na)).Warn("skipping nil or malformed node network attachment entry")
   195  				continue
   196  			}
   197  
   198  			// now, check if the attachment exists and shares the same IP address.
   199  			if ip, ok := attachments[na.Network.ID]; !ok || na.Addresses[0] != ip {
   200  				// if the map entry exists, then the network still exists, and the
   201  				// IP must be what has changed
   202  				removeAttachments[na.Network.ID] = !ok
   203  			}
   204  		}
   205  	}
   206  
   207  	if (ingressNA == nil) && (node.Attachment != nil) && (len(node.Attachment.Addresses) > 0) {
   208  		ingressNA = node.Attachment
   209  		attachments[ingressNA.Network.ID] = ingressNA.Addresses[0]
   210  	}
   211  
   212  	if ingressNA == nil {
   213  		e.backend.ReleaseIngress()
   214  		return e.backend.GetAttachmentStore().ResetAttachments(attachments)
   215  	}
   216  
   217  	options := types.NetworkCreate{
   218  		Driver: ingressNA.Network.DriverState.Name,
   219  		IPAM: &network.IPAM{
   220  			Driver: ingressNA.Network.IPAM.Driver.Name,
   221  		},
   222  		Options: ingressNA.Network.DriverState.Options,
   223  		Ingress: true,
   224  	}
   225  
   226  	for _, ic := range ingressNA.Network.IPAM.Configs {
   227  		c := network.IPAMConfig{
   228  			Subnet:  ic.Subnet,
   229  			IPRange: ic.Range,
   230  			Gateway: ic.Gateway,
   231  		}
   232  		options.IPAM.Config = append(options.IPAM.Config, c)
   233  	}
   234  
   235  	_, err := e.backend.SetupIngress(clustertypes.NetworkCreateRequest{
   236  		ID: ingressNA.Network.ID,
   237  		NetworkCreateRequest: types.NetworkCreateRequest{
   238  			Name:          ingressNA.Network.Spec.Annotations.Name,
   239  			NetworkCreate: options,
   240  		},
   241  	}, ingressNA.Addresses[0])
   242  	if err != nil {
   243  		return err
   244  	}
   245  
   246  	var (
   247  		activeEndpointsError *libnetwork.ActiveEndpointsError
   248  		errNoSuchNetwork     libnetwork.ErrNoSuchNetwork
   249  	)
   250  
   251  	// now, finally, remove any network LB attachments that we no longer have.
   252  	for nw, gone := range removeAttachments {
   253  		err := e.backend.DeleteManagedNetwork(nw)
   254  		switch {
   255  		case err == nil:
   256  			continue
   257  		case errors.As(err, &activeEndpointsError):
   258  			// this is the purpose of the boolean in the map. it's literally
   259  			// just to log an appropriate, informative error. i'm unsure if
   260  			// this can ever actually occur, but we need to know if it does.
   261  			if gone {
   262  				swarmlog.G(ctx).Warnf("network %s should be removed, but still has active attachments", nw)
   263  			} else {
   264  				swarmlog.G(ctx).Warnf("network %s should have its node LB IP changed, but cannot be removed because of active attachments", nw)
   265  			}
   266  			continue
   267  		case errors.As(err, &errNoSuchNetwork):
   268  			// NoSuchNetworkError indicates the network is already gone.
   269  			continue
   270  		default:
   271  			swarmlog.G(ctx).Errorf("network %s remove failed: %v", nw, err)
   272  		}
   273  	}
   274  
   275  	// now update our copy of the node object, reset the attachment store, and
   276  	// return
   277  	e.nodeObj = node
   278  
   279  	return e.backend.GetAttachmentStore().ResetAttachments(attachments)
   280  }
   281  
   282  // Controller returns a docker container runner.
   283  func (e *executor) Controller(t *api.Task) (exec.Controller, error) {
   284  	dependencyGetter := template.NewTemplatedDependencyGetter(agent.Restrict(e.dependencies, t), t, nil)
   285  
   286  	// Get the node description from the executor field
   287  	e.mutex.Lock()
   288  	nodeDescription := e.node
   289  	e.mutex.Unlock()
   290  
   291  	if t.Spec.GetAttachment() != nil {
   292  		return newNetworkAttacherController(e.backend, e.imageBackend, e.volumeBackend, t, nodeDescription, dependencyGetter)
   293  	}
   294  
   295  	var ctlr exec.Controller
   296  	switch r := t.Spec.GetRuntime().(type) {
   297  	case *api.TaskSpec_Generic:
   298  		swarmlog.G(context.TODO()).WithFields(log.Fields{
   299  			"kind":     r.Generic.Kind,
   300  			"type_url": r.Generic.Payload.TypeUrl,
   301  		}).Debug("custom runtime requested")
   302  		runtimeKind, err := naming.Runtime(t.Spec)
   303  		if err != nil {
   304  			return ctlr, err
   305  		}
   306  		switch runtimeKind {
   307  		case string(swarmtypes.RuntimePlugin):
   308  			if !e.backend.HasExperimental() {
   309  				return ctlr, fmt.Errorf("runtime type %q only supported in experimental", swarmtypes.RuntimePlugin)
   310  			}
   311  			c, err := plugin.NewController(e.pluginBackend, t)
   312  			if err != nil {
   313  				return ctlr, err
   314  			}
   315  			ctlr = c
   316  		default:
   317  			return ctlr, fmt.Errorf("unsupported runtime type: %q", runtimeKind)
   318  		}
   319  	case *api.TaskSpec_Container:
   320  		c, err := newController(e.backend, e.imageBackend, e.volumeBackend, t, nodeDescription, dependencyGetter)
   321  		if err != nil {
   322  			return ctlr, err
   323  		}
   324  		ctlr = c
   325  	default:
   326  		return ctlr, fmt.Errorf("unsupported runtime: %q", r)
   327  	}
   328  
   329  	return ctlr, nil
   330  }
   331  
   332  func (e *executor) SetNetworkBootstrapKeys(keys []*api.EncryptionKey) error {
   333  	nwKeys := []*networktypes.EncryptionKey{}
   334  	for _, key := range keys {
   335  		nwKey := &networktypes.EncryptionKey{
   336  			Subsystem:   key.Subsystem,
   337  			Algorithm:   int32(key.Algorithm),
   338  			Key:         make([]byte, len(key.Key)),
   339  			LamportTime: key.LamportTime,
   340  		}
   341  		copy(nwKey.Key, key.Key)
   342  		nwKeys = append(nwKeys, nwKey)
   343  	}
   344  	e.backend.SetNetworkBootstrapKeys(nwKeys)
   345  
   346  	return nil
   347  }
   348  
   349  func (e *executor) Secrets() exec.SecretsManager {
   350  	return e.dependencies.Secrets()
   351  }
   352  
   353  func (e *executor) Configs() exec.ConfigsManager {
   354  	return e.dependencies.Configs()
   355  }
   356  
   357  func (e *executor) Volumes() exec.VolumesManager {
   358  	return e.dependencies.Volumes()
   359  }
   360  
   361  type sortedPlugins []api.PluginDescription
   362  
   363  func (sp sortedPlugins) Len() int { return len(sp) }
   364  
   365  func (sp sortedPlugins) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
   366  
   367  func (sp sortedPlugins) Less(i, j int) bool {
   368  	if sp[i].Type != sp[j].Type {
   369  		return sp[i].Type < sp[j].Type
   370  	}
   371  	return sp[i].Name < sp[j].Name
   372  }