Skip to content

Commit 7187467

Browse files
committed
Allow spec loaders to inject allowed args
This makes it so frontends can have their own args that the dalec core does not need to know about. This also moves the custom args for windowscross local to that implementation. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 17ab9e3 commit 7187467

5 files changed

Lines changed: 130 additions & 67 deletions

File tree

frontend/build.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,39 @@ import (
1515
"github.com/pkg/errors"
1616
)
1717

18-
func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform) (*dalec.Spec, error) {
18+
type LoadConfig struct {
19+
SubstituteOpts []dalec.SubstituteOpt
20+
}
21+
22+
type LoadOpt func(*LoadConfig)
23+
24+
func WithAllowArgs(args ...string) LoadOpt {
25+
return func(cfg *LoadConfig) {
26+
set := make(map[string]struct{}, len(args))
27+
for _, arg := range args {
28+
set[arg] = struct{}{}
29+
}
30+
cfg.SubstituteOpts = append(cfg.SubstituteOpts, func(cfg *dalec.SubstituteConfig) {
31+
orig := cfg.AllowArg
32+
33+
cfg.AllowArg = func(key string) bool {
34+
if orig != nil && orig(key) {
35+
return true
36+
}
37+
_, ok := set[key]
38+
return ok
39+
}
40+
})
41+
}
42+
}
43+
44+
func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform, opts ...LoadOpt) (*dalec.Spec, error) {
45+
cfg := LoadConfig{}
46+
47+
for _, o := range opts {
48+
o(&cfg)
49+
}
50+
1951
src, err := client.ReadEntrypoint(ctx, "Dockerfile")
2052
if err != nil {
2153
return nil, fmt.Errorf("could not read spec file: %w", err)
@@ -35,7 +67,7 @@ func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.P
3567
fillPlatformArgs("TARGET", args, *platform)
3668
fillPlatformArgs("BUILD", args, client.BuildPlatforms[0])
3769

38-
if err := spec.SubstituteArgs(args); err != nil {
70+
if err := spec.SubstituteArgs(args, cfg.SubstituteOpts...); err != nil {
3971
return nil, errors.Wrap(err, "error resolving build args")
4072
}
4173
return spec, nil

frontend/mux.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,14 @@ func (m *BuildMux) loadSpec(ctx context.Context, client gwclient.Client) (*dalec
204204
}
205205

206206
// Note: this is not suitable for passing to builds since it does not have platform information
207-
spec, err := LoadSpec(ctx, dc, nil)
207+
spec, err := LoadSpec(ctx, dc, nil, func(cfg *LoadConfig) {
208+
cfg.SubstituteOpts = append(cfg.SubstituteOpts, func(cfg *dalec.SubstituteConfig) {
209+
// Allow any args here since we aren't trying to validate the spec at this point.
210+
cfg.AllowArg = func(string) bool {
211+
return true
212+
}
213+
})
214+
})
208215
if err != nil {
209216
return nil, err
210217
}

frontend/windows/handle_container.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
const (
2626
defaultBaseImage = "mcr.microsoft.com/windows/nanoserver:1809"
2727
windowsSystemDir = "/Windows/System32/"
28+
29+
argBasesPathKey = "DALEC_WINDOWSCROSS_BASES_PATH"
30+
argBasesContextKey = "DALEC_WINDOWSCROSS_BASES_CONTEXT"
2831
)
2932

3033
var (
@@ -64,13 +67,9 @@ var defaultImageBases = &ImageBases{
6467
}
6568

6669
func getImageBases(ctx context.Context, client gwclient.Client, sOpt dalec.SourceOpts) (*ImageBases, error) {
67-
const (
68-
argBasesPathKey = "build-arg:DALEC_WINDOWSCROSS_BASES_PATH"
69-
argBasesContextKey = "build-arg:DALEC_WINDOWSCROSS_BASES_CONTEXT"
70-
)
7170

7271
bOpts := client.BuildOpts().Opts
73-
p := bOpts[argBasesPathKey]
72+
p := bOpts["build-arg:"+argBasesPathKey]
7473
if p == "" {
7574
return nil, nil
7675
}
@@ -80,7 +79,7 @@ func getImageBases(ctx context.Context, client gwclient.Client, sOpt dalec.Sourc
8079
Path: p,
8180
}
8281

83-
if name := bOpts[argBasesContextKey]; name != "" {
82+
if name := bOpts["build-arg:"+argBasesContextKey]; name != "" {
8483
src.Context.Name = name
8584
}
8685

@@ -206,7 +205,10 @@ func handleContainer(ctx context.Context, client gwclient.Client) (*gwclient.Res
206205
}
207206

208207
rb, err := dcBuild(ctx, dc, func(ctx context.Context, platform *ocispecs.Platform, idx int) (ref gwclient.Reference, retCfg, retBaseCfg *dalec.DockerImageSpec, retErr error) {
209-
spec, err := frontend.LoadSpec(ctx, dc, platform)
208+
spec, err := frontend.LoadSpec(ctx, dc, platform, frontend.WithAllowArgs(
209+
argBasesPathKey,
210+
argBasesContextKey,
211+
))
210212
if err != nil {
211213
return nil, nil, nil, err
212214
}

0 commit comments

Comments
 (0)