From e98a476dc8185bfbac8a74a05ee3db0406724be1 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 17 Feb 2022 22:21:08 -0800 Subject: [PATCH] set build-args from docker proxy configuration For backward compatibility with docker build. Signed-off-by: Tonis Tiigi --- build/build.go | 26 +++++++++++++++++--------- commands/util.go | 12 +++++++----- store/storeutil/storeutil.go | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/build/build.go b/build/build.go index 9dcecb58..e14a4e8f 100644 --- a/build/build.go +++ b/build/build.go @@ -87,11 +87,12 @@ type Inputs struct { } type DriverInfo struct { - Driver driver.Driver - Name string - Platform []specs.Platform - Err error - ImageOpt imagetools.Opt + Driver driver.Driver + Name string + Platform []specs.Platform + Err error + ImageOpt imagetools.Opt + ProxyConfig map[string]string } type DockerAPI interface { @@ -338,7 +339,8 @@ func toRepoOnly(in string) (string, error) { return strings.Join(out, ","), nil } -func toSolveOpt(ctx context.Context, d driver.Driver, multiDriver bool, opt Options, bopts gateway.BuildOpts, configDir string, pw progress.Writer, dl dockerLoadCallback) (solveOpt *client.SolveOpt, release func(), err error) { +func toSolveOpt(ctx context.Context, di DriverInfo, multiDriver bool, opt Options, bopts gateway.BuildOpts, configDir string, pw progress.Writer, dl dockerLoadCallback) (solveOpt *client.SolveOpt, release func(), err error) { + d := di.Driver defers := make([]func(), 0, 2) releaseF := func() { for _, f := range defers { @@ -541,6 +543,12 @@ func toSolveOpt(ctx context.Context, d driver.Driver, multiDriver bool, opt Opti so.FrontendAttrs["label:"+k] = v } + for k, v := range di.ProxyConfig { + if _, ok := opt.BuildArgs[k]; !ok { + so.FrontendAttrs["build-arg:"+k] = v + } + } + // set platforms if len(opt.Platforms) != 0 { pp := make([]string, len(opt.Platforms)) @@ -635,12 +643,12 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do multiDriver := len(m[k]) > 1 hasMobyDriver := false for i, dp := range m[k] { - d := drivers[dp.driverIndex].Driver - if d.IsMobyDriver() { + di := drivers[dp.driverIndex] + if di.Driver.IsMobyDriver() { hasMobyDriver = true } opt.Platforms = dp.platforms - so, release, err := toSolveOpt(ctx, d, multiDriver, opt, dp.bopts, configDir, w, func(name string) (io.WriteCloser, func(), error) { + so, release, err := toSolveOpt(ctx, di, multiDriver, opt, dp.bopts, configDir, w, func(name string) (io.WriteCloser, func(), error) { return newDockerLoader(ctx, docker, name, w) }) if err != nil { diff --git a/commands/util.go b/commands/util.go index 03a6e416..e9d58bb2 100644 --- a/commands/util.go +++ b/commands/util.go @@ -73,8 +73,9 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N func(i int, n store.Node) { eg.Go(func() error { di := build.DriverInfo{ - Name: n.Name, - Platform: n.Platforms, + Name: n.Name, + Platform: n.Platforms, + ProxyConfig: storeutil.GetProxyConfig(dockerCli), } defer func() { dis[i] = di @@ -264,9 +265,10 @@ func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, defaultOnly b } return []build.DriverInfo{ { - Name: "default", - Driver: d, - ImageOpt: imageopt, + Name: "default", + Driver: d, + ImageOpt: imageopt, + ProxyConfig: storeutil.GetProxyConfig(dockerCli), }, }, nil } diff --git a/store/storeutil/storeutil.go b/store/storeutil/storeutil.go index b87e3f48..494a878f 100644 --- a/store/storeutil/storeutil.go +++ b/store/storeutil/storeutil.go @@ -37,6 +37,32 @@ func GetCurrentEndpoint(dockerCli command.Cli) (string, error) { return de, nil } +func GetProxyConfig(dockerCli command.Cli) map[string]string { + cfg := dockerCli.ConfigFile() + host := dockerCli.Client().DaemonHost() + + proxy, ok := cfg.Proxies[host] + if !ok { + proxy = cfg.Proxies["default"] + } + + m := map[string]string{} + + if v := proxy.HTTPProxy; v != "" { + m["HTTP_PROXY"] = v + } + if v := proxy.HTTPSProxy; v != "" { + m["HTTPS_PROXY"] = v + } + if v := proxy.NoProxy; v != "" { + m["NO_PROXY"] = v + } + if v := proxy.FTPProxy; v != "" { + m["FTP_PROXY"] = v + } + return m +} + // GetDockerEndpoint returns docker endpoint string for given context func GetDockerEndpoint(dockerCli command.Cli, name string) (string, error) { list, err := dockerCli.ContextStore().List()