2015-05-04 17:55:36 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-09-18 15:54:08 -04:00
|
|
|
"errors"
|
2015-05-04 17:55:36 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-09-18 15:54:08 -04:00
|
|
|
"net"
|
2015-05-04 17:55:36 -04:00
|
|
|
"net/http"
|
2015-09-16 20:49:34 -04:00
|
|
|
"net/http/httptest"
|
2015-05-04 17:55:36 -04:00
|
|
|
"os"
|
2015-09-23 22:31:50 -04:00
|
|
|
"os/signal"
|
2015-05-04 17:55:36 -04:00
|
|
|
"strings"
|
2015-09-23 22:31:50 -04:00
|
|
|
"syscall"
|
2015-09-18 15:54:08 -04:00
|
|
|
"time"
|
2015-05-04 17:55:36 -04:00
|
|
|
|
2016-05-24 19:17:19 -04:00
|
|
|
"github.com/BurntSushi/toml"
|
2015-09-16 01:26:27 -04:00
|
|
|
"github.com/codegangsta/cli"
|
2015-12-23 18:07:33 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2015-09-18 15:54:08 -04:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
2015-06-05 16:31:12 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-05-04 17:55:36 -04:00
|
|
|
|
2016-09-07 16:32:35 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2015-05-04 17:55:36 -04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
"github.com/docker/libnetwork/api"
|
2017-05-25 13:45:38 -04:00
|
|
|
"github.com/docker/libnetwork/cluster"
|
2015-06-11 09:52:46 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-10-05 07:21:15 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-09-16 20:49:34 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-06-14 12:00:27 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2016-02-26 17:58:11 -05:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-06-14 12:00:27 -04:00
|
|
|
"github.com/docker/libnetwork/options"
|
2015-10-05 17:53:25 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-04 17:55:36 -04:00
|
|
|
"github.com/gorilla/mux"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-28 14:14:46 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-05-04 17:55:36 -04:00
|
|
|
)
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
const (
|
2015-05-04 17:55:36 -04:00
|
|
|
// DefaultHTTPHost is used if only port is provided to -H flag e.g. docker -d -H tcp://:8080
|
2015-09-08 22:26:54 -04:00
|
|
|
DefaultHTTPHost = "0.0.0.0"
|
2015-05-04 17:55:36 -04:00
|
|
|
// DefaultHTTPPort is the default http port used by dnet
|
|
|
|
DefaultHTTPPort = 2385
|
|
|
|
// DefaultUnixSocket exported
|
|
|
|
DefaultUnixSocket = "/var/run/dnet.sock"
|
2015-06-11 09:52:46 -04:00
|
|
|
cfgFileEnv = "LIBNETWORK_CFG"
|
|
|
|
defaultCfgFile = "/etc/default/libnetwork.toml"
|
2015-09-18 15:54:08 -04:00
|
|
|
defaultHeartbeat = time.Duration(10) * time.Second
|
|
|
|
ttlFactor = 2
|
2015-05-04 17:55:36 -04:00
|
|
|
)
|
|
|
|
|
2015-09-16 01:26:27 -04:00
|
|
|
var epConn *dnetConnection
|
|
|
|
|
2015-05-04 17:55:36 -04:00
|
|
|
func main() {
|
2015-06-05 16:31:12 -04:00
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:55:36 -04:00
|
|
|
_, stdout, stderr := term.StdStreams()
|
|
|
|
logrus.SetOutput(stderr)
|
|
|
|
|
2015-09-16 01:26:27 -04:00
|
|
|
err := dnetApp(stdout, stderr)
|
2015-05-04 17:55:36 -04:00
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:17:19 -04:00
|
|
|
// ParseConfig parses the libnetwork configuration file
|
|
|
|
func (d *dnetConnection) parseOrchestrationConfig(tomlCfgFile string) error {
|
|
|
|
dummy := &dnetConnection{}
|
|
|
|
|
|
|
|
if _, err := toml.DecodeFile(tomlCfgFile, dummy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dummy.Orchestration != nil {
|
|
|
|
d.Orchestration = dummy.Orchestration
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnetConnection) parseConfig(cfgFile string) (*config.Config, error) {
|
2015-06-11 09:52:46 -04:00
|
|
|
if strings.Trim(cfgFile, " ") == "" {
|
|
|
|
cfgFile = os.Getenv(cfgFileEnv)
|
|
|
|
if strings.Trim(cfgFile, " ") == "" {
|
|
|
|
cfgFile = defaultCfgFile
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 19:17:19 -04:00
|
|
|
|
|
|
|
if err := d.parseOrchestrationConfig(cfgFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-11 09:52:46 -04:00
|
|
|
return config.ParseConfig(cfgFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processConfig(cfg *config.Config) []config.Option {
|
|
|
|
options := []config.Option{}
|
|
|
|
if cfg == nil {
|
|
|
|
return options
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
|
2015-06-14 12:00:27 -04:00
|
|
|
dn := "bridge"
|
2015-06-11 09:52:46 -04:00
|
|
|
if strings.TrimSpace(cfg.Daemon.DefaultNetwork) != "" {
|
2015-06-14 12:00:27 -04:00
|
|
|
dn = cfg.Daemon.DefaultNetwork
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
2015-06-14 12:00:27 -04:00
|
|
|
options = append(options, config.OptionDefaultNetwork(dn))
|
|
|
|
|
|
|
|
dd := "bridge"
|
2015-06-11 09:52:46 -04:00
|
|
|
if strings.TrimSpace(cfg.Daemon.DefaultDriver) != "" {
|
2015-06-14 12:00:27 -04:00
|
|
|
dd = cfg.Daemon.DefaultDriver
|
|
|
|
}
|
|
|
|
options = append(options, config.OptionDefaultDriver(dd))
|
|
|
|
|
|
|
|
if cfg.Daemon.Labels != nil {
|
|
|
|
options = append(options, config.OptionLabels(cfg.Daemon.Labels))
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
|
|
|
|
if dcfg, ok := cfg.Scopes[datastore.GlobalScope]; ok && dcfg.IsValid() {
|
|
|
|
options = append(options, config.OptionKVProvider(dcfg.Client.Provider))
|
|
|
|
options = append(options, config.OptionKVProviderURL(dcfg.Client.Address))
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
dOptions, err := startDiscovery(&cfg.Cluster)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Infof("Skipping discovery : %s", err.Error())
|
|
|
|
} else {
|
|
|
|
options = append(options, dOptions...)
|
|
|
|
}
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
func startDiscovery(cfg *config.ClusterCfg) ([]config.Option, error) {
|
|
|
|
if cfg == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
return nil, errors.New("discovery requires a valid configuration")
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hb := time.Duration(cfg.Heartbeat) * time.Second
|
|
|
|
if hb == 0 {
|
|
|
|
hb = defaultHeartbeat
|
|
|
|
}
|
2017-03-12 20:47:46 -04:00
|
|
|
logrus.Infof("discovery : %s %s", cfg.Discovery, hb.String())
|
2015-12-23 18:07:33 -05:00
|
|
|
d, err := discovery.New(cfg.Discovery, hb, ttlFactor*hb, map[string]string{})
|
2015-09-18 15:54:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Address == "" {
|
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil || len(addrs) == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ip, _, _ := net.ParseCIDR(addrs[0].String())
|
|
|
|
cfg.Address = ip.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip := net.ParseIP(cfg.Address); ip == nil {
|
|
|
|
return nil, errors.New("address config should be either ipv4 or ipv6 address")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Register(cfg.Address + ":0"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := []config.Option{config.OptionDiscoveryWatcher(d), config.OptionDiscoveryAddress(cfg.Address)}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(hb):
|
|
|
|
if err := d.Register(cfg.Address + ":0"); err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 01:26:27 -04:00
|
|
|
func dnetApp(stdout, stderr io.Writer) error {
|
|
|
|
app := cli.NewApp()
|
2015-05-04 17:55:36 -04:00
|
|
|
|
2015-09-16 01:26:27 -04:00
|
|
|
app.Name = "dnet"
|
|
|
|
app.Usage = "A self-sufficient runtime for container networking."
|
|
|
|
app.Flags = dnetFlags
|
|
|
|
app.Before = processFlags
|
|
|
|
app.Commands = dnetCommands
|
2015-05-04 17:55:36 -04:00
|
|
|
|
2015-09-16 01:26:27 -04:00
|
|
|
app.Run(os.Args)
|
2015-05-04 17:55:36 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-14 12:00:27 -04:00
|
|
|
func createDefaultNetwork(c libnetwork.NetworkController) {
|
|
|
|
nw := c.Config().Daemon.DefaultNetwork
|
|
|
|
d := c.Config().Daemon.DefaultDriver
|
|
|
|
createOptions := []libnetwork.NetworkOption{}
|
|
|
|
genericOption := options.Generic{}
|
|
|
|
|
|
|
|
if nw != "" && d != "" {
|
|
|
|
// Bridge driver is special due to legacy reasons
|
|
|
|
if d == "bridge" {
|
2015-10-06 16:51:39 -04:00
|
|
|
genericOption[netlabel.GenericData] = map[string]string{
|
2015-10-08 22:27:14 -04:00
|
|
|
"BridgeName": "docker0",
|
|
|
|
"DefaultBridge": "true",
|
2015-06-14 12:00:27 -04:00
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
createOptions = append(createOptions,
|
|
|
|
libnetwork.NetworkOptionGeneric(genericOption),
|
2015-10-05 17:53:25 -04:00
|
|
|
ipamOption(nw))
|
|
|
|
}
|
|
|
|
|
2015-10-08 22:27:14 -04:00
|
|
|
if n, err := c.NetworkByName(nw); err == nil {
|
|
|
|
logrus.Debugf("Default network %s already present. Deleting it", nw)
|
|
|
|
if err = n.Delete(); err != nil {
|
|
|
|
logrus.Debugf("Network could not be deleted: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-14 12:00:27 -04:00
|
|
|
}
|
2015-10-08 22:27:14 -04:00
|
|
|
|
2016-02-29 14:49:04 -05:00
|
|
|
_, err := c.NewNetwork(d, nw, "", createOptions...)
|
2015-06-14 12:00:27 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error creating default network : %s : %v", nw, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:55:36 -04:00
|
|
|
type dnetConnection struct {
|
|
|
|
// proto holds the client protocol i.e. unix.
|
|
|
|
proto string
|
|
|
|
// addr holds the client address.
|
2016-05-24 19:17:19 -04:00
|
|
|
addr string
|
|
|
|
Orchestration *NetworkOrchestration
|
2017-05-25 13:45:38 -04:00
|
|
|
configEvent chan cluster.ConfigEventType
|
2016-05-24 19:17:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkOrchestration exported
|
|
|
|
type NetworkOrchestration struct {
|
|
|
|
Agent bool
|
|
|
|
Manager bool
|
|
|
|
Bind string
|
|
|
|
Peer string
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 01:26:27 -04:00
|
|
|
func (d *dnetConnection) dnetDaemon(cfgFile string) error {
|
2015-09-16 20:49:34 -04:00
|
|
|
if err := startTestDriver(); err != nil {
|
2016-11-14 19:41:54 -05:00
|
|
|
return fmt.Errorf("failed to start test driver: %v", err)
|
2015-09-16 20:49:34 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 19:17:19 -04:00
|
|
|
cfg, err := d.parseConfig(cfgFile)
|
2015-06-11 09:52:46 -04:00
|
|
|
var cOptions []config.Option
|
|
|
|
if err == nil {
|
|
|
|
cOptions = processConfig(cfg)
|
2016-05-24 19:17:19 -04:00
|
|
|
} else {
|
|
|
|
logrus.Errorf("Error parsing config %v", err)
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
2015-10-28 22:10:38 -04:00
|
|
|
|
|
|
|
bridgeConfig := options.Generic{
|
|
|
|
"EnableIPForwarding": true,
|
|
|
|
"EnableIPTables": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
bridgeOption := options.Generic{netlabel.GenericData: bridgeConfig}
|
|
|
|
|
|
|
|
cOptions = append(cOptions, config.OptionDriverConfig("bridge", bridgeOption))
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
controller, err := libnetwork.New(cOptions...)
|
2015-05-04 17:55:36 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error starting dnetDaemon :", err)
|
|
|
|
return err
|
|
|
|
}
|
2016-05-24 19:17:19 -04:00
|
|
|
controller.SetClusterProvider(d)
|
|
|
|
|
|
|
|
if d.Orchestration.Agent || d.Orchestration.Manager {
|
2017-05-25 13:45:38 -04:00
|
|
|
d.configEvent <- cluster.EventNodeReady
|
2016-05-24 19:17:19 -04:00
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
|
2015-06-14 12:00:27 -04:00
|
|
|
createDefaultNetwork(controller)
|
2015-05-04 17:55:36 -04:00
|
|
|
httpHandler := api.NewHTTPHandler(controller)
|
|
|
|
r := mux.NewRouter().StrictSlash(false)
|
2015-05-23 15:20:30 -04:00
|
|
|
post := r.PathPrefix("/{.*}/networks").Subrouter()
|
2015-05-19 16:04:16 -04:00
|
|
|
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
2015-05-24 12:30:41 -04:00
|
|
|
post = r.PathPrefix("/networks").Subrouter()
|
|
|
|
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
2015-06-11 11:30:45 -04:00
|
|
|
post = r.PathPrefix("/{.*}/services").Subrouter()
|
|
|
|
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
|
|
|
post = r.PathPrefix("/services").Subrouter()
|
|
|
|
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
2015-09-08 02:18:50 -04:00
|
|
|
post = r.PathPrefix("/{.*}/sandboxes").Subrouter()
|
|
|
|
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
|
|
|
post = r.PathPrefix("/sandboxes").Subrouter()
|
|
|
|
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
2015-09-23 22:31:50 -04:00
|
|
|
|
|
|
|
handleSignals(controller)
|
2015-10-05 07:21:15 -04:00
|
|
|
setupDumpStackTrap()
|
2015-09-23 22:31:50 -04:00
|
|
|
|
2015-05-04 17:55:36 -04:00
|
|
|
return http.ListenAndServe(d.addr, r)
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:17:19 -04:00
|
|
|
func (d *dnetConnection) IsManager() bool {
|
|
|
|
return d.Orchestration.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnetConnection) IsAgent() bool {
|
|
|
|
return d.Orchestration.Agent
|
|
|
|
}
|
|
|
|
|
2016-07-19 21:17:30 -04:00
|
|
|
func (d *dnetConnection) GetAdvertiseAddress() string {
|
|
|
|
return d.Orchestration.Bind
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:51:01 -04:00
|
|
|
func (d *dnetConnection) GetDataPathAddress() string {
|
|
|
|
return d.Orchestration.Bind
|
|
|
|
}
|
|
|
|
|
2016-07-19 21:17:30 -04:00
|
|
|
func (d *dnetConnection) GetLocalAddress() string {
|
2016-05-24 19:17:19 -04:00
|
|
|
return d.Orchestration.Bind
|
|
|
|
}
|
|
|
|
|
2016-09-22 14:38:35 -04:00
|
|
|
func (d *dnetConnection) GetListenAddress() string {
|
|
|
|
return d.Orchestration.Bind
|
|
|
|
}
|
|
|
|
|
2017-04-27 19:58:42 -04:00
|
|
|
func (d *dnetConnection) GetRemoteAddressList() []string {
|
|
|
|
return []string{d.Orchestration.Peer}
|
2016-05-24 19:17:19 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:45:38 -04:00
|
|
|
func (d *dnetConnection) ListenClusterEvents() <-chan cluster.ConfigEventType {
|
2016-05-24 19:17:19 -04:00
|
|
|
return d.configEvent
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:36:12 -04:00
|
|
|
func (d *dnetConnection) AttachNetwork(string, string, []string) (*network.NetworkingConfig, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnetConnection) DetachNetwork(string, string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnetConnection) UpdateAttachment(string, string, *network.NetworkingConfig) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-31 15:22:36 -04:00
|
|
|
func (d *dnetConnection) WaitForDetachment(context.Context, string, string, string, string) error {
|
2016-08-22 21:36:12 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 22:31:50 -04:00
|
|
|
func handleSignals(controller libnetwork.NetworkController) {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signals := []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT}
|
|
|
|
signal.Notify(c, signals...)
|
|
|
|
go func() {
|
2016-02-12 08:07:00 -05:00
|
|
|
for range c {
|
2015-09-23 22:31:50 -04:00
|
|
|
controller.Stop()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:49:34 -04:00
|
|
|
func startTestDriver() error {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
if server == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
return errors.New("Failed to start an HTTP Server")
|
2015-09-16 20:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, `{"Implements": ["%s"]}`, driverapi.NetworkPluginEndpointType)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2016-11-21 20:29:53 -05:00
|
|
|
fmt.Fprint(w, `{"Scope":"global"}`)
|
2015-09-16 20:49:34 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2016-11-21 20:29:53 -05:00
|
|
|
fmt.Fprint(w, "null")
|
2015-09-16 20:49:34 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2016-11-21 20:29:53 -05:00
|
|
|
fmt.Fprint(w, "null")
|
2015-09-16 20:49:34 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.CreateEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2016-11-21 20:29:53 -05:00
|
|
|
fmt.Fprint(w, "null")
|
2015-09-16 20:49:34 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.DeleteEndpoint", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2016-11-21 20:29:53 -05:00
|
|
|
fmt.Fprint(w, "null")
|
2015-09-16 20:49:34 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.Join", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2016-11-21 20:29:53 -05:00
|
|
|
fmt.Fprint(w, "null")
|
2015-09-16 20:49:34 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.Leave", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
2016-11-21 20:29:53 -05:00
|
|
|
fmt.Fprint(w, "null")
|
2015-09-16 20:49:34 -04:00
|
|
|
})
|
|
|
|
|
2015-09-30 11:16:58 -04:00
|
|
|
if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
|
2015-09-16 20:49:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-20 11:57:02 -04:00
|
|
|
return ioutil.WriteFile("/etc/docker/plugins/test.spec", []byte(server.URL), 0644)
|
2015-09-16 20:49:34 -04:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:55:36 -04:00
|
|
|
func newDnetConnection(val string) (*dnetConnection, error) {
|
2016-04-08 15:53:46 -04:00
|
|
|
url, err := opts.ParseHost(false, val)
|
2015-05-04 17:55:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
protoAddrParts := strings.SplitN(url, "://", 2)
|
|
|
|
if len(protoAddrParts) != 2 {
|
2016-11-21 20:29:53 -05:00
|
|
|
return nil, errors.New("bad format, expected tcp://ADDR")
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
if strings.ToLower(protoAddrParts[0]) != "tcp" {
|
2016-11-21 20:29:53 -05:00
|
|
|
return nil, errors.New("dnet currently only supports tcp transport")
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:45:38 -04:00
|
|
|
return &dnetConnection{protoAddrParts[0], protoAddrParts[1], &NetworkOrchestration{}, make(chan cluster.ConfigEventType, 10)}, nil
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 11:42:14 -04:00
|
|
|
func (d *dnetConnection) httpCall(method, path string, data interface{}, headers map[string][]string) (io.ReadCloser, http.Header, int, error) {
|
2015-05-04 17:55:36 -04:00
|
|
|
var in io.Reader
|
|
|
|
in, err := encodeData(data)
|
|
|
|
if err != nil {
|
2015-06-15 11:42:14 -04:00
|
|
|
return nil, nil, -1, err
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
2015-05-24 12:30:41 -04:00
|
|
|
req, err := http.NewRequest(method, fmt.Sprintf("%s", path), in)
|
2015-05-04 17:55:36 -04:00
|
|
|
if err != nil {
|
2015-06-15 11:42:14 -04:00
|
|
|
return nil, nil, -1, err
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setupRequestHeaders(method, data, req, headers)
|
|
|
|
|
|
|
|
req.URL.Host = d.addr
|
|
|
|
req.URL.Scheme = "http"
|
|
|
|
|
|
|
|
httpClient := &http.Client{}
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
statusCode := -1
|
|
|
|
if resp != nil {
|
|
|
|
statusCode = resp.StatusCode
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-06-15 11:42:14 -04:00
|
|
|
return nil, nil, statusCode, fmt.Errorf("error when trying to connect: %v", err)
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if statusCode < 200 || statusCode >= 400 {
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2015-06-15 11:42:14 -04:00
|
|
|
return nil, nil, statusCode, err
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
2015-06-15 11:42:14 -04:00
|
|
|
return nil, nil, statusCode, fmt.Errorf("error : %s", bytes.TrimSpace(body))
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 11:42:14 -04:00
|
|
|
return resp.Body, resp.Header, statusCode, nil
|
2015-05-04 17:55:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupRequestHeaders(method string, data interface{}, req *http.Request, headers map[string][]string) {
|
|
|
|
if data != nil {
|
|
|
|
if headers == nil {
|
|
|
|
headers = make(map[string][]string)
|
|
|
|
}
|
|
|
|
headers["Content-Type"] = []string{"application/json"}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedPayload := (method == "POST" || method == "PUT")
|
|
|
|
|
|
|
|
if expectedPayload && req.Header.Get("Content-Type") == "" {
|
|
|
|
req.Header.Set("Content-Type", "text/plain")
|
|
|
|
}
|
|
|
|
|
|
|
|
if headers != nil {
|
|
|
|
for k, v := range headers {
|
|
|
|
req.Header[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeData(data interface{}) (*bytes.Buffer, error) {
|
|
|
|
params := bytes.NewBuffer(nil)
|
|
|
|
if data != nil {
|
|
|
|
if err := json.NewEncoder(params).Encode(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return params, nil
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
|
|
|
|
func ipamOption(bridgeName string) libnetwork.NetworkOption {
|
2016-09-17 01:40:44 -04:00
|
|
|
if nws, _, err := netutils.ElectInterfaceAddresses(bridgeName); err == nil {
|
|
|
|
ipamV4Conf := &libnetwork.IpamConf{PreferredPool: nws[0].String()}
|
|
|
|
hip, _ := types.GetHostPartIP(nws[0].IP, nws[0].Mask)
|
2015-10-05 17:53:25 -04:00
|
|
|
if hip.IsGlobalUnicast() {
|
2016-09-17 01:40:44 -04:00
|
|
|
ipamV4Conf.Gateway = nws[0].IP.String()
|
2015-10-05 17:53:25 -04:00
|
|
|
}
|
2016-01-07 15:57:22 -05:00
|
|
|
return libnetwork.NetworkOptionIpam("default", "", []*libnetwork.IpamConf{ipamV4Conf}, nil, nil)
|
2015-10-05 17:53:25 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|