Skip to content

Commit

Permalink
Merge pull request #22 from KevFan/issues/10
Browse files Browse the repository at this point in the history
refactor: GetURL() -> GetLocator()
  • Loading branch information
KevFan authored Sep 2, 2024
2 parents 5445f17 + b9625fd commit e5173b2
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 137 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type MyPolicySpec struct {
TargetRef gwapiv1alpha2.LocalPolicyTargetReference
}

func (p *MyPolicy) GetURL() string {
return machinery.UrlFromObject(p)
func (p *MyPolicy) GetLocator() string {
return machinery.LocatorFromObject(p)
}

func (p *MyPolicy) GetTargetRefs() []machinery.PolicyTargetReference {
Expand Down
4 changes: 2 additions & 2 deletions controller/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) GetLocator() string {
return machinery.LocatorFromObject(o)
}

// ObjectAs casts an Object generically into any kind
Expand Down
4 changes: 2 additions & 2 deletions examples/color_policy/color_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type ColorPolicy struct {

var _ machinery.Policy = &ColorPolicy{}

func (p *ColorPolicy) GetURL() string {
return machinery.UrlFromObject(p)
func (p *ColorPolicy) GetLocator() string {
return machinery.LocatorFromObject(p)
}

func (p *ColorPolicy) GetTargetRefs() []machinery.PolicyTargetReference {
Expand Down
5 changes: 3 additions & 2 deletions examples/json_patch/color_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -19,8 +20,8 @@ type ColorPolicy struct {

var _ machinery.Policy = &ColorPolicy{}

func (p *ColorPolicy) GetURL() string {
return machinery.UrlFromObject(p)
func (p *ColorPolicy) GetLocator() string {
return machinery.LocatorFromObject(p)
}

func (p *ColorPolicy) GetTargetRefs() []machinery.PolicyTargetReference {
Expand Down
4 changes: 2 additions & 2 deletions examples/kuadrant/apis/v1alpha2/dnspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (p *DNSPolicy) GetName() string {
return p.Name
}

func (p *DNSPolicy) GetURL() string {
return machinery.UrlFromObject(p)
func (p *DNSPolicy) GetLocator() string {
return machinery.LocatorFromObject(p)
}

func (p *DNSPolicy) GetTargetRefs() []machinery.PolicyTargetReference {
Expand Down
4 changes: 2 additions & 2 deletions examples/kuadrant/apis/v1alpha2/tlspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (p *TLSPolicy) GetName() string {
return p.Name
}

func (p *TLSPolicy) GetURL() string {
return machinery.UrlFromObject(p)
func (p *TLSPolicy) GetLocator() string {
return machinery.LocatorFromObject(p)
}

func (p *TLSPolicy) GetTargetRefs() []machinery.PolicyTargetReference {
Expand Down
4 changes: 2 additions & 2 deletions examples/kuadrant/apis/v1beta3/authpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (p *AuthPolicy) GetName() string {
return p.Name
}

func (p *AuthPolicy) GetURL() string {
return machinery.UrlFromObject(p)
func (p *AuthPolicy) GetLocator() string {
return machinery.LocatorFromObject(p)
}

func (p *AuthPolicy) GetTargetRefs() []machinery.PolicyTargetReference {
Expand Down
4 changes: 2 additions & 2 deletions examples/kuadrant/apis/v1beta3/ratelimitpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (p *RateLimitPolicy) GetName() string {
return p.Name
}

func (p *RateLimitPolicy) GetURL() string {
return machinery.UrlFromObject(p)
func (p *RateLimitPolicy) GetLocator() string {
return machinery.LocatorFromObject(p)
}

func (p *RateLimitPolicy) GetTargetRefs() []machinery.PolicyTargetReference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/kuadrant/reconcilers/envoy_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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].GetURL() == gateway.GetURL() && 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"
})
Expand Down
4 changes: 2 additions & 2 deletions examples/kuadrant/reconcilers/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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].GetURL() == gateway.GetURL() && 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"
})
Expand Down
12 changes: 6 additions & 6 deletions machinery/core_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Namespace struct {

var _ Targetable = &Namespace{}

func (n *Namespace) GetURL() string {
return UrlFromObject(n)
func (n *Namespace) GetLocator() string {
return LocatorFromObject(n)
}

func (n *Namespace) SetPolicies(policies []Policy) {
Expand All @@ -42,8 +42,8 @@ type Service struct {

var _ Targetable = &Service{}

func (s *Service) GetURL() string {
return UrlFromObject(s)
func (s *Service) GetLocator() string {
return LocatorFromObject(s)
}

func (s *Service) SetPolicies(policies []Policy) {
Expand Down Expand Up @@ -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) GetLocator() string {
return namespacedSectionName(LocatorFromObject(p.Service), gwapiv1.SectionName(p.Name))
}

func (p *ServicePort) GetNamespace() string {
Expand Down
4 changes: 2 additions & 2 deletions machinery/gateway_api_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ type TestPolicySpec struct {

var _ Policy = &TestPolicy{}

func (p *TestPolicy) GetURL() string {
return UrlFromObject(p)
func (p *TestPolicy) GetLocator() string {
return LocatorFromObject(p)
}

func (p *TestPolicy) GetTargetRefs() []PolicyTargetReference {
Expand Down
4 changes: 2 additions & 2 deletions machinery/gateway_api_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,15 +638,15 @@ 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.GetLocator() == gateway.GetLocator() && l.Name == *parentRef.SectionName
})
if !ok {
return nil
}
return []Object{listener}
}
return lo.FilterMap(listeners, func(l *Listener, _ int) (Object, bool) {
return l, l.Gateway.GetURL() == gateway.GetURL()
return l, l.Gateway.GetLocator() == gateway.GetLocator()
})
}
}
Expand Down
Loading

0 comments on commit e5173b2

Please sign in to comment.