From 5a564aee9eefccf9c38419fa40c5667189916c1c Mon Sep 17 00:00:00 2001 From: KevFan Date: Thu, 22 Aug 2024 09:59:20 +0100 Subject: [PATCH 1/2] refactor: GetURL() -> GetIdentity() Signed-off-by: KevFan --- README.md | 4 +- controller/object.go | 4 +- examples/color_policy/color_policy.go | 4 +- examples/json_patch/color_policy.go | 5 +- .../kuadrant/apis/v1alpha2/dnspolicy_types.go | 4 +- .../kuadrant/apis/v1alpha2/tlspolicy_types.go | 4 +- .../kuadrant/apis/v1beta3/authpolicy_types.go | 4 +- .../apis/v1beta3/ratelimitpolicy_types.go | 4 +- .../kuadrant/reconcilers/envoy_gateway.go | 2 +- examples/kuadrant/reconcilers/istio.go | 2 +- machinery/core_types.go | 12 +-- machinery/gateway_api_test_helper.go | 4 +- machinery/gateway_api_topology.go | 4 +- machinery/gateway_api_types.go | 82 +++++++++---------- machinery/test_helper.go | 26 +++--- machinery/topology.go | 26 +++--- machinery/topology_test.go | 28 +++---- machinery/types.go | 6 +- 18 files changed, 113 insertions(+), 112 deletions(-) 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. From b9625fd67ae0989f0ddea42c8023514a67a8be5f Mon Sep 17 00:00:00 2001 From: KevFan Date: Fri, 30 Aug 2024 13:27:05 +0100 Subject: [PATCH 2/2] refactor: GetIdentity() -> GetLocator() Signed-off-by: KevFan --- README.md | 4 +- controller/object.go | 4 +- examples/color_policy/color_policy.go | 4 +- examples/json_patch/color_policy.go | 4 +- .../kuadrant/apis/v1alpha2/dnspolicy_types.go | 4 +- .../kuadrant/apis/v1alpha2/tlspolicy_types.go | 4 +- .../kuadrant/apis/v1beta3/authpolicy_types.go | 4 +- .../apis/v1beta3/ratelimitpolicy_types.go | 4 +- .../effective_policies_reconciler.go | 6 +- .../kuadrant/reconcilers/envoy_gateway.go | 4 +- examples/kuadrant/reconcilers/istio.go | 4 +- machinery/core_types.go | 12 +-- machinery/gateway_api_test_helper.go | 4 +- machinery/gateway_api_topology.go | 4 +- machinery/gateway_api_types.go | 86 +++++++++---------- machinery/test_helper.go | 26 +++--- machinery/topology.go | 42 ++++----- machinery/topology_test.go | 42 ++++----- machinery/types.go | 12 +-- 19 files changed, 137 insertions(+), 137 deletions(-) diff --git a/README.md b/README.md index 01d853a..f233a0d 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,8 @@ type MyPolicySpec struct { TargetRef gwapiv1alpha2.LocalPolicyTargetReference } -func (p *MyPolicy) GetIdentity() string { - return machinery.IdentityFromObject(p) +func (p *MyPolicy) GetLocator() string { + return machinery.LocatorFromObject(p) } func (p *MyPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/controller/object.go b/controller/object.go index ea1630f..fdf17f1 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) GetIdentity() string { - return machinery.IdentityFromObject(o) +func (o *RuntimeObject) GetLocator() string { + return machinery.LocatorFromObject(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 9e8cf57..b3521a3 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) GetIdentity() string { - return machinery.IdentityFromObject(p) +func (p *ColorPolicy) GetLocator() string { + return machinery.LocatorFromObject(p) } func (p *ColorPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/json_patch/color_policy.go b/examples/json_patch/color_policy.go index b1c63ab..57e63c7 100644 --- a/examples/json_patch/color_policy.go +++ b/examples/json_patch/color_policy.go @@ -20,8 +20,8 @@ type ColorPolicy struct { var _ machinery.Policy = &ColorPolicy{} -func (p *ColorPolicy) GetIdentity() string { - return machinery.IdentityFromObject(p) +func (p *ColorPolicy) GetLocator() string { + return machinery.LocatorFromObject(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 7810955..111c161 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) GetIdentity() string { - return machinery.IdentityFromObject(p) +func (p *DNSPolicy) GetLocator() string { + return machinery.LocatorFromObject(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 7b078dd..9ddb0da 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) GetIdentity() string { - return machinery.IdentityFromObject(p) +func (p *TLSPolicy) GetLocator() string { + return machinery.LocatorFromObject(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 bd536ea..3833089 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) GetIdentity() string { - return machinery.IdentityFromObject(p) +func (p *AuthPolicy) GetLocator() string { + return machinery.LocatorFromObject(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 e69bdd9..0e953ad 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) GetIdentity() string { - return machinery.IdentityFromObject(p) +func (p *RateLimitPolicy) GetLocator() string { + return machinery.LocatorFromObject(p) } func (p *RateLimitPolicy) GetTargetRefs() []machinery.PolicyTargetReference { diff --git a/examples/kuadrant/reconcilers/effective_policies_reconciler.go b/examples/kuadrant/reconcilers/effective_policies_reconciler.go index db9690f..322f7b4 100644 --- a/examples/kuadrant/reconcilers/effective_policies_reconciler.go +++ b/examples/kuadrant/reconcilers/effective_policies_reconciler.go @@ -103,10 +103,10 @@ func effectivePolicyForPath[T machinery.Policy](ctx context.Context, path []mach return lo.Map(policies, func(p kuadrantapis.MergeablePolicy, _ int) machinery.Policy { return p }) }) - pathURLs := lo.Map(path, machinery.MapTargetableToURLFunc) + pathLocators := lo.Map(path, machinery.MapTargetableToLocatorFunc) if len(policies) == 0 { - logger.Info("no policies for path", "kind", reflect.TypeOf(new(T)), "path", pathURLs) + logger.Info("no policies for path", "kind", reflect.TypeOf(new(T)), "path", pathLocators) return nil } @@ -116,7 +116,7 @@ func effectivePolicyForPath[T machinery.Policy](ctx context.Context, path []mach }, policies[len(policies)-1]) jsonEffectivePolicy, _ := json.Marshal(effectivePolicy) - logger.Info("effective policy", "kind", reflect.TypeOf(new(T)), "path", pathURLs, "effectivePolicy", string(jsonEffectivePolicy)) + logger.Info("effective policy", "kind", reflect.TypeOf(new(T)), "path", pathLocators, "effectivePolicy", string(jsonEffectivePolicy)) concreteEffectivePolicy, _ := effectivePolicy.(T) return &concreteEffectivePolicy diff --git a/examples/kuadrant/reconcilers/envoy_gateway.go b/examples/kuadrant/reconcilers/envoy_gateway.go index 2ce5a6e..477e989 100644 --- a/examples/kuadrant/reconcilers/envoy_gateway.go +++ b/examples/kuadrant/reconcilers/envoy_gateway.go @@ -41,10 +41,10 @@ func (p *EnvoyGatewayProvider) ReconcileSecurityPolicies(ctx context.Context, _ for _, gateway := range gateways { paths := lo.Filter(authPaths, func(path []machinery.Targetable, _ int) bool { if len(path) != 4 { // should never happen - logger.Error(fmt.Errorf("unexpected topology path length to build Envoy SecurityPolicy"), "path", lo.Map(path, machinery.MapTargetableToURLFunc)) + logger.Error(fmt.Errorf("unexpected topology path length to build Envoy SecurityPolicy"), "path", lo.Map(path, machinery.MapTargetableToLocatorFunc)) return false } - return path[0].GetIdentity() == gateway.GetIdentity() && lo.ContainsBy(targetables.Parents(path[0]), func(parent machinery.Targetable) bool { + return path[0].GetLocator() == gateway.GetLocator() && 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 df09b9b..d670c2b 100644 --- a/examples/kuadrant/reconcilers/istio.go +++ b/examples/kuadrant/reconcilers/istio.go @@ -44,10 +44,10 @@ func (p *IstioGatewayProvider) ReconcileAuthorizationPolicies(ctx context.Contex for _, gateway := range gateways { paths := lo.Filter(authPaths, func(path []machinery.Targetable, _ int) bool { if len(path) != 4 { // should never happen - logger.Error(fmt.Errorf("unexpected topology path length to build Istio AuthorizationPolicy"), "path", lo.Map(path, machinery.MapTargetableToURLFunc)) + logger.Error(fmt.Errorf("unexpected topology path length to build Istio AuthorizationPolicy"), "path", lo.Map(path, machinery.MapTargetableToLocatorFunc)) return false } - return path[0].GetIdentity() == gateway.GetIdentity() && lo.ContainsBy(targetables.Parents(path[0]), func(parent machinery.Targetable) bool { + return path[0].GetLocator() == gateway.GetLocator() && 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 121f70c..47fb40e 100644 --- a/machinery/core_types.go +++ b/machinery/core_types.go @@ -22,8 +22,8 @@ type Namespace struct { var _ Targetable = &Namespace{} -func (n *Namespace) GetIdentity() string { - return IdentityFromObject(n) +func (n *Namespace) GetLocator() string { + return LocatorFromObject(n) } func (n *Namespace) SetPolicies(policies []Policy) { @@ -42,8 +42,8 @@ type Service struct { var _ Targetable = &Service{} -func (s *Service) GetIdentity() string { - return IdentityFromObject(s) +func (s *Service) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return namespacedSectionName(IdentityFromObject(p.Service), gwapiv1.SectionName(p.Name)) +func (p *ServicePort) GetLocator() string { + return namespacedSectionName(LocatorFromObject(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 8eddf8f..e57c491 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) GetIdentity() string { - return IdentityFromObject(p) +func (p *TestPolicy) GetLocator() string { + return LocatorFromObject(p) } func (p *TestPolicy) GetTargetRefs() []PolicyTargetReference { diff --git a/machinery/gateway_api_topology.go b/machinery/gateway_api_topology.go index c7362a4..2c54b4c 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.GetIdentity() == gateway.GetIdentity() && l.Name == *parentRef.SectionName + return l.Gateway.GetLocator() == gateway.GetLocator() && 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.GetIdentity() == gateway.GetIdentity() + return l, l.Gateway.GetLocator() == gateway.GetLocator() }) } } diff --git a/machinery/gateway_api_types.go b/machinery/gateway_api_types.go index bcd8add..7533e7f 100644 --- a/machinery/gateway_api_types.go +++ b/machinery/gateway_api_types.go @@ -31,11 +31,11 @@ var ( UDPRouteRuleGroupKind = gwapiv1alpha2.SchemeGroupVersion.WithKind("UDPRouteRule").GroupKind() ) -const nameSectionNameURLSeparator = '#' +const nameSectionNameLocatorSeparator = '#' // 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 GetIdentity() must return a unique identifier for the wrapped object that matches the one +// The implementation of GetLocator() 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) GetIdentity() string { - return IdentityFromObject(g) +func (g *GatewayClass) GetLocator() string { + return LocatorFromObject(g) } func (g *GatewayClass) SetPolicies(policies []Policy) { @@ -66,8 +66,8 @@ type Gateway struct { var _ Targetable = &Gateway{} -func (g *Gateway) GetIdentity() string { - return IdentityFromObject(g) +func (g *Gateway) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return namespacedSectionName(IdentityFromObject(l.Gateway), l.Name) +func (l *Listener) GetLocator() string { + return namespacedSectionName(LocatorFromObject(l.Gateway), l.Name) } func (l *Listener) GetNamespace() string { @@ -125,8 +125,8 @@ type HTTPRoute struct { var _ Targetable = &HTTPRoute{} -func (r *HTTPRoute) GetIdentity() string { - return IdentityFromObject(r) +func (r *HTTPRoute) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return namespacedSectionName(IdentityFromObject(r.HTTPRoute), r.Name) +func (r *HTTPRouteRule) GetLocator() string { + return namespacedSectionName(LocatorFromObject(r.HTTPRoute), r.Name) } func (r *HTTPRouteRule) GetNamespace() string { @@ -185,8 +185,8 @@ type GRPCRoute struct { var _ Targetable = &GRPCRoute{} -func (r *GRPCRoute) GetIdentity() string { - return IdentityFromObject(r) +func (r *GRPCRoute) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return namespacedSectionName(IdentityFromObject(r.GRPCRoute), r.Name) +func (r *GRPCRouteRule) GetLocator() string { + return namespacedSectionName(LocatorFromObject(r.GRPCRoute), r.Name) } func (r *GRPCRouteRule) GetNamespace() string { @@ -244,8 +244,8 @@ type TCPRoute struct { var _ Targetable = &TCPRoute{} -func (r *TCPRoute) GetIdentity() string { - return IdentityFromObject(r) +func (r *TCPRoute) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return namespacedSectionName(IdentityFromObject(r.TCPRoute), r.Name) +func (r *TCPRouteRule) GetLocator() string { + return namespacedSectionName(LocatorFromObject(r.TCPRoute), r.Name) } func (r *TCPRouteRule) GetNamespace() string { @@ -304,8 +304,8 @@ type TLSRoute struct { var _ Targetable = &TLSRoute{} -func (r *TLSRoute) GetIdentity() string { - return IdentityFromObject(r) +func (r *TLSRoute) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return namespacedSectionName(IdentityFromObject(r.TLSRoute), r.Name) +func (r *TLSRouteRule) GetLocator() string { + return namespacedSectionName(LocatorFromObject(r.TLSRoute), r.Name) } func (r *TLSRouteRule) GetNamespace() string { @@ -364,8 +364,8 @@ type UDPRoute struct { var _ Targetable = &UDPRoute{} -func (r *UDPRoute) GetIdentity() string { - return IdentityFromObject(r) +func (r *UDPRoute) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return namespacedSectionName(IdentityFromObject(r.UDPRoute), r.Name) +func (r *UDPRouteRule) GetLocator() string { + return namespacedSectionName(LocatorFromObject(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 GetIdentity() functions return the unique identifier of the +// targetRef instances can be treated as Objects whose GetLocator() functions return the unique identifier of the // corresponding targetable the reference points to. -// This is the reason why GetIdentity() was adopted to get the unique identifiers of topology objects instead of more +// This is the reason why GetLocator() 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) GetIdentity() string { - return IdentityFromObject(t) +func (t NamespacedPolicyTargetReference) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return IdentityFromObject(t) +func (t LocalPolicyTargetReference) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return IdentityFromObject(t) +func (t LocalPolicyTargetReferenceWithSectionName) GetLocator() string { + return LocatorFromObject(t) } func (t LocalPolicyTargetReferenceWithSectionName) GetNamespace() string { @@ -526,8 +526,8 @@ type ReferenceGrant struct { var _ Object = &ReferenceGrant{} -func (o *ReferenceGrant) GetIdentity() string { - return IdentityFromObject(o) +func (o *ReferenceGrant) GetLocator() string { + return LocatorFromObject(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) GetIdentity() string { - return IdentityFromObject(p) +func (p *BackendTLSPolicy) GetLocator() string { + return LocatorFromObject(p) } func (p *BackendTLSPolicy) GetTargetRefs() []PolicyTargetReference { @@ -569,8 +569,8 @@ type BackendLBPolicy struct { var _ Policy = &BackendLBPolicy{} -func (p *BackendLBPolicy) GetIdentity() string { - return IdentityFromObject(p) +func (p *BackendLBPolicy) GetLocator() string { + return LocatorFromObject(p) } func (p *BackendLBPolicy) GetTargetRefs() []PolicyTargetReference { @@ -595,5 +595,5 @@ func (p *BackendLBPolicy) Merge(other Policy) Policy { } func namespacedSectionName(namespace string, sectionName gwapiv1.SectionName) string { - return fmt.Sprintf("%s%s%s", namespace, string(nameSectionNameURLSeparator), sectionName) + return fmt.Sprintf("%s%s%s", namespace, string(nameSectionNameLocatorSeparator), sectionName) } diff --git a/machinery/test_helper.go b/machinery/test_helper.go index df80b88..7377656 100644 --- a/machinery/test_helper.go +++ b/machinery/test_helper.go @@ -56,8 +56,8 @@ func (a *Apple) GetNamespace() string { return "" } -func (a *Apple) GetIdentity() string { - return IdentityFromObject(a) +func (a *Apple) GetLocator() string { + return LocatorFromObject(a) } func (a *Apple) GroupVersionKind() schema.GroupVersionKind { @@ -97,8 +97,8 @@ func (o *Orange) GetNamespace() string { return o.Namespace } -func (o *Orange) GetIdentity() string { - return IdentityFromObject(o) +func (o *Orange) GetLocator() string { + return LocatorFromObject(o) } func (o *Orange) GroupVersionKind() schema.GroupVersionKind { @@ -133,8 +133,8 @@ func (b *Banana) GetNamespace() string { return "" } -func (b *Banana) GetIdentity() string { - return IdentityFromObject(b) +func (b *Banana) GetLocator() string { + return LocatorFromObject(b) } func (b *Banana) GroupVersionKind() schema.GroupVersionKind { @@ -204,8 +204,8 @@ func (i *Info) GetName() string { return i.Name } -func (i *Info) GetIdentity() string { - return IdentityFromObject(i) +func (i *Info) GetLocator() string { + return LocatorFromObject(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.GetIdentity() == info.Ref + return obj.GetLocator() == info.Ref }) }, } @@ -234,8 +234,8 @@ type FruitPolicySpec struct { var _ Policy = &FruitPolicy{} -func (p *FruitPolicy) GetIdentity() string { - return IdentityFromObject(p) +func (p *FruitPolicy) GetLocator() string { + return LocatorFromObject(p) } func (p *FruitPolicy) GetTargetRefs() []PolicyTargetReference { @@ -286,8 +286,8 @@ func (t FruitPolicyTargetReference) SetGroupVersionKind(gvk schema.GroupVersionK t.Kind = gvk.Kind } -func (t FruitPolicyTargetReference) GetIdentity() string { - return IdentityFromObject(t) +func (t FruitPolicyTargetReference) GetLocator() string { + return LocatorFromObject(t) } func (t FruitPolicyTargetReference) GetNamespace() string { diff --git a/machinery/topology.go b/machinery/topology.go index e70990e..47dc7c2 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.GetIdentity()] == nil { - policiesByTargetRef[targetRef.GetIdentity()] = make([]Policy, 0) + if policiesByTargetRef[targetRef.GetLocator()] == nil { + policiesByTargetRef[targetRef.GetLocator()] = make([]Policy, 0) } - policiesByTargetRef[targetRef.GetIdentity()] = append(policiesByTargetRef[targetRef.GetIdentity()], policy) + policiesByTargetRef[targetRef.GetLocator()] = append(policiesByTargetRef[targetRef.GetLocator()], policy) } } targetables := lo.Map(o.Targetables, func(t Targetable, _ int) Targetable { - t.SetPolicies(policiesByTargetRef[t.GetIdentity()]) + t.SetPolicies(policiesByTargetRef[t.GetLocator()]) return t }) @@ -113,9 +113,9 @@ func NewTopology(options ...TopologyOptionsFunc) *Topology { return &Topology{ graph: graph, - objects: lo.SliceToMap(o.Objects, associateURL[Object]), - targetables: lo.SliceToMap(targetables, associateURL[Targetable]), - policies: lo.SliceToMap(policies, associateURL[Policy]), + objects: lo.SliceToMap(o.Objects, associateLocator[Object]), + targetables: lo.SliceToMap(targetables, associateLocator[Targetable]), + policies: lo.SliceToMap(policies, associateLocator[Policy]), } } @@ -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.GetIdentity())) + n := graph.Node(string(object.GetLocator())) 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.GetIdentity())) + targetNode, found := graph.FindNodeById(string(targetRef.GetLocator())) if !found { continue } @@ -198,16 +198,16 @@ 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.GetIdentity())) - c, foundChild := graph.FindNodeById(string(child.GetIdentity())) + p, foundParent := graph.FindNodeById(string(parent.GetLocator())) + c, foundChild := graph.FindNodeById(string(child.GetLocator())) if foundParent && foundChild { edge := graph.Edge(p, c) edge.Attr("comment", name) } } -func associateURL[T Object](obj T) (string, T) { - return obj.GetIdentity(), obj +func associateLocator[T Object](obj T) (string, T) { + return obj.GetLocator(), 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.GetIdentity() + return edge.To().ID() == item.GetLocator() }) { 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.GetIdentity()], func(edge dot.Edge, _ int) (T, bool) { + return lo.FilterMap(c.topology.graph.EdgesMap()[item.GetLocator()], 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.GetIdentity() - if visited[currentURL] { + currentLocator := current.GetLocator() + if visited[currentLocator] { return } - path = append(path, c.items[currentURL]) - visited[currentURL] = true - if currentURL == to.GetIdentity() { + path = append(path, c.items[currentLocator]) + visited[currentLocator] = true + if currentLocator == to.GetLocator() { pathCopy := make([]T, len(path)) copy(pathCopy, path) *paths = append(*paths, pathCopy) @@ -321,5 +321,5 @@ func (c *collection[T]) dfs(current, to Object, path []T, paths *[][]T, visited } } path = path[:len(path)-1] - visited[currentURL] = false + visited[currentLocator] = false } diff --git a/machinery/topology_test.go b/machinery/topology_test.go index 6658d3b..ee21db3 100644 --- a/machinery/topology_test.go +++ b/machinery/topology_test.go @@ -42,10 +42,10 @@ func TestTopologyRoots(t *testing.T) { if expected := len(apples); len(roots) != expected { t.Errorf("expected %d roots, got %d", expected, len(roots)) } - rootURLs := lo.Map(roots, MapTargetableToURLFunc) + rootLocators := lo.Map(roots, MapTargetableToLocatorFunc) for _, apple := range apples { - if !lo.Contains(rootURLs, apple.GetIdentity()) { - t.Errorf("expected root %s not found", apple.GetIdentity()) + if !lo.Contains(rootLocators, apple.GetLocator()) { + t.Errorf("expected root %s not found", apple.GetLocator()) } } } @@ -75,21 +75,21 @@ func TestTopologyParents(t *testing.T) { if expected := 2; len(parents) != expected { t.Errorf("expected %d parent, got %d", expected, len(parents)) } - parentURLs := lo.Map(parents, MapTargetableToURLFunc) - if !lo.Contains(parentURLs, apple1.GetIdentity()) { - t.Errorf("expected parent %s not found", apple1.GetIdentity()) + parentLocators := lo.Map(parents, MapTargetableToLocatorFunc) + if !lo.Contains(parentLocators, apple1.GetLocator()) { + t.Errorf("expected parent %s not found", apple1.GetLocator()) } - if !lo.Contains(parentURLs, apple2.GetIdentity()) { - t.Errorf("expected parent %s not found", apple2.GetIdentity()) + if !lo.Contains(parentLocators, apple2.GetLocator()) { + t.Errorf("expected parent %s not found", apple2.GetLocator()) } // orange-2 parents = topology.Targetables().Parents(orange2) if expected := 1; len(parents) != expected { t.Errorf("expected %d parent, got %d", expected, len(parents)) } - parentURLs = lo.Map(parents, MapTargetableToURLFunc) - if !lo.Contains(parentURLs, apple2.GetIdentity()) { - t.Errorf("expected parent %s not found", apple2.GetIdentity()) + parentLocators = lo.Map(parents, MapTargetableToLocatorFunc) + if !lo.Contains(parentLocators, apple2.GetLocator()) { + t.Errorf("expected parent %s not found", apple2.GetLocator()) } } @@ -118,21 +118,21 @@ func TestTopologyChildren(t *testing.T) { if expected := 1; len(children) != expected { t.Errorf("expected %d child, got %d", expected, len(children)) } - childURLs := lo.Map(children, MapTargetableToURLFunc) - if !lo.Contains(childURLs, orange1.GetIdentity()) { - t.Errorf("expected child %s not found", orange1.GetIdentity()) + childLocators := lo.Map(children, MapTargetableToLocatorFunc) + if !lo.Contains(childLocators, orange1.GetLocator()) { + t.Errorf("expected child %s not found", orange1.GetLocator()) } // apple-2 children = topology.Targetables().Children(apple2) if expected := 2; len(children) != expected { t.Errorf("expected %d child, got %d", expected, len(children)) } - childURLs = lo.Map(children, MapTargetableToURLFunc) - if !lo.Contains(childURLs, orange1.GetIdentity()) { - t.Errorf("expected child %s not found", orange1.GetIdentity()) + childLocators = lo.Map(children, MapTargetableToLocatorFunc) + if !lo.Contains(childLocators, orange1.GetLocator()) { + t.Errorf("expected child %s not found", orange1.GetLocator()) } - if !lo.Contains(childURLs, orange2.GetIdentity()) { - t.Errorf("expected child %s not found", orange2.GetIdentity()) + if !lo.Contains(childLocators, orange2.GetLocator()) { + t.Errorf("expected child %s not found", orange2.GetLocator()) } } @@ -208,10 +208,10 @@ func TestTopologyPaths(t *testing.T) { t.Errorf("expected %d paths, got %d", len(tc.expectedPaths), len(paths)) } expectedPaths := lo.Map(tc.expectedPaths, func(expectedPath []Targetable, _ int) string { - return strings.Join(lo.Map(expectedPath, MapTargetableToURLFunc), "→") + return strings.Join(lo.Map(expectedPath, MapTargetableToLocatorFunc), "→") }) for _, path := range paths { - pathString := strings.Join(lo.Map(path, MapTargetableToURLFunc), "→") + pathString := strings.Join(lo.Map(path, MapTargetableToLocatorFunc), "→") if !lo.Contains(expectedPaths, pathString) { t.Errorf("expected path %v not found", pathString) } diff --git a/machinery/types.go b/machinery/types.go index 01658f7..fce2efe 100644 --- a/machinery/types.go +++ b/machinery/types.go @@ -8,19 +8,19 @@ import ( k8stypes "k8s.io/apimachinery/pkg/types" ) -const kindNameURLSeparator = ':' +const kindNameLocatorSeparator = ':' type Object interface { schema.ObjectKind GetNamespace() string GetName() string - GetIdentity() string + GetLocator() string } -func IdentityFromObject(obj Object) string { +func LocatorFromObject(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) + return fmt.Sprintf("%s%s%s", strings.ToLower(obj.GroupVersionKind().GroupKind().String()), string(kindNameLocatorSeparator), name) } func AsObject[T Object](t T, _ int) Object { @@ -39,8 +39,8 @@ type Targetable interface { Policies() []Policy } -func MapTargetableToURLFunc(t Targetable, _ int) string { - return t.GetIdentity() +func MapTargetableToLocatorFunc(t Targetable, _ int) string { + return t.GetLocator() } // Policy targets objects and can be merged with another Policy based on a given MergeStrategy.