8000 api/types/network: separate Summary from Inspect · moby/moby@1a86389 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a86389

Browse files
committed
api/types/network: separate Summary from Inspect
While the network Summary and Inspect types have been aliases in Go's type system, in practice there is a difference: the Containers and Services fields are only populated when inspecting a network. Split out the common fields into a base network.Network struct which is embedded in the network.Summary and network.Inspect types. Signed-off-by: Cory Snider <csnider@mirantis.com>
1 parent 291e129 commit 1a86389

File tree

11 files changed

+147
-81
lines changed

11 files changed

+147
-81
lines changed

api/types/network/network.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,36 @@ type CreateRequest struct {
3838
CheckDuplicate *bool `json:",omitempty"`
3939
}
4040

41+
type Network struct {
42+
Name string // Name is the name of the network
43+
ID string `json:"Id"` // ID uniquely identifies a network on a single machine
44+
Created time.Time // Created is the time the network created
45+
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level)
46+
Driver string // Driver is the Driver name used to create the network (e.g. `bridge`, `overlay`)
47+
EnableIPv4 bool // EnableIPv4 represents whether IPv4 is enabled
48+
EnableIPv6 bool // EnableIPv6 represents whether IPv6 is enabled
49+
IPAM IPAM // IPAM is the network's IP Address Management
50+
Internal bool // Internal represents if the network is used internal only
51+
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
52+
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
53+
ConfigFrom ConfigReference // ConfigFrom specifies the source which will provide the configuration for this network.
54+
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.
55+
Options map[string]string // Options holds the network specific options to use for when creating the network
56+
Labels map[string]string // Labels holds metadata specific to the network being created
57+
Peers []PeerInfo `json:",omitempty"` // List of peer nodes for an overlay network
58+
}
59+
4160
// Inspect is the body of the "get network" http response message.
4261
type Inspect struct {
43-
Name string // Name is the name of the network
44-
ID string `json:"Id"` // ID uniquely identifies a network on a single machine
45-
Created time.Time // Created is the time the network created
46-
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level)
47-
Driver string // Driver is the Driver name used to create the network (e.g. `bridge`, `overlay`)
48-
EnableIPv4 bool // EnableIPv4 represents whether IPv4 is enabled
49-
EnableIPv6 bool // EnableIPv6 represents whether IPv6 is enabled
50-
IPAM IPAM // IPAM is the network's IP Address Management
51-
Internal bool // Internal represents if the network is used internal only
52-
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
53-
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
54-
ConfigFrom ConfigReference // ConfigFrom specifies the source which will provide the configuration for this network.
55-
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.
62+
Network
5663
Containers map[string]EndpointResource // Containers contains endpoints belonging to the network
57-
Options map[string]string // Options holds the network specific options to use for when creating the network
58-
Labels map[string]string // Labels holds metadata specific to the network being created
59-
Peers []PeerInfo `json:",omitempty"` // List of peer nodes for an overlay network
6064
Services map[string]ServiceInfo `json:",omitempty"`
6165
}
6266

63-
// Summary is used as response when listing networks. It currently is an alias
64-
// for [Inspect], but may diverge in the future, as not all information may
65-
// be included when listing networks.
66-
type Summary = Inspect
67+
// Summary is used as response when listing networks.
68+
type Summary struct {
69+
Network
70+
}
6771

