|
@@ -3,7 +3,7 @@ Package libnetwork provides the basic functionality and extension points to
|
|
create network namespaces and allocate interfaces for containers to use.
|
|
create network namespaces and allocate interfaces for containers to use.
|
|
|
|
|
|
// Create a new controller instance
|
|
// Create a new controller instance
|
|
- controller, _err := libnetwork.New()
|
|
|
|
|
|
+ controller, _err := libnetwork.New("/etc/default/libnetwork.toml")
|
|
|
|
|
|
// Select and configure the network driver
|
|
// Select and configure the network driver
|
|
networkType := "bridge"
|
|
networkType := "bridge"
|
|
@@ -47,13 +47,14 @@ package libnetwork
|
|
|
|
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
- "errors"
|
|
|
|
- "fmt"
|
|
|
|
|
|
+ "os"
|
|
|
|
+ "strings"
|
|
"sync"
|
|
"sync"
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
log "github.com/Sirupsen/logrus"
|
|
"github.com/docker/docker/pkg/plugins"
|
|
"github.com/docker/docker/pkg/plugins"
|
|
"github.com/docker/docker/pkg/stringid"
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
+ "github.com/docker/libnetwork/config"
|
|
"github.com/docker/libnetwork/datastore"
|
|
"github.com/docker/libnetwork/datastore"
|
|
"github.com/docker/libnetwork/driverapi"
|
|
"github.com/docker/libnetwork/driverapi"
|
|
"github.com/docker/libnetwork/sandbox"
|
|
"github.com/docker/libnetwork/sandbox"
|
|
@@ -61,9 +62,6 @@ import (
|
|
"github.com/docker/swarm/pkg/store"
|
|
"github.com/docker/swarm/pkg/store"
|
|
)
|
|
)
|
|
|
|
|
|
-// TODO: Move it to error.go once the error refactoring is done
|
|
|
|
-var ErrInvalidDatastore = errors.New("Datastore is not initialized")
|
|
|
|
-
|
|
|
|
// NetworkController provides the interface for controller instance which manages
|
|
// NetworkController provides the interface for controller instance which manages
|
|
// networks.
|
|
// networks.
|
|
type NetworkController interface {
|
|
type NetworkController interface {
|
|
@@ -104,12 +102,13 @@ type controller struct {
|
|
networks networkTable
|
|
networks networkTable
|
|
drivers driverTable
|
|
drivers driverTable
|
|
sandboxes sandboxTable
|
|
sandboxes sandboxTable
|
|
|
|
+ cfg *config.Config
|
|
store datastore.DataStore
|
|
store datastore.DataStore
|
|
sync.Mutex
|
|
sync.Mutex
|
|
}
|
|
}
|
|
|
|
|
|
// New creates a new instance of network controller.
|
|
// New creates a new instance of network controller.
|
|
-func New() (NetworkController, error) {
|
|
|
|
|
|
+func New(configFile string) (NetworkController, error) {
|
|
c := &controller{
|
|
c := &controller{
|
|
networks: networkTable{},
|
|
networks: networkTable{},
|
|
sandboxes: sandboxTable{},
|
|
sandboxes: sandboxTable{},
|
|
@@ -118,28 +117,53 @@ func New() (NetworkController, error) {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- if err := c.initDataStore(); err != nil {
|
|
|
|
- log.Errorf("Failed to Initialize Datastore : %v", err)
|
|
|
|
- // TODO : Should we fail if the initDataStore fail here ?
|
|
|
|
|
|
+ if err := c.initConfig(configFile); err == nil {
|
|
|
|
+ if err := c.initDataStore(); err != nil {
|
|
|
|
+ // Failing to initalize datastore is a bad situation to be in.
|
|
|
|
+ // But it cannot fail creating the Controller
|
|
|
|
+ log.Warnf("Failed to Initialize Datastore due to %v. Operating in non-clustered mode", err)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // Missing Configuration file is not a failure scenario
|
|
|
|
+ // But without that, datastore cannot be initialized.
|
|
|
|
+ log.Debugf("Unable to Parse LibNetwork Config file : %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
- go c.watchNewNetworks()
|
|
|
|
return c, nil
|
|
return c, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *controller) initDataStore() error {
|
|
|
|
- /* TODO : Duh ! make this configurable */
|
|
|
|
- config := &datastore.StoreConfiguration{}
|
|
|
|
- config.Provider = "consul"
|
|
|
|
- config.Addrs = []string{"localhost:8500"}
|
|
|
|
|
|
+const (
|
|
|
|
+ cfgFileEnv = "LIBNETWORK_CFG"
|
|
|
|
+ defaultCfgFile = "/etc/default/libnetwork.toml"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+func (c *controller) initConfig(configFile string) error {
|
|
|
|
+ cfgFile := configFile
|
|
|
|
+ if strings.Trim(cfgFile, " ") == "" {
|
|
|
|
+ cfgFile = os.Getenv(cfgFileEnv)
|
|
|
|
+ if strings.Trim(cfgFile, " ") == "" {
|
|
|
|
+ cfgFile = defaultCfgFile
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ cfg, err := config.ParseConfig(cfgFile)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return ErrInvalidConfigFile(cfgFile)
|
|
|
|
+ }
|
|
|
|
+ c.Lock()
|
|
|
|
+ c.cfg = cfg
|
|
|
|
+ c.Unlock()
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
|
|
- store, err := datastore.NewDataStore(config)
|
|
|
|
|
|
+func (c *controller) initDataStore() error {
|
|
|
|
+ store, err := datastore.NewDataStore(&c.cfg.Datastore)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
c.Lock()
|
|
c.Lock()
|
|
c.store = store
|
|
c.store = store
|
|
c.Unlock()
|
|
c.Unlock()
|
|
|
|
+ go c.watchNewNetworks()
|
|
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
@@ -233,7 +257,7 @@ func (c *controller) newNetworkFromStore(n *network) {
|
|
}
|
|
}
|
|
|
|
|
|
func (c *controller) addNetworkToStore(n *network) error {
|
|
func (c *controller) addNetworkToStore(n *network) error {
|
|
- if IsReservedNetwork(n.Name()) {
|
|
|
|
|
|
+ if isReservedNetwork(n.Name()) {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
if c.store == nil {
|
|
if c.store == nil {
|
|
@@ -263,7 +287,6 @@ func (c *controller) watchNewNetworks() {
|
|
// Skip any watch notification for a network that has not changed
|
|
// Skip any watch notification for a network that has not changed
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
- fmt.Printf("WATCHED : %v = %v\n", kve.Key(), n)
|
|
|
|
c.newNetworkFromStore(&n)
|
|
c.newNetworkFromStore(&n)
|
|
}
|
|
}
|
|
})
|
|
})
|