Browse code

libnet: remove Endpoint.anonymous

No more concept of "anonymous endpoints". The equivalent is now an
endpoint with no DNSNames set.

Some of the code removed by this commit was mutating user-supplied
endpoint's Aliases to add container's short ID to that list. In order to
preserve backward compatibility for the ContainerInspect endpoint, this
commit also takes care of adding that short ID (and the container
hostname) to `EndpointSettings.Aliases` before returning the response.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>

Albin Kerouanton authored on 2023/11/30 07:02:56
Showing 8 changed files
... ...
@@ -15,6 +15,8 @@ import (
15 15
 	"github.com/docker/docker/daemon/config"
16 16
 	"github.com/docker/docker/daemon/network"
17 17
 	"github.com/docker/docker/errdefs"
18
+	"github.com/docker/docker/internal/sliceutil"
19
+	"github.com/docker/docker/pkg/stringid"
18 20
 	"github.com/docker/go-connections/nat"
19 21
 )
20 22
 
... ...
@@ -27,6 +29,18 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, size bo
27 27
 		return daemon.containerInspectPre120(ctx, name)
28 28
 	case versions.Equal(version, "1.20"):
29 29
 		return daemon.containerInspect120(name)
30
+	case versions.LessThan(version, "1.45"):
31
+		ctr, err := daemon.ContainerInspectCurrent(ctx, name, size)
32
+		if err != nil {
33
+			return nil, err
34
+		}
35
+
36
+		shortCID := stringid.TruncateID(ctr.ID)
37
+		for _, ep := range ctr.NetworkSettings.Networks {
38
+			ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
39
+		}
40
+
41
+		return ctr, nil
30 42
 	default:
31 43
 		return daemon.ContainerInspectCurrent(ctx, name, size)
32 44
 	}
... ...
@@ -70,6 +70,11 @@ keywords: "API, Docker, rcli, REST, documentation"
70 70
 * `GET /info` now includes `status` properties in `Runtimes`.
71 71
 * A new field named `DNSNames` and containing all non-fully qualified DNS names
72 72
   a container takes on a specific network has been added to `GET /containers/{name:.*}/json`.
73
+* The `Aliases` field returned in calls to `GET /containers/{name:.*}/json` in v1.44  and older
74
+  versions contains the short container ID. This will change in the next API version,  v1.45.
75
+  Starting with that API version, this specific value will  be removed from the `Aliases` field
76
+  such that this field will reflect exactly the values originally submitted to the
77
+  `POST /containers/create` endpoint. The newly introduced `DNSNames` should now be used instead.
73 78
 
74 79
 ## v1.43 API changes
75 80
 
... ...
@@ -13,7 +13,8 @@ source hack/make/.integration-test-helpers
13 13
 # --deselect=tests/integration/api_container_test.py::AttachContainerTest::test_attach_no_stream
14 14
 # TODO re-enable test_attach_no_stream after https://github.com/docker/docker-py/issues/2513 is resolved
15 15
 # TODO re-enable test_run_container_reading_socket_ws. It's reported in https://github.com/docker/docker-py/issues/1478, and we're getting that error in our tests.
16
-: "${PY_TEST_OPTIONS:=--junitxml=${DEST}/junit-report.xml --deselect=tests/integration/api_container_test.py::AttachContainerTest::test_attach_no_stream --deselect=tests/integration/api_container_test.py::AttachContainerTest::test_run_container_reading_socket_ws}"
16
+# TODO re-enable test_run_with_networking_config once this issue is fixed: https://github.com/moby/moby/pull/46853#issuecomment-1864679942.
17
+: "${PY_TEST_OPTIONS:=--junitxml=${DEST}/junit-report.xml --deselect=tests/integration/api_container_test.py::AttachContainerTest::test_attach_no_stream --deselect=tests/integration/api_container_test.py::AttachContainerTest::test_run_container_reading_socket_ws --deselect=tests/integration/models_containers_test.py::ContainerCollectionTest::test_run_with_networking_config}"
17 18
 
18 19
 # build --squash is not supported with containerd integration.
19 20
 if [ -n "$TEST_INTEGRATION_USE_SNAPSHOTTER" ]; then
