Skip to content

Commit

Permalink
Merge pull request moby#47887 from thaJeztah/move_more_network_api_ty…
Browse files Browse the repository at this point in the history
…pes_2

api/types: migrate NetworkResource to api/types/network
  • Loading branch information
akerouanton authored Jun 5, 2024
2 parents c5c4abb + 69b2a05 commit c6aaabc
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 108 deletions.
8 changes: 4 additions & 4 deletions api/server/router/network/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Backend is all the methods that need to be implemented
// to provide network specific functionality.
type Backend interface {
GetNetworks(filters.Args, backend.NetworkListConfig) ([]types.NetworkResource, error)
GetNetworks(filters.Args, backend.NetworkListConfig) ([]network.Inspect, error)
CreateNetwork(nc types.NetworkCreateRequest) (*network.CreateResponse, error)
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
DisconnectContainerFromNetwork(containerName string, networkName string, force bool) error
Expand All @@ -23,9 +23,9 @@ type Backend interface {
// ClusterBackend is all the methods that need to be implemented
// to provide cluster network specific functionality.
type ClusterBackend interface {
GetNetworks(filters.Args) ([]types.NetworkResource, error)
GetNetwork(name string) (types.NetworkResource, error)
GetNetworksByName(name string) ([]types.NetworkResource, error)
GetNetworks(filters.Args) ([]network.Inspect, error)
GetNetwork(name string) (network.Inspect, error)
GetNetworksByName(name string) ([]network.Inspect, error)
CreateNetwork(nc types.NetworkCreateRequest) (string, error)
RemoveNetwork(name string) error
}
20 changes: 10 additions & 10 deletions api/server/router/network/network_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWrit
return err
}

var list []types.NetworkResource
var list []network.Summary
nr, err := n.cluster.GetNetworks(filter)
if err == nil {
list = nr
Expand Down Expand Up @@ -60,7 +60,7 @@ func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWrit
}

if list == nil {
list = []types.NetworkResource{}
list = []network.Summary{}
}

return httputils.WriteJSON(w, http.StatusOK, list)
Expand Down Expand Up @@ -109,8 +109,8 @@ func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r

// For full name and partial ID, save the result first, and process later
// in case multiple records was found based on the same term
listByFullName := map[string]types.NetworkResource{}
listByPartialID := map[string]types.NetworkResource{}
listByFullName := map[string]network.Inspect{}
listByPartialID := map[string]network.Inspect{}

// TODO(@cpuguy83): All this logic for figuring out which network to return does not belong here
// Instead there should be a backend function to just get one network.
Expand Down Expand Up @@ -311,9 +311,9 @@ func (n *networkRouter) postNetworksPrune(ctx context.Context, w http.ResponseWr
// For full name and partial ID, save the result first, and process later
// in case multiple records was found based on the same term
// TODO (yongtang): should we wrap with version here for backward compatibility?
func (n *networkRouter) findUniqueNetwork(term string) (types.NetworkResource, error) {
listByFullName := map[string]types.NetworkResource{}
listByPartialID := map[string]types.NetworkResource{}
func (n *networkRouter) findUniqueNetwork(term string) (network.Inspect, error) {
listByFullName := map[string]network.Inspect{}
listByPartialID := map[string]network.Inspect{}

filter := filters.NewArgs(filters.Arg("idOrName", term))
networks, _ := n.backend.GetNetworks(filter, backend.NetworkListConfig{Detailed: true})
Expand Down Expand Up @@ -363,7 +363,7 @@ func (n *networkRouter) findUniqueNetwork(term string) (types.NetworkResource, e
}
}
if len(listByFullName) > 1 {
return types.NetworkResource{}, errdefs.InvalidParameter(errors.Errorf("network %s is ambiguous (%d matches found based on name)", term, len(listByFullName)))
return network.Inspect{}, errdefs.InvalidParameter(errors.Errorf("network %s is ambiguous (%d matches found based on name)", term, len(listByFullName)))
}

// Find based on partial ID, returns true only if no duplicates
Expand All @@ -373,8 +373,8 @@ func (n *networkRouter) findUniqueNetwork(term string) (types.NetworkResource, e
}
}
if len(listByPartialID) > 1 {
return types.NetworkResource{}, errdefs.InvalidParameter(errors.Errorf("network %s is ambiguous (%d matches found based on ID prefix)", term, len(listByPartialID)))
return network.Inspect{}, errdefs.InvalidParameter(errors.Errorf("network %s is ambiguous (%d matches found based on ID prefix)", term, len(listByPartialID)))
}

return types.NetworkResource{}, errdefs.NotFound(libnetwork.ErrNoSuchNetwork(term))
return network.Inspect{}, errdefs.NotFound(libnetwork.ErrNoSuchNetwork(term))
}
28 changes: 28 additions & 0 deletions api/types/network/network.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package network // import "github.com/docker/docker/api/types/network"

import (
"time"

"github.com/docker/docker/api/types/filters"
)

Expand Down Expand Up @@ -42,6 +44,32 @@ type DisconnectOptions struct {
Force bool
}

// Inspect is the body of the "get network" http response message.
type Inspect struct {
Name string // Name is the name of the network
ID string `json:"Id"` // ID uniquely identifies a network on a single machine
Created time.Time // Created is the time the network created
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level)
Driver string // Driver is the Driver name used to create the network (e.g. `bridge`, `overlay`)
EnableIPv6 bool // EnableIPv6 represents whether to enable IPv6
IPAM IPAM // IPAM is the network's IP Address Management
Internal bool // Internal represents if the network is used internal only
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
ConfigFrom ConfigReference // ConfigFrom specifies the source which will provide the configuration for this network.
ConfigOnly bool // ConfigOnly networks are place-holder networks for network configurations to be used by other networks. ConfigOnly networks cannot be used directly to run containers or services.
Containers map[string]EndpointResource // Containers contains endpoints belonging to the network
Options map[string]string // Options holds the network specific options to use for when creating the network
Labels map[string]string // Labels holds metadata specific to the network being created
Peers []PeerInfo `json:",omitempty"` // List of peer nodes for an overlay network
Services map[string]ServiceInfo `json:",omitempty"`
}

