Implement parsing for `RUN --mount=type=bind,from=...`

This commit is contained in:
Tianon Gravi 2025-01-09 11:23:20 -08:00
parent 0c6df94b46
commit 0e00438cf2
2 changed files with 45 additions and 2 deletions

View File

@ -106,6 +106,49 @@ func ParseReader(dockerfile io.Reader) (*Metadata, error) {
// make sure to add ":latest" if it's implied
from = latestizeRepoTag(from)
meta.Froms = append(meta.Froms, from)
}
case "RUN": // TODO combine this and the above COPY-parsing code somehow sanely
for _, arg := range fields[1:] {
if !strings.HasPrefix(arg, "--") {
// doesn't appear to be a "flag"; time to bail!
break
}
if !strings.HasPrefix(arg, "--mount=") {
// ignore any flags we're not interested in
continue
}
csv := arg[len("--mount="):]
// TODO more correct CSV parsing
fields := strings.Split(csv, ",")
var mountType, from string
for _, field := range fields {
if strings.HasPrefix(field, "type=") {
mountType = field[len("type="):]
continue
}
if strings.HasPrefix(field, "from=") {
from = field[len("from="):]
continue
}
}
if mountType != "bind" || from == "" {
// this is probably something we should be worried about, but not something we're interested in parsing
continue
}
if stageFrom, ok := meta.StageNameFroms[from]; ok {
// see note above regarding stage names in FROM
from = stageFrom
} else if stageNumber, err := strconv.Atoi(from); err == nil && stageNumber < len(meta.StageFroms) {
// must be a stage number, we should resolve it too
from = meta.StageFroms[stageNumber]
}
// make sure to add ":latest" if it's implied
from = latestizeRepoTag(from)
meta.Froms = append(meta.Froms, from)
}
}

View File

@ -101,7 +101,7 @@ func TestParse(t *testing.T) {
`,
metadata: dockerfile.Metadata{
StageFroms: []string{"scratch"},
Froms: []string{"scratch"}, // TODO this should include "busybox:uclibc"
Froms: []string{"scratch", "busybox:uclibc"},
},
},
{
@ -117,7 +117,7 @@ func TestParse(t *testing.T) {
StageFroms: []string{"busybox:uclibc", "scratch"},
StageNames: []string{"bb"},
StageNameFroms: map[string]string{"bb": "busybox:uclibc"},
Froms: []string{"busybox:uclibc", "scratch"}, // TODO this should end with "busybox:uclibc"
Froms: []string{"busybox:uclibc", "scratch", "busybox:uclibc"},
},
},
} {