Skip to content

Commit 42edf1e

Browse files
committed
feat(staged-dockerfile): implement global/Run mounts
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
1 parent 2fdab3b commit 42edf1e

File tree

9 files changed

+236
-221
lines changed

9 files changed

+236
-221
lines changed

cmd/buildah-test/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ echo STOP
4747
defer os.RemoveAll("/tmp/build_stage.sh")
4848

4949
if err := b.RunCommand(ctx, "mycontainer", []string{"/.werf/build_stage.sh"}, buildah.RunCommandOpts{
50-
LegacyMounts: []specs.Mount{
50+
GlobalMounts: []*specs.Mount{
5151
{
5252
Type: "bind",
5353
Source: "/tmp/build_stage.sh",

pkg/build/image/build_context_archive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ func (a *BuildContextArchive) CalculateGlobsChecksum(ctx context.Context, globs
138138
}
139139
}
140140

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

146146
return pathsChecksum, nil
147147
}
148148

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

pkg/build/stage/instruction/run.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,23 @@ func (stg *Run) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai
6161
}
6262
}
6363

64-
// TODO(ilya-lesikov): should bind mount with context as src be counted as dependency?
64+
if stg.UsesBuildContext() {
65+
var paths []string
66+
for _, mnt := range mounts {
67+
if mnt.Type != instructions.MountTypeBind || mnt.Source == "" {
68+
continue
69+
}
70+
paths = append(paths, mnt.Source)
71+
}
72+
73+
if len(paths) > 0 {
74+
if srcChecksum, err := buildContextArchive.CalculatePathsChecksum(ctx, paths); err != nil {
75+
return "", fmt.Errorf("unable to calculate build context paths checksum: %w", err)
76+
} else {
77+
args = append(args, "SourcesChecksum", srcChecksum)
78+
}
79+
}
80+
}
6581

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

pkg/build/stage/instruction/stubs_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,18 @@ func (buildContext *BuildContextStub) CalculateGlobsChecksum(ctx context.Context
101101

102102
return util.Sha256Hash(args...), nil
103103
}
104+
105+
func (buildContext *BuildContextStub) CalculatePathsChecksum(ctx context.Context, paths []string) (string, error) {
106+
var args []string
107+
108+
for _, p := range paths {
109+
for _, f := range buildContext.Files {
110+
if f.Name == p {
111+
args = append(args, string(f.Data))
112+
break
113+
}
114+
}
115+
}
116+
117+
return util.Sha256Hash(args...), nil
118+
}

pkg/buildah/common.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ type RunCommandOpts struct {
7070
WorkingDir string
7171
User string
7272
Envs []string
73-
Mounts []instructions.Mount
74-
LegacyMounts []specs.Mount // TODO(ilya-lesikov): migrate to Mounts
73+
// Mounts as allowed to be passed from command line.
74+
GlobalMounts []*specs.Mount
75+
// 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).
76+
RunMounts []*instructions.Mount
7577
}
7678

7779
type RmiOpts struct {

0 commit comments

Comments
 (0)