Fix network inspect for default networks.

- Keep old fields in NetworkSetting to respect the deprecation policy.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-10-30 14:57:15 -04:00
Родитель 05534b6f80
Коммит f301c5765a
7 изменённых файлов: 123 добавлений и 49 удалений

Просмотреть файл

@ -286,6 +286,7 @@ type ContainerJSON struct {
// NetworkSettings exposes the network settings in the api
type NetworkSettings struct {
NetworkSettingsBase
DefaultNetworkSettings
Networks map[string]*network.EndpointSettings
}
@ -302,6 +303,20 @@ type NetworkSettingsBase struct {
SecondaryIPv6Addresses []network.Address
}
// DefaultNetworkSettings holds network information
// during the 2 release deprecation period.
// It will be removed in Docker 1.11.
type DefaultNetworkSettings struct {
EndpointID string
Gateway string
GlobalIPv6Address string
GlobalIPv6PrefixLen int
IPAddress string
IPPrefixLen int
IPv6Gateway string
MacAddress string
}
// MountPoint represents a mount point configuration inside the container.
type MountPoint struct {
Name string `json:",omitempty"`

Просмотреть файл

@ -36,12 +36,5 @@ type StatsJSON struct {
// NetworkSettings is a backward compatible struct for APIs prior to 1.21
type NetworkSettings struct {
types.NetworkSettingsBase
EndpointID string
Gateway string
GlobalIPv6Address string
GlobalIPv6PrefixLen int
IPAddress string
IPPrefixLen int
IPv6Gateway string
MacAddress string
types.DefaultNetworkSettings
}

Просмотреть файл

@ -28,7 +28,7 @@ func (daemon *Daemon) ContainerInspect(name string, size bool) (*types.Container
mountPoints := addMountPoints(container)
networkSettings := &types.NetworkSettings{
types.NetworkSettingsBase{
NetworkSettingsBase: types.NetworkSettingsBase{
Bridge: container.NetworkSettings.Bridge,
SandboxID: container.NetworkSettings.SandboxID,
HairpinMode: container.NetworkSettings.HairpinMode,
@ -39,7 +39,8 @@ func (daemon *Daemon) ContainerInspect(name string, size bool) (*types.Container
SecondaryIPAddresses: container.NetworkSettings.SecondaryIPAddresses,
SecondaryIPv6Addresses: container.NetworkSettings.SecondaryIPv6Addresses,
},
container.NetworkSettings.Networks,
DefaultNetworkSettings: daemon.getDefaultNetworkSettings(container.NetworkSettings.Networks),
Networks: container.NetworkSettings.Networks,
}
return &types.ContainerJSON{base, mountPoints, container.Config, networkSettings}, nil
@ -68,7 +69,7 @@ func (daemon *Daemon) ContainerInspect120(name string) (*v1p20.ContainerJSON, er
container.Config.ExposedPorts,
container.hostConfig.VolumeDriver,
}
networkSettings := getBackwardsCompatibleNetworkSettings(container.NetworkSettings)
networkSettings := daemon.getBackwardsCompatibleNetworkSettings(container.NetworkSettings)
return &v1p20.ContainerJSON{base, mountPoints, config, networkSettings}, nil
}
@ -167,7 +168,7 @@ func (daemon *Daemon) VolumeInspect(name string) (*types.Volume, error) {
return volumeToAPIType(v), nil
}
func getBackwardsCompatibleNetworkSettings(settings *network.Settings) *v1p20.NetworkSettings {
func (daemon *Daemon) getBackwardsCompatibleNetworkSettings(settings *network.Settings) *v1p20.NetworkSettings {
result := &v1p20.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Bridge: settings.Bridge,
@ -180,16 +181,26 @@ func getBackwardsCompatibleNetworkSettings(settings *network.Settings) *v1p20.Ne
SecondaryIPAddresses: settings.SecondaryIPAddresses,
SecondaryIPv6Addresses: settings.SecondaryIPv6Addresses,
},
DefaultNetworkSettings: daemon.getDefaultNetworkSettings(settings.Networks),
}
if bridgeSettings := settings.Networks["bridge"]; bridgeSettings != nil {
result.EndpointID = bridgeSettings.EndpointID
result.Gateway = bridgeSettings.Gateway
result.GlobalIPv6Address = bridgeSettings.GlobalIPv6Address
result.GlobalIPv6PrefixLen = bridgeSettings.GlobalIPv6PrefixLen
result.IPAddress = bridgeSettings.IPAddress
result.IPPrefixLen = bridgeSettings.IPPrefixLen
result.IPv6Gateway = bridgeSettings.IPv6Gateway
result.MacAddress = bridgeSettings.MacAddress
}
return result
}
// getDefaultNetworkSettings creates the deprecated structure that holds the information
// about the bridge network for a container.
func (daemon *Daemon) getDefaultNetworkSettings(networks map[string]*network.EndpointSettings) types.DefaultNetworkSettings {
var settings types.DefaultNetworkSettings
if defaultNetwork, ok := networks["bridge"]; ok {
settings.EndpointID = defaultNetwork.EndpointID
settings.Gateway = defaultNetwork.Gateway
settings.GlobalIPv6Address = defaultNetwork.GlobalIPv6Address
settings.GlobalIPv6PrefixLen = defaultNetwork.GlobalIPv6PrefixLen
settings.IPAddress = defaultNetwork.IPAddress
settings.IPPrefixLen = defaultNetwork.IPPrefixLen
settings.IPv6Gateway = defaultNetwork.IPv6Gateway
settings.MacAddress = defaultNetwork.MacAddress
}
return settings
}

Просмотреть файл

@ -50,7 +50,7 @@ func (daemon *Daemon) ContainerInspectPre120(name string) (*v1p19.ContainerJSON,
container.hostConfig.CPUShares,
container.hostConfig.CpusetCpus,
}
networkSettings := getBackwardsCompatibleNetworkSettings(container.NetworkSettings)
networkSettings := daemon.getBackwardsCompatibleNetworkSettings(container.NetworkSettings)
return &v1p19.ContainerJSON{base, volumes, volumesRW, config, networkSettings}, nil
}

Просмотреть файл

@ -2,11 +2,12 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions/v1p20"
"github.com/docker/docker/pkg/integration/checker"
"github.com/docker/docker/pkg/stringutils"
"github.com/go-check/check"
)
@ -23,19 +24,15 @@ func (s *DockerSuite) TestInspectApiContainerResponse(c *check.C) {
version string
keys []string
}{
{"1.20", append(keysBase, "Mounts")},
{"1.19", append(keysBase, "Volumes", "VolumesRW")},
{"v1.20", append(keysBase, "Mounts")},
{"v1.19", append(keysBase, "Volumes", "VolumesRW")},
}
for _, cs := range cases {
endpoint := fmt.Sprintf("/v%s/containers/%s/json", cs.version, cleanedContainerID)
status, body, err := sockRequest("GET", endpoint, nil)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)
body := getInspectBody(c, cs.version, cleanedContainerID)
var inspectJSON map[string]interface{}
if err = json.Unmarshal(body, &inspectJSON); err != nil {
if err := json.Unmarshal(body, &inspectJSON); err != nil {
c.Fatalf("unable to unmarshal body for version %s: %v", cs.version, err)
}
@ -57,15 +54,12 @@ func (s *DockerSuite) TestInspectApiContainerVolumeDriverLegacy(c *check.C) {
cleanedContainerID := strings.TrimSpace(out)
cases := []string{"1.19", "1.20"}
cases := []string{"v1.19", "v1.20"}
for _, version := range cases {
endpoint := fmt.Sprintf("/v%s/containers/%s/json", version, cleanedContainerID)
status, body, err := sockRequest("GET", endpoint, nil)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)
body := getInspectBody(c, version, cleanedContainerID)
var inspectJSON map[string]interface{}
if err = json.Unmarshal(body, &inspectJSON); err != nil {
if err := json.Unmarshal(body, &inspectJSON); err != nil {
c.Fatalf("unable to unmarshal body for version %s: %v", version, err)
}
@ -85,13 +79,10 @@ func (s *DockerSuite) TestInspectApiContainerVolumeDriver(c *check.C) {
cleanedContainerID := strings.TrimSpace(out)
endpoint := fmt.Sprintf("/v1.21/containers/%s/json", cleanedContainerID)
status, body, err := sockRequest("GET", endpoint, nil)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)
body := getInspectBody(c, "v1.21", cleanedContainerID)
var inspectJSON map[string]interface{}
if err = json.Unmarshal(body, &inspectJSON); err != nil {
if err := json.Unmarshal(body, &inspectJSON); err != nil {
c.Fatalf("unable to unmarshal body for version 1.21: %v", err)
}
@ -140,15 +131,12 @@ func (s *DockerSuite) TestInspectApiEmptyFieldsInConfigPre121(c *check.C) {
cleanedContainerID := strings.TrimSpace(out)
cases := []string{"1.19", "1.20"}
cases := []string{"v1.19", "v1.20"}
for _, version := range cases {
endpoint := fmt.Sprintf("/v%s/containers/%s/json", version, cleanedContainerID)
status, body, err := sockRequest("GET", endpoint, nil)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)
body := getInspectBody(c, version, cleanedContainerID)
var inspectJSON map[string]interface{}
if err = json.Unmarshal(body, &inspectJSON); err != nil {
if err := json.Unmarshal(body, &inspectJSON); err != nil {
c.Fatalf("unable to unmarshal body for version %s: %v", version, err)
}
@ -164,3 +152,33 @@ func (s *DockerSuite) TestInspectApiEmptyFieldsInConfigPre121(c *check.C) {
}
}
}
func (s *DockerSuite) TestInspectApiBridgeNetworkSettings120(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
cleanedContainerID := strings.TrimSpace(out)
body := getInspectBody(c, "v1.20", cleanedContainerID)
var inspectJSON v1p20.ContainerJSON
err := json.Unmarshal(body, &inspectJSON)
c.Assert(err, checker.IsNil)
settings := inspectJSON.NetworkSettings
c.Assert(settings.IPAddress, checker.Not(checker.HasLen), 0)
}
func (s *DockerSuite) TestInspectApiBridgeNetworkSettings121(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
cleanedContainerID := strings.TrimSpace(out)
body := getInspectBody(c, "v1.21", cleanedContainerID)
var inspectJSON types.ContainerJSON
err := json.Unmarshal(body, &inspectJSON)
c.Assert(err, checker.IsNil)
settings := inspectJSON.NetworkSettings
c.Assert(settings.IPAddress, checker.Not(checker.HasLen), 0)
c.Assert(settings.Networks["bridge"], checker.Not(checker.IsNil))
c.Assert(settings.IPAddress, checker.Equals, settings.Networks["bridge"].IPAddress)
}

Просмотреть файл

@ -13,6 +13,7 @@ import (
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions/v1p20"
"github.com/docker/docker/pkg/integration/checker"
"github.com/docker/libnetwork/driverapi"
remoteapi "github.com/docker/libnetwork/drivers/remote/api"
@ -641,3 +642,31 @@ func (s *DockerNetworkSuite) TestDockerNetworkMacInspect(c *check.C) {
c.Assert(err, checker.IsNil)
c.Assert(mac, checker.Equals, "a0:b1:c2:d3:e4:f5")
}
func (s *DockerSuite) TestInspectApiMultipeNetworks(c *check.C) {
dockerCmd(c, "network", "create", "mybridge1")
dockerCmd(c, "network", "create", "mybridge2")
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
id := strings.TrimSpace(out)
c.Assert(waitRun(id), check.IsNil)
dockerCmd(c, "network", "connect", "mybridge1", id)
dockerCmd(c, "network", "connect", "mybridge2", id)
body := getInspectBody(c, "v1.20", id)
var inspect120 v1p20.ContainerJSON
err := json.Unmarshal(body, &inspect120)
c.Assert(err, checker.IsNil)
versionedIP := inspect120.NetworkSettings.IPAddress
body = getInspectBody(c, "v1.21", id)
var inspect121 types.ContainerJSON
err = json.Unmarshal(body, &inspect121)
c.Assert(err, checker.IsNil)
c.Assert(inspect121.NetworkSettings.Networks, checker.HasLen, 3)
bridge := inspect121.NetworkSettings.Networks["bridge"]
c.Assert(bridge.IPAddress, checker.Equals, versionedIP)
c.Assert(bridge.IPAddress, checker.Equals, inspect121.NetworkSettings.IPAddress)
}

Просмотреть файл

@ -1587,3 +1587,11 @@ func waitInspect(name, expr, expected string, timeout time.Duration) error {
}
return nil
}
func getInspectBody(c *check.C, version, id string) []byte {
endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
status, body, err := sockRequest("GET", endpoint, nil)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)
return body
}