... ...
@@ -5,6 +5,7 @@ import (
5 5
 
6 6
 	containertypes "github.com/docker/docker/api/types/container"
7 7
 	"github.com/docker/docker/api/types/network"
8
+	"github.com/docker/docker/client"
8 9
 	"github.com/docker/docker/integration/internal/container"
9 10
 	net "github.com/docker/docker/integration/internal/network"
10 11
 	"github.com/docker/docker/integration/internal/swarm"
... ...
@@ -13,13 +14,13 @@ import (
13 13
 	"gotest.tools/v3/skip"
14 14
 )
15 15
 
16
-func TestDockerNetworkConnectAlias(t *testing.T) {
16
+func TestDockerNetworkConnectAliasPreV144(t *testing.T) {
17 17
 	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
18 18
 	ctx := setupTest(t)
19 19
 
20 20
 	d := swarm.NewSwarm(ctx, t, testEnv)
21 21
 	defer d.Stop(t)
22
-	client := d.NewClientT(t)
22
+	client := d.NewClientT(t, client.WithVersion("1.43"))
23 23
 	defer client.Close()
24 24
 
25 25
 	name := t.Name() + "test-alias"
... ...
@@ -47,7 +47,7 @@ func (sb *Sandbox) setupDefaultGW() error {
47 47
 		}
48 48
 	}
49 49
 
50
-	createOptions := []EndpointOption{CreateOptionAnonymous()}
50
+	createOptions := []EndpointOption{}
51 51
 
52 52
 	var gwName string
53 53
 	if len(sb.containerID) <= gwEPlen {
... ...
@@ -31,7 +31,6 @@ type Endpoint struct {
31 31
 	joinInfo     *endpointJoinInfo
32 32
 	sandboxID    string
33 33
 	exposedPorts []types.TransportPort
34
-	anonymous    bool
35 34
 	// dnsNames holds all the non-fully qualified DNS names associated to this endpoint. Order matters: first entry
36 35
 	// will be used for the PTR records associated to the endpoint's IPv4 and IPv6 addresses.
37 36
 	dnsNames          []string
... ...
@@ -67,7 +66,6 @@ func (ep *Endpoint) MarshalJSON() ([]byte, error) {
67 67
 		epMap["generic"] = ep.generic
68 68
 	}
69 69
 	epMap["sandbox"] = ep.sandboxID
70
-	epMap["anonymous"] = ep.anonymous
71 70
 	epMap["dnsNames"] = ep.dnsNames
72 71
 	epMap["disableResolution"] = ep.disableResolution
73 72
 	epMap["svcName"] = ep.svcName
... ...
@@ -159,8 +157,9 @@ func (ep *Endpoint) UnmarshalJSON(b []byte) (err error) {
159 159
 		}
160 160
 	}
161 161
 
162
+	var anonymous bool
162 163
 	if v, ok := epMap["anonymous"]; ok {
163
-		ep.anonymous = v.(bool)
164
+		anonymous = v.(bool)
164 165
 	}
165 166
 	if v, ok := epMap["disableResolution"]; ok {
166 167
 		ep.disableResolution = v.(bool)
... ...
@@ -206,7 +205,7 @@ func (ep *Endpoint) UnmarshalJSON(b []byte) (err error) {
206 206
 	if !hasDNSNames {
207 207
 		// The field dnsNames was introduced in v25.0. If we don't have it, the on-disk state was written by an older
208 208
 		// daemon, thus we need to populate dnsNames based off of myAliases and anonymous values.
209
-		if !ep.anonymous {
209
+		if !anonymous {
210 210
 			myAliases = append([]string{ep.name}, myAliases...)
211 211
 		}
212 212
 		ep.dnsNames = sliceutil.Dedup(myAliases)
... ...
@@ -229,7 +228,6 @@ func (ep *Endpoint) CopyTo(o datastore.KVObject) error {
229 229
 	dstEp.sandboxID = ep.sandboxID
230 230
 	dstEp.dbIndex = ep.dbIndex
231 231
 	dstEp.dbExists = ep.dbExists
232
-	dstEp.anonymous = ep.anonymous
233 232
 	dstEp.disableResolution = ep.disableResolution
234 233
 	dstEp.svcName = ep.svcName
235 234
 	dstEp.svcID = ep.svcID
... ...
@@ -949,14 +947,6 @@ func CreateOptionDNS(dns []string) EndpointOption {
949 949
 	}
950 950
 }
951 951
 
952
-// CreateOptionAnonymous function returns an option setter for setting
953
-// this endpoint as anonymous
954
-func CreateOptionAnonymous() EndpointOption {
955
-	return func(ep *Endpoint) {
956
-		ep.anonymous = true
957
-	}
958
-}
959
-
960 952
 // CreateOptionDNSNames specifies the list of (non fully qualified) DNS names associated to an endpoint. These will be
961 953
 // used to populate the embedded DNS server. Order matters: first name will be used to generate PTR records.
962 954
 func CreateOptionDNSNames(names []string) EndpointOption {
... ...
@@ -192,7 +192,6 @@ func TestEndpointMarshalling(t *testing.T) {
192 192
 		name:      "Bau",
193 193
 		id:        "efghijklmno",
194 194
 		sandboxID: "ambarabaciccicocco",
195
-		anonymous: true,
196 195
 		iface: &EndpointInterface{
197 196
 			mac: []byte{11, 12, 13, 14, 15, 16},
198 197
 			addr: &net.IPNet{
... ...
@@ -220,7 +219,7 @@ func TestEndpointMarshalling(t *testing.T) {
220 220
 		t.Fatal(err)
221 221
 	}
222 222
 
223
-	if e.name != ee.name || e.id != ee.id || e.sandboxID != ee.sandboxID || !reflect.DeepEqual(e.dnsNames, ee.dnsNames) || !compareEndpointInterface(e.iface, ee.iface) || e.anonymous != ee.anonymous {
223
+	if e.name != ee.name || e.id != ee.id || e.sandboxID != ee.sandboxID || !reflect.DeepEqual(e.dnsNames, ee.dnsNames) || !compareEndpointInterface(e.iface, ee.iface) {
224 224
 		t.Fatalf("JSON marsh/unmarsh failed.\nOriginal:\n%#v\nDecoded:\n%#v\nOriginal iface: %#v\nDecodediface:\n%#v", e, ee, e.iface, ee.iface)
225 225
 	}
226 226
 }
... ...
@@ -2162,10 +2162,6 @@ func (n *Network) createLoadBalancerSandbox() (retErr error) {
2162 2162
 		CreateOptionIpam(n.loadBalancerIP, nil, nil, nil),
2163 2163
 		CreateOptionLoadBalancer(),
2164 2164
 	}
2165
-	if n.hasLoadBalancerEndpoint() && !n.ingress {
2166
-		// Mark LB endpoints as anonymous so they don't show up in DNS
2167
-		epOptions = append(epOptions, CreateOptionAnonymous())
2168
-	}
2169 2165
 	ep, err := n.createEndpoint(endpointName, epOptions...)
2170 2166
 	if err != nil {
2171 2167
 		return err