Skip to content

Commit

Permalink
update based on pr comments
Browse files Browse the repository at this point in the history
Signed-off-by: Talon Bowler <[email protected]>
  • Loading branch information
daghack committed Jul 10, 2024
1 parent f959e14 commit 8100733
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 57 deletions.
111 changes: 57 additions & 54 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,15 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
buildContext := &mutableOutput{}
ctxPaths := map[string]struct{}{}

dockerIgnorePatterns, err := opt.Client.DockerIgnorePatterns(ctx)
if err != nil {
return nil, err
}
dockerIgnoreMatcher, err := patternmatcher.New(dockerIgnorePatterns)
if err != nil {
return nil, err
}

for _, d := range allDispatchStates.states {
if !opt.AllStages {
if _, ok := allReachable[d]; !ok || d.noinit {
Expand Down Expand Up @@ -633,29 +642,25 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
}

d.state = d.state.Network(opt.NetworkMode)
dockerIgnorePatterns, err := opt.Client.DockerIgnorePatterns(ctx)
if err != nil {
return nil, err
}

opt := dispatchOpt{
allDispatchStates: allDispatchStates,
metaArgs: optMetaArgs,
buildArgValues: opt.BuildArgs,
shlex: shlex,
buildContext: llb.NewState(buildContext),
proxyEnv: proxyEnv,
cacheIDNamespace: opt.CacheIDNamespace,
buildPlatforms: platformOpt.buildPlatforms,
targetPlatform: platformOpt.targetPlatform,
extraHosts: opt.ExtraHosts,
shmSize: opt.ShmSize,
ulimit: opt.Ulimits,
cgroupParent: opt.CgroupParent,
llbCaps: opt.LLBCaps,
sourceMap: opt.SourceMap,
lint: lint,
dockerIgnorePatterns: dockerIgnorePatterns,
allDispatchStates: allDispatchStates,
metaArgs: optMetaArgs,
buildArgValues: opt.BuildArgs,
shlex: shlex,
buildContext: llb.NewState(buildContext),
proxyEnv: proxyEnv,
cacheIDNamespace: opt.CacheIDNamespace,
buildPlatforms: platformOpt.buildPlatforms,
targetPlatform: platformOpt.targetPlatform,
extraHosts: opt.ExtraHosts,
shmSize: opt.ShmSize,
ulimit: opt.Ulimits,
cgroupParent: opt.CgroupParent,
llbCaps: opt.LLBCaps,
sourceMap: opt.SourceMap,
lint: lint,
dockerIgnoreMatcher: dockerIgnoreMatcher,
}

if err = dispatchOnBuildTriggers(d, d.image.Config.OnBuild, opt); err != nil {
Expand Down Expand Up @@ -814,23 +819,23 @@ func toCommand(ic instructions.Command, allDispatchStates *dispatchStates) (comm
}

type dispatchOpt struct {
allDispatchStates *dispatchStates
metaArgs []instructions.KeyValuePairOptional
buildArgValues map[string]string
shlex *shell.Lex
buildContext llb.State
proxyEnv *llb.ProxyEnv
cacheIDNamespace string
targetPlatform ocispecs.Platform
buildPlatforms []ocispecs.Platform
extraHosts []llb.HostIP
shmSize int64
ulimit []pb.Ulimit
cgroupParent string
llbCaps *apicaps.CapSet
sourceMap *llb.SourceMap
lint *linter.Linter
dockerIgnorePatterns []string
allDispatchStates *dispatchStates
metaArgs []instructions.KeyValuePairOptional
buildArgValues map[string]string
shlex *shell.Lex
buildContext llb.State
proxyEnv *llb.ProxyEnv
cacheIDNamespace string
targetPlatform ocispecs.Platform
buildPlatforms []ocispecs.Platform
extraHosts []llb.HostIP
shmSize int64
ulimit []pb.Ulimit
cgroupParent string
llbCaps *apicaps.CapSet
sourceMap *llb.SourceMap
lint *linter.Linter
dockerIgnoreMatcher *patternmatcher.PatternMatcher
}

func getEnv(state llb.State) shell.EnvGetter {
Expand Down Expand Up @@ -917,8 +922,9 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
keepGitDir: c.KeepGitDir,
checksum: checksum,
location: c.Location(),
ignoreMatcher: opt.dockerIgnoreMatcher,
opt: opt,
}, opt.lint)
})
}
if err == nil {
for _, src := range c.SourcePaths {
Expand Down Expand Up @@ -951,27 +957,30 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
err = dispatchArg(d, c, &opt)
case *instructions.CopyCommand:
l := opt.buildContext
var ignoreMatcher *patternmatcher.PatternMatcher
if len(cmd.sources) != 0 {
src := cmd.sources[0]
if !src.noinit {
return errors.Errorf("cannot copy from stage %q, it needs to be defined before current stage %q", c.From, d.stageName)
}
l = src.state
} else {
ignoreMatcher = opt.dockerIgnoreMatcher
}
err = dispatchCopy(d, copyConfig{
params: c.SourcesAndDest,
excludePatterns: c.ExcludePatterns,
source: l,
fromStage: c.From,
isAddCommand: false,
cmdToPrint: c,
chown: c.Chown,
chmod: c.Chmod,
link: c.Link,
parents: c.Parents,
location: c.Location(),
ignoreMatcher: ignoreMatcher,
opt: opt,
}, opt.lint)
})
if err == nil {
if len(cmd.sources) == 0 {
for _, src := range c.SourcePaths {
Expand Down Expand Up @@ -1323,7 +1332,7 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo
return nil
}

func dispatchCopy(d *dispatchState, cfg copyConfig, lint *linter.Linter) error {
func dispatchCopy(d *dispatchState, cfg copyConfig) error {
dest, err := pathRelativeToWorkingDir(d.state, cfg.params.DestPath, *d.platform)
if err != nil {
return err
Expand Down Expand Up @@ -1391,11 +1400,6 @@ func dispatchCopy(d *dispatchState, cfg copyConfig, lint *linter.Linter) error {

var a *llb.FileAction

dockerIgnoreMatcher, err := patternmatcher.New(cfg.opt.dockerIgnorePatterns)
if err != nil {
return errors.Wrap(err, "creating dockerignore pattern matcher")
}

for _, src := range cfg.params.SourcePaths {
commitMessage.WriteString(" " + src)
gitRef, gitRefErr := gitutil.ParseGitRef(src)
Expand Down Expand Up @@ -1453,7 +1457,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig, lint *linter.Linter) error {
a = a.Copy(st, f, dest, opts...)
}
} else {
validateCopySourcePath(dockerIgnoreMatcher, src, &cfg, lint)
validateCopySourcePath(src, &cfg)
var patterns []string
if cfg.parents {
// detect optional pivot point
Expand Down Expand Up @@ -1565,12 +1569,12 @@ type copyConfig struct {
cmdToPrint fmt.Stringer
chown string
chmod string
fromStage string
link bool
keepGitDir bool
checksum digest.Digest
parents bool
location []parser.Range
ignoreMatcher *patternmatcher.PatternMatcher
opt dispatchOpt
}

Expand Down Expand Up @@ -1884,23 +1888,22 @@ func addReachableStages(s *dispatchState, stages map[*dispatchState]struct{}) {
}
}

func validateCopySourcePath(matcher *patternmatcher.PatternMatcher, src string, cfg *copyConfig, lint *linter.Linter) error {
if cfg.fromStage != "" {
func validateCopySourcePath(src string, cfg *copyConfig) error {
if cfg.ignoreMatcher == nil {
return nil
}

cmd := "Copy"
if cfg.isAddCommand {
cmd = "Add"
}

ok, err := matcher.MatchesOrParentMatches(src)
ok, err := cfg.ignoreMatcher.MatchesOrParentMatches(src)
if err != nil {
return err
}
if ok {
msg := linter.RuleCopyIgnoredFile.Format(cmd, src)
lint.Run(&linter.RuleCopyIgnoredFile, cfg.location, msg)
cfg.opt.lint.Run(&linter.RuleCopyIgnoredFile, cfg.location, msg)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (bc *Client) DockerIgnorePatterns(ctx context.Context) ([]string, error) {
if err != nil {
return nil, err
}
if !bctx.localBuild {
if bctx.context == nil {
return nil, nil
}

Expand Down
2 changes: 0 additions & 2 deletions frontend/dockerui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type buildContext struct {
dockerfileLocalName string
filename string
forceLocalDockerfile bool
localBuild bool
}

func (bc *Client) marshalOpts() []llb.ConstraintsOpt {
Expand Down Expand Up @@ -114,7 +113,6 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
}
bctx.dockerfile = bctx.context
} else if (&gwcaps).Supports(gwpb.CapFrontendInputs) == nil {
bctx.localBuild = true
inputs, err := bc.client.Inputs(ctx)
if err != nil {
return nil, errors.Wrapf(err, "failed to get frontend inputs")
Expand Down

0 comments on commit 8100733

Please sign in to comment.