浏览代码

Merge pull request #293 from mavenugo/dlabels

support for daemon labels
Jana Radhakrishnan 10 年之前
父节点
当前提交
c80ef08b9d
共有 3 个文件被更改,包括 46 次插入5 次删除
  1. 13 0
      libnetwork/config/config.go
  2. 22 0
      libnetwork/config/config_test.go
  3. 11 5
      libnetwork/netlabel/labels.go

+ 13 - 0
libnetwork/config/config.go

@@ -4,6 +4,7 @@ import (
 	"strings"
 
 	"github.com/BurntSushi/toml"
+	"github.com/docker/libnetwork/netlabel"
 )
 
 // Config encapsulates configurations of various Libnetwork components
@@ -18,6 +19,7 @@ type DaemonCfg struct {
 	Debug          bool
 	DefaultNetwork string
 	DefaultDriver  string
+	Labels         []string
 }
 
 // ClusterCfg represents cluster configuration
@@ -66,6 +68,17 @@ func OptionDefaultDriver(dd string) Option {
 	}
 }
 
+// OptionLabels function returns an option setter for labels
+func OptionLabels(labels []string) Option {
+	return func(c *Config) {
+		for _, label := range labels {
+			if strings.HasPrefix(label, netlabel.Prefix) {
+				c.Daemon.Labels = append(c.Daemon.Labels, label)
+			}
+		}
+	}
+}
+
 // OptionKVProvider function returns an option setter for kvstore provider
 func OptionKVProvider(provider string) Option {
 	return func(c *Config) {

+ 22 - 0
libnetwork/config/config_test.go

@@ -1,8 +1,10 @@
 package config
 
 import (
+	"strings"
 	"testing"
 
+	"github.com/docker/libnetwork/netlabel"
 	_ "github.com/docker/libnetwork/netutils"
 )
 
@@ -19,3 +21,23 @@ func TestConfig(t *testing.T) {
 		t.Fatal("Error parsing a valid configuration file :", err)
 	}
 }
+
+func TestOptionsLabels(t *testing.T) {
+	c := &Config{}
+	l := []string{
+		"com.docker.network.key1=value1",
+		"com.docker.storage.key1=value1",
+		"com.docker.network.driver.key1=value1",
+		"com.docker.network.driver.key2=value2",
+	}
+	f := OptionLabels(l)
+	f(c)
+	if len(c.Daemon.Labels) != 3 {
+		t.Fatalf("Expecting 3 labels, seen %d", len(c.Daemon.Labels))
+	}
+	for _, l := range c.Daemon.Labels {
+		if !strings.HasPrefix(l, netlabel.Prefix) {
+			t.Fatalf("config must accept only libnetwork labels. Not : %s", l)
+		}
+	}
+}

+ 11 - 5
libnetwork/netlabel/labels.go

@@ -1,18 +1,24 @@
 package netlabel
 
 const (
+	// Prefix constant marks the reserved label space for libnetwork
+	Prefix = "com.docker.network"
+
+	// DriverPrefix constant marks the reserved label space for libnetwork drivers
+	DriverPrefix = Prefix + ".driver"
+
 	// GenericData constant that helps to identify an option as a Generic constant
-	GenericData = "io.docker.network.generic"
+	GenericData = Prefix + ".generic"
 
 	// PortMap constant represents Port Mapping
-	PortMap = "io.docker.network.endpoint.portmap"
+	PortMap = Prefix + ".portmap"
 
 	// MacAddress constant represents Mac Address config of a Container
-	MacAddress = "io.docker.network.endpoint.macaddress"
+	MacAddress = Prefix + ".endpoint.macaddress"
 
 	// ExposedPorts constant represents exposedports of a Container
-	ExposedPorts = "io.docker.network.endpoint.exposedports"
+	ExposedPorts = Prefix + ".endpoint.exposedports"
 
 	//EnableIPv6 constant represents enabling IPV6 at network level
-	EnableIPv6 = "io.docker.network.enable_ipv6"
+	EnableIPv6 = Prefix + ".enable_ipv6"
 )