diff --git a/README.md b/README.md index 2588d16..01d853a 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,8 @@ type MyPolicySpec struct { TargetRef gwapiv1alpha2.LocalPolicyTargetReference } -func (p *MyPolicy) GetURL() string { - return machinery.UrlFromObject(p) +func (p *MyPolicy) GetIdentity() string { + return machinery.IdentityFromObject(p) } func (p *MyPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/controller/object.go b/controller/object.go index 3f24acd..ea1630f 100644 --- a/controller/object.go +++ b/controller/object.go @@ -32,8 +32,8 @@ func (o *RuntimeObject) GetName() string { return o.Object.GetName() } -func (o *RuntimeObject) GetURL() string { - return machinery.UrlFromObject(o) +func (o *RuntimeObject) GetIdentity() string { + return machinery.IdentityFromObject(o) } // ObjectAs casts an Object generically into any kind diff --git a/examples/color_policy/color_policy.go b/examples/color_policy/color_policy.go index 05c1d9a..9e8cf57 100644 --- a/examples/color_policy/color_policy.go +++ b/examples/color_policy/color_policy.go @@ -16,8 +16,8 @@ type ColorPolicy struct { var _ machinery.Policy = &ColorPolicy{} -func (p *ColorPolicy) GetURL() string { - return machinery.UrlFromObject(p) +func (p *ColorPolicy) GetIdentity() string { + return machinery.IdentityFromObject(p) } func (p *ColorPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/json_patch/color_policy.go b/examples/json_patch/color_policy.go index ab324ae..b1c63ab 100644 --- a/examples/json_patch/color_policy.go +++ b/examples/json_patch/color_policy.go @@ -7,6 +7,7 @@ import ( gwapi "sigs.k8s.io/gateway-api/apis/v1alpha2" jsonpatch "github.com/evanphx/json-patch" + "github.com/kuadrant/policy-machinery/machinery" ) @@ -19,8 +20,8 @@ type ColorPolicy struct { var _ machinery.Policy = &ColorPolicy{} -func (p *ColorPolicy) GetURL() string { - return machinery.UrlFromObject(p) +func (p *ColorPolicy) GetIdentity() string { + return machinery.IdentityFromObject(p) } func (p *ColorPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/kuadrant/apis/v1alpha2/dnspolicy_types.go b/examples/kuadrant/apis/v1alpha2/dnspolicy_types.go index a290450..7810955 100644 --- a/examples/kuadrant/apis/v1alpha2/dnspolicy_types.go +++ b/examples/kuadrant/apis/v1alpha2/dnspolicy_types.go @@ -44,8 +44,8 @@ func (p *DNSPolicy) GetName() string { return p.Name } -func (p *DNSPolicy) GetURL() string { - return machinery.UrlFromObject(p) +func (p *DNSPolicy) GetIdentity() string { + return machinery.IdentityFromObject(p) } func (p *DNSPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/kuadrant/apis/v1alpha2/tlspolicy_types.go b/examples/kuadrant/apis/v1alpha2/tlspolicy_types.go index b3b7866..7b078dd 100644 --- a/examples/kuadrant/apis/v1alpha2/tlspolicy_types.go +++ b/examples/kuadrant/apis/v1alpha2/tlspolicy_types.go @@ -46,8 +46,8 @@ func (p *TLSPolicy) GetName() string { return p.Name } -func (p *TLSPolicy) GetURL() string { - return machinery.UrlFromObject(p) +func (p *TLSPolicy) GetIdentity() string { + return machinery.IdentityFromObject(p) } func (p *TLSPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/kuadrant/apis/v1beta3/authpolicy_types.go b/examples/kuadrant/apis/v1beta3/authpolicy_types.go index 2aea6dd..bd536ea 100644 --- a/examples/kuadrant/apis/v1beta3/authpolicy_types.go +++ b/examples/kuadrant/apis/v1beta3/authpolicy_types.go @@ -49,8 +49,8 @@ func (p *AuthPolicy) GetName() string { return p.Name } -func (p *AuthPolicy) GetURL() string { - return machinery.UrlFromObject(p) +func (p *AuthPolicy) GetIdentity() string { + return machinery.IdentityFromObject(p) } func (p *AuthPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/kuadrant/apis/v1beta3/ratelimitpolicy_types.go b/examples/kuadrant/apis/v1beta3/ratelimitpolicy_types.go index 7c117cd..e69bdd9 100644 --- a/examples/kuadrant/apis/v1beta3/ratelimitpolicy_types.go +++ b/examples/kuadrant/apis/v1beta3/ratelimitpolicy_types.go @@ -56,8 +56,8 @@ func (p *RateLimitPolicy) GetName() string { return p.Name } -func (p *RateLimitPolicy) GetURL() string { - return machinery.UrlFromObject(p) +func (p *RateLimitPolicy) GetIdentity() string { + return machinery.IdentityFromObject(p) } func (p *RateLimitPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/kuadrant/reconcilers/envoy_gateway.go b/examples/kuadrant/reconcilers/envoy_gateway.go index 157c250..2ce5a6e 100644 --- a/examples/kuadrant/reconcilers/envoy_gateway.go +++ b/examples/kuadrant/reconcilers/envoy_gateway.go @@ -44,7 +44,7 @@ func (p *EnvoyGatewayProvider) ReconcileSecurityPolicies(ctx context.Context, _ logger.Error(fmt.Errorf("unexpected topology path length to build Envoy SecurityPolicy"), "path", lo.Map(path, machinery.MapTargetableToURLFunc)) return false } - return path[0].GetURL() == gateway.GetURL() && lo.ContainsBy(targetables.Parents(path[0]), func(parent machinery.Targetable) bool { + return path[0].GetIdentity() == gateway.GetIdentity() && lo.ContainsBy(targetables.Parents(path[0]), func(parent machinery.Targetable) bool { gc, ok := parent.(*machinery.GatewayClass) return ok && gc.Spec.ControllerName == "gateway.envoyproxy.io/gatewayclass-controller" }) diff --git a/examples/kuadrant/reconcilers/istio.go b/examples/kuadrant/reconcilers/istio.go index af37d0a..df09b9b 100644 --- a/examples/kuadrant/reconcilers/istio.go +++ b/examples/kuadrant/reconcilers/istio.go @@ -47,7 +47,7 @@ func (p *IstioGatewayProvider) ReconcileAuthorizationPolicies(ctx context.Contex logger.Error(fmt.Errorf("unexpected topology path length to build Istio AuthorizationPolicy"), "path", lo.Map(path, machinery.MapTargetableToURLFunc)) return false } - return path[0].GetURL() == gateway.GetURL() && lo.ContainsBy(targetables.Parents(path[0]), func(parent machinery.Targetable) bool { + return path[0].GetIdentity() == gateway.GetIdentity() && lo.ContainsBy(targetables.Parents(path[0]), func(parent machinery.Targetable) bool { gc, ok := parent.(*machinery.GatewayClass) return ok && gc.Spec.ControllerName == "istio.io/gateway-controller" }) diff --git a/machinery/core_types.go b/machinery/core_types.go index 75917fa..121f70c 100644 --- a/machinery/core_types.go +++ b/machinery/core_types.go @@ -22,8 +22,8 @@ type Namespace struct { var _ Targetable = &Namespace{} -func (n *Namespace) GetURL() string { - return UrlFromObject(n) +func (n *Namespace) GetIdentity() string { + return IdentityFromObject(n) } func (n *Namespace) SetPolicies(policies []Policy) { @@ -42,8 +42,8 @@ type Service struct { var _ Targetable = &Service{} -func (s *Service) GetURL() string { - return UrlFromObject(s) +func (s *Service) GetIdentity() string { + return IdentityFromObject(s) } func (s *Service) SetPolicies(policies []Policy) { @@ -71,8 +71,8 @@ func (p *ServicePort) GroupVersionKind() schema.GroupVersionKind { func (p *ServicePort) SetGroupVersionKind(schema.GroupVersionKind) {} -func (p *ServicePort) GetURL() string { - return namespacedSectionName(UrlFromObject(p.Service), gwapiv1.SectionName(p.Name)) +func (p *ServicePort) GetIdentity() string { + return namespacedSectionName(IdentityFromObject(p.Service), gwapiv1.SectionName(p.Name)) } func (p *ServicePort) GetNamespace() string { diff --git a/machinery/gateway_api_test_helper.go b/machinery/gateway_api_test_helper.go index e98974a..8eddf8f 100644 --- a/machinery/gateway_api_test_helper.go +++ b/machinery/gateway_api_test_helper.go @@ -539,8 +539,8 @@ type TestPolicySpec struct { var _ Policy = &TestPolicy{} -func (p *TestPolicy) GetURL() string { - return UrlFromObject(p) +func (p *TestPolicy) GetIdentity() string { + return IdentityFromObject(p) } func (p *TestPolicy) GetTargetRefs() []PolicyTargetReference { diff --git a/machinery/gateway_api_topology.go b/machinery/gateway_api_topology.go index 78a1262..c7362a4 100644 --- a/machinery/gateway_api_topology.go +++ b/machinery/gateway_api_topology.go @@ -638,7 +638,7 @@ func findListenerFromParentRefFunc(gateways []*Gateway, listeners []*Listener, r } if parentRef.SectionName != nil { listener, ok := lo.Find(listeners, func(l *Listener) bool { - return l.Gateway.GetURL() == gateway.GetURL() && l.Name == *parentRef.SectionName + return l.Gateway.GetIdentity() == gateway.GetIdentity() && l.Name == *parentRef.SectionName }) if !ok { return nil @@ -646,7 +646,7 @@ func findListenerFromParentRefFunc(gateways []*Gateway, listeners []*Listener, r return []Object{listener} } return lo.FilterMap(listeners, func(l *Listener, _ int) (Object, bool) { - return l, l.Gateway.GetURL() == gateway.GetURL() + return l, l.Gateway.GetIdentity() == gateway.GetIdentity() }) } } diff --git a/machinery/gateway_api_types.go b/machinery/gateway_api_types.go index ddb0d33..bcd8add 100644 --- a/machinery/gateway_api_types.go +++ b/machinery/gateway_api_types.go @@ -35,7 +35,7 @@ const nameSectionNameURLSeparator = '#' // These are wrappers for Gateway API types so instances can be used as targetables in the topology. // Targateables typically store back references to the policies that are attached to them. -// The implementation of GetURL() must return a unique identifier for the wrapped object that matches the one +// The implementation of GetIdentity() must return a unique identifier for the wrapped object that matches the one // generated by policy targetRefs that implement the PolicyTargetReference interface for values pointing to the object. type GatewayClass struct { @@ -46,8 +46,8 @@ type GatewayClass struct { var _ Targetable = &GatewayClass{} -func (g *GatewayClass) GetURL() string { - return UrlFromObject(g) +func (g *GatewayClass) GetIdentity() string { + return IdentityFromObject(g) } func (g *GatewayClass) SetPolicies(policies []Policy) { @@ -66,8 +66,8 @@ type Gateway struct { var _ Targetable = &Gateway{} -func (g *Gateway) GetURL() string { - return UrlFromObject(g) +func (g *Gateway) GetIdentity() string { + return IdentityFromObject(g) } func (g *Gateway) SetPolicies(policies []Policy) { @@ -97,8 +97,8 @@ func (l *Listener) GroupVersionKind() schema.GroupVersionKind { func (l *Listener) SetGroupVersionKind(schema.GroupVersionKind) {} -func (l *Listener) GetURL() string { - return namespacedSectionName(UrlFromObject(l.Gateway), l.Name) +func (l *Listener) GetIdentity() string { + return namespacedSectionName(IdentityFromObject(l.Gateway), l.Name) } func (l *Listener) GetNamespace() string { @@ -125,8 +125,8 @@ type HTTPRoute struct { var _ Targetable = &HTTPRoute{} -func (r *HTTPRoute) GetURL() string { - return UrlFromObject(r) +func (r *HTTPRoute) GetIdentity() string { + return IdentityFromObject(r) } func (r *HTTPRoute) SetPolicies(policies []Policy) { @@ -157,8 +157,8 @@ func (r *HTTPRouteRule) GroupVersionKind() schema.GroupVersionKind { func (r *HTTPRouteRule) SetGroupVersionKind(schema.GroupVersionKind) {} -func (r *HTTPRouteRule) GetURL() string { - return namespacedSectionName(UrlFromObject(r.HTTPRoute), r.Name) +func (r *HTTPRouteRule) GetIdentity() string { + return namespacedSectionName(IdentityFromObject(r.HTTPRoute), r.Name) } func (r *HTTPRouteRule) GetNamespace() string { @@ -185,8 +185,8 @@ type GRPCRoute struct { var _ Targetable = &GRPCRoute{} -func (r *GRPCRoute) GetURL() string { - return UrlFromObject(r) +func (r *GRPCRoute) GetIdentity() string { + return IdentityFromObject(r) } func (r *GRPCRoute) SetPolicies(policies []Policy) { r.attachedPolicies = policies @@ -216,8 +216,8 @@ func (r *GRPCRouteRule) GroupVersionKind() schema.GroupVersionKind { func (r *GRPCRouteRule) SetGroupVersionKind(schema.GroupVersionKind) {} -func (r *GRPCRouteRule) GetURL() string { - return namespacedSectionName(UrlFromObject(r.GRPCRoute), r.Name) +func (r *GRPCRouteRule) GetIdentity() string { + return namespacedSectionName(IdentityFromObject(r.GRPCRoute), r.Name) } func (r *GRPCRouteRule) GetNamespace() string { @@ -244,8 +244,8 @@ type TCPRoute struct { var _ Targetable = &TCPRoute{} -func (r *TCPRoute) GetURL() string { - return UrlFromObject(r) +func (r *TCPRoute) GetIdentity() string { + return IdentityFromObject(r) } func (r *TCPRoute) SetPolicies(policies []Policy) { @@ -276,8 +276,8 @@ func (r *TCPRouteRule) GroupVersionKind() schema.GroupVersionKind { func (r *TCPRouteRule) SetGroupVersionKind(schema.GroupVersionKind) {} -func (r *TCPRouteRule) GetURL() string { - return namespacedSectionName(UrlFromObject(r.TCPRoute), r.Name) +func (r *TCPRouteRule) GetIdentity() string { + return namespacedSectionName(IdentityFromObject(r.TCPRoute), r.Name) } func (r *TCPRouteRule) GetNamespace() string { @@ -304,8 +304,8 @@ type TLSRoute struct { var _ Targetable = &TLSRoute{} -func (r *TLSRoute) GetURL() string { - return UrlFromObject(r) +func (r *TLSRoute) GetIdentity() string { + return IdentityFromObject(r) } func (r *TLSRoute) SetPolicies(policies []Policy) { @@ -336,8 +336,8 @@ func (r *TLSRouteRule) GroupVersionKind() schema.GroupVersionKind { func (r *TLSRouteRule) SetGroupVersionKind(schema.GroupVersionKind) {} -func (r *TLSRouteRule) GetURL() string { - return namespacedSectionName(UrlFromObject(r.TLSRoute), r.Name) +func (r *TLSRouteRule) GetIdentity() string { + return namespacedSectionName(IdentityFromObject(r.TLSRoute), r.Name) } func (r *TLSRouteRule) GetNamespace() string { @@ -364,8 +364,8 @@ type UDPRoute struct { var _ Targetable = &UDPRoute{} -func (r *UDPRoute) GetURL() string { - return UrlFromObject(r) +func (r *UDPRoute) GetIdentity() string { + return IdentityFromObject(r) } func (r *UDPRoute) SetPolicies(policies []Policy) { @@ -396,8 +396,8 @@ func (r *UDPRouteRule) GroupVersionKind() schema.GroupVersionKind { func (r *UDPRouteRule) SetGroupVersionKind(schema.GroupVersionKind) {} -func (r *UDPRouteRule) GetURL() string { - return namespacedSectionName(UrlFromObject(r.UDPRoute), r.Name) +func (r *UDPRouteRule) GetIdentity() string { + return namespacedSectionName(IdentityFromObject(r.UDPRoute), r.Name) } func (r *UDPRouteRule) GetNamespace() string { @@ -417,9 +417,9 @@ func (r *UDPRouteRule) Policies() []Policy { } // These are Gateway API target reference types that implement the PolicyTargetReference interface, so policies' -// targetRef instances can be treated as Objects whose GetURL() functions return the unique identifier of the +// targetRef instances can be treated as Objects whose GetIdentity() functions return the unique identifier of the // corresponding targetable the reference points to. -// This is the reason why GetURL() was adopted to get the unique identifiers of topology objects instead of more +// This is the reason why GetIdentity() was adopted to get the unique identifiers of topology objects instead of more // obvious Kubernetes objects' GetUID() (k8s.io/apimachinery/pkg/apis/meta/v1). type NamespacedPolicyTargetReference struct { @@ -441,8 +441,8 @@ func (t NamespacedPolicyTargetReference) SetGroupVersionKind(gvk schema.GroupVer t.Kind = gwapiv1alpha2.Kind(gvk.Kind) } -func (t NamespacedPolicyTargetReference) GetURL() string { - return UrlFromObject(t) +func (t NamespacedPolicyTargetReference) GetIdentity() string { + return IdentityFromObject(t) } func (t NamespacedPolicyTargetReference) GetNamespace() string { @@ -472,8 +472,8 @@ func (t LocalPolicyTargetReference) SetGroupVersionKind(gvk schema.GroupVersionK t.Kind = gwapiv1alpha2.Kind(gvk.Kind) } -func (t LocalPolicyTargetReference) GetURL() string { - return UrlFromObject(t) +func (t LocalPolicyTargetReference) GetIdentity() string { + return IdentityFromObject(t) } func (t LocalPolicyTargetReference) GetNamespace() string { @@ -503,8 +503,8 @@ func (t LocalPolicyTargetReferenceWithSectionName) SetGroupVersionKind(gvk schem t.Kind = gwapiv1alpha2.Kind(gvk.Kind) } -func (t LocalPolicyTargetReferenceWithSectionName) GetURL() string { - return UrlFromObject(t) +func (t LocalPolicyTargetReferenceWithSectionName) GetIdentity() string { + return IdentityFromObject(t) } func (t LocalPolicyTargetReferenceWithSectionName) GetNamespace() string { @@ -526,8 +526,8 @@ type ReferenceGrant struct { var _ Object = &ReferenceGrant{} -func (o *ReferenceGrant) GetURL() string { - return UrlFromObject(o) +func (o *ReferenceGrant) GetIdentity() string { + return IdentityFromObject(o) } // These are wrappers for Gateway API types so instances can be used as policies in the topology. @@ -538,8 +538,8 @@ type BackendTLSPolicy struct { var _ Policy = &BackendTLSPolicy{} -func (p *BackendTLSPolicy) GetURL() string { - return UrlFromObject(p) +func (p *BackendTLSPolicy) GetIdentity() string { + return IdentityFromObject(p) } func (p *BackendTLSPolicy) GetTargetRefs() []PolicyTargetReference { @@ -569,8 +569,8 @@ type BackendLBPolicy struct { var _ Policy = &BackendLBPolicy{} -func (p *BackendLBPolicy) GetURL() string { - return UrlFromObject(p) +func (p *BackendLBPolicy) GetIdentity() string { + return IdentityFromObject(p) } func (p *BackendLBPolicy) GetTargetRefs() []PolicyTargetReference { diff --git a/machinery/test_helper.go b/machinery/test_helper.go index d7b825c..df80b88 100644 --- a/machinery/test_helper.go +++ b/machinery/test_helper.go @@ -56,8 +56,8 @@ func (a *Apple) GetNamespace() string { return "" } -func (a *Apple) GetURL() string { - return UrlFromObject(a) +func (a *Apple) GetIdentity() string { + return IdentityFromObject(a) } func (a *Apple) GroupVersionKind() schema.GroupVersionKind { @@ -97,8 +97,8 @@ func (o *Orange) GetNamespace() string { return o.Namespace } -func (o *Orange) GetURL() string { - return UrlFromObject(o) +func (o *Orange) GetIdentity() string { + return IdentityFromObject(o) } func (o *Orange) GroupVersionKind() schema.GroupVersionKind { @@ -133,8 +133,8 @@ func (b *Banana) GetNamespace() string { return "" } -func (b *Banana) GetURL() string { - return UrlFromObject(b) +func (b *Banana) GetIdentity() string { + return IdentityFromObject(b) } func (b *Banana) GroupVersionKind() schema.GroupVersionKind { @@ -204,8 +204,8 @@ func (i *Info) GetName() string { return i.Name } -func (i *Info) GetURL() string { - return UrlFromObject(i) +func (i *Info) GetIdentity() string { + return IdentityFromObject(i) } func LinkInfoFrom(kind string, objects []Object) LinkFunc { @@ -215,7 +215,7 @@ func LinkInfoFrom(kind string, objects []Object) LinkFunc { Func: func(child Object) []Object { info := child.(*Info) return lo.Filter(objects, func(obj Object, _ int) bool { - return obj.GetURL() == info.Ref + return obj.GetIdentity() == info.Ref }) }, } @@ -234,8 +234,8 @@ type FruitPolicySpec struct { var _ Policy = &FruitPolicy{} -func (p *FruitPolicy) GetURL() string { - return UrlFromObject(p) +func (p *FruitPolicy) GetIdentity() string { + return IdentityFromObject(p) } func (p *FruitPolicy) GetTargetRefs() []PolicyTargetReference { @@ -286,8 +286,8 @@ func (t FruitPolicyTargetReference) SetGroupVersionKind(gvk schema.GroupVersionK t.Kind = gvk.Kind } -func (t FruitPolicyTargetReference) GetURL() string { - return UrlFromObject(t) +func (t FruitPolicyTargetReference) GetIdentity() string { + return IdentityFromObject(t) } func (t FruitPolicyTargetReference) GetNamespace() string { diff --git a/machinery/topology.go b/machinery/topology.go index c022a14..e70990e 100644 --- a/machinery/topology.go +++ b/machinery/topology.go @@ -76,15 +76,15 @@ func NewTopology(options ...TopologyOptionsFunc) *Topology { for i := range policies { policy := policies[i] for _, targetRef := range policy.GetTargetRefs() { - if policiesByTargetRef[targetRef.GetURL()] == nil { - policiesByTargetRef[targetRef.GetURL()] = make([]Policy, 0) + if policiesByTargetRef[targetRef.GetIdentity()] == nil { + policiesByTargetRef[targetRef.GetIdentity()] = make([]Policy, 0) } - policiesByTargetRef[targetRef.GetURL()] = append(policiesByTargetRef[targetRef.GetURL()], policy) + policiesByTargetRef[targetRef.GetIdentity()] = append(policiesByTargetRef[targetRef.GetIdentity()], policy) } } targetables := lo.Map(o.Targetables, func(t Targetable, _ int) Targetable { - t.SetPolicies(policiesByTargetRef[t.GetURL()]) + t.SetPolicies(policiesByTargetRef[t.GetIdentity()]) return t }) @@ -161,7 +161,7 @@ func (t *Topology) ToDot() string { func addObjectsToGraph[T Object](graph *dot.Graph, objects []T) []dot.Node { return lo.Map(objects, func(object T, _ int) dot.Node { name := strings.TrimPrefix(namespacedName(object.GetNamespace(), object.GetName()), string(k8stypes.Separator)) - n := graph.Node(string(object.GetURL())) + n := graph.Node(string(object.GetIdentity())) n.Label(fmt.Sprintf("%s\n%s", object.GroupVersionKind().Kind, name)) n.Attr("shape", "ellipse") return n @@ -186,7 +186,7 @@ func addPoliciesToGraph[T Policy](graph *dot.Graph, policies []T) { ) // Policy -> Target edges for _, targetRef := range policies[i].GetTargetRefs() { - targetNode, found := graph.FindNodeById(string(targetRef.GetURL())) + targetNode, found := graph.FindNodeById(string(targetRef.GetIdentity())) if !found { continue } @@ -198,8 +198,8 @@ func addPoliciesToGraph[T Policy](graph *dot.Graph, policies []T) { } func addEdgeToGraph(graph *dot.Graph, name string, parent, child Object) { - p, foundParent := graph.FindNodeById(string(parent.GetURL())) - c, foundChild := graph.FindNodeById(string(child.GetURL())) + p, foundParent := graph.FindNodeById(string(parent.GetIdentity())) + c, foundChild := graph.FindNodeById(string(child.GetIdentity())) if foundParent && foundChild { edge := graph.Edge(p, c) edge.Attr("comment", name) @@ -207,7 +207,7 @@ func addEdgeToGraph(graph *dot.Graph, name string, parent, child Object) { } func associateURL[T Object](obj T) (string, T) { - return obj.GetURL(), obj + return obj.GetIdentity(), obj } type collection[T Object] struct { @@ -269,7 +269,7 @@ func (c *collection[T]) Parents(item Object) []T { var parents []T for from, edges := range c.topology.graph.EdgesMap() { if !lo.ContainsBy(edges, func(edge dot.Edge) bool { - return edge.To().ID() == item.GetURL() + return edge.To().ID() == item.GetIdentity() }) { continue } @@ -284,7 +284,7 @@ func (c *collection[T]) Parents(item Object) []T { // Children returns all children of a given item in the collection. func (c *collection[T]) Children(item Object) []T { - return lo.FilterMap(c.topology.graph.EdgesMap()[item.GetURL()], func(edge dot.Edge, _ int) (T, bool) { + return lo.FilterMap(c.topology.graph.EdgesMap()[item.GetIdentity()], func(edge dot.Edge, _ int) (T, bool) { child, found := c.items[edge.To().ID()] return child, found }) @@ -305,13 +305,13 @@ func (c *collection[T]) Paths(from, to Object) [][]T { // dfs performs a depth-first search to find all paths from a source item to a destination item in the collection. func (c *collection[T]) dfs(current, to Object, path []T, paths *[][]T, visited map[string]bool) { - currentURL := current.GetURL() + currentURL := current.GetIdentity() if visited[currentURL] { return } path = append(path, c.items[currentURL]) visited[currentURL] = true - if currentURL == to.GetURL() { + if currentURL == to.GetIdentity() { pathCopy := make([]T, len(path)) copy(pathCopy, path) *paths = append(*paths, pathCopy) diff --git a/machinery/topology_test.go b/machinery/topology_test.go index ae38e8f..6658d3b 100644 --- a/machinery/topology_test.go +++ b/machinery/topology_test.go @@ -44,8 +44,8 @@ func TestTopologyRoots(t *testing.T) { } rootURLs := lo.Map(roots, MapTargetableToURLFunc) for _, apple := range apples { - if !lo.Contains(rootURLs, apple.GetURL()) { - t.Errorf("expected root %s not found", apple.GetURL()) + if !lo.Contains(rootURLs, apple.GetIdentity()) { + t.Errorf("expected root %s not found", apple.GetIdentity()) } } } @@ -76,11 +76,11 @@ func TestTopologyParents(t *testing.T) { t.Errorf("expected %d parent, got %d", expected, len(parents)) } parentURLs := lo.Map(parents, MapTargetableToURLFunc) - if !lo.Contains(parentURLs, apple1.GetURL()) { - t.Errorf("expected parent %s not found", apple1.GetURL()) + if !lo.Contains(parentURLs, apple1.GetIdentity()) { + t.Errorf("expected parent %s not found", apple1.GetIdentity()) } - if !lo.Contains(parentURLs, apple2.GetURL()) { - t.Errorf("expected parent %s not found", apple2.GetURL()) + if !lo.Contains(parentURLs, apple2.GetIdentity()) { + t.Errorf("expected parent %s not found", apple2.GetIdentity()) } // orange-2 parents = topology.Targetables().Parents(orange2) @@ -88,8 +88,8 @@ func TestTopologyParents(t *testing.T) { t.Errorf("expected %d parent, got %d", expected, len(parents)) } parentURLs = lo.Map(parents, MapTargetableToURLFunc) - if !lo.Contains(parentURLs, apple2.GetURL()) { - t.Errorf("expected parent %s not found", apple2.GetURL()) + if !lo.Contains(parentURLs, apple2.GetIdentity()) { + t.Errorf("expected parent %s not found", apple2.GetIdentity()) } } @@ -119,8 +119,8 @@ func TestTopologyChildren(t *testing.T) { t.Errorf("expected %d child, got %d", expected, len(children)) } childURLs := lo.Map(children, MapTargetableToURLFunc) - if !lo.Contains(childURLs, orange1.GetURL()) { - t.Errorf("expected child %s not found", orange1.GetURL()) + if !lo.Contains(childURLs, orange1.GetIdentity()) { + t.Errorf("expected child %s not found", orange1.GetIdentity()) } // apple-2 children = topology.Targetables().Children(apple2) @@ -128,11 +128,11 @@ func TestTopologyChildren(t *testing.T) { t.Errorf("expected %d child, got %d", expected, len(children)) } childURLs = lo.Map(children, MapTargetableToURLFunc) - if !lo.Contains(childURLs, orange1.GetURL()) { - t.Errorf("expected child %s not found", orange1.GetURL()) + if !lo.Contains(childURLs, orange1.GetIdentity()) { + t.Errorf("expected child %s not found", orange1.GetIdentity()) } - if !lo.Contains(childURLs, orange2.GetURL()) { - t.Errorf("expected child %s not found", orange2.GetURL()) + if !lo.Contains(childURLs, orange2.GetIdentity()) { + t.Errorf("expected child %s not found", orange2.GetIdentity()) } } diff --git a/machinery/types.go b/machinery/types.go index 3deb6f3..01658f7 100644 --- a/machinery/types.go +++ b/machinery/types.go @@ -15,10 +15,10 @@ type Object interface { GetNamespace() string GetName() string - GetURL() string + GetIdentity() string } -func UrlFromObject(obj Object) string { +func IdentityFromObject(obj Object) string { name := strings.TrimPrefix(namespacedName(obj.GetNamespace(), obj.GetName()), string(k8stypes.Separator)) return fmt.Sprintf("%s%s%s", strings.ToLower(obj.GroupVersionKind().GroupKind().String()), string(kindNameURLSeparator), name) } @@ -40,7 +40,7 @@ type Targetable interface { } func MapTargetableToURLFunc(t Targetable, _ int) string { - return t.GetURL() + return t.GetIdentity() } // Policy targets objects and can be merged with another Policy based on a given MergeStrategy.