Skip to content

Commit e60aba2

Browse files
AlinsRanronething
authored andcommitted
fix: route names with the same prefix were mistakenly deleted (#2472)
1 parent a5c4fb7 commit e60aba2

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

internal/provider/adc/cache/indexer.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ type LabelIndexer struct {
5656
GetLabels func(obj any) map[string]string
5757
}
5858

59+
// ref: https://pkg.go.dev/github.com/hashicorp/go-memdb#Txn.Get
60+
// by adding suffixes to avoid prefix matching
61+
func (emi *LabelIndexer) genKey(labelValues []string) []byte {
62+
return []byte(strings.Join(labelValues, "/") + "\x00")
63+
}
64+
5965
func (emi *LabelIndexer) FromObject(obj any) (bool, []byte, error) {
6066
labels := emi.GetLabels(obj)
6167
var labelValues []string
@@ -69,7 +75,7 @@ func (emi *LabelIndexer) FromObject(obj any) (bool, []byte, error) {
6975
return false, nil, nil
7076
}
7177

72-
return true, []byte(strings.Join(labelValues, "/")), nil
78+
return true, emi.genKey(labelValues), nil
7379
}
7480

7581
func (emi *LabelIndexer) FromArgs(args ...any) ([]byte, error) {
@@ -86,5 +92,5 @@ func (emi *LabelIndexer) FromArgs(args ...any) ([]byte, error) {
8692
labelValues = append(labelValues, value)
8793
}
8894

89-
return []byte(strings.Join(labelValues, "/")), nil
95+
return emi.genKey(labelValues), nil
9096
}

test/e2e/apisix/route.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,42 @@ spec:
374374
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin-service-e2e-test"}, new(apiv2.ApisixUpstream), apisixUpstreamSpec1)
375375
Eventually(request).WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
376376
})
377+
378+
It("Multiple ApisixRoute with same prefix name", func() {
379+
const apisixRouteSpec = `
380+
apiVersion: apisix.apache.org/v2
381+
kind: ApisixRoute
382+
metadata:
383+
name: %s
384+
spec:
385+
ingressClassName: apisix
386+
http:
387+
- name: rule0
388+
match:
389+
hosts:
390+
- %s
391+
paths:
392+
- /*
393+
backends:
394+
- serviceName: httpbin-service-e2e-test
395+
servicePort: 80
396+
`
397+
By("apply ApisixRoute")
398+
var apisixRoute apiv2.ApisixRoute
399+
for _, id := range []string{"11111", "1111", "111", "11", "1"} {
400+
name := fmt.Sprintf("route-%s", id)
401+
host := fmt.Sprintf("httpbin-%s", id)
402+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: name}, &apisixRoute, fmt.Sprintf(apisixRouteSpec, name, host))
403+
}
404+
405+
By("verify ApisixRoute works")
406+
for _, id := range []string{"1", "11", "111", "1111", "11111"} {
407+
host := fmt.Sprintf("httpbin-%s", id)
408+
Eventually(func() int {
409+
return s.NewAPISIXClient().GET("/get").WithHost(host).Expect().Raw().StatusCode
410+
}).WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
411+
}
412+
})
377413
})
378414

379415
Context("Test ApisixRoute reference ApisixUpstream", func() {

0 commit comments

Comments
 (0)