github.com/kobeld/docker@v1.12.0-rc1/daemon/cluster/executor/container/executor.go (about)

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