2015-09-25 06:19:17 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-07-24 18:34:45 -04:00
|
|
|
"fmt"
|
2015-09-25 06:19:17 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-07-25 06:15:54 -04:00
|
|
|
"github.com/docker/docker/errors"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
|
|
|
"github.com/docker/engine-api/types/network"
|
2015-09-25 06:19:17 -04:00
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := r.Form.Get("filters")
|
|
|
|
netFilters, err := filters.FromParam(filter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
list := []types.NetworkResource{}
|
2015-09-25 06:19:17 -04:00
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
if nr, err := n.clusterProvider.GetNetworks(); err == nil {
|
|
|
|
for _, nw := range nr {
|
|
|
|
list = append(list, nw)
|
|
|
|
}
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
2015-11-10 03:57:06 -05:00
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// Combine the network list returned by Docker daemon if it is not already
|
|
|
|
// returned by the cluster manager
|
|
|
|
SKIP:
|
|
|
|
for _, nw := range n.backend.GetNetworks() {
|
|
|
|
for _, nl := range list {
|
|
|
|
if nl.ID == nw.ID() {
|
|
|
|
continue SKIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list = append(list, *n.buildNetworkResource(nw))
|
2015-11-10 03:57:06 -05:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
list, err = filterNetworks(list, netFilters)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-25 06:19:17 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err := n.backend.FindNetwork(vars["id"])
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
2016-06-13 22:52:49 -04:00
|
|
|
if nr, err := n.clusterProvider.GetNetwork(vars["id"]); err == nil {
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, nr)
|
|
|
|
}
|
2015-09-25 06:19:17 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, n.buildNetworkResource(nw))
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-04-13 04:33:46 -04:00
|
|
|
var create types.NetworkCreateRequest
|
2015-09-25 06:19:17 -04:00
|
|
|
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&create); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-29 21:08:55 -04:00
|
|
|
if _, err := n.clusterProvider.GetNetwork(create.Name); err == nil {
|
|
|
|
return libnetwork.NetworkNameError(create.Name)
|
|
|
|
}
|
|
|
|
|
2016-03-28 13:41:06 -04:00
|
|
|
nw, err := n.backend.CreateNetwork(create)
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
2016-06-13 22:52:49 -04:00
|
|
|
if _, ok := err.(libnetwork.ManagerRedirectError); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
id, err := n.clusterProvider.CreateNetwork(create)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nw = &types.NetworkCreateResponse{ID: id}
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:41:06 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusCreated, nw)
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
var connect types.NetworkConnect
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&connect); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err := n.backend.FindNetwork(vars["id"])
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-24 18:34:45 -04:00
|
|
|
if nw.Info().Dynamic() {
|
2016-07-25 06:15:54 -04:00
|
|
|
err := fmt.Errorf("operation not supported for swarm scoped networks")
|
|
|
|
return errors.NewRequestForbiddenError(err)
|
2016-07-24 18:34:45 -04:00
|
|
|
}
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
return n.backend.ConnectContainerToNetwork(connect.Container, nw.Name(), connect.EndpointConfig)
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
var disconnect types.NetworkDisconnect
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&disconnect); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err := n.backend.FindNetwork(vars["id"])
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-24 18:34:45 -04:00
|
|
|
if nw.Info().Dynamic() {
|
2016-07-25 06:15:54 -04:00
|
|
|
err := fmt.Errorf("operation not supported for swarm scoped networks")
|
|
|
|
return errors.NewRequestForbiddenError(err)
|
2016-07-24 18:34:45 -04:00
|
|
|
}
|
|
|
|
|
2016-01-12 23:56:36 -05:00
|
|
|
return n.backend.DisconnectContainerFromNetwork(disconnect.Container, nw, disconnect.Force)
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) deleteNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-03-05 11:41:51 -05:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
if _, err := n.clusterProvider.GetNetwork(vars["id"]); err == nil {
|
|
|
|
return n.clusterProvider.RemoveNetwork(vars["id"])
|
|
|
|
}
|
2016-03-05 11:41:51 -05:00
|
|
|
if err := n.backend.DeleteNetwork(vars["id"]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
func (n *networkRouter) buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
|
2015-09-25 06:19:17 -04:00
|
|
|
r := &types.NetworkResource{}
|
|
|
|
if nw == nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
info := nw.Info()
|
2015-09-25 06:19:17 -04:00
|
|
|
r.Name = nw.Name()
|
|
|
|
r.ID = nw.ID()
|
2016-03-16 17:52:34 -04:00
|
|
|
r.Scope = info.Scope()
|
2016-06-13 22:52:49 -04:00
|
|
|
if n.clusterProvider.IsManager() {
|
|
|
|
if _, err := n.clusterProvider.GetNetwork(nw.Name()); err == nil {
|
|
|
|
r.Scope = "swarm"
|
|
|
|
}
|
|
|
|
} else if info.Dynamic() {
|
|
|
|
r.Scope = "swarm"
|
|
|
|
}
|
2015-09-25 06:19:17 -04:00
|
|
|
r.Driver = nw.Type()
|
2016-03-16 17:52:34 -04:00
|
|
|
r.EnableIPv6 = info.IPv6Enabled()
|
|
|
|
r.Internal = info.Internal()
|
|
|
|
r.Options = info.DriverOptions()
|
2015-09-25 06:19:17 -04:00
|
|
|
r.Containers = make(map[string]types.EndpointResource)
|
2016-03-16 17:52:34 -04:00
|
|
|
buildIpamResources(r, info)
|
|
|
|
r.Internal = info.Internal()
|
|
|
|
r.Labels = info.Labels()
|
2015-10-09 14:21:48 -04:00
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
epl := nw.Endpoints()
|
|
|
|
for _, e := range epl {
|
2015-11-03 17:15:34 -05:00
|
|
|
ei := e.Info()
|
|
|
|
if ei == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sb := ei.Sandbox()
|
2016-07-06 05:54:46 -04:00
|
|
|
tmpID := e.ID()
|
|
|
|
key := "ep-" + tmpID
|
2016-03-12 16:29:25 -05:00
|
|
|
if sb != nil {
|
|
|
|
key = sb.ContainerID()
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 05:54:46 -04:00
|
|
|
r.Containers[key] = buildEndpointResource(tmpID, e.Name(), ei)
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
func buildIpamResources(r *types.NetworkResource, nwInfo libnetwork.NetworkInfo) {
|
|
|
|
id, opts, ipv4conf, ipv6conf := nwInfo.IpamConfig()
|
2015-10-09 14:21:48 -04:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
ipv4Info, ipv6Info := nwInfo.IpamInfo()
|
2015-12-27 23:56:57 -05:00
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
r.IPAM.Driver = id
|
|
|
|
|
2015-10-23 15:28:39 -04:00
|
|
|
r.IPAM.Options = opts
|
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
r.IPAM.Config = []network.IPAMConfig{}
|
|
|
|
for _, ip4 := range ipv4conf {
|
2015-12-27 23:56:57 -05:00
|
|
|
if ip4.PreferredPool == "" {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-09 14:21:48 -04:00
|
|
|
iData := network.IPAMConfig{}
|
|
|
|
iData.Subnet = ip4.PreferredPool
|
|
|
|
iData.IPRange = ip4.SubPool
|
|
|
|
iData.Gateway = ip4.Gateway
|
|
|
|
iData.AuxAddress = ip4.AuxAddresses
|
|
|
|
r.IPAM.Config = append(r.IPAM.Config, iData)
|
|
|
|
}
|
|
|
|
|
2015-12-27 23:56:57 -05:00
|
|
|
if len(r.IPAM.Config) == 0 {
|
|
|
|
for _, ip4Info := range ipv4Info {
|
|
|
|
iData := network.IPAMConfig{}
|
|
|
|
iData.Subnet = ip4Info.IPAMData.Pool.String()
|
2016-08-11 21:08:54 -04:00
|
|
|
iData.Gateway = ip4Info.IPAMData.Gateway.IP.String()
|
2015-12-27 23:56:57 -05:00
|
|
|
r.IPAM.Config = append(r.IPAM.Config, iData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasIpv6Conf := false
|
2015-10-09 14:21:48 -04:00
|
|
|
for _, ip6 := range ipv6conf {
|
2015-12-27 23:56:57 -05:00
|
|
|
if ip6.PreferredPool == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasIpv6Conf = true
|
2015-10-09 14:21:48 -04:00
|
|
|
iData := network.IPAMConfig{}
|
|
|
|
iData.Subnet = ip6.PreferredPool
|
|
|
|
iData.IPRange = ip6.SubPool
|
|
|
|
iData.Gateway = ip6.Gateway
|
|
|
|
iData.AuxAddress = ip6.AuxAddresses
|
|
|
|
r.IPAM.Config = append(r.IPAM.Config, iData)
|
|
|
|
}
|
2015-12-27 23:56:57 -05:00
|
|
|
|
|
|
|
if !hasIpv6Conf {
|
|
|
|
for _, ip6Info := range ipv6Info {
|
|
|
|
iData := network.IPAMConfig{}
|
|
|
|
iData.Subnet = ip6Info.IPAMData.Pool.String()
|
|
|
|
iData.Gateway = ip6Info.IPAMData.Gateway.String()
|
|
|
|
r.IPAM.Config = append(r.IPAM.Config, iData)
|
|
|
|
}
|
|
|
|
}
|
2015-10-09 14:21:48 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 05:54:46 -04:00
|
|
|
func buildEndpointResource(id string, name string, info libnetwork.EndpointInfo) types.EndpointResource {
|
2015-09-25 06:19:17 -04:00
|
|
|
er := types.EndpointResource{}
|
|
|
|
|
2016-07-06 05:54:46 -04:00
|
|
|
er.EndpointID = id
|
|
|
|
er.Name = name
|
|
|
|
ei := info
|
2015-11-03 17:15:34 -05:00
|
|
|
if ei == nil {
|
|
|
|
return er
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface := ei.Iface(); iface != nil {
|
2015-09-25 06:19:17 -04:00
|
|
|
if mac := iface.MacAddress(); mac != nil {
|
|
|
|
er.MacAddress = mac.String()
|
|
|
|
}
|
2015-10-10 12:43:03 -04:00
|
|
|
if ip := iface.Address(); ip != nil && len(ip.IP) > 0 {
|
|
|
|
er.IPv4Address = ip.String()
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-10 12:43:03 -04:00
|
|
|
if ipv6 := iface.AddressIPv6(); ipv6 != nil && len(ipv6.IP) > 0 {
|
|
|
|
er.IPv6Address = ipv6.String()
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return er
|
|
|
|
}
|