Skip to content

Commit

Permalink
feat(staged-dockerfile): implement global/Run mounts
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Nov 1, 2022
1 parent 2fdab3b commit 42edf1e
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 221 deletions.
2 changes: 1 addition & 1 deletion cmd/buildah-test/main.go
Expand Up @@ -47,7 +47,7 @@ echo STOP
defer os.RemoveAll("/tmp/build_stage.sh")

if err := b.RunCommand(ctx, "mycontainer", []string{"/.werf/build_stage.sh"}, buildah.RunCommandOpts{
LegacyMounts: []specs.Mount{
GlobalMounts: []*specs.Mount{
{
Type: "bind",
Source: "/tmp/build_stage.sh",
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/image/build_context_archive.go
Expand Up @@ -138,15 +138,15 @@ func (a *BuildContextArchive) CalculateGlobsChecksum(ctx context.Context, globs
}
}

pathsChecksum, err := a.calculatePathsChecksum(ctx, matches)
pathsChecksum, err := a.CalculatePathsChecksum(ctx, matches)
if err != nil {
return "", fmt.Errorf("unable to calculate build context paths checksum: %w", err)
}

return pathsChecksum, nil
}

func (a *BuildContextArchive) calculatePathsChecksum(ctx context.Context, paths []string) (string, error) {
func (a *BuildContextArchive) CalculatePathsChecksum(ctx context.Context, paths []string) (string, error) {
sort.Strings(paths)
paths = util.UniqStrings(paths)

Expand Down
18 changes: 17 additions & 1 deletion pkg/build/stage/instruction/run.go
Expand Up @@ -61,7 +61,23 @@ func (stg *Run) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai
}
}

// TODO(ilya-lesikov): should bind mount with context as src be counted as dependency?
if stg.UsesBuildContext() {
var paths []string
for _, mnt := range mounts {
if mnt.Type != instructions.MountTypeBind || mnt.Source == "" {
continue
}
paths = append(paths, mnt.Source)
}

if len(paths) > 0 {
if srcChecksum, err := buildContextArchive.CalculatePathsChecksum(ctx, paths); err != nil {
return "", fmt.Errorf("unable to calculate build context paths checksum: %w", err)
} else {
args = append(args, "SourcesChecksum", srcChecksum)
}
}
}

return util.Sha256Hash(args...), nil
}
15 changes: 15 additions & 0 deletions pkg/build/stage/instruction/stubs_test.go
Expand Up @@ -101,3 +101,18 @@ func (buildContext *BuildContextStub) CalculateGlobsChecksum(ctx context.Context

return util.Sha256Hash(args...), nil
}

func (buildContext *BuildContextStub) CalculatePathsChecksum(ctx context.Context, paths []string) (string, error) {
var args []string

for _, p := range paths {
for _, f := range buildContext.Files {
if f.Name == p {
args = append(args, string(f.Data))
break
}
}
}

return util.Sha256Hash(args...), nil
}
6 changes: 4 additions & 2 deletions pkg/buildah/common.go
Expand Up @@ -70,8 +70,10 @@ type RunCommandOpts struct {
WorkingDir string
User string
Envs []string
Mounts []instructions.Mount
LegacyMounts []specs.Mount // TODO(ilya-lesikov): migrate to Mounts
// Mounts as allowed to be passed from command line.
GlobalMounts []*specs.Mount
// Mounts as allowed in Dockerfile RUN --mount option. Have more restrictions than GlobalMounts (e.g. Source of bind-mount can't be outside of ContextDir or container root).
RunMounts []*instructions.Mount
}

type RmiOpts struct {
Expand Down

0 comments on commit 42edf1e

Please sign in to comment.