Browse code

libnetwork: provide endpoint name for IPAM drivers

Signed-off-by: Olli Janatuinen <olli.janatuinen@gmail.com>

Olli Janatuinen authored on 2025/08/01 03:18:59
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"
... ...
@@ -1217,13 +1217,11 @@ func (n *Network) createEndpoint(ctx context.Context, name string, options ...En
1217 1217
 		return nil, err
1218 1218
 	}
1219 1219
 
1220
+	ep.ipamOptions = map[string]string{netlabel.EndpointName: name}
1220 1221
 	if capability.RequiresMACAddress {
1221 1222
 		if ep.iface.mac == nil {
1222 1223
 			ep.iface.mac = netutils.GenerateRandomMAC()
1223 1224
 		}
1224
-		if ep.ipamOptions == nil {
1225
-			ep.ipamOptions = make(map[string]string)
1226
-		}
1227 1225
 		ep.ipamOptions[netlabel.MacAddress] = ep.iface.mac.String()
1228 1226
 	}
1229 1227