diff --git a/internal/controller/graph/dag.go b/internal/controller/graph/dag.go index e14391d4d55..f686ced7621 100644 --- a/internal/controller/graph/dag.go +++ b/internal/controller/graph/dag.go @@ -23,6 +23,8 @@ import ( "errors" "fmt" "sort" + + "golang.org/x/exp/maps" ) type DAG struct { @@ -260,10 +262,12 @@ func (d *DAG) Root() Vertex { func (d *DAG) Merge(subDag *DAG) { for v := range subDag.vertices { - if len(d.inAdj(v)) == 0 { - d.AddConnectRoot(v) + if len(subDag.inAdj(v)) == 0 { + d.Connect(d.Root(), v) } } + maps.Copy(d.vertices, subDag.vertices) + maps.Copy(d.edges, subDag.edges) } // String returns a string representation of the DAG in topology order diff --git a/internal/controller/graph/dag_test.go b/internal/controller/graph/dag_test.go index 1e019929fc1..ac782bcce3a 100644 --- a/internal/controller/graph/dag_test.go +++ b/internal/controller/graph/dag_test.go @@ -306,20 +306,37 @@ func TestEquals(t *testing.T) { func TestMerge(t *testing.T) { dag1 := NewDAG() dag2 := NewDAG() - v1, v2, v3 := 1, 2, 3 + v1, v2, v3, v4, v5, v6 := 1, 2, 3, 4, 5, 6 dag1.AddVertex(v1) dag1.AddVertex(v2) dag1.Connect(v1, v2) dag2.AddVertex(v2) dag2.AddVertex(v3) + dag2.AddVertex(v4) + dag2.AddVertex(v5) + dag2.AddVertex(v6) dag2.Connect(v2, v3) + dag2.Connect(v2, v4) + dag2.Connect(v3, v5) + dag2.Connect(v4, v5) + dag2.Connect(v6, v4) + dag2.Connect(v6, v5) dagExpected := NewDAG() dagExpected.AddVertex(v1) dagExpected.AddVertex(v2) dagExpected.AddVertex(v3) + dagExpected.AddVertex(v4) + dagExpected.AddVertex(v5) + dagExpected.AddVertex(v6) dagExpected.Connect(v1, v2) - dagExpected.Connect(v1, v3) + dagExpected.Connect(v1, v6) + dagExpected.Connect(v2, v3) + dagExpected.Connect(v2, v4) + dagExpected.Connect(v3, v5) + dagExpected.Connect(v4, v5) + dagExpected.Connect(v6, v4) + dagExpected.Connect(v6, v5) dag1.Merge(dag2) if !dag1.Equals(dagExpected, less) {