Skip to content

Commit

Permalink
fix: Linking policies to other objects
Browse files Browse the repository at this point in the history
Fixes an issue where it wasn't possible to create a link to a policy
from an object of a different type outside of linking targettables.
Currently creating an edge to a policy from an object would silently
fail since the policy is not in the topology at the time of creating the
links.

Signed-off-by: Michael Nairn <[email protected]>
  • Loading branch information
mikenairn committed Oct 20, 2024
1 parent db72801 commit 2dc665c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 1 addition & 2 deletions machinery/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func NewTopology(options ...TopologyOptionsFunc) (*Topology, error) {

addObjectsToGraph(graph, o.Objects)
addTargetablesToGraph(graph, targetables)
addPoliciesToGraph(graph, policies)

linkables := append(o.Objects, lo.Map(targetables, AsObject[Targetable])...)
linkables = append(linkables, lo.Map(policies, AsObject[Policy])...)
Expand All @@ -118,8 +119,6 @@ func NewTopology(options ...TopologyOptionsFunc) (*Topology, error) {
}
}

addPoliciesToGraph(graph, policies)

var err error
if !o.AllowLoops && !isDAG(graph) {
err = errors.New("loop detected in the graph, check linking functions")
Expand Down
5 changes: 4 additions & 1 deletion machinery/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ func TestTopologyAll(t *testing.T) {
objects := []*Info{
{Name: "info-1", Ref: "apple.example.test:apple-1"},
{Name: "info-2", Ref: "orange.example.test:my-namespace/orange-1"},
{Name: "info-3", Ref: "fruitpolicy.test:my-namespace/policy-1"},
}
apples := []*Apple{{Name: "apple-1"}}
oranges := []*Orange{
Expand Down Expand Up @@ -543,6 +544,7 @@ func TestTopologyAll(t *testing.T) {
LinkApplesToOranges(apples),
LinkInfoFrom("Apple", lo.Map(apples, AsObject[*Apple])),
LinkInfoFrom("Orange", lo.Map(oranges, AsObject[*Orange])),
LinkInfoFrom("Policy", lo.Map(policies, AsObject[Policy])),
),
)

Expand All @@ -553,13 +555,14 @@ func TestTopologyAll(t *testing.T) {
SaveToOutputDir(t, topology.ToDot(), "../tests/out", ".dot")

expectedLinks := map[string][]string{
"policy-1": {"apple-1"},
"policy-1": {"apple-1", "info-3"},
"policy-2": {"orange-1"},
"apple-1": {"orange-1", "orange-2", "info-1"},
"orange-1": {"info-2"},
"orange-2": {},
"info-1": {},
"info-2": {},
"info-3": {},
}

links := make(map[string][]string)
Expand Down

0 comments on commit 2dc665c

Please sign in to comment.