Skip to content

Commit

Permalink
[backport] gateway2: allow child policies to always set fields unset …
Browse files Browse the repository at this point in the history
…on the parent (#10550)
  • Loading branch information
shashankram authored and kevin-shelaga committed Jan 8, 2025
1 parent 5ea47f2 commit 8967d10
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
14 changes: 14 additions & 0 deletions changelog/v1.18.4/policy-prefixrewrite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/solo-projects/issues/7601
resolvesIssue: false
description: |
When merging parent-child policies, the merging should allow child
policies to augment parent policies such that fields unset on the
parent can be set by the child. There is a bug when using policy
override capability with route delegation that disallows this when
the annotation specifies non-wildcard fields, such that even if
a field is unset by the parent only the fields specified in the
override annotation are merged in - which is incorrect because
the annotation only applies to fields that are being overriden
(set by the parent). This change fixes the bug.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"errors"
"time"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/wrapperspb"
"istio.io/istio/pkg/kube/krt"
Expand Down Expand Up @@ -737,7 +739,7 @@ var _ = Describe("RouteOptionsPlugin", func() {
var _ = DescribeTable("mergeOptionsForRoute",
func(route *gwv1.HTTPRoute, dst, src *v1.RouteOptions, expectedOptions *v1.RouteOptions, expectedResult glooutils.OptionsMergeResult) {
mergedOptions, result := mergeOptionsForRoute(context.TODO(), route, dst, src)
Expect(proto.Equal(mergedOptions, expectedOptions)).To(BeTrue())
Expect(cmp.Diff(mergedOptions, expectedOptions, protocmp.Transform())).To(BeEmpty())
Expect(result).To(Equal(expectedResult))
},
Entry("prefer dst options by default",
Expand Down Expand Up @@ -890,6 +892,40 @@ var _ = DescribeTable("mergeOptionsForRoute",
},
glooutils.OptionsMergedFull,
),
Entry("override and augment dst options with annotation: specific fields",
&gwv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{policyOverrideAnnotation: "faults,timeout"},
},
},
&v1.RouteOptions{
Faults: &faultinjection.RouteFaults{
Abort: &faultinjection.RouteAbort{
HttpStatus: 500,
},
},
Timeout: durationpb.New(5 * time.Second),
},
&v1.RouteOptions{
PrefixRewrite: &wrapperspb.StringValue{Value: "/src"},
Faults: &faultinjection.RouteFaults{
Abort: &faultinjection.RouteAbort{
HttpStatus: 100,
},
},
Timeout: durationpb.New(10 * time.Second),
},
&v1.RouteOptions{
Faults: &faultinjection.RouteFaults{
Abort: &faultinjection.RouteAbort{
HttpStatus: 100,
},
},
PrefixRewrite: &wrapperspb.StringValue{Value: "/src"},
Timeout: durationpb.New(10 * time.Second),
},
glooutils.OptionsMergedFull,
),
)

func routeOption() *solokubev1.RouteOption {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ spec:
- path:
type: PathPrefix
value: /a/1
filters:
- type: URLRewrite
urlRewrite:
path:
replacePrefixMatch: /rewrite/path
type: ReplacePrefixMatch
backendRefs:
- name: svc-a
port: 8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ listeners:
headerManipulation:
requestHeadersToRemove:
- override
prefixRewrite: /rewrite/path
name: httproute-route-a-a-0-0
routeAction:
single:
Expand Down
5 changes: 4 additions & 1 deletion projects/gloo/pkg/utils/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ func MergeRouteOptionsWithOverrides(dst, src *v1.RouteOptions, allowedOverrides
var dstFieldsOverwritten int
for i := range dstValue.NumField() {
dstField, srcField := dstValue.Field(i), srcValue.Field(i)
if overwriteByDefault && !(allowedOverrides.Has(wildcardField) ||
// Allow overrides if the field in dst is unset as merging from src into dst by default
// allows src to augment dst by setting fields unset in dst, hence the override check only
// applies when the field in dst is set: !isEmptyValue(dstField).
if !isEmptyValue(dstField) && overwriteByDefault && !(allowedOverrides.Has(wildcardField) ||
allowedOverrides.Has(strings.ToLower(dstValue.Type().Field(i).Name))) {
continue
}
Expand Down

0 comments on commit 8967d10

Please sign in to comment.