// Summary is used as response when listing networks. It currently is an alias
// for [Inspect], but may diverge in the future, as not all information may
// be included when listing networks.
type Summary = Inspect

// Address represents an IP address
type Address struct {
Addr string
Expand Down
21 changes: 0 additions & 21 deletions api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,27 +423,6 @@ type MountPoint struct {
Propagation mount.Propagation
}

// NetworkResource is the body of the "get network" http response message
type NetworkResource struct {
Name string // Name is the requested name of the network
ID string `json:"Id"` // ID uniquely identifies a network on a single machine
Created time.Time // Created is the time the network created
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level)
Driver string // Driver is the Driver name used to create the network (e.g. `bridge`, `overlay`)
EnableIPv6 bool // EnableIPv6 represents whether to enable IPv6
IPAM network.IPAM // IPAM is the network's IP Address Management
Internal bool // Internal represents if the network is used internal only
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
ConfigFrom network.ConfigReference // ConfigFrom specifies the source which will provide the configuration for this network.
ConfigOnly bool // ConfigOnly networks are place-holder networks for network configurations to be used by other networks. ConfigOnly networks cannot be used directly to run containers or services.
Containers map[string]network.EndpointResource // Containers contains endpoints belonging to the network
Options map[string]string // Options holds the network specific options to use for when creating the network
Labels map[string]string // Labels holds metadata specific to the network being created
Peers []network.PeerInfo `json:",omitempty"` // List of peer nodes for an overlay network
Services map[string]network.ServiceInfo `json:",omitempty"`
}

// NetworkCreate is the expected body of the "create network" http request message
type NetworkCreate struct {
// Deprecated: CheckDuplicate is deprecated since API v1.44, but it defaults to true when sent by the client
Expand Down
5 changes: 5 additions & 0 deletions api/types/types_deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ type NetworkDisconnect = network.DisconnectOptions
//
// Deprecated: use [network.EndpointResource].
type EndpointResource = network.EndpointResource

// NetworkResource is the body of the "get network" http response message/
//
// Deprecated: use [network.Inspect] or [network.Summary] (for list operations).
type NetworkResource = network.Inspect
6 changes: 3 additions & 3 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ type NetworkAPIClient interface {
NetworkConnect(ctx context.Context, network, container string, config *network.EndpointSettings) error
NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (network.CreateResponse, error)
NetworkDisconnect(ctx context.Context, network, container string, force bool) error
NetworkInspect(ctx context.Context, network string, options network.InspectOptions) (types.NetworkResource, error)
NetworkInspectWithRaw(ctx context.Context, network string, options network.InspectOptions) (types.NetworkResource, []byte, error)
NetworkList(ctx context.Context, options network.ListOptions) ([]types.NetworkResource, error)
NetworkInspect(ctx context.Context, network string, options network.InspectOptions) (network.Inspect, error)
NetworkInspectWithRaw(ctx context.Context, network string, options network.InspectOptions) (network.Inspect, []byte, error)
NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error)
NetworkRemove(ctx context.Context, network string) error
NetworksPrune(ctx context.Context, pruneFilter filters.Args) (types.NetworksPruneReport, error)
}
Expand Down
13 changes: 6 additions & 7 deletions client/network_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import (
"io"
"net/url"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
)

