Skip to content

Commit

Permalink
Support multiple mounts on RUN layers (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
andremarianiello authored Nov 14, 2023
1 parent 1c7aef9 commit 2afb05b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 101 deletions.
48 changes: 23 additions & 25 deletions internal/dockerfile2dot/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,36 +154,34 @@ func addEdgesForStage(
simplifiedDockerfile SimplifiedDockerfile, layers bool, edgestyle string,
) {
for layerIndex, layer := range stage.Layers {
if layer.WaitFor.Name == "" {
continue
}

edgeAttrs := map[string]string{}
if layer.WaitFor.Type == waitForType(waitForCopy) {
edgeAttrs["arrowhead"] = "empty"
if edgestyle == "default" {
edgeAttrs["style"] = "dashed"
for _, waitFor := range layer.WaitFors {
edgeAttrs := map[string]string{}
if waitFor.Type == waitForType(waitForCopy) {
edgeAttrs["arrowhead"] = "empty"
if edgestyle == "default" {
edgeAttrs["style"] = "dashed"
}
} else if waitFor.Type == waitForType(waitForMount) {
edgeAttrs["arrowhead"] = "ediamond"
if edgestyle == "default" {
edgeAttrs["style"] = "dotted"
}
}
} else if layer.WaitFor.Type == waitForType(waitForMount) {
edgeAttrs["arrowhead"] = "ediamond"
if edgestyle == "default" {
edgeAttrs["style"] = "dotted"

sourceNodeID, additionalEdgeAttrs := getWaitForNodeID(
simplifiedDockerfile, waitFor.Name, layers,
)
for k, v := range additionalEdgeAttrs {
edgeAttrs[k] = v
}
}

sourceNodeID, additionalEdgeAttrs := getWaitForNodeID(
simplifiedDockerfile, layer.WaitFor.Name, layers,
)
for k, v := range additionalEdgeAttrs {
edgeAttrs[k] = v
}
targetNodeID := fmt.Sprintf("stage_%d", stageIndex)
if layers {
targetNodeID = targetNodeID + fmt.Sprintf("_layer_%d", layerIndex)
}

targetNodeID := fmt.Sprintf("stage_%d", stageIndex)
if layers {
targetNodeID = targetNodeID + fmt.Sprintf("_layer_%d", layerIndex)
_ = graph.AddEdge(sourceNodeID, targetNodeID, true, edgeAttrs)
}

_ = graph.AddEdge(sourceNodeID, targetNodeID, true, edgeAttrs)
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/dockerfile2dot/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func TestBuildDotFile(t *testing.T) {
Layers: []Layer{
{
Label: "FROM...",
WaitFor: WaitFor{
WaitFors: []WaitFor{{
Name: "build",
Type: waitForType(waitForFrom),
},
}},
},
},
},
Expand Down Expand Up @@ -74,10 +74,10 @@ func TestBuildDotFile(t *testing.T) {
Layers: []Layer{
{
Label: "FROM...",
WaitFor: WaitFor{
WaitFors: []WaitFor{{
Name: "build",
Type: waitForType(waitForFrom),
},
}},
},
},
},
Expand Down
64 changes: 32 additions & 32 deletions internal/dockerfile2dot/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func dockerfileToSimplifiedDockerfile(
layer := newLayer(child, argReplacements, maxLabelLength)

// Set the waitFor ID
layer.WaitFor = WaitFor{
layer.WaitFors = []WaitFor{{
Name: replaceArgVars(child.Next.Value, argReplacements),
Type: waitForType(waitForFrom),
}
}}

simplifiedDockerfile.Stages[stageIndex].Layers = append(
simplifiedDockerfile.Stages[stageIndex].Layers,
Expand All @@ -83,10 +83,10 @@ func dockerfileToSimplifiedDockerfile(
regex := regexp.MustCompile("--from=(.+)")
result := regex.FindSubmatch([]byte(flag))
if len(result) > 1 {
layer.WaitFor = WaitFor{
layer.WaitFors = []WaitFor{{
Name: string(result[1]),
Type: waitForType(waitForCopy),
}
}}
}
}

Expand All @@ -103,11 +103,13 @@ func dockerfileToSimplifiedDockerfile(
// If there is a "--mount=(.*)from=..." option, set the waitFor ID
for _, flag := range child.Flags {
regex := regexp.MustCompile("--mount=.*from=(.+?)(?:,| |$)")
result := regex.FindSubmatch([]byte(flag))
if len(result) > 1 {
layer.WaitFor = WaitFor{
Name: string(result[1]),
Type: waitForType(waitForMount),
matches := regex.FindAllSubmatch([]byte(flag), -1)
for _, match := range matches {
if len(match) > 1 {
layer.WaitFors = append(layer.WaitFors, WaitFor{
Name: string(match[1]),
Type: waitForType(waitForMount),
})
}
}
}
Expand Down Expand Up @@ -155,33 +157,31 @@ func addExternalImages(
) {
for _, stage := range simplifiedDockerfile.Stages {
for _, layer := range stage.Layers {
// Check if the layer waits for anything
if layer.WaitFor.Name == "" {
continue
}
for _, waitFor := range layer.WaitFors {

// Check if the layer waits for a stage
if _, ok := stages[layer.WaitFor.Name]; ok {
continue
}
// Check if the layer waits for a stage
if _, ok := stages[waitFor.Name]; ok {
continue
}

// Check if we already added the external image
externalImageAlreadyAdded := false
for _, externalImage := range simplifiedDockerfile.ExternalImages {
if externalImage.Name == layer.WaitFor.Name {
externalImageAlreadyAdded = true
break
// Check if we already added the external image
externalImageAlreadyAdded := false
for _, externalImage := range simplifiedDockerfile.ExternalImages {
if externalImage.Name == waitFor.Name {
externalImageAlreadyAdded = true
break
}
}
if externalImageAlreadyAdded {
continue
}
}
if externalImageAlreadyAdded {
continue
}

// Add the external image
simplifiedDockerfile.ExternalImages = append(
simplifiedDockerfile.ExternalImages,
ExternalImage{Name: layer.WaitFor.Name},
)
// Add the external image
simplifiedDockerfile.ExternalImages = append(
simplifiedDockerfile.ExternalImages,
ExternalImage{Name: waitFor.Name},
)
}
}
}
}
Expand Down
Loading

0 comments on commit 2afb05b

Please sign in to comment.