Skip to content

Commit

Permalink
Merge pull request moby#47873 from thaJeztah/move_more_network_api_types
Browse files Browse the repository at this point in the history
api/types: move more networking-types to api/types/network
  • Loading branch information
thaJeztah authored May 30, 2024
2 parents 2ebf191 + 68bf0e7 commit e622cea
Show file tree
Hide file tree
Showing 27 changed files with 198 additions and 139 deletions.
2 changes: 1 addition & 1 deletion api/server/router/network/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// to provide network specific functionality.
type Backend interface {
GetNetworks(filters.Args, backend.NetworkListConfig) ([]types.NetworkResource, error)
CreateNetwork(nc types.NetworkCreateRequest) (*types.NetworkCreateResponse, error)
CreateNetwork(nc types.NetworkCreateRequest) (*network.CreateResponse, error)
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
DisconnectContainerFromNetwork(containerName string, networkName string, force bool) error
DeleteNetwork(networkID string) error
Expand Down
6 changes: 3 additions & 3 deletions api/server/router/network/network_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
if err != nil {
return err
}
nw = &types.NetworkCreateResponse{
nw = &network.CreateResponse{
ID: id,
}
}
Expand All @@ -239,7 +239,7 @@ func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseW
return err
}

var connect types.NetworkConnect
var connect network.ConnectOptions
if err := httputils.ReadJSON(r, &connect); err != nil {
return err
}
Expand All @@ -256,7 +256,7 @@ func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.Respon
return err
}

var disconnect types.NetworkDisconnect
var disconnect network.DisconnectOptions
if err := httputils.ReadJSON(r, &disconnect); err != nil {
return err
}
Expand Down
32 changes: 20 additions & 12 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,24 @@ definitions:
type: "string"
example: "10.133.77.91"

NetworkCreateResponse:
description: "OK response to NetworkCreate operation"
type: "object"
title: "NetworkCreateResponse"
x-go-name: "CreateResponse"
required: [Id, Warning]
properties:
Id:
description: "The ID of the created network."
type: "string"
x-nullable: false
example: "b5c4fc71e8022147cd25de22b22173de4e3b170134117172eb595cb91b4e7e5d"
Warning:
description: "Warnings encountered when creating the container"
type: "string"
x-nullable: false
example: ""

BuildInfo:
type: "object"
properties:
Expand Down Expand Up @@ -10144,19 +10162,9 @@ paths:
- "application/json"
responses:
201:
description: "No error"
description: "Network created successfully"
schema:
type: "object"
title: "NetworkCreateResponse"
properties:
Id:
description: "The ID of the created network."
type: "string"
Warning:
type: "string"
example:
Id: "22be93d5babb089c5aab8dbc369042fad48ff791584ca2da2100db837a1c7c30"
Warning: ""
$ref: "#/definitions/NetworkCreateResponse"
400:
description: "bad parameter"
schema:
Expand Down
19 changes: 19 additions & 0 deletions api/types/network/create_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package network

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

// CreateResponse NetworkCreateResponse
//
// OK response to NetworkCreate operation
// swagger:model CreateResponse
type CreateResponse struct {

// The ID of the created network.
// Required: true
ID string `json:"Id"`

// Warnings encountered when creating the container
// Required: true
Warning string `json:"Warning"`
}
30 changes: 30 additions & 0 deletions api/types/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ const (
NetworkNat = "nat"
)

// InspectOptions holds parameters to inspect network.
type InspectOptions struct {
Scope string
Verbose bool
}

// ConnectOptions represents the data to be used to connect a container to the
// network.
type ConnectOptions struct {
Container string
EndpointConfig *EndpointSettings `json:",omitempty"`
}

// DisconnectOptions represents the data to be used to disconnect a container
// from the network.
type DisconnectOptions struct {
Container string
Force bool
}