// NetworkInspect returns the information for a specific network configured in the docker host.
func (cli *Client) NetworkInspect(ctx context.Context, networkID string, options network.InspectOptions) (types.NetworkResource, error) {
func (cli *Client) NetworkInspect(ctx context.Context, networkID string, options network.InspectOptions) (network.Inspect, error) {
networkResource, _, err := cli.NetworkInspectWithRaw(ctx, networkID, options)
return networkResource, err
}

// NetworkInspectWithRaw returns the information for a specific network configured in the docker host and its raw representation.
func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string, options network.InspectOptions) (types.NetworkResource, []byte, error) {
func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string, options network.InspectOptions) (network.Inspect, []byte, error) {
if networkID == "" {
return types.NetworkResource{}, nil, objectNotFoundError{object: "network", id: networkID}
return network.Inspect{}, nil, objectNotFoundError{object: "network", id: networkID}
}
query := url.Values{}
if options.Verbose {
Expand All @@ -33,15 +32,15 @@ func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string,
resp, err := cli.get(ctx, "/networks/"+networkID, query, nil)
defer ensureReaderClosed(resp)
if err != nil {
return types.NetworkResource{}, nil, err
return network.Inspect{}, nil, err
}

raw, err := io.ReadAll(resp.body)
if err != nil {
return types.NetworkResource{}, nil, err
return network.Inspect{}, nil, err
}

var nw types.NetworkResource
var nw network.Inspect
err = json.NewDecoder(bytes.NewReader(raw)).Decode(&nw)
return nw, raw, err
}
5 changes: 2 additions & 3 deletions client/network_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
Expand Down Expand Up @@ -47,12 +46,12 @@ func TestNetworkInspect(t *testing.T) {
s := map[string]network.ServiceInfo{
"web": {},
}
content, err = json.Marshal(types.NetworkResource{
content, err = json.Marshal(network.Inspect{
Name: "mynetwork",
Services: s,
})
} else {
content, err = json.Marshal(types.NetworkResource{
content, err = json.Marshal(network.Inspect{
Name: "mynetwork",
})
}
Expand Down
5 changes: 2 additions & 3 deletions client/network_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"encoding/json"
"net/url"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
)

// NetworkList returns the list of networks configured in the docker host.
func (cli *Client) NetworkList(ctx context.Context, options network.ListOptions) ([]types.NetworkResource, error) {
func (cli *Client) NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error) {
query := url.Values{}
if options.Filters.Len() > 0 {
//nolint:staticcheck // ignore SA1019 for old code
Expand All @@ -22,7 +21,7 @@ func (cli *Client) NetworkList(ctx context.Context, options network.ListOptions)

query.Set("filters", filterJSON)
}
var networkResources []types.NetworkResource
var networkResources []network.Summary
resp, err := cli.get(ctx, "/networks", query, nil)
defer ensureReaderClosed(resp)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions client/network_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/errdefs"
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestNetworkList(t *testing.T) {
if actualFilters != listCase.expectedFilters {
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
}
content, err := json.Marshal([]types.NetworkResource{
content, err := json.Marshal([]network.Summary{
{
Name: "network",
Driver: "bridge",
Expand Down
6 changes: 3 additions & 3 deletions daemon/cluster.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package daemon // import "github.com/docker/docker/daemon"

import (
apitypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
lncluster "github.com/docker/docker/libnetwork/cluster"
)

Expand All @@ -21,7 +21,7 @@ type ClusterStatus interface {

// NetworkManager provides methods to manage networks
type NetworkManager interface {
GetNetwork(input string) (apitypes.NetworkResource, error)
GetNetworks(filters.Args) ([]apitypes.NetworkResource, error)
GetNetwork(input string) (network.Inspect, error)
GetNetworks(filters.Args) ([]network.Inspect, error)
RemoveNetwork(input string) error
}
4 changes: 2 additions & 2 deletions daemon/cluster/convert/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func swarmPortConfigToAPIPortConfig(portConfig *swarmapi.PortConfig) types.PortC
}

// BasicNetworkFromGRPC converts a grpc Network to a NetworkResource.
func BasicNetworkFromGRPC(n swarmapi.Network) basictypes.NetworkResource {
func BasicNetworkFromGRPC(n swarmapi.Network) networktypes.Inspect {
spec := n.Spec
var ipam networktypes.IPAM
if n.IPAM != nil {
Expand All @@ -157,7 +157,7 @@ func BasicNetworkFromGRPC(n swarmapi.Network) basictypes.NetworkResource {
}
}

nr := basictypes.NetworkResource{
nr := networktypes.Inspect{
ID: n.ID,
Name: n.Spec.Annotations.Name,
Scope: scope.Swarm,
Expand Down
Loading

0 comments on commit c6aaabc

Please sign in to comment.