Skip to content

Commit 63352d6

Browse files
committed
fix: scope BackendTrafficPolicy attachment by targetRef group/kind
Matching only by name allowed a policy to attach across resource kinds when names collide (e.g. a ServiceImport-targeted policy attaching to a Service backend). Validate the targetRef group/kind against the backend ref (applying Gateway API defaults: empty group, Service kind) before matching.
1 parent 3c78cad commit 63352d6

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

internal/adc/translator/httproute_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,25 @@ func TestAttachBackendTrafficPolicyToUpstreamSectionName(t *testing.T) {
401401
},
402402
wantScheme: apiv2.SchemeHTTPS,
403403
},
404+
{
405+
name: "targetRef kind mismatch does not attach to a same-named service",
406+
ref: newRef(webPort),
407+
policies: map[types.NamespacedName]*v1alpha1.BackendTrafficPolicy{
408+
{Namespace: namespace, Name: "p"}: {
409+
ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: namespace},
410+
Spec: v1alpha1.BackendTrafficPolicySpec{
411+
TargetRefs: []v1alpha1.BackendPolicyTargetReferenceWithSectionName{{
412+
LocalPolicyTargetReference: gatewayv1alpha2.LocalPolicyTargetReference{
413+
Name: gatewayv1alpha2.ObjectName(serviceName),
414+
Kind: gatewayv1alpha2.Kind("ServiceImport"),
415+
},
416+
}},
417+
Scheme: apiv2.SchemeHTTPS,
418+
},
419+
},
420+
},
421+
wantScheme: "",
422+
},
404423
}
405424

406425
translator := NewTranslator(logr.Discard())

internal/adc/translator/policies.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
adctypes "github.com/apache/apisix-ingress-controller/api/adc"
3030
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3131
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
32+
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
3233
)
3334

3435
func convertBackendRef(namespace, name, kind string) gatewayv1.BackendRef {
@@ -43,6 +44,17 @@ func (t *Translator) AttachBackendTrafficPolicyToUpstream(ref gatewayv1.BackendR
4344
if len(policies) == 0 {
4445
return
4546
}
47+
// Resolve the backend ref group/kind, applying the Gateway API defaults
48+
// (empty group = core, Service kind) so a targetRef is only matched against
49+
// a backend of the same resource type.
50+
refGroup := ""
51+
if ref.Group != nil {
52+
refGroup = string(*ref.Group)
53+
}
54+
refKind := internaltypes.KindService
55+
if ref.Kind != nil {
56+
refKind = string(*ref.Kind)
57+
}
4658
// A targetRef with sectionName scopes the policy to a specific Service port
4759
// (matched by port name). It takes precedence over a whole-Service targetRef
4860
// (no sectionName) that matches the same backend.
@@ -55,6 +67,12 @@ func (t *Translator) AttachBackendTrafficPolicyToUpstream(ref gatewayv1.BackendR
5567
if ref.Name != targetRef.Name {
5668
continue
5769
}
70+
if targetRef.Group != "" && string(targetRef.Group) != refGroup {
71+
continue
72+
}
73+
if targetRef.Kind != "" && string(targetRef.Kind) != refKind {
74+
continue
75+
}
5876
if targetRef.SectionName != nil && *targetRef.SectionName != "" {
5977
if backendRefMatchesSectionName(ref, po.Namespace, string(*targetRef.SectionName), services) {
6078
specificPolicy = po

0 commit comments

Comments
 (0)