From 35d10a96553dfa9935cb40a219eab6fdacc2beca Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 16 Jun 2025 11:37:27 +0200 Subject: [PATCH] bake: multi ips support for extra hosts Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> (cherry picked from commit 989978a42b59d03d862405c74fc0cdc6e8eb18ae) --- bake/compose.go | 6 ++---- bake/compose_test.go | 3 ++- build/utils.go | 26 ++++++++++++++++---------- build/utils_test.go | 5 +++++ tests/bake.go | 3 +++ tests/build.go | 19 +++++++++++++++++++ 6 files changed, 47 insertions(+), 15 deletions(-) diff --git a/bake/compose.go b/bake/compose.go index b913d560..84c1fd4f 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -125,10 +125,8 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf extraHosts := map[string]*string{} if s.Build.ExtraHosts != nil { for k, v := range s.Build.ExtraHosts { - for _, ip := range v { - vv := ip - extraHosts[k] = &vv - } + vv := strings.Join(v, ",") + extraHosts[k] = &vv } } diff --git a/bake/compose_test.go b/bake/compose_test.go index 6e08fdc4..9a5e97de 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -34,6 +34,7 @@ services: - type=local,dest=path/to/cache extra_hosts: - "somehost:162.242.195.82" + - "somehost:162.242.195.83" - "myhostv6:::1" ssh: - key=/path/to/key @@ -79,7 +80,7 @@ secrets: require.Equal(t, ptrstr("123"), c.Targets[1].Args["buildno"]) require.Equal(t, []string{"type=local,src=path/to/cache"}, stringify(c.Targets[1].CacheFrom)) require.Equal(t, []string{"type=local,dest=path/to/cache"}, stringify(c.Targets[1].CacheTo)) - require.Equal(t, map[string]*string{"myhostv6": ptrstr("::1"), "somehost": ptrstr("162.242.195.82")}, c.Targets[1].ExtraHosts) + require.Equal(t, map[string]*string{"myhostv6": ptrstr("::1"), "somehost": ptrstr("162.242.195.82,162.242.195.83")}, c.Targets[1].ExtraHosts) require.Equal(t, "none", *c.Targets[1].NetworkMode) require.Equal(t, []string{"default", "key=/path/to/key"}, stringify(c.Targets[1].SSH)) require.Equal(t, []string{ diff --git a/build/utils.go b/build/utils.go index 3d095ae6..52d50188 100644 --- a/build/utils.go +++ b/build/utils.go @@ -77,24 +77,30 @@ func toBuildkitExtraHosts(ctx context.Context, inp []string, nodeDriver *driver. } // If the IP Address is a "host-gateway", replace this value with the // IP address provided by the worker's label. + var ips []string if ip == mobyHostGatewayName { hgip, err := nodeDriver.HostGatewayIP(ctx) if err != nil { return "", errors.Wrap(err, "unable to derive the IP value for host-gateway") } - ip = hgip.String() + ips = append(ips, hgip.String()) } else { - // If the address is enclosed in square brackets, extract it (for IPv6, but - // permit it for IPv4 as well; we don't know the address family here, but it's - // unambiguous). - if len(ip) > 2 && ip[0] == '[' && ip[len(ip)-1] == ']' { - ip = ip[1 : len(ip)-1] - } - if net.ParseIP(ip) == nil { - return "", errors.Errorf("invalid host %s", h) + for _, v := range strings.Split(ip, ",") { + // If the address is enclosed in square brackets, extract it + // (for IPv6, but permit it for IPv4 as well; we don't know the + // address family here, but it's unambiguous). + if len(v) > 2 && v[0] == '[' && v[len(v)-1] == ']' { + v = v[1 : len(v)-1] + } + if net.ParseIP(v) == nil { + return "", errors.Errorf("invalid host %s", h) + } + ips = append(ips, v) } } - hosts = append(hosts, host+"="+ip) + for _, v := range ips { + hosts = append(hosts, host+"="+v) + } } return strings.Join(hosts, ","), nil } diff --git a/build/utils_test.go b/build/utils_test.go index 02b597b9..3983af96 100644 --- a/build/utils_test.go +++ b/build/utils_test.go @@ -72,6 +72,11 @@ func TestToBuildkitExtraHosts(t *testing.T) { doc: "IPv6 localhost, non-canonical, eq sep", input: []string{`ipv6local=0:0:0:0:0:0:0:1`}, }, + { + doc: "Multi IPs", + input: []string{`myhost=162.242.195.82,162.242.195.83`}, + expectedOut: `myhost=162.242.195.82,myhost=162.242.195.83`, + }, { doc: "IPv6 localhost, non-canonical, eq sep, brackets", input: []string{`ipv6local=[0:0:0:0:0:0:0:1]`}, diff --git a/tests/bake.go b/tests/bake.go index fa6e2f4d..0ce6d216 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -2167,11 +2167,14 @@ func testBakeExtraHosts(t *testing.T, sb integration.Sandbox) { dockerfile := []byte(` FROM busybox RUN cat /etc/hosts | grep myhost | grep 1.2.3.4 +RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.81 +RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.82 `) bakefile := []byte(` target "default" { extra-hosts = { myhost = "1.2.3.4" + myhostmulti = "162.242.195.81,162.242.195.82" } } `) diff --git a/tests/build.go b/tests/build.go index 2e2b9e02..e179163c 100644 --- a/tests/build.go +++ b/tests/build.go @@ -77,6 +77,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){ testBuildDefaultLoad, testBuildCall, testCheckCallOutput, + testBuildExtraHosts, } func testBuild(t *testing.T, sb integration.Sandbox) { @@ -1322,6 +1323,24 @@ cOpy Dockerfile . }) } +func testBuildExtraHosts(t *testing.T, sb integration.Sandbox) { + dockerfile := []byte(` +FROM busybox +RUN cat /etc/hosts | grep myhost | grep 1.2.3.4 +RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.81 +RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.82 +`) + dir := tmpdir(t, fstest.CreateFile("Dockerfile", dockerfile, 0600)) + cmd := buildxCmd(sb, withArgs("build", + "--add-host=myhost=1.2.3.4", + "--add-host=myhostmulti=162.242.195.81", + "--add-host=myhostmulti=162.242.195.82", + "--output=type=cacheonly", dir), + ) + out, err := cmd.CombinedOutput() + require.NoError(t, err, string(out)) +} + func createTestProject(t *testing.T) string { dockerfile := []byte(` FROM busybox:latest AS base