6872
// Address represents an IP address
6973
type Address struct {

client/network_inspect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ func TestNetworkInspect(t *testing.T) {
4646
"web": {},
4747
}
4848
content, err = json.Marshal(network.Inspect{
49-
Name: "mynetwork",
49+
Network: network.Network{Name: "mynetwork"},
5050
Services: s,
5151
})
5252
} else {
5353
content, err = json.Marshal(network.Inspect{
54-
Name: "mynetwork",
54+
Network: network.Network{Name: "mynetwork"},
5555
})
5656
}
5757
if err != nil {

client/network_list_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ func TestNetworkList(t *testing.T) {
7474
}
7575
content, err := json.Marshal([]network.Summary{
7676
{
77-
Name: "network",
78-
Driver: "bridge",
77+
Network: network.Network{
78+
Name: "network",
79+
Driver: "bridge",
80+
},
7981
},
8082
})
8183
if err != nil {

daemon/cluster/convert/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func swarmPortConfigToAPIPortConfig(portConfig *swarmapi.PortConfig) types.PortC
138138
}
139139

140140
// BasicNetworkFromGRPC converts a grpc Network to a NetworkResource.
141-
func BasicNetworkFromGRPC(n swarmapi.Network) network.Inspect {
141+
func BasicNetworkFromGRPC(n swarmapi.Network) network.Network {
142142
spec := n.Spec
143143
var ipam network.IPAM
144144
if n.IPAM != nil {
@@ -157,7 +157,7 @@ func BasicNetworkFromGRPC(n swarmapi.Network) network.Inspect {
157157
}
158158
}
159159

160-
nr := network.Inspect{
160+
nr := network.Network{
161161
ID: n.ID,
162162
Name: n.Spec.Annotations.Name,
163163
Scope: scope.Swarm,

daemon/cluster/networks.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,28 @@ func (c *Cluster) GetNetworks(filter networkSettings.Filter) ([]network.Inspect,
3636
continue
3737
}
3838
if filter.Matches(convert.FilterNetwork{N: n}) {
39-
filtered = append(filtered, convert.BasicNetworkFromGRPC(*n))
39+
filtered = append(filtered, network.Inspect{
40+
Network: convert.BasicNetworkFromGRPC(*n),
41+
Containers: map[string]network.EndpointResource{},
42+
})
43+
}
44+
}
45+
46+
return filtered, nil
47+
}
48+
49+
func (c *Cluster) GetNetworkSummaries(filter networkSettings.Filter) ([]network.Summary, error) {
50+
list, err := c.listNetworks(context.TODO(), nil)
51+
if err != nil {
52+
return nil, err
53+
}
54+
var filtered []network.Summary< B3F5 /span>
55+
for _, n := range list {
56+
if n.Spec.Annotations.Labels["com.docker.swarm.predefined"] == "true" {
57+
continue
58+
}
59+
if filter.Matches(convert.FilterNetwork{N: n}) {
60+
filtered = append(filtered, network.Summary{Network: convert.BasicNetworkFromGRPC(*n)})
4061
}
4162
}
4263

@@ -70,12 +91,15 @@ func (c *Cluster) GetNetwork(input string) (network.Inspect, error) {
7091
}); err != nil {
7192
return network.Inspect{}, err
7293
}
73-
return convert.BasicNetworkFromGRPC(*nw), nil
94+
return network.Inspect{
95+
Network: convert.BasicNetworkFromGRPC(*nw),
96+
Containers: map[string]network.EndpointResource{},
97+
}, nil
7498
}
7599

76100
// GetNetworksByName returns cluster managed networks by name.
77101
// It is ok to have multiple networks here. #18864
78-
func (c *Cluster) GetNetworksByName(name string) ([]network.Inspect, error) {
102+
func (c *Cluster) GetNetworksByName(name string) ([]network.Network, error) {
79103
// Note that swarmapi.GetNetworkRequest.Name is not functional.
80104
// So we cannot just use that with c.GetNetwork.
81105
list, err := c.listNetworks(context.TODO(), &swarmapi.ListNetworksRequest_Filters{
@@ -84,7 +108,7 @@ func (c *Cluster) GetNetworksByName(name string) ([]network.Inspect, error) {
84108
if err != nil {
85109
return nil, err
86110
}
87-
nr := make([]network.Inspect, len(list))
111+
nr := make([]network.Network, len(list))
88112
for i, n := range list {
89113
nr[i] = convert.BasicNetworkFromGRPC(*n)
90114
}

daemon/network.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ func (daemon *Daemon) GetNetworks(filter network.Filter, config backend.NetworkL
595595
networks := make([]networktypes.Inspect, 0, len(allNetworks))
596596
for _, n := range allNetworks {
597597
if filter.Matches(n) {
598-
nr := buildNetworkResource(n)
599-
if config.Detailed {
600-
nr.Containers = buildContainerAttachments(n)
601-
if config.Verbose {
602-
nr.Services = buildServiceAttachments(n)
603-
}
598+
nr := networktypes.Inspect{
599+
Network: buildNetworkResource(n),
600+
Containers: buildContainerAttachments(n),
601+
}
602+
if config.WithServices {
603+
nr.Services = buildServiceAttachments(n)
604604
}
605605
networks = append(networks, nr)
606606
}
@@ -609,14 +609,27 @@ func (daemon *Daemon) GetNetworks(filter network.Filter, config backend.NetworkL
609609
return networks, nil
610610
}
611611

612+
func (daemon *Daemon) GetNetworkSummaries(filter network.Filter) ([]networktypes.Summary, error) {
613+
allNetworks := daemon.getAllNetworks()
614+
networks := make([]networktypes.Summary, 0, len(allNetworks))
615+
for _, n := range allNetworks {
616+
if filter.Matches(n) {
617+
nr := networktypes.Summary{Network: buildNetworkResource(n)}
618+
networks = append(networks, nr)
619+
}
620+
}
621+
622+
return networks, nil
623+
}
624+
612625
// buildNetworkResource builds a [types.NetworkResource] from the given
613626
// [libnetwork.Network], to be returned by the API.
614-
func buildNetworkResource(nw *libnetwork.Network) networktypes.Inspect {
627+
func buildNetworkResource(nw *libnetwork.Network) networktypes.Network {
615628
if nw == nil {
616-
return networktypes.Inspect{}
629+
return networktypes.Network{}
617630
}
618631

619-
return networktypes.Inspect{
632+
return networktypes.Network{
620633
Name: nw.Name(),
621634
ID: nw.ID(),
622635
Created: nw.Created(),
@@ -630,7 +643,6 @@ func buildNetworkResource(nw *libnetwork.Network) networktypes.Inspect {
630643
Ingress: nw.Ingress(),
631644
ConfigFrom: networktypes.ConfigReference{Network: nw.ConfigFrom()},
632645
ConfigOnly: nw.ConfigOnly(),
633-
Containers: map[string]networktypes.EndpointResource{},
634646
Options: nw.DriverOptions(),
635647
Labels: nw.Labels(),
636648
Peers: buildPeerInfoResources(nw.Peers()),

daemon/server/backend/backend.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,5 @@ type PluginDisableConfig struct {
172172

173173
// NetworkListConfig stores the options available for listing networks
174174
type NetworkListConfig struct {
175-
// TODO(@cpuguy83): naming is hard, this is pulled from what was being used in the router before moving here
176-
Detailed bool
177-
Verbose bool
175+
WithServices bool
178176
}

daemon/server/router/network/backend.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// to provide network specific functionality.
1414
type Backend interface {
1515
GetNetworks(dnetwork.Filter, backend.NetworkListConfig) ([]network.Inspect, error)
16+
GetNetworkSummaries(dnetwork.Filter) ([]network.Summary, error)
1617
CreateNetwork(ctx context.Context, nc network.CreateRequest) (*network.CreateResponse, error)
1718
ConnectContainerToNetwork(ctx context.Context, containerName, n 57CB etworkName string, endpointConfig *network.EndpointSettings) error
1819
DisconnectContainerFromNetwork(containerName string, networkName string, force bool) error
@@ -24,8 +25,9 @@ type Backend interface {
2425
// to provide cluster network specific functionality.
2526
type ClusterBackend interface {
2627
GetNetworks(dnetwork.Filter) ([]network.Inspect, error)
28+
GetNetworkSummaries(dnetwork.Filter) ([]network.Summary, error)
2729
GetNetwork(name string) (network.Inspect, error)
28-
GetNetworksByName(name string) ([]network.Inspect, error)
30+
GetNetworksByName(name string) ([]network.Network, error)
2931
CreateNetwork(nc network.CreateRequest) (string, error)
3032
RemoveNetwork(name string) error
3133
}

daemon/server/router/network/network_routes.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,51 @@ func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWrit
3434
return err
3535
}
3636

37-
var list []network.Summary
38-
nr, err := n.cluster.GetNetworks(filter)
39-
if err == nil {
40-
list = nr
41-
}
37+
if versions.LessThan(httputils.VersionFromContext(ctx), "1.28") {
38+
list, _ := n.cluster.GetNetworks(filter)
39+
var idx map[string]bool
40+
if len(list) > 0 {
41+
idx = make(map[string]bool, len(list))
42+
for _, n := range list {
43+
idx[n.ID] = true
44+
}
45+
}
4246

43-
// Combine the network list returned by Docker daemon if it is not already
44-
// returned by the cluster manager
45-
localNetworks, err := n.backend.GetNetworks(filter, backend.NetworkListConfig{Detailed: versions.LessThan(httputils.VersionFromContext(ctx), "1.28")})
46-
if err != nil {
47-
return err
47+
localNetworks, err := n.backend.GetNetworks(filter, backend.NetworkListConfig{WithServices: false})
48+
if err != nil {
49+
return err
50+
}
51+
for _, n := range localNetworks {
52+
if !idx[n.ID] {
53+
list = append(list, n)
54+
}
55+
}
56+
if list == nil {
57+
list = []network.Inspect{}
58+
}
59+
return httputils.WriteJSON(w, http.StatusOK, list)
4860
}
4961

62+
list, _ := n.cluster.GetNetworkSummaries(filter)
5063
var idx map[string]bool
5164
if len(list) > 0 {
5265
idx = make(map[string]bool, len(list))
5366
for _, n := range list {
5467
idx[n.ID] = true
5568
}
5669
}
70+
71+
// Combine the network list returned by Docker daemon if it is not already
72+
// returned by the cluster manager
73+
localNetworks, err := n.backend.GetNetworkSummaries(filter)
74+
if err != nil {
75+
return err
76+
}
77+
5778
for _, n := range localNetworks {
58-
if idx[n.ID] {
59-
continue
79+
if !idx[n.ID] {
80+
list = append(list, n)
6081
}
61-
list = append(list, n)
6282
}
6383

6484
if list == nil {
@@ -126,7 +146,7 @@ func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r
126146
}
127147
filter.IDAlsoMatchesName = true
128148

129-
networks, _ := n.backend.GetNetworks(filter, backend.NetworkListConfig{Detailed: true, Verbose: verbose})
149+
networks, _ := n.backend.GetNetworks(filter, backend.NetworkListConfig{WithServices: verbose})
130150
for _, nw := range networks {
131151
if nw.ID == term {
132152
return httputils.WriteJSON(w, http.StatusOK, nw)
@@ -150,7 +170,7 @@ func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r
150170
// return the network. Skipped using isMatchingScope because it is true if the scope
151171
// is not set which would be case if the client API v1.30
152172
if strings.HasPrefix(nwk.ID, term) || networkScope == scope.Swarm {
153-
// If we have a previous match "backend", return it, we need verbose when enabled
173+
// If we have a previous match "backend", return it
154174
// ex: overlay/partial_ID or name/swarm_scope
155175
if nwv, ok := listByPartialID[nwk.ID]; ok {
156176
nwk = nwv
@@ -336,7 +356,7 @@ func (n *networkRouter) findUniqueNetwork(term string) (network.Inspect, error)
336356
}
337357
filter.IDAlsoMatchesName = true
338358

339-
networks, _ := n.backend.GetNetworks(filter, backend.NetworkListConfig{Detailed: true})
359+
networks, _ := n.backend.GetNetworks(filter, backend.NetworkListConfig{})
340360
for _, nw := range networks {
341361
if nw.ID == term {
342362
return nw, nil

integration/network/delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"gotest.tools/v3/skip"
1313
)
1414

15-
func containsNetwork(nws []networktypes.Inspect, networkID string) bool {
15+
func containsNetwork(nws []networktypes.Summary, networkID string) bool {
1616
for _, n := range nws {
1717
if n.ID == networkID {
1818
return true

0 commit comments

Comments
 (0)
0