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 <[email protected]>
  • Loading branch information
cpuguy83 committed Dec 18, 2024
1 parent 52f22e9 commit 22a7e3c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 67 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
16 changes: 9 additions & 7 deletions frontend/windows/handle_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
const (
defaultBaseImage = "mcr.microsoft.com/windows/nanoserver:1809"
windowsSystemDir = "/Windows/System32/"

argBasesPathKey = "DALEC_WINDOWSCROSS_BASES_PATH"
argBasesContextKey = "DALEC_WINDOWSCROSS_BASES_CONTEXT"
)

var (
Expand Down Expand Up @@ -58,13 +61,9 @@ func (ib *ImageBases) len() int {
}

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

bOpts := client.BuildOpts().Opts
p := bOpts[argBasesPathKey]
p := bOpts["build-arg:"+argBasesPathKey]
if p == "" {
return nil, nil
}
Expand All @@ -74,7 +73,7 @@ func getImageBases(ctx context.Context, client gwclient.Client, sOpt dalec.Sourc
Path: p,
}

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

Expand Down Expand Up @@ -210,7 +209,10 @@ func handleContainer(ctx context.Context, client gwclient.Client) (*gwclient.Res
}

rb, err := dcBuild(ctx, dc, func(ctx context.Context, platform *ocispecs.Platform, idx int) (ref gwclient.Reference, retCfg, retBaseCfg *dalec.DockerImageSpec, retErr error) {
spec, err := frontend.LoadSpec(ctx, dc, platform)
spec, err := frontend.LoadSpec(ctx, dc, platform, frontend.WithAllowArgs(
argBasesPathKey,
argBasesContextKey,
))
if err != nil {
return nil, nil, nil, err
}
Expand Down
Loading

0 comments on commit 22a7e3c

Please sign in to comment.