Browse code

Merge pull request #50586 from olljanat/endpoint-name-label

libnetwork: provide endpoint name for IPAM drivers

Sebastiaan van Stijn authored on 2025/09/05 03:20:25
Showing 3 changed files
... ...
@@ -362,6 +362,34 @@ func TestAuxAddresses(t *testing.T) {
362 362
 	}
363 363
 }
364 364
 
365
+func TestEndpointNameLabel(t *testing.T) {
366
+	skip.If(t, runtime.GOOS == "windows", "test causes sync issue with Windows HNS")
367
+	defer netnsutils.SetupTestOSContext(t)()
368
+
369
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()))
370
+	assert.NilError(t, err)
371
+	defer c.Stop()
372
+
373
+	ipamOpt := NetworkOptionIpam(defaultipam.DriverName, "", []*IpamConf{{PreferredPool: "10.35.0.0/16", Gateway: "10.35.255.253"}}, nil, nil)
374
+	gnw, err := c.NewNetwork(context.Background(), "bridge", "label-test", "",
375
+		NetworkOptionEnableIPv4(true),
376
+		ipamOpt,
377
+	)
378
+	assert.NilError(t, err)
379
+	defer func() {
380
+		err := gnw.Delete()
381
+		assert.NilError(t, err)
382
+	}()
383
+
384
+	createOptions := CreateOptionIPAM(net.ParseIP("10.35.0.10"), nil, nil)
385
+	ep, err := gnw.CreateEndpoint(context.Background(), "ep1", createOptions)
386
+	assert.NilError(t, err)
387
+
388
+	assert.Check(t, is.Equal(ep.ipamOptions[netlabel.EndpointName], "ep1"), "got: %s; expected: ep1", ep.ipamOptions[netlabel.EndpointName])
389
+
390
+	defer ep.Delete(context.Background(), false) //nolint:errcheck
391
+}
392
+
365 393
 func TestUpdateSvcRecord(t *testing.T) {
366 394
 	skip.If(t, runtime.GOOS == "windows", "bridge driver and IPv6, only works on linux")
367 395
 
... ...
@@ -26,6 +26,9 @@ const (
26 26
 	// DNSServers A list of DNS servers associated with the endpoint
27 27
 	DNSServers = Prefix + ".endpoint.dnsservers"
28 28
 
29
+	// EndpointName constant represents the container's Name
30
+	EndpointName = Prefix + ".endpoint.name"
31
+
29 32
 	// EndpointSysctls is a comma separated list interface-specific sysctls
30 33
 	// where the interface name is represented by the string "IFNAME".
31 34
 	EndpointSysctls = Prefix + ".endpoint.sysctls"
... ...
@@ -1222,13 +1222,11 @@ func (n *Network) createEndpoint(ctx context.Context, name string, options ...En
1222 1222
 		return nil, err
1223 1223
 	}
1224 1224
 
1225
+	ep.ipamOptions = map[string]string{netlabel.EndpointName: name}
1225 1226
 	if capability.RequiresMACAddress {
1226 1227
 		if ep.iface.mac == nil {
1227 1228
 			ep.iface.mac = netutils.GenerateRandomMAC()
1228 1229
 		}
1229
-		if ep.ipamOptions == nil {
1230
-			ep.ipamOptions = make(map[string]string)
1231
-		}
1232 1230
 		ep.ipamOptions[netlabel.MacAddress] = ep.iface.mac.String()
1233 1231
 	}
1234 1232