From ff49ae7edaf7f8b705f8a3d5f95e8a6b39f286f9 Mon Sep 17 00:00:00 2001 From: Sunjay Bhatia Date: Fri, 11 Nov 2022 01:57:48 +0000 Subject: [PATCH] Use PolicySettings to express what Policy is in effect Signed-off-by: Sunjay Bhatia --- go.mod | 2 +- go.sum | 4 +- internal/dag/gatewayapi_processor.go | 57 +++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 38acc06cfae..ac0fb2d3faf 100644 --- a/go.mod +++ b/go.mod @@ -45,4 +45,4 @@ require ( sigs.k8s.io/kustomize/kyaml v0.10.17 ) -replace sigs.k8s.io/gateway-api => github.com/sunjayBhatia/gateway-api v0.0.0-20221111001054-0cf3fa56af68 +replace sigs.k8s.io/gateway-api => github.com/sunjayBhatia/gateway-api v0.0.0-20221111013255-20e2f70ba9cd diff --git a/go.sum b/go.sum index 9606f128f6c..8fb543ce5f3 100644 --- a/go.sum +++ b/go.sum @@ -1100,8 +1100,8 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= -github.com/sunjayBhatia/gateway-api v0.0.0-20221111001054-0cf3fa56af68 h1:VXb1UC0933STso1KO/97s6ZOzsxqtedK7eH8Rwd4kIg= -github.com/sunjayBhatia/gateway-api v0.0.0-20221111001054-0cf3fa56af68/go.mod h1:x0AP6gugkFV8fC/oTlnOMU0pnmuzIR8LfIPRVUjxSqA= +github.com/sunjayBhatia/gateway-api v0.0.0-20221111013255-20e2f70ba9cd h1:vTnbx8oOVZTFPq6rEDZxgArUACu8nim4Vs6uLZ7pS/w= +github.com/sunjayBhatia/gateway-api v0.0.0-20221111013255-20e2f70ba9cd/go.mod h1:x0AP6gugkFV8fC/oTlnOMU0pnmuzIR8LfIPRVUjxSqA= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/internal/dag/gatewayapi_processor.go b/internal/dag/gatewayapi_processor.go index 22443e35c5a..37c0a248ea3 100644 --- a/internal/dag/gatewayapi_processor.go +++ b/internal/dag/gatewayapi_processor.go @@ -14,9 +14,9 @@ package dag import ( - "encoding/json" "fmt" "net/http" + "strconv" "strings" "time" @@ -1115,6 +1115,12 @@ func (p *GatewayAPIProcessor) computeHTTPRoute(route *gatewayapi_v1beta1.HTTPRou // Note: Using this at the moment as a hack so we can use the existing conversion helpers. finalLocalRLP := new(contour_api_v1.LocalRateLimitPolicy) + type policyValue struct { + policyName string + value string + } + fields := map[string]policyValue{} + // Cycle through defaults first, and apply them on top of each other in // forwards list order. for _, rlp := range allRLPs { @@ -1124,15 +1130,31 @@ func (p *GatewayAPIProcessor) computeHTTPRoute(route *gatewayapi_v1beta1.HTTPRou if rlp.Spec.Default.Local.Requests != 0 { finalLocalRLP.Requests = rlp.Spec.Default.Local.Requests + fields["local.requests"] = policyValue{ + policyName: rlp.Name, + value: strconv.FormatUint(uint64(rlp.Spec.Default.Local.Requests), 10), + } } if rlp.Spec.Default.Local.Unit != "" { finalLocalRLP.Unit = rlp.Spec.Default.Local.Unit + fields["local.unit"] = policyValue{ + policyName: rlp.Name, + value: rlp.Spec.Default.Local.Unit, + } } if rlp.Spec.Default.Local.Burst != 0 { finalLocalRLP.Burst = rlp.Spec.Default.Local.Burst + fields["local.burst"] = policyValue{ + policyName: rlp.Name, + value: strconv.FormatUint(uint64(rlp.Spec.Default.Local.Burst), 10), + } } if rlp.Spec.Default.Local.ResponseStatusCode != 0 { finalLocalRLP.ResponseStatusCode = rlp.Spec.Default.Local.ResponseStatusCode + fields["local.responseStatusCode"] = policyValue{ + policyName: rlp.Name, + value: strconv.FormatUint(uint64(rlp.Spec.Default.Local.ResponseStatusCode), 10), + } } // TODO: skipped headers for now. } @@ -1148,15 +1170,31 @@ func (p *GatewayAPIProcessor) computeHTTPRoute(route *gatewayapi_v1beta1.HTTPRou if rlp.Spec.Override.Local.Requests != 0 { finalLocalRLP.Requests = rlp.Spec.Override.Local.Requests + fields["local.requests"] = policyValue{ + policyName: rlp.Name, + value: strconv.FormatUint(uint64(rlp.Spec.Override.Local.Requests), 10), + } } if rlp.Spec.Override.Local.Unit != "" { finalLocalRLP.Unit = rlp.Spec.Override.Local.Unit + fields["local.unit"] = policyValue{ + policyName: rlp.Name, + value: rlp.Spec.Override.Local.Unit, + } } if rlp.Spec.Override.Local.Burst != 0 { finalLocalRLP.Burst = rlp.Spec.Override.Local.Burst + fields["local.burst"] = policyValue{ + policyName: rlp.Name, + value: strconv.FormatUint(uint64(rlp.Spec.Override.Local.Burst), 10), + } } if rlp.Spec.Override.Local.ResponseStatusCode != 0 { finalLocalRLP.ResponseStatusCode = rlp.Spec.Override.Local.ResponseStatusCode + fields["local.responseStatusCode"] = policyValue{ + policyName: rlp.Name, + value: strconv.FormatUint(uint64(rlp.Spec.Override.Local.ResponseStatusCode), 10), + } } // TODO: skipped headers for now. } @@ -1172,11 +1210,20 @@ func (p *GatewayAPIProcessor) computeHTTPRoute(route *gatewayapi_v1beta1.HTTPRou p.WithField("ratelimitpolicy", finalLocalRLP).Info("about to add effective policy") // Add EffectivePolicyConfiguration to Status. - policyContent, _ := json.Marshal(finalLocalRLP) + policySettings := []gatewayapi_v1beta1.PolicySetting{} + for field, value := range fields { + if value.value != "" { + policySettings = append(policySettings, gatewayapi_v1beta1.PolicySetting{ + Field: field, + Value: value.value, + PolicyName: value.policyName, + }) + } + } routeAccessor.AddEffectivePolicyConfig(gatewayapi_v1beta1.RouteEffectivePolicyConfiguration{ - PolicyType: "projectcontour.io/RateLimitPolicy", - SectionName: gatewayapi_v1beta1.SectionName("unsupportedfornow"), - PolicyValue: string(policyContent), + PolicyType: "projectcontour.io/RateLimitPolicy", + SectionName: gatewayapi_v1beta1.SectionName("unsupportedfornow"), + PolicySettings: policySettings, }) }