Skip to content

Commit

Permalink
more descriptive wasm actionset names
Browse files Browse the repository at this point in the history
Signed-off-by: Guilherme Cassolato <[email protected]>
  • Loading branch information
guicassolato committed Oct 17, 2024
1 parent 5f1109a commit 4042873
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
9 changes: 4 additions & 5 deletions api/v1/merge_strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ limitations under the License.
package v1

import (
"crypto/sha256"
"encoding/hex"
"sort"
"strings"

"github.com/samber/lo"
"k8s.io/apimachinery/pkg/runtime"
k8stypes "k8s.io/apimachinery/pkg/types"

"github.com/kuadrant/policy-machinery/controller"
"github.com/kuadrant/policy-machinery/machinery"
Expand Down Expand Up @@ -196,7 +195,7 @@ func PoliciesInPath(path []machinery.Targetable, predicate func(machinery.Policy
}

func PathID(path []machinery.Targetable) string {
s := strings.Join(lo.Map(path, machinery.MapTargetableToLocatorFunc), ">")
hash := sha256.Sum256([]byte(s))
return hex.EncodeToString(hash[:8])
return strings.Join(lo.Map(path, func(t machinery.Targetable, _ int) string {
return strings.TrimPrefix(k8stypes.NamespacedName{Namespace: t.GetNamespace(), Name: t.GetName()}.String(), string(k8stypes.Separator))
}), "|")
}
4 changes: 2 additions & 2 deletions pkg/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func BuildActionSetsForPath(pathID string, path []machinery.Targetable, policyRu
return action, true
})

return lo.FlatMap(kuadrantgatewayapi.HostnamesFromListenerAndHTTPRoute(listener.Listener, httpRoute.HTTPRoute), func(hostname gatewayapiv1.Hostname, i int) []kuadrantgatewayapi.HTTPRouteMatchConfig {
return lo.FlatMap(kuadrantgatewayapi.HostnamesFromListenerAndHTTPRoute(listener.Listener, httpRoute.HTTPRoute), func(hostname gatewayapiv1.Hostname, _ int) []kuadrantgatewayapi.HTTPRouteMatchConfig {
return lo.Map(httpRouteRule.Matches, func(httpRouteMatch gatewayapiv1.HTTPRouteMatch, j int) kuadrantgatewayapi.HTTPRouteMatchConfig {
actionSet := ActionSet{
Name: fmt.Sprintf("%d-%s-%d", i, pathID, j),
Name: fmt.Sprintf("%s|match-%d|", pathID, j+1, hostname),

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Unit Tests (1.22.x, ubuntu-latest)

fmt.Sprintf call needs 2 args but has 3 args

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Lint

printf: fmt.Sprintf call needs 2 args but has 3 args (govet)

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Integration Tests for kuadrant-operator/tests/bare_k8s

fmt.Sprintf call needs 2 args but has 3 args

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Integration Tests for kuadrant-operator/tests/[gatewayapi-provider] (istio)

fmt.Sprintf call needs 2 args but has 3 args

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Integration Tests for kuadrant-operator/controllers (istio, istioctl)

fmt.Sprintf call needs 2 args but has 3 args

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Integration Tests for kuadrant-operator/tests/gatewayapi

fmt.Sprintf call needs 2 args but has 3 args

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Integration Tests for kuadrant-operator/controllers (envoygateway)

fmt.Sprintf call needs 2 args but has 3 args

Check failure on line 65 in pkg/wasm/utils.go

View workflow job for this annotation

GitHub Actions / Integration Tests for kuadrant-operator/tests/[gatewayapi-provider] (envoygateway)

fmt.Sprintf call needs 2 args but has 3 args
Actions: actions,
}
routeRuleConditions := RouteRuleConditions{
Expand Down
4 changes: 2 additions & 2 deletions tests/envoygateway/extension_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var _ = Describe("wasm controller", func() {
mHTTPRoute,
&machinery.HTTPRouteRule{HTTPRouteRule: &gwRoute.Spec.Rules[0], HTTPRoute: mHTTPRoute},
})
actionSetName = fmt.Sprintf("%d-%s-%d", 0, pathID, 0) // Hostname: 0, HTTPRouteMatch: 0
actionSetName = fmt.Sprintf("%s|match-%d|", pathID, 1, gwRoute.Spec.Hostnames[0])

gwPolicy = policyFactory(func(policy *kuadrantv1beta3.RateLimitPolicy) {
policy.Name = "gw"
Expand Down Expand Up @@ -272,7 +272,7 @@ var _ = Describe("wasm controller", func() {
mHTTPRoute,
&machinery.HTTPRouteRule{HTTPRouteRule: &gwRoute.Spec.Rules[0], HTTPRoute: mHTTPRoute},
})
actionSetName = fmt.Sprintf("%d-%s-%d", 0, pathID, 0) // Hostname: 0, HTTPRouteMatch: 0
actionSetName = fmt.Sprintf("%s|match-%d|", pathID, 1, gwRoute.Spec.Hostnames[0])

routePolicy = policyFactory(func(policy *kuadrantv1beta3.RateLimitPolicy) {
policy.Name = "route"
Expand Down
46 changes: 23 additions & 23 deletions tests/istio/extension_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -310,7 +310,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
// GET api.toystore.io/toys*
actionSet := existingWASMConfig.ActionSets[0]
pathID := kuadrantv1.PathID(append(basePath, httpRouteRuleToys))
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%d-%s-%d", 1, pathID, 0))) // Hostname: 1, HTTPRouteMatch: 0
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%s|match-%d|", pathID, 1, "api.toystore.io")))
Expect(actionSet.RouteRuleConditions.Hostnames).To(Equal([]string{"api.toystore.io"}))
Expect(actionSet.RouteRuleConditions.Matches).To(ContainElements(
wasm.Predicate{
Expand Down Expand Up @@ -373,7 +373,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
// POST api.toystore.io/toys*
actionSet = existingWASMConfig.ActionSets[1]
pathID = kuadrantv1.PathID(append(basePath, httpRouteRuleToys))
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%d-%s-%d", 1, pathID, 1))) // Hostname: 1, HTTPRouteMatch: 1
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%s|match-%d|", pathID, 2, "api.toystore.io")))
Expect(actionSet.RouteRuleConditions.Hostnames).To(Equal([]string{"api.toystore.io"}))
Expect(actionSet.RouteRuleConditions.Matches).To(ContainElements(
wasm.Predicate{
Expand Down Expand Up @@ -436,7 +436,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
// api.toystore.io/assets*
actionSet = existingWASMConfig.ActionSets[2]
pathID = kuadrantv1.PathID(append(basePath, httpRouteRuleAssets))
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%d-%s-%d", 1, pathID, 2))) // Hostname: 1, HTTPRouteMatch: 2
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%s|match-%d|", pathID, 3, "api.toystore.io")))
Expect(actionSet.RouteRuleConditions.Hostnames).To(Equal([]string{"api.toystore.io"}))
Expect(actionSet.RouteRuleConditions.Matches).To(ContainElements(
wasm.Predicate{
Expand Down Expand Up @@ -494,7 +494,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
// GET *.toystore.acme.com/toys*
actionSet = existingWASMConfig.ActionSets[3]
pathID = kuadrantv1.PathID(append(basePath, httpRouteRuleToys))
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%d-%s-%d", 0, pathID, 0))) // Hostname: 0, HTTPRouteMatch: 0
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%s|match-%d|", pathID, 1, "*.toystore.acme.com")))
Expect(actionSet.RouteRuleConditions.Hostnames).To(Equal([]string{"*.toystore.acme.com"}))
Expect(actionSet.RouteRuleConditions.Matches).To(ContainElements(
wasm.Predicate{
Expand Down Expand Up @@ -557,7 +557,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
// POST *.toystore.acme.com/toys*
actionSet = existingWASMConfig.ActionSets[4]
pathID = kuadrantv1.PathID(append(basePath, httpRouteRuleToys))
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%d-%s-%d", 0, pathID, 1))) // Hostname: 0, HTTPRouteMatch: 1
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%s|match-%d|", pathID, 2, "*.toystore.acme.com")))
Expect(actionSet.RouteRuleConditions.Hostnames).To(Equal([]string{"*.toystore.acme.com"}))
Expect(actionSet.RouteRuleConditions.Matches).To(ContainElements(
wasm.Predicate{
Expand Down Expand Up @@ -620,7 +620,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
// *.toystore.acme.com/assets*
actionSet = existingWASMConfig.ActionSets[5]
pathID = kuadrantv1.PathID(append(basePath, httpRouteRuleAssets))
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%d-%s-%d", 0, pathID, 2))) // Hostname: 0, HTTPRouteMatch: 2
Expect(actionSet.Name).To(Equal(fmt.Sprintf("%s|match-%d|", pathID, 3, "*.toystore.acme.com")))
Expect(actionSet.RouteRuleConditions.Hostnames).To(Equal([]string{"*.toystore.acme.com"}))
Expect(actionSet.RouteRuleConditions.Matches).To(ContainElements(
wasm.Predicate{
Expand Down Expand Up @@ -746,7 +746,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -970,9 +970,9 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*"},
Hostnames: []string{"*.example.com"},
Matches: []wasm.Predicate{
{
Selector: "request.method",
Expand Down Expand Up @@ -1183,7 +1183,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -1314,7 +1314,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -1517,7 +1517,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.a.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.a.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -1611,7 +1611,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.b.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.b.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -1789,9 +1789,9 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.a.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*"},
Hostnames: []string{"*.a.example.com"},
Matches: []wasm.Predicate{
{
Selector: "request.method",
Expand Down Expand Up @@ -1901,7 +1901,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.a.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.a.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -2115,7 +2115,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.a.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.a.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -2224,7 +2224,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0), // Route A affected by RLP 1 -> Route A
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, "*.a.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*.a.example.com"},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -2258,9 +2258,9 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
},
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID_B, 0), // Route B affected by RLP 1 -> Gateway
Name: fmt.Sprintf("%s|match-%d|", pathID_B, 1, "*.b.example.com"),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{"*"},
Hostnames: []string{"*.b.example.com"},
Matches: []wasm.Predicate{
{
Selector: "request.method",
Expand Down Expand Up @@ -2400,7 +2400,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, gwHostname),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{gwHostname},
Matches: []wasm.Predicate{
Expand Down Expand Up @@ -2479,7 +2479,7 @@ var _ = Describe("Rate Limiting WasmPlugin controller", func() {
},
ActionSets: []wasm.ActionSet{
{
Name: fmt.Sprintf("%d-%s-%d", 0, pathID, 0),
Name: fmt.Sprintf("%s|match-%d|", pathID, 1, hostname),
RouteRuleConditions: wasm.RouteRuleConditions{
Hostnames: []string{hostname},
Matches: []wasm.Predicate{
Expand Down

0 comments on commit 4042873

Please sign in to comment.