Skip to content

Commit

Permalink
refactor of the topology functions so the graph can be searched and n…
Browse files Browse the repository at this point in the history
…avigated for generic objects

The listing functions Targetables(filters...) and Policies(filters...) have been modified to now return pointers to collections of that type, that can be navigated through the graph methods Roots(), Parents(), Children(), and Paths().

+ makes policies linkable.
  • Loading branch information
guicassolato committed Jul 15, 2024
1 parent c43b635 commit b72f969
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 148 deletions.
8 changes: 5 additions & 3 deletions examples/color_policy/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,21 @@ func TestKuadrantMergeBasedOnTopology(t *testing.T) {

machinery.SaveToOutputDir(t, topology.ToDot(), "../../tests/out", ".dot")

gateways := topology.Targetables(func(o machinery.Object) bool {
targetables := topology.Targetables()

gateways := targetables.Items(func(o machinery.Object) bool {
_, ok := o.(*machinery.Gateway)
return ok
})
httpRouteRules := topology.Targetables(func(o machinery.Object) bool {
httpRouteRules := targetables.Items(func(o machinery.Object) bool {
_, ok := o.(*machinery.HTTPRouteRule)
return ok
})

effectivePoliciesByPath := make(map[string]ColorPolicy)

for _, httpRouteRule := range httpRouteRules {
for _, path := range topology.Paths(gateways[0], httpRouteRule) {
for _, path := range targetables.Paths(gateways[0], httpRouteRule) {
// Gather all policies in the path sorted from the least specific (gateway) to the most specific (httprouterule)
// Since in this example there are no targetables with more than one policy attached to it, we can safely just
// flat the slices of policies; otherwise we would need to ensure that the policies at the same level are sorted
Expand Down
8 changes: 5 additions & 3 deletions examples/json_patch/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,21 @@ func TestJSONPatchMergeBasedOnTopology(t *testing.T) {

machinery.SaveToOutputDir(t, topology.ToDot(), "../../tests/out", ".dot")

gateways := topology.Targetables(func(o machinery.Object) bool {
targetables := topology.Targetables()

gateways := targetables.Items(func(o machinery.Object) bool {
_, ok := o.(*machinery.Gateway)
return ok
})
httpRouteRules := topology.Targetables(func(o machinery.Object) bool {
httpRouteRules := targetables.Items(func(o machinery.Object) bool {
_, ok := o.(*machinery.HTTPRouteRule)
return ok
})

effectivePoliciesByPath := make(map[string]ColorPolicy)

for _, httpRouteRule := range httpRouteRules {
for _, path := range topology.Paths(gateways[0], httpRouteRule) {
for _, path := range targetables.Paths(gateways[0], httpRouteRule) {
// Gather all policies in the path sorted from the least specific (gateway) to the most specific (httprouterule)
// Since in this example there are no targetables with more than one policy attached to it, we can safely just
// flat the slices of policies; otherwise we would need to ensure that the policies at the same level are sorted
Expand Down
12 changes: 7 additions & 5 deletions examples/kuadrant/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,28 @@ func reconcile(eventType controller.EventType, oldObj, newObj controller.Runtime
// update the topology file
saveTopologyToFile(topology)

targetables := topology.Targetables()

// reconcile policies
gateways := topology.Targetables(func(o machinery.Object) bool {
gateways := targetables.Items(func(o machinery.Object) bool {
_, ok := o.(*machinery.Gateway)
return ok
})

listeners := topology.Targetables(func(o machinery.Object) bool {
listeners := targetables.Items(func(o machinery.Object) bool {
_, ok := o.(*machinery.Listener)
return ok
})

httpRouteRules := topology.Targetables(func(o machinery.Object) bool {
httpRouteRules := targetables.Items(func(o machinery.Object) bool {
_, ok := o.(*machinery.HTTPRouteRule)
return ok
})

for _, gateway := range gateways {
// reconcile Gateway -> Listener policies
for _, listener := range listeners {
paths := topology.Paths(gateway, listener)
paths := targetables.Paths(gateway, listener)
for i := range paths {
effectivePolicyForPath[*kuadrantv1alpha2.DNSPolicy](paths[i])
effectivePolicyForPath[*kuadrantv1alpha2.TLSPolicy](paths[i])
Expand All @@ -108,7 +110,7 @@ func reconcile(eventType controller.EventType, oldObj, newObj controller.Runtime

// reconcile Gateway -> HTTPRouteRule policies
for _, httpRouteRule := range httpRouteRules {
paths := topology.Paths(gateway, httpRouteRule)
paths := targetables.Paths(gateway, httpRouteRule)
for i := range paths {
effectivePolicyForPath[*kuadrantv1beta3.AuthPolicy](paths[i])
effectivePolicyForPath[*kuadrantv1beta3.RateLimitPolicy](paths[i])
Expand Down
8 changes: 4 additions & 4 deletions machinery/gateway_api_topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func TestGatewayAPITopology(t *testing.T) {
)

links := make(map[string][]string)
for _, root := range topology.Roots() {
linksFromNode(topology, root, links)
for _, root := range topology.Targetables().Roots() {
linksFromTargetable(topology, root, links)
}
for from, tos := range links {
expectedTos := tc.expectedLinks[from]
Expand Down Expand Up @@ -245,8 +245,8 @@ func TestGatewayAPITopologyWithSectionNames(t *testing.T) {
)

links := make(map[string][]string)
for _, root := range topology.Roots() {
linksFromNode(topology, root, links)
for _, root := range topology.Targetables().Roots() {
linksFromTargetable(topology, root, links)
}
for from, tos := range links {
expectedTos := tc.expectedLinks[from]
Expand Down
10 changes: 5 additions & 5 deletions machinery/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func SaveToOutputDir(t *testing.T, out *bytes.Buffer, outDir, ext string) {
}
}

func linksFromNode(topology *Topology, node Targetable, edges map[string][]string) {
if _, ok := edges[node.GetName()]; ok {
func linksFromTargetable(topology *Topology, targetable Targetable, edges map[string][]string) {
if _, ok := edges[targetable.GetName()]; ok {
return
}
children := topology.Children(node)
edges[node.GetName()] = lo.Map(children, func(child Targetable, _ int) string { return child.GetName() })
children := topology.Targetables().Children(targetable)
edges[targetable.GetName()] = lo.Map(children, func(child Targetable, _ int) string { return child.GetName() })
for _, child := range children {
linksFromNode(topology, child, edges)
linksFromTargetable(topology, child, edges)
}
}

Expand Down
Loading

0 comments on commit b72f969

Please sign in to comment.