Skip to content

Commit 86cfe12

Browse files
committed
fix: respect HTTP proxy settings
This should correctly apply `HTTP_PROXY` settings when set via the build args. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent e2c007a commit 86cfe12

File tree

4 files changed

+90
-18
lines changed

4 files changed

+90
-18
lines changed

internal/pkg/convert/graph.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type GraphLLB struct {
2929

3030
baseImageProcessor llbProcessor
3131
cache map[*solver.PackageNode]llb.State
32+
33+
commonRunOptions []llb.RunOption
3234
}
3335

3436
type llbProcessor func(llb.State) llb.State
@@ -41,6 +43,10 @@ func NewGraphLLB(graph *solver.PackageGraph, options *environment.Options) *Grap
4143
cache: make(map[*solver.PackageNode]llb.State),
4244
}
4345

46+
if options.ProxyEnv != nil {
47+
result.commonRunOptions = append(result.commonRunOptions, llb.WithProxy(*options.ProxyEnv))
48+
}
49+
4450
result.buildBaseImages()
4551
result.buildChecksummer()
4652
result.buildLocalContext()
@@ -83,11 +89,15 @@ func (graph *GraphLLB) buildBaseImages() {
8389
constants.DefaultBaseImage,
8490
llb.WithCustomName(graph.Options.CommonPrefix+"base"),
8591
).Run(
86-
llb.Shlex("apk --no-cache --update add bash"),
87-
llb.WithCustomName(graph.Options.CommonPrefix+"base-apkinstall"),
92+
append(graph.commonRunOptions,
93+
llb.Shlex("apk --no-cache --update add bash"),
94+
llb.WithCustomName(graph.Options.CommonPrefix+"base-apkinstall"),
95+
)...,
8896
).Run(
89-
llb.Args([]string{"ln", "-svf", "/bin/bash", "/bin/sh"}),
90-
llb.WithCustomName(graph.Options.CommonPrefix+"base-symlink"),
97+
append(graph.commonRunOptions,
98+
llb.Args([]string{"ln", "-svf", "/bin/bash", "/bin/sh"}),
99+
llb.WithCustomName(graph.Options.CommonPrefix+"base-symlink"),
100+
)...,
91101
).Root())
92102

93103
graph.BaseImages[v1alpha2.Scratch] = graph.baseImageProcessor(llb.Scratch())
@@ -98,8 +108,10 @@ func (graph *GraphLLB) buildChecksummer() {
98108
constants.DefaultBaseImage,
99109
llb.WithCustomName(graph.Options.CommonPrefix+"cksum"),
100110
).Run(
101-
llb.Shlex("apk --no-cache --update add coreutils"),
102-
llb.WithCustomName(graph.Options.CommonPrefix+"cksum-apkinstall"),
111+
append(graph.commonRunOptions,
112+
llb.Shlex("apk --no-cache --update add coreutils"),
113+
llb.WithCustomName(graph.Options.CommonPrefix+"cksum-apkinstall"),
114+
)...,
103115
).Root()
104116
}
105117

