Skip to content

Commit

Permalink
Allow spec loaders to inject allowed args
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cpuguy83 committed Dec 18, 2024
1 parent c404433 commit 8cc2df0
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 56 deletions.
36 changes: 34 additions & 2 deletions frontend/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,39 @@ import (
"github.com/pkg/errors"
)

func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform) (*dalec.Spec, error) {
type LoadConfig struct {
SubstituteOpts []dalec.SubstituteOpt
}

type LoadOpt func(*LoadConfig)

func WithAllowArgs(args ...string) LoadOpt {
return func(cfg *LoadConfig) {
set := make(map[string]struct{}, len(args))
for _, arg := range args {
set[arg] = struct{}{}
}
cfg.SubstituteOpts = append(cfg.SubstituteOpts, func(cfg *dalec.SubstituteConfig) {
orig := cfg.AllowArg

cfg.AllowArg = func(key string) bool {
if orig != nil && orig(key) {
return true
}
_, ok := set[key]
return ok
}
})
}
}

func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.Platform, opts ...LoadOpt) (*dalec.Spec, error) {
cfg := LoadConfig{}

for _, o := range opts {
o(&cfg)
}

src, err := client.ReadEntrypoint(ctx, "Dockerfile")
if err != nil {
return nil, fmt.Errorf("could not read spec file: %w", err)
Expand All @@ -35,7 +67,7 @@ func LoadSpec(ctx context.Context, client *dockerui.Client, platform *ocispecs.P
fillPlatformArgs("TARGET", args, *platform)
fillPlatformArgs("BUILD", args, client.BuildPlatforms[0])

if err := spec.SubstituteArgs(args); err != nil {
if err := spec.SubstituteArgs(args, cfg.SubstituteOpts...); err != nil {
return nil, errors.Wrap(err, "error resolving build args")
}
return spec, nil
Expand Down
9 changes: 8 additions & 1 deletion frontend/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,14 @@ func (m *BuildMux) loadSpec(ctx context.Context, client gwclient.Client) (*dalec
}

// Note: this is not suitable for passing to builds since it does not have platform information
spec, err := LoadSpec(ctx, dc, nil)
spec, err := LoadSpec(ctx, dc, nil, func(cfg *LoadConfig) {
cfg.SubstituteOpts = append(cfg.SubstituteOpts, func(cfg *dalec.SubstituteConfig) {
// Allow any args here since we aren't trying to validate the spec at this point.
cfg.AllowArg = func(string) bool {
return true
}
})
})
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8cc2df0

Please sign in to comment.