Skip to content

Commit 989978a

Browse files
committed
bake: multi ips support for extra hosts
Signed-off-by: CrazyMax <[email protected]>
1 parent eb43f4c commit 989978a

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

bake/compose.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf
125125
extraHosts := map[string]*string{}
126126
if s.Build.ExtraHosts != nil {
127127
for k, v := range s.Build.ExtraHosts {
128-
for _, ip := range v {
129-
vv := ip
130-
extraHosts[k] = &vv
131-
}
128+
vv := strings.Join(v, ",")
129+
extraHosts[k] = &vv
132130
}
133131
}
134132

bake/compose_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ services:
3434
- type=local,dest=path/to/cache
3535
extra_hosts:
3636
- "somehost:162.242.195.82"
37+
- "somehost:162.242.195.83"
3738
- "myhostv6:::1"
3839
ssh:
3940
- key=/path/to/key
@@ -79,7 +80,7 @@ secrets:
7980
require.Equal(t, ptrstr("123"), c.Targets[1].Args["buildno"])
8081
require.Equal(t, []string{"type=local,src=path/to/cache"}, stringify(c.Targets[1].CacheFrom))
8182
require.Equal(t, []string{"type=local,dest=path/to/cache"}, stringify(c.Targets[1].CacheTo))
82-
require.Equal(t, map[string]*string{"myhostv6": ptrstr("::1"), "somehost": ptrstr("162.242.195.82")}, c.Targets[1].ExtraHosts)
83+
require.Equal(t, map[string]*string{"myhostv6": ptrstr("::1"), "somehost": ptrstr("162.242.195.82,162.242.195.83")}, c.Targets[1].ExtraHosts)
8384
require.Equal(t, "none", *c.Targets[1].NetworkMode)
8485
require.Equal(t, []string{"default", "key=/path/to/key"}, stringify(c.Targets[1].SSH))
8586
require.Equal(t, []string{

build/utils.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,30 @@ func toBuildkitExtraHosts(ctx context.Context, inp []string, nodeDriver *driver.
7777
}
7878
// If the IP Address is a "host-gateway", replace this value with the
7979
// IP address provided by the worker's label.
80+
var ips []string
8081
if ip == mobyHostGatewayName {
8182
hgip, err := nodeDriver.HostGatewayIP(ctx)
8283
if err != nil {
8384
return "", errors.Wrap(err, "unable to derive the IP value for host-gateway")
8485
}
85-
ip = hgip.String()
86+
ips = append(ips, hgip.String())
8687
} else {
87-
// If the address is enclosed in square brackets, extract it (for IPv6, but
88-
// permit it for IPv4 as well; we don't know the address family here, but it's
89-
// unambiguous).
90-
if len(ip) > 2 && ip[0] == '[' && ip[len(ip)-1] == ']' {
91-
ip = ip[1 : len(ip)-1]
92-
}
93-
if net.ParseIP(ip) == nil {
94-
return "", errors.Errorf("invalid host %s", h)
88+
for _, v := range strings.Split(ip, ",") {
89+
// If the address is enclosed in square brackets, extract it
90+
// (for IPv6, but permit it for IPv4 as well; we don't know the
91+
// address family here, but it's unambiguous).
92+
if len(v) > 2 && v[0] == '[' && v[len(v)-1] == ']' {
93+
v = v[1 : len(v)-1]
94+
}
95+
if net.ParseIP(v) == nil {
96+
return "", errors.Errorf("invalid host %s", h)
97+
}
98+
ips = append(ips, v)
9599
}
96100
}
97-
hosts = append(hosts, host+"="+ip)
101+
for _, v := range ips {
102+
hosts = append(hosts, host+"="+v)
103+
}
98104
}
99105
return strings.Join(hosts, ","), nil
100106
}

build/utils_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ func TestToBuildkitExtraHosts(t *testing.T) {
7272
doc: "IPv6 localhost, non-canonical, eq sep",
7373
input: []string{`ipv6local=0:0:0:0:0:0:0:1`},
7474
},
75+
{
76+
doc: "Multi IPs",
77+
input: []string{`myhost=162.242.195.82,162.242.195.83`},
78+
expectedOut: `myhost=162.242.195.82,myhost=162.242.195.83`,
79+
},
7580
{
7681
doc: "IPv6 localhost, non-canonical, eq sep, brackets",
7782
input: []string{`ipv6local=[0:0:0:0:0:0:0:1]`},

tests/bake.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,11 +2167,14 @@ func testBakeExtraHosts(t *testing.T, sb integration.Sandbox) {
21672167
dockerfile := []byte(`
21682168
FROM busybox
21692169
RUN cat /etc/hosts | grep myhost | grep 1.2.3.4
2170+
RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.81
2171+
RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.82
21702172
`)
21712173
bakefile := []byte(`
21722174
target "default" {
21732175
extra-hosts = {
21742176
myhost = "1.2.3.4"
2177+
myhostmulti = "162.242.195.81,162.242.195.82"
21752178
}
21762179
}
21772180
`)

tests/build.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
7777
testBuildDefaultLoad,
7878
testBuildCall,
7979
testCheckCallOutput,
80+
testBuildExtraHosts,
8081
}
8182

8283
func testBuild(t *testing.T, sb integration.Sandbox) {
@@ -1322,6 +1323,24 @@ cOpy Dockerfile .
13221323
})
13231324
}
13241325

1326+
func testBuildExtraHosts(t *testing.T, sb integration.Sandbox) {
1327+
dockerfile := []byte(`
1328+
FROM busybox
1329+
RUN cat /etc/hosts | grep myhost | grep 1.2.3.4
1330+
RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.81
1331+
RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.82
1332+
`)
1333+
dir := tmpdir(t, fstest.CreateFile("Dockerfile", dockerfile, 0600))
1334+
cmd := buildxCmd(sb, withArgs("build",
1335+
"--add-host=myhost=1.2.3.4",
1336+
"--add-host=myhostmulti=162.242.195.81",
1337+
"--add-host=myhostmulti=162.242.195.82",
1338+
"--output=type=cacheonly", dir),
1339+
)
1340+
out, err := cmd.CombinedOutput()
1341+
require.NoError(t, err, string(out))
1342+
}
1343+
13251344
func createTestProject(t *testing.T) string {
13261345
dockerfile := []byte(`
13271346
FROM busybox:latest AS base

0 commit comments

Comments
 (0)