Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(GenerateDAG): split the algorithm in two parts #599

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions pkg/dib/generate_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
// GenerateDAG discovers and parses all Dockerfiles at a given path,
// and generates the DAG representing the relationships between images.
func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs map[string]string) (*dag.DAG, error) {
graph, err := buildGraph(buildPath, registryPrefix)
if err != nil {
return nil, err
}

return computeHashes(graph, customHashListPath, buildArgs)
}

func buildGraph(buildPath, registryPrefix string) (*dag.DAG, error) {
var allFiles []string
cache := make(map[string]*dag.Node)
allParents := make(map[string][]dockerfile.ImageRef)
Expand Down Expand Up @@ -121,19 +130,6 @@
}
}

if err := generateHashes(graph, allFiles, customHashListPath, buildArgs); err != nil {
return nil, err
}

return graph, nil
}

func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string, buildArgs map[string]string) error {
customHumanizedHashList, err := LoadCustomHashList(customHashListPath)
if err != nil {
return fmt.Errorf("could not load custom humanized hash list: %w", err)
}

fileBelongsTo := map[string]*dag.Node{}
for _, file := range allFiles {
fileBelongsTo[file] = nil
Expand Down Expand Up @@ -173,6 +169,15 @@
}
})

return graph, nil
}

func computeHashes(graph *dag.DAG, customHashListPath string, buildArgs map[string]string) (*dag.DAG, error) {
customHumanizedHashList, err := LoadCustomHashList(customHashListPath)
if err != nil {
return nil, fmt.Errorf("could not load custom humanized hash list: %w", err)

Check warning on line 178 in pkg/dib/generate_dag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dib/generate_dag.go#L178

Added line #L178 was not covered by tests
}

for {
needRepass := false
err := graph.WalkErr(func(node *dag.Node) error {
Expand Down Expand Up @@ -221,10 +226,10 @@
return nil
})
if err != nil {
return err
return nil, err

Check warning on line 229 in pkg/dib/generate_dag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dib/generate_dag.go#L229

Added line #L229 was not covered by tests
}
if !needRepass {
return nil
return graph, nil
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/dib/generate_dag_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dib

import "testing"

func Test_buildGraph(t *testing.T) {
t.Parallel()

// Implement me.
}