Skip to content

Commit

Permalink
Use PolicySettings to express what Policy is in effect
Browse files Browse the repository at this point in the history
Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
  • Loading branch information
sunjayBhatia committed Nov 11, 2022
1 parent fa7282d commit ff49ae7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
57 changes: 52 additions & 5 deletions internal/dag/gatewayapi_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
package dag

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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 {
Expand All @@ -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.
}
Expand All @@ -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.
}
Expand All @@ -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,
})

}
Expand Down

0 comments on commit ff49ae7

Please sign in to comment.