internal/pkg/convert/node.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ func (node *NodeLLB) base() (llb.State, error) {
6464
func (node *NodeLLB) install(root llb.State) llb.State {
6565
if len(node.Pkg.Install) > 0 {
6666
root = root.Run(
67-
llb.Args(
68-
append([]string{"/sbin/apk", "add", "--no-cache"}, node.Pkg.Install...)),
69-
llb.WithCustomName(node.Prefix+"apk-install"),
67+
append(node.Graph.commonRunOptions,
68+
llb.Args(
69+
append([]string{"/sbin/apk", "add", "--no-cache"}, node.Pkg.Install...)),
70+
llb.WithCustomName(node.Prefix+"apk-install"),
71+
)...,
7072
).Root()
7173
}
7274

@@ -168,8 +170,10 @@ func (node *NodeLLB) stepDownload(root llb.State, step v1alpha2.Step) llb.State
168170
Mkdir("/empty", constants.DefaultDirMode),
169171
llb.WithCustomName(node.Prefix+"cksum-prepare"),
170172
).Run(
171-
llb.Shlex("sha512sum -c --strict /checksums"),
172-
llb.WithCustomName(node.Prefix+"cksum-verify"),
173+
append(node.Graph.commonRunOptions,
174+
llb.Shlex("sha512sum -c --strict /checksums"),
175+
llb.WithCustomName(node.Prefix+"cksum-verify"),
176+
)...,
173177
).Root()
174178

175179
root = root.File(
@@ -211,12 +215,14 @@ func (node *NodeLLB) stepScripts(root llb.State, i int, step v1alpha2.Step) llb.
211215
} {
212216
for _, instruction := range script.Instructions {
213217
root = root.Run(
214-
llb.Args([]string{
215-
node.Pkg.Shell.Get(),
216-
"-c",
217-
instruction.Script(),
218-
}),
219-
llb.WithCustomName(fmt.Sprintf("%s%s-%d", node.Prefix, script.Desc, i)),
218+
append(node.Graph.commonRunOptions,
219+
llb.Args([]string{
220+
node.Pkg.Shell.Get(),
221+
"-c",
222+
instruction.Script(),
223+
}),
224+
llb.WithCustomName(fmt.Sprintf("%s%s-%d", node.Prefix, script.Desc, i)),
225+
)...,
220226
).Root()
221227
}
222228
}

internal/pkg/environment/options.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
package environment
66

7-
import "github.com/talos-systems/bldr/internal/pkg/types"
7+
import (
8+
"github.com/moby/buildkit/client/llb"
9+
10+
"github.com/talos-systems/bldr/internal/pkg/types"
11+
)
812

913
// Options for bldr.
1014
type Options struct {
1115
BuildPlatform Platform
1216
TargetPlatform Platform
1317
Target string
1418
CommonPrefix string
19+
ProxyEnv *llb.ProxyEnv
1520
}
1621

1722
// GetVariables returns set of variables set for options.

internal/pkg/pkgfile/build.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const (
3030
keyTargetPlatform = "platform"
3131
keyMultiPlatform = "multi-platform"
3232

33+
buildArgPrefix = "build-arg:"
34+
3335
localNameDockerfile = "dockerfile"
3436
sharedKeyHint = constants.PkgYaml
3537
)
@@ -39,7 +41,9 @@ const (
3941
//nolint:gocyclo
4042
func Build(ctx context.Context, c client.Client, options *environment.Options) (*client.Result, error) {
4143
opts := c.BuildOpts().Opts
44+
4245
options.Target = opts[keyTarget]
46+
options.ProxyEnv = proxyEnvFromBuildArgs(filter(opts, buildArgPrefix))
4347

4448
platforms := []environment.Platform{options.TargetPlatform}
4549

@@ -213,3 +217,48 @@ func fetchPkgs(ctx context.Context, c client.Client) (client.Reference, error) {
213217

214218
return res.SingleRef()
215219
}
220+
221+
func proxyEnvFromBuildArgs(args map[string]string) *llb.ProxyEnv {
222+
pe := &llb.ProxyEnv{}
223+
isNil := true
224+
225+
for k, v := range args {
226+
if strings.EqualFold(k, "http_proxy") {
227+
pe.HTTPProxy = v
228+
isNil = false
229+
}
230+
231+
if strings.EqualFold(k, "https_proxy") {
232+
pe.HTTPSProxy = v
233+
isNil = false
234+
}
235+
236+
if strings.EqualFold(k, "ftp_proxy") {
237+
pe.FTPProxy = v
238+
isNil = false
239+
}
240+
241+
if strings.EqualFold(k, "no_proxy") {
242+
pe.NoProxy = v
243+
isNil = false
244+
}
245+
}
246+
247+
if isNil {
248+
return nil
249+
}
250+
251+
return pe
252+
}
253+
254+
func filter(opt map[string]string, key string) map[string]string {
255+
m := map[string]string{}
256+
257+
for k, v := range opt {
258+
if strings.HasPrefix(k, key) {
259+
m[strings.TrimPrefix(k, key)] = v
260+
}
261+
}
262+
263+
return m
264+
}

0 commit comments

Comments
 (0)