diff --git a/libnetwork/network.go b/libnetwork/network.go index 422d1f2062..e4b6e4adaa 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -63,6 +63,7 @@ type NetworkInfo interface { Scope() string IPv6Enabled() bool Internal() bool + Labels() map[string]string } // EndpointWalker is a client provided function which will be used to walk the Endpoints. @@ -150,6 +151,7 @@ type network struct { networkType string id string scope string + labels map[string]string ipamType string ipamOptions map[string]string addrSpace string @@ -309,6 +311,14 @@ func (n *network) CopyTo(o datastore.KVObject) error { dstN.internal = n.internal dstN.inDelete = n.inDelete + // copy labels + if dstN.labels == nil { + dstN.labels = make(map[string]string, len(n.labels)) + } + for k, v := range n.labels { + dstN.labels[k] = v + } + for _, v4conf := range n.ipamV4Config { dstV4Conf := &IpamConf{} v4conf.CopyTo(dstV4Conf) @@ -359,6 +369,7 @@ func (n *network) MarshalJSON() ([]byte, error) { netMap["id"] = n.id netMap["networkType"] = n.networkType netMap["scope"] = n.scope + netMap["labels"] = n.labels netMap["ipamType"] = n.ipamType netMap["addrSpace"] = n.addrSpace netMap["enableIPv6"] = n.enableIPv6 @@ -411,6 +422,15 @@ func (n *network) UnmarshalJSON(b []byte) (err error) { n.networkType = netMap["networkType"].(string) n.enableIPv6 = netMap["enableIPv6"].(bool) + // if we weren't unmarshaling to netMap we could simply set n.labels + // unfortunately, we can't because map[string]interface{} != map[string]string + if labels, ok := netMap["labels"].(map[string]interface{}); ok { + n.labels = make(map[string]string, len(labels)) + for label, value := range labels { + n.labels[label] = value.(string) + } + } + if v, ok := netMap["generic"]; ok { n.generic = v.(map[string]interface{}) // Restore opts in their map[string]string form @@ -539,7 +559,7 @@ func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ip } } -// NetworkOptionDriverOpts function returns an option setter for any parameter described by a map +// NetworkOptionDriverOpts function returns an option setter for any driver parameter described by a map func NetworkOptionDriverOpts(opts map[string]string) NetworkOption { return func(n *network) { if n.generic == nil { @@ -553,6 +573,13 @@ func NetworkOptionDriverOpts(opts map[string]string) NetworkOption { } } +// NetworkOptionLabels function returns an option setter for labels specific to a network +func NetworkOptionLabels(labels map[string]string) NetworkOption { + return func(n *network) { + n.labels = labels + } +} + // NetworkOptionDeferIPv6Alloc instructs the network to defer the IPV6 address allocation until after the endpoint has been created // It is being provided to support the specific docker daemon flags where user can deterministically assign an IPv6 address // to a container as combination of fixed-cidr-v6 + mac-address @@ -1285,3 +1312,15 @@ func (n *network) IPv6Enabled() bool { return n.enableIPv6 } + +func (n *network) Labels() map[string]string { + n.Lock() + defer n.Unlock() + + var lbls = make(map[string]string, len(n.labels)) + for k, v := range n.labels { + lbls[k] = v + } + + return lbls +}