Skip to content

Commit

Permalink
refactor: common.NamespacedNameFromLocator func
Browse files Browse the repository at this point in the history
Signed-off-by: Guilherme Cassolato <[email protected]>
  • Loading branch information
guicassolato committed Oct 22, 2024
1 parent 7d83a8c commit fa62eb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 3 additions & 7 deletions controllers/ratelimitpolicy_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"slices"
"strings"
"sync"

envoygatewayv1alpha1 "github.com/envoyproxy/gateway/api/v1alpha1"
Expand Down Expand Up @@ -147,12 +146,9 @@ func (r *rateLimitPolicyStatusUpdater) enforcedCondition(policy *kuadrantv1beta3
return kuadrant.EnforcedCondition(policy, kuadrant.NewErrNoRoutes(policyKind), false)
}
// all rules of the policy have been overridden by at least one other policy
overridingPoliciesKeys := lo.FilterMap(lo.Uniq(lo.Flatten(lo.Values(overridingPolicies))), func(locator string, _ int) (k8stypes.NamespacedName, bool) {
if locator == "" {
return k8stypes.NamespacedName{}, false
}
namespacedName := strings.SplitN(strings.TrimPrefix(locator, fmt.Sprintf("%s:", strings.ToLower(policy.GroupVersionKind().GroupKind().String()))), string(k8stypes.Separator), 2) // TODO: machinery.NamespacedNameFromLocator(locator)
return k8stypes.NamespacedName{Namespace: namespacedName[0], Name: namespacedName[1]}, true
overridingPoliciesKeys := lo.FilterMap(lo.Uniq(lo.Flatten(lo.Values(overridingPolicies))), func(policyLocator string, _ int) (k8stypes.NamespacedName, bool) {
policyKey, err := common.NamespacedNameFromLocator(policyLocator)
return policyKey, err == nil
})
return kuadrant.EnforcedCondition(policy, kuadrant.NewErrOverridden(policyKind, overridingPoliciesKeys), false)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/common/policy_machinery_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package common

import (
"fmt"
"strings"

"github.com/kuadrant/policy-machinery/machinery"
"github.com/samber/lo"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"

Expand Down Expand Up @@ -94,3 +96,16 @@ func ObjectsInRequestPath(path []machinery.Targetable) (*machinery.GatewayClass,

return gatewayClass, gateway, listener, httpRoute, httpRouteRule, nil
}

// NamespacedNameFromLocator returns a k8s namespaced name from a Policy Machinery object locator
func NamespacedNameFromLocator(locator string) (k8stypes.NamespacedName, error) {
parts := strings.SplitN(locator, ":", 2) // <groupKind>:<namespacedName>
if len(parts) != 2 {
return k8stypes.NamespacedName{}, fmt.Errorf("invalid locator: %s", locator)
}
namespacedName := strings.SplitN(parts[1], string(k8stypes.Separator), 2)
if len(namespacedName) == 1 {
return k8stypes.NamespacedName{Name: namespacedName[0]}, nil
}
return k8stypes.NamespacedName{Namespace: namespacedName[0], Name: namespacedName[1]}, nil
}

0 comments on commit fa62eb0

Please sign in to comment.