Skip to content

Commit

Permalink
Merge pull request #599 from radiofrance/refact/GenerateDAG-split
Browse files Browse the repository at this point in the history
refact(GenerateDAG): split the algorithm in two parts
  • Loading branch information
antoinegelloz authored Oct 9, 2024
2 parents 22fa93f + 0e6bb2b commit 618b899
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
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 @@ const (
// 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 @@ func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs
}
}

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 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string
}
})

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)
}

for {
needRepass := false
err := graph.WalkErr(func(node *dag.Node) error {
Expand Down Expand Up @@ -221,10 +226,10 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string
return nil
})
if err != nil {
return err
return nil, err
}
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.
}

0 comments on commit 618b899

Please sign in to comment.