From 77ed999572135ab50b17b4d534fd417da00ebfb4 Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Wed, 17 Apr 2019 03:10:34 +0000 Subject: [PATCH] build: handle --add-host Signed-off-by: Tibor Vass --- build/build.go | 7 +++++++ build/utils.go | 21 +++++++++++++++++++++ commands/build.go | 7 ++----- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/build/build.go b/build/build.go index 76a916d7..3c5f3ad9 100644 --- a/build/build.go +++ b/build/build.go @@ -36,6 +36,7 @@ type Options struct { BuildArgs map[string]string Pull bool ImageIDFile string + ExtraHosts []string NoCache bool Target string @@ -227,6 +228,12 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, pw so.FrontendAttrs["platform"] = strings.Join(pp, ",") } + extraHosts, err := toBuildkitExtraHosts(opt.ExtraHosts) + if err != nil { + return nil, err + } + so.FrontendAttrs["add-hosts"] = extraHosts + var statusCh chan *client.SolveStatus if pw != nil { statusCh = pw.Status() diff --git a/build/utils.go b/build/utils.go index f1bf28c8..72376a07 100644 --- a/build/utils.go +++ b/build/utils.go @@ -3,7 +3,11 @@ package build import ( "archive/tar" "bytes" + "net" "os" + "strings" + + "github.com/pkg/errors" ) // archiveHeaderSize is the number of bytes in an archive header @@ -32,3 +36,20 @@ func isArchive(header []byte) bool { _, err := r.Next() return err == nil } + +// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format +func toBuildkitExtraHosts(inp []string) (string, error) { + if len(inp) == 0 { + return "", nil + } + hosts := make([]string, 0, len(inp)) + for _, h := range inp { + parts := strings.Split(h, ":") + + if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil { + return "", errors.Errorf("invalid host %s", h) + } + hosts = append(hosts, parts[0]+"="+parts[1]) + } + return strings.Join(hosts, ","), nil +} diff --git a/commands/build.go b/commands/build.go index 149f03ed..8af1e0d7 100644 --- a/commands/build.go +++ b/commands/build.go @@ -31,9 +31,9 @@ type buildOptions struct { ssh []string outputs []string imageIDFile string + extraHosts []string // unimplemented - extraHosts []string squash bool quiet bool networkMode string @@ -62,9 +62,6 @@ type commonOptions struct { } func runBuild(dockerCli command.Cli, in buildOptions) error { - if len(in.extraHosts) > 0 { - return errors.Errorf("extra hosts currently not implemented") - } if in.squash { return errors.Errorf("squash currently not implemented") } @@ -90,6 +87,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) error { NoCache: in.noCache, Target: in.target, ImageIDFile: in.imageIDFile, + ExtraHosts: in.extraHosts, } platforms, err := build.ParsePlatformSpecs(in.platforms) @@ -169,7 +167,6 @@ func buildCmd(dockerCli command.Cli) *cobra.Command { flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer") flags.MarkHidden("quiet") flags.MarkHidden("network") - flags.MarkHidden("add-host") flags.MarkHidden("squash") // hidden flags