github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/api/server/router/network/network_routes.go (about)

     1  package network
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/docker/docker/api/server/httputils"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/filters"
    12  	"github.com/docker/docker/api/types/network"
    13  	"github.com/docker/libnetwork"
    14  	"github.com/docker/libnetwork/networkdb"
    15  )
    16  
    17  func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    18  	if err := httputils.ParseForm(r); err != nil {
    19  		return err
    20  	}
    21  
    22  	filter := r.Form.Get("filters")
    23  	netFilters, err := filters.FromParam(filter)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	list := []types.NetworkResource{}
    29  
    30  	if nr, err := n.clusterProvider.GetNetworks(); err == nil {
    31  		list = append(list, nr...)
    32  	}
    33  
    34  	// Combine the network list returned by Docker daemon if it is not already
    35  	// returned by the cluster manager
    36  SKIP:
    37  	for _, nw := range n.backend.GetNetworks() {
    38  		for _, nl := range list {
    39  			if nl.ID == nw.ID() {
    40  				continue SKIP
    41  			}
    42  		}
    43  		list = append(list, *n.buildNetworkResource(nw))
    44  	}
    45  
    46  	list, err = filterNetworks(list, netFilters)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	return httputils.WriteJSON(w, http.StatusOK, list)
    51  }
    52  
    53  func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    54  	if err := httputils.ParseForm(r); err != nil {
    55  		return err
    56  	}
    57  
    58  	nw, err := n.backend.FindNetwork(vars["id"])
    59  	if err != nil {
    60  		if nr, err := n.clusterProvider.GetNetwork(vars["id"]); err == nil {
    61  			return httputils.WriteJSON(w, http.StatusOK, nr)
    62  		}
    63  		return err
    64  	}
    65  	return httputils.WriteJSON(w, http.StatusOK, n.buildNetworkResource(nw))
    66  }
    67  
    68  func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    69  	var create types.NetworkCreateRequest
    70  
    71  	if err := httputils.ParseForm(r); err != nil {
    72  		return err
    73  	}
    74  
    75  	if err := httputils.CheckForJSON(r); err != nil {
    76  		return err
    77  	}
    78  
    79  	if err := json.NewDecoder(r.Body).Decode(&create); err != nil {
    80  		return err
    81  	}
    82  
    83  	if _, err := n.clusterProvider.GetNetwork(create.Name); err == nil {
    84  		return libnetwork.NetworkNameError(create.Name)
    85  	}
    86  
    87  	nw, err := n.backend.CreateNetwork(create)
    88  	if err != nil {
    89  		if _, ok := err.(libnetwork.ManagerRedirectError); !ok {
    90  			return err
    91  		}
    92  		id, err := n.clusterProvider.CreateNetwork(create)
    93  		if err != nil {
    94  			return err
    95  		}
    96  		nw = &types.NetworkCreateResponse{ID: id}
    97  	}
    98  
    99  	return httputils.WriteJSON(w, http.StatusCreated, nw)
   100  }
   101  
   102  func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   103  	var connect types.NetworkConnect
   104  	if err := httputils.ParseForm(r); err != nil {
   105  		return err
   106  	}
   107  
   108  	if err := httputils.CheckForJSON(r); err != nil {
   109  		return err
   110  	}
   111  
   112  	if err := json.NewDecoder(r.Body).Decode(&connect); err != nil {
   113  		return err
   114  	}
   115  
   116  	return n.backend.ConnectContainerToNetwork(connect.Container, vars["id"], connect.EndpointConfig)
   117  }
   118  
   119  func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   120  	var disconnect types.NetworkDisconnect
   121  	if err := httputils.ParseForm(r); err != nil {
   122  		return err
   123  	}
   124  
   125  	if err := httputils.CheckForJSON(r); err != nil {
   126  		return err
   127  	}
   128  
   129  	if err := json.NewDecoder(r.Body).Decode(&disconnect); err != nil {
   130  		return err
   131  	}
   132  
   133  	return n.backend.DisconnectContainerFromNetwork(disconnect.Container, vars["id"], disconnect.Force)
   134  }
   135  
   136  func (n *networkRouter) deleteNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   137  	if err := httputils.ParseForm(r); err != nil {
   138  		return err
   139  	}
   140  	if _, err := n.clusterProvider.GetNetwork(vars["id"]); err == nil {
   141  		if err = n.clusterProvider.RemoveNetwork(vars["id"]); err != nil {
   142  			return err
   143  		}
   144  		w.WriteHeader(http.StatusNoContent)
   145  		return nil
   146  	}
   147  	if err := n.backend.DeleteNetwork(vars["id"]); err != nil {
   148  		return err
   149  	}
   150  	w.WriteHeader(http.StatusNoContent)
   151  	return nil
   152  }
   153  
   154  func (n *networkRouter) buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
   155  	r := &types.NetworkResource{}
   156  	if nw == nil {
   157  		return r
   158  	}
   159  
   160  	info := nw.Info()
   161  	r.Name = nw.Name()
   162  	r.ID = nw.ID()
   163  	r.Created = info.Created()
   164  	r.Scope = info.Scope()
   165  	if n.clusterProvider.IsManager() {
   166  		if _, err := n.clusterProvider.GetNetwork(nw.Name()); err == nil {
   167  			r.Scope = "swarm"
   168  		}
   169  	} else if info.Dynamic() {
   170  		r.Scope = "swarm"
   171  	}
   172  	r.Driver = nw.Type()
   173  	r.EnableIPv6 = info.IPv6Enabled()
   174  	r.Internal = info.Internal()
   175  	r.Attachable = info.Attachable()
   176  	r.Options = info.DriverOptions()
   177  	r.Containers = make(map[string]types.EndpointResource)
   178  	buildIpamResources(r, info)
   179  	r.Labels = info.Labels()
   180  
   181  	peers := info.Peers()
   182  	if len(peers) != 0 {
   183  		r.Peers = buildPeerInfoResources(peers)
   184  	}
   185  
   186  	epl := nw.Endpoints()
   187  	for _, e := range epl {
   188  		ei := e.Info()
   189  		if ei == nil {
   190  			continue
   191  		}
   192  		sb := ei.Sandbox()
   193  		tmpID := e.ID()
   194  		key := "ep-" + tmpID
   195  		if sb != nil {
   196  			key = sb.ContainerID()
   197  		}
   198  
   199  		r.Containers[key] = buildEndpointResource(tmpID, e.Name(), ei)
   200  	}
   201  	return r
   202  }
   203  
   204  func buildPeerInfoResources(peers []networkdb.PeerInfo) []network.PeerInfo {
   205  	peerInfo := make([]network.PeerInfo, 0, len(peers))
   206  	for _, peer := range peers {
   207  		peerInfo = append(peerInfo, network.PeerInfo{
   208  			Name: peer.Name,
   209  			IP:   peer.IP,
   210  		})
   211  	}
   212  	return peerInfo
   213  }
   214  
   215  func buildIpamResources(r *types.NetworkResource, nwInfo libnetwork.NetworkInfo) {
   216  	id, opts, ipv4conf, ipv6conf := nwInfo.IpamConfig()
   217  
   218  	ipv4Info, ipv6Info := nwInfo.IpamInfo()
   219  
   220  	r.IPAM.Driver = id
   221  
   222  	r.IPAM.Options = opts
   223  
   224  	r.IPAM.Config = []network.IPAMConfig{}
   225  	for _, ip4 := range ipv4conf {
   226  		if ip4.PreferredPool == "" {
   227  			continue
   228  		}
   229  		iData := network.IPAMConfig{}
   230  		iData.Subnet = ip4.PreferredPool
   231  		iData.IPRange = ip4.SubPool
   232  		iData.Gateway = ip4.Gateway
   233  		iData.AuxAddress = ip4.AuxAddresses
   234  		r.IPAM.Config = append(r.IPAM.Config, iData)
   235  	}
   236  
   237  	if len(r.IPAM.Config) == 0 {
   238  		for _, ip4Info := range ipv4Info {
   239  			iData := network.IPAMConfig{}
   240  			iData.Subnet = ip4Info.IPAMData.Pool.String()
   241  			iData.Gateway = ip4Info.IPAMData.Gateway.IP.String()
   242  			r.IPAM.Config = append(r.IPAM.Config, iData)
   243  		}
   244  	}
   245  
   246  	hasIpv6Conf := false
   247  	for _, ip6 := range ipv6conf {
   248  		if ip6.PreferredPool == "" {
   249  			continue
   250  		}
   251  		hasIpv6Conf = true
   252  		iData := network.IPAMConfig{}
   253  		iData.Subnet = ip6.PreferredPool
   254  		iData.IPRange = ip6.SubPool
   255  		iData.Gateway = ip6.Gateway
   256  		iData.AuxAddress = ip6.AuxAddresses
   257  		r.IPAM.Config = append(r.IPAM.Config, iData)
   258  	}
   259  
   260  	if !hasIpv6Conf {
   261  		for _, ip6Info := range ipv6Info {
   262  			iData := network.IPAMConfig{}
   263  			iData.Subnet = ip6Info.IPAMData.Pool.String()
   264  			iData.Gateway = ip6Info.IPAMData.Gateway.String()
   265  			r.IPAM.Config = append(r.IPAM.Config, iData)
   266  		}
   267  	}
   268  }
   269  
   270  func buildEndpointResource(id string, name string, info libnetwork.EndpointInfo) types.EndpointResource {
   271  	er := types.EndpointResource{}
   272  
   273  	er.EndpointID = id
   274  	er.Name = name
   275  	ei := info
   276  	if ei == nil {
   277  		return er
   278  	}
   279  
   280  	if iface := ei.Iface(); iface != nil {
   281  		if mac := iface.MacAddress(); mac != nil {
   282  			er.MacAddress = mac.String()
   283  		}
   284  		if ip := iface.Address(); ip != nil && len(ip.IP) > 0 {
   285  			er.IPv4Address = ip.String()
   286  		}
   287  
   288  		if ipv6 := iface.AddressIPv6(); ipv6 != nil && len(ipv6.IP) > 0 {
   289  			er.IPv6Address = ipv6.String()
   290  		}
   291  	}
   292  	return er
   293  }
   294  
   295  func (n *networkRouter) postNetworksPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   296  	if err := httputils.ParseForm(r); err != nil {
   297  		return err
   298  	}
   299  
   300  	pruneReport, err := n.backend.NetworksPrune(filters.Args{})
   301  	if err != nil {
   302  		return err
   303  	}
   304  	return httputils.WriteJSON(w, http.StatusOK, pruneReport)
   305  }