Skip to content

Commit 60e0e4f

Browse files
committed
Add ext-auth edge cases test case
updated outputs to what i think is the correct output (not current output) current failing: - for section name http1, the config needs to be disabled on the listener and enabled on the virtualHost. - section-name-gw-extauth should not be present on tls2 filter chain Signed-off-by: Yuval Kohavi <[email protected]>
1 parent 4682e60 commit 60e0e4f

40 files changed

+896
-222
lines changed

api/v1alpha1/shared_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type LocalPolicyTargetReference struct {
1919

2020
// The name of the target resource.
2121
Name gwv1.ObjectName `json:"name"`
22+
23+
// The section name of the target resource.
24+
// +optional
25+
SectionName *gwv1.SectionName `json:"sectionName,omitempty"`
2226
}
2327

2428
// Select the object to attach the policy by Group, Kind, and its labels.

internal/kgateway/extensions2/plugins/trafficpolicy/traffic_policy_plugin.go

Lines changed: 89 additions & 123 deletions
Large diffs are not rendered by default.

internal/kgateway/extensions2/pluginutils/policy.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/kgateway-dev/kgateway/v2/api/v1alpha1"
77
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/ir"
8+
"k8s.io/utils/ptr"
89
)
910

1011
func TargetRefsToPolicyRefs(
@@ -14,9 +15,10 @@ func TargetRefsToPolicyRefs(
1415
refs := make([]ir.PolicyRef, 0, len(targetRefs)+len(targetSelectors))
1516
for _, targetRef := range targetRefs {
1617
refs = append(refs, ir.PolicyRef{
17-
Group: string(targetRef.Group),
18-
Kind: string(targetRef.Kind),
19-
Name: string(targetRef.Name),
18+
Group: string(targetRef.Group),
19+
Kind: string(targetRef.Kind),
20+
Name: string(targetRef.Name),
21+
SectionName: string(ptr.Deref(targetRef.SectionName, "")),
2022
})
2123
}
2224
for _, targetSelector := range targetSelectors {

internal/kgateway/krtcollections/policy.go

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,12 @@ type targetRefIndexKey struct {
305305
Group string
306306
Kind string
307307
Name string
308-
SectionName string
309308
Namespace string
309+
SectionName string
310310
}
311311

312312
func (k targetRefIndexKey) String() string {
313-
return fmt.Sprintf("%s/%s/%s/%s", k.Group, k.Kind, k.Name, k.Namespace)
313+
return fmt.Sprintf("%s/%s/%s/%s/%s", k.Group, k.Kind, k.Name, k.Namespace, k.SectionName)
314314
}
315315

316316
// HTTPRouteSelector is used to lookup HttpRouteIR using one of the following ways:
@@ -529,52 +529,31 @@ func (p *PolicyIndex) getTargetingPoliciesMaybeForBackends(
529529

530530
// no need for ref grants here as target refs are namespace local
531531
refIndexKey := targetRefIndexKey{
532-
Group: targetRef.Group,
533-
Kind: targetRef.Kind,
534-
Name: targetRef.Name,
535-
Namespace: targetRef.Namespace,
532+
Group: targetRef.Group,
533+
Kind: targetRef.Kind,
534+
Name: targetRef.Name,
535+
Namespace: targetRef.Namespace,
536+
SectionName: sectionName,
536537
}
538+
537539
policies := p.fetchByTargetRef(kctx, refIndexKey, onlyBackends)
538-
var sectionNamePolicies []ir.PolicyWrapper
539-
if sectionName != "" {
540-
refIndexKey.SectionName = sectionName
541-
sectionNamePolicies = p.fetchByTargetRef(kctx, refIndexKey, onlyBackends)
542-
}
543540
// Lookup policies that select targetLabels
544541
if len(targetLabels) > 0 {
545542
refIndexKeyByNamespace := targetRefIndexKey{
546-
Group: targetRef.Group,
547-
Kind: targetRef.Kind,
548-
Namespace: targetRef.Namespace,
543+
Group: targetRef.Group,
544+
Kind: targetRef.Kind,
545+
Namespace: targetRef.Namespace,
546+
SectionName: sectionName,
549547
}
550548
policiesByLabel := p.fetchByTargetRefLabels(kctx, refIndexKeyByNamespace, onlyBackends, targetLabels)
551549
policies = append(policies, policiesByLabel...)
552-
var sectionNamePoliciesByLabel []ir.PolicyWrapper
553-
if sectionName != "" {
554-
refIndexKeyByNamespace.SectionName = sectionName
555-
sectionNamePoliciesByLabel = p.fetchByTargetRefLabels(kctx, refIndexKeyByNamespace, onlyBackends, targetLabels)
556-
}
557-
sectionNamePolicies = append(sectionNamePolicies, sectionNamePoliciesByLabel...)
558550
}
559551

560552
for _, p := range policies {
561553
ret = append(ret, ir.PolicyAtt{
562554
Generation: p.Policy.GetGeneration(),
563555
GroupKind: p.GetGroupKind(),
564556
PolicyIr: p.PolicyIR,
565-
PolicyRef: &ir.AttachedPolicyRef{
566-
Group: p.Group,
567-
Kind: p.Kind,
568-
Name: p.Name,
569-
Namespace: p.Namespace,
570-
},
571-
Errors: p.Errors,
572-
})
573-
}
574-
for _, p := range sectionNamePolicies {
575-
ret = append(ret, ir.PolicyAtt{
576-
GroupKind: p.GetGroupKind(),
577-
PolicyIr: p.PolicyIR,
578557
PolicyRef: &ir.AttachedPolicyRef{
579558
Group: p.Group,
580559
Kind: p.Kind,
@@ -585,6 +564,7 @@ func (p *PolicyIndex) getTargetingPoliciesMaybeForBackends(
585564
Errors: p.Errors,
586565
})
587566
}
567+
588568
slices.SortFunc(ret, func(a, b ir.PolicyAtt) int {
589569
return a.PolicyIr.CreationTime().Compare(b.PolicyIr.CreationTime())
590570
})

internal/kgateway/query/httproute.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ func (r *RouteInfo) Clone() *RouteInfo {
100100

101101
// UniqueRouteName returns a unique name for the route based on the route kind, name, namespace,
102102
// and the given indexes.
103-
func (r *RouteInfo) UniqueRouteName(ruleIdx, matchIdx int) string {
104-
return fmt.Sprintf("%s-%s-%s-%d-%d", strings.ToLower(r.GetKind()), r.GetName(), r.GetNamespace(), ruleIdx, matchIdx)
103+
func (r *RouteInfo) UniqueRouteName(ruleIdx, matchIdx int, ruleName string) string {
104+
if ruleName == "" {
105+
return fmt.Sprintf("%s-%s-%s-%d-%d", strings.ToLower(r.GetKind()), r.GetName(), r.GetNamespace(), ruleIdx, matchIdx)
106+
}
107+
return fmt.Sprintf("%s-%s-%s-%d-%d-%s", strings.ToLower(r.GetKind()), r.GetName(), r.GetNamespace(), ruleIdx, matchIdx, ruleName)
105108
}
106109

107110
// GetRouteChain recursively resolves all backends for the given route object.

internal/kgateway/translator/gateway/gateway_translator_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ var _ = DescribeTable("Basic GatewayTranslator Tests",
219219
}
220220
},
221221
}),
222+
Entry(
223+
"TrafficPolicy edge cases",
224+
translatorTestCase{
225+
inputFile: "traffic-policy/extauth.yaml",
226+
outputFile: "traffic-policy/extauth.yaml",
227+
gwNN: types.NamespacedName{
228+
Namespace: "infra",
229+
Name: "example-gateway",
230+
},
231+
}),
222232
Entry(
223233
"tcp gateway with basic routing",
224234
translatorTestCase{

0 commit comments

Comments
 (0)