// Address represents an IP address
type Address struct {
Addr string
Expand Down Expand Up @@ -45,6 +65,16 @@ type ServiceInfo struct {
Tasks []Task
}

// EndpointResource contains network resources allocated and used for a
// container in a network.
type EndpointResource struct {
Name string
EndpointID string
MacAddress string
IPv4Address string
IPv6Address string
}

// NetworkingConfig represents the container's networking configuration for each of its interfaces
// Carries the networking configs specified in the `docker run` and `docker network connect` commands
type NetworkingConfig struct {
Expand Down
67 changes: 17 additions & 50 deletions api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,32 +425,23 @@ type MountPoint struct {

// 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]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"`
}

// EndpointResource contains network resources allocated and used for a container in a network
type EndpointResource struct {
Name string
EndpointID string
MacAddress string
IPv4Address string
IPv6Address string
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
Expand All @@ -477,30 +468,6 @@ type NetworkCreateRequest struct {
Name string // Name is the requested name of the network.
}

// NetworkCreateResponse is the response message sent by the server for network create call
type NetworkCreateResponse struct {
ID string `json:"Id"`
Warning string
}

// NetworkConnect represents the data to be used to connect a container to the network
type NetworkConnect struct {
Container string
EndpointConfig *network.EndpointSettings `json:",omitempty"`
}

// NetworkDisconnect represents the data to be used to disconnect a container from the network
type NetworkDisconnect struct {
Container string
Force bool
}

// NetworkInspectOptions holds parameters to inspect network
type NetworkInspectOptions struct {
Scope string
Verbose bool
}

// DiskUsageObject represents an object type used for disk usage query filtering.
type DiskUsageObject string

Expand Down
26 changes: 26 additions & 0 deletions api/types/types_deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"
)

// ImageImportOptions holds information to import images from the client host.
Expand Down Expand Up @@ -33,3 +34,28 @@ type ImageListOptions = image.ListOptions
//
// Deprecated: use [image.RemoveOptions].
type ImageRemoveOptions = image.RemoveOptions

// NetworkCreateResponse is the response message sent by the server for network create call.
//
// Deprecated: use [network.CreateResponse].
type NetworkCreateResponse = network.CreateResponse

// NetworkInspectOptions holds parameters to inspect network.
//
// Deprecated: use [network.InspectOptions].
type NetworkInspectOptions = network.InspectOptions

// NetworkConnect represents the data to be used to connect a container to the network
//
// Deprecated: use [network.ConnectOptions].
type NetworkConnect = network.ConnectOptions

// NetworkDisconnect represents the data to be used to disconnect a container from the network
//
// Deprecated: use [network.DisconnectOptions].
type NetworkDisconnect = network.DisconnectOptions

// EndpointResource contains network resources allocated and used for a container in a network.
//
// Deprecated: use [network.EndpointResource].
type EndpointResource = network.EndpointResource
6 changes: 3 additions & 3 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ type ImageAPIClient interface {
// NetworkAPIClient defines API client methods for the networks
type NetworkAPIClient interface {
NetworkConnect(ctx context.Context, network, container string, config *network.EndpointSettings) error
NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, 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 types.NetworkInspectOptions) (types.NetworkResource, error)
NetworkInspectWithRaw(ctx context.Context, network string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, 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 types.NetworkListOptions) ([]types.NetworkResource, error)
NetworkRemove(ctx context.Context, network string) error
NetworksPrune(ctx context.Context, pruneFilter filters.Args) (types.NetworksPruneReport, error)
Expand Down
3 changes: 1 addition & 2 deletions client/network_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package client // import "github.com/docker/docker/client"
import (
"context"

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

// NetworkConnect connects a container to an existent network in the docker host.
func (cli *Client) NetworkConnect(ctx context.Context, networkID, containerID string, config *network.EndpointSettings) error {
nc := types.NetworkConnect{
nc := network.ConnectOptions{
Container: containerID,
EndpointConfig: config,
}
Expand Down
5 changes: 2 additions & 3 deletions client/network_connect_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 @@ -39,7 +38,7 @@ func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}

var connect types.NetworkConnect
var connect network.ConnectOptions
if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
return nil, err
}
Expand Down Expand Up @@ -78,7 +77,7 @@ func TestNetworkConnect(t *testing.T) {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}

var connect types.NetworkConnect
var connect network.ConnectOptions
if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions client/network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"encoding/json"

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

// NetworkCreate creates a new network in the docker host.
func (cli *Client) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) {
var response types.NetworkCreateResponse
func (cli *Client) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (network.CreateResponse, error) {
var response network.CreateResponse

// Make sure we negotiated (if the client is configured to do so),
// as code below contains API-version specific handling of options.
Expand Down
3 changes: 2 additions & 1 deletion client/network_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
Expand Down Expand Up @@ -50,7 +51,7 @@ func TestNetworkCreate(t *testing.T) {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}

content, err := json.Marshal(types.NetworkCreateResponse{
content, err := json.Marshal(network.CreateResponse{
ID: "network_id",
Warning: "warning",
})
Expand Down
7 changes: 5 additions & 2 deletions client/network_disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package client // import "github.com/docker/docker/client"
import (
"context"

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

// NetworkDisconnect disconnects a container from an existent network in the docker host.
func (cli *Client) NetworkDisconnect(ctx context.Context, networkID, containerID string, force bool) error {
nd := types.NetworkDisconnect{Container: containerID, Force: force}
nd := network.DisconnectOptions{
Container: containerID,
Force: force,
}
resp, err := cli.post(ctx, "/networks/"+networkID+"/disconnect", nil, nd, nil)
ensureReaderClosed(resp)
return err
Expand Down
4 changes: 2 additions & 2 deletions client/network_disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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"
is "gotest.tools/v3/assert/cmp"
Expand Down Expand Up @@ -38,7 +38,7 @@ func TestNetworkDisconnect(t *testing.T) {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}

var disconnect types.NetworkDisconnect
var disconnect network.DisconnectOptions
if err := json.NewDecoder(req.Body).Decode(&disconnect); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit e622cea

Please sign in to comment.