2016-06-13 22:52:49 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2016-07-25 13:38:24 -04:00
|
|
|
"sort"
|
2016-06-13 22:52:49 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-11-23 07:58:15 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2016-06-13 22:52:49 -04:00
|
|
|
executorpkg "github.com/docker/docker/daemon/cluster/executor"
|
|
|
|
clustertypes "github.com/docker/docker/daemon/cluster/provider"
|
|
|
|
networktypes "github.com/docker/libnetwork/types"
|
|
|
|
"github.com/docker/swarmkit/agent/exec"
|
2016-10-27 13:34:58 -04:00
|
|
|
"github.com/docker/swarmkit/agent/secrets"
|
2016-06-13 22:52:49 -04:00
|
|
|
"github.com/docker/swarmkit/api"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type executor struct {
|
|
|
|
backend executorpkg.Backend
|
2016-10-27 13:34:58 -04:00
|
|
|
secrets exec.SecretsManager
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecutor returns an executor from the docker client.
|
|
|
|
func NewExecutor(b executorpkg.Backend) exec.Executor {
|
|
|
|
return &executor{
|
|
|
|
backend: b,
|
2016-10-27 13:34:58 -04:00
|
|
|
secrets: secrets.NewManager(),
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe returns the underlying node description from the docker client.
|
|
|
|
func (e *executor) Describe(ctx context.Context) (*api.NodeDescription, error) {
|
|
|
|
info, err := e.backend.SystemInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:16:31 -04:00
|
|
|
plugins := map[api.PluginDescription]struct{}{}
|
2016-06-13 22:52:49 -04:00
|
|
|
addPlugins := func(typ string, names []string) {
|
|
|
|
for _, name := range names {
|
2016-07-25 20:16:31 -04:00
|
|
|
plugins[api.PluginDescription{
|
2016-06-13 22:52:49 -04:00
|
|
|
Type: typ,
|
|
|
|
Name: name,
|
2016-07-25 20:16:31 -04:00
|
|
|
}] = struct{}{}
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:20:37 -05:00
|
|
|
// add v1 plugins
|
2016-06-13 22:52:49 -04:00
|
|
|
addPlugins("Volume", info.Plugins.Volume)
|
|
|
|
// Add builtin driver "overlay" (the only builtin multi-host driver) to
|
|
|
|
// the plugin list by default.
|
|
|
|
addPlugins("Network", append([]string{"overlay"}, info.Plugins.Network...))
|
|
|
|
addPlugins("Authorization", info.Plugins.Authorization)
|
|
|
|
|
2016-12-13 20:20:37 -05:00
|
|
|
// add v2 plugins
|
2016-11-23 07:58:15 -05:00
|
|
|
v2Plugins, err := e.backend.PluginManager().List(filters.NewArgs())
|
2016-12-13 20:20:37 -05:00
|
|
|
if err == nil {
|
|
|
|
for _, plgn := range v2Plugins {
|
|
|
|
for _, typ := range plgn.Config.Interface.Types {
|
|
|
|
if typ.Prefix != "docker" || !plgn.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
plgnTyp := typ.Capability
|
|
|
|
if typ.Capability == "volumedriver" {
|
|
|
|
plgnTyp = "Volume"
|
|
|
|
} else if typ.Capability == "networkdriver" {
|
|
|
|
plgnTyp = "Network"
|
|
|
|
}
|
|
|
|
plugins[api.PluginDescription{
|
|
|
|
Type: plgnTyp,
|
2016-12-12 18:05:53 -05:00
|
|
|
Name: plgn.Name,
|
2016-12-13 20:20:37 -05:00
|
|
|
}] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:16:31 -04:00
|
|
|
pluginFields := make([]api.PluginDescription, 0, len(plugins))
|
|
|
|
for k := range plugins {
|
|
|
|
pluginFields = append(pluginFields, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sortedPlugins(pluginFields))
|
2016-07-25 13:38:24 -04:00
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// parse []string labels into a map[string]string
|
|
|
|
labels := map[string]string{}
|
|
|
|
for _, l := range info.Labels {
|
|
|
|
stringSlice := strings.SplitN(l, "=", 2)
|
|
|
|
// this will take the last value in the list for a given key
|
|
|
|
// ideally, one shouldn't assign multiple values to the same key
|
|
|
|
if len(stringSlice) > 1 {
|
|
|
|
labels[stringSlice[0]] = stringSlice[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
description := &api.NodeDescription{
|
|
|
|
Hostname: info.Name,
|
|
|
|
Platform: &api.Platform{
|
|
|
|
Architecture: info.Architecture,
|
|
|
|
OS: info.OSType,
|
|
|
|
},
|
|
|
|
Engine: &api.EngineDescription{
|
|
|
|
EngineVersion: info.ServerVersion,
|
|
|
|
Labels: labels,
|
2016-07-25 20:16:31 -04:00
|
|
|
Plugins: pluginFields,
|
2016-06-13 22:52:49 -04:00
|
|
|
},
|
|
|
|
Resources: &api.Resources{
|
|
|
|
NanoCPUs: int64(info.NCPU) * 1e9,
|
|
|
|
MemoryBytes: info.MemTotal,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return description, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) Configure(ctx context.Context, node *api.Node) error {
|
|
|
|
na := node.Attachment
|
|
|
|
if na == nil {
|
2017-03-09 14:52:25 -05:00
|
|
|
e.backend.ReleaseIngress()
|
2016-06-13 22:52:49 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
options := types.NetworkCreate{
|
|
|
|
Driver: na.Network.DriverState.Name,
|
2016-08-31 11:25:14 -04:00
|
|
|
IPAM: &network.IPAM{
|
2016-06-13 22:52:49 -04:00
|
|
|
Driver: na.Network.IPAM.Driver.Name,
|
|
|
|
},
|
|
|
|
Options: na.Network.DriverState.Options,
|
2017-03-09 14:52:25 -05:00
|
|
|
Ingress: true,
|
2016-06-13 22:52:49 -04:00
|
|
|
CheckDuplicate: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ic := range na.Network.IPAM.Configs {
|
|
|
|
c := network.IPAMConfig{
|
|
|
|
Subnet: ic.Subnet,
|
|
|
|
IPRange: ic.Range,
|
|
|
|
Gateway: ic.Gateway,
|
|
|
|
}
|
|
|
|
options.IPAM.Config = append(options.IPAM.Config, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.backend.SetupIngress(clustertypes.NetworkCreateRequest{
|
2016-11-29 06:36:56 -05:00
|
|
|
ID: na.Network.ID,
|
|
|
|
NetworkCreateRequest: types.NetworkCreateRequest{
|
2016-06-13 22:52:49 -04:00
|
|
|
Name: na.Network.Spec.Annotations.Name,
|
|
|
|
NetworkCreate: options,
|
|
|
|
},
|
|
|
|
}, na.Addresses[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Controller returns a docker container runner.
|
2016-10-27 13:34:58 -04:00
|
|
|
func (e *executor) Controller(t *api.Task) (exec.Controller, error) {
|
2016-08-23 19:50:15 -04:00
|
|
|
if t.Spec.GetAttachment() != nil {
|
2016-10-27 13:34:58 -04:00
|
|
|
return newNetworkAttacherController(e.backend, t, e.secrets)
|
2016-08-23 19:50:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-14 09:53:02 -04:00
|
|
|
ctlr, err := newController(e.backend, t, secrets.Restrict(e.secrets, t))
|
2016-06-13 22:52:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctlr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) SetNetworkBootstrapKeys(keys []*api.EncryptionKey) error {
|
|
|
|
nwKeys := []*networktypes.EncryptionKey{}
|
|
|
|
for _, key := range keys {
|
|
|
|
nwKey := &networktypes.EncryptionKey{
|
|
|
|
Subsystem: key.Subsystem,
|
|
|
|
Algorithm: int32(key.Algorithm),
|
|
|
|
Key: make([]byte, len(key.Key)),
|
|
|
|
LamportTime: key.LamportTime,
|
|
|
|
}
|
|
|
|
copy(nwKey.Key, key.Key)
|
|
|
|
nwKeys = append(nwKeys, nwKey)
|
|
|
|
}
|
|
|
|
e.backend.SetNetworkBootstrapKeys(nwKeys)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-25 13:38:24 -04:00
|
|
|
|
2016-10-27 13:34:58 -04:00
|
|
|
func (e *executor) Secrets() exec.SecretsManager {
|
|
|
|
return e.secrets
|
|
|
|
}
|
|
|
|
|
2016-07-25 13:38:24 -04:00
|
|
|
type sortedPlugins []api.PluginDescription
|
|
|
|
|
|
|
|
func (sp sortedPlugins) Len() int { return len(sp) }
|
|
|
|
|
|
|
|
func (sp sortedPlugins) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
|
|
|
|
|
|
|
|
func (sp sortedPlugins) Less(i, j int) bool {
|
|
|
|
if sp[i].Type != sp[j].Type {
|
|
|
|
return sp[i].Type < sp[j].Type
|
|
|
|
}
|
|
|
|
return sp[i].Name < sp[j].Name
|
|
|
|
}
|