Skip to content

Commit 4745958

Browse files
authored
fix: route names with the same prefix were mistakenly deleted (#2472)
1 parent 2b9b787 commit 4745958

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
@@ -356,6 +356,42 @@ spec:
356356
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin-service-e2e-test"}, new(apiv2.ApisixUpstream), apisixUpstreamSpec1)
357357
Eventually(request).WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
358358
})
359+
360+
It("Multiple ApisixRoute with same prefix name", func() {
361+
const apisixRouteSpec = `
362+
apiVersion: apisix.apache.org/v2
363+
kind: ApisixRoute
364+
metadata:
365+
name: %s
366+
spec:
367+
ingressClassName: apisix
368+
http:
369+
- name: rule0
370+
match:
371+
hosts:
372+
- %s
373+
paths:
374+
- /*
375+
backends:
376+
- serviceName: httpbin-service-e2e-test
377+
servicePort: 80
378+
`
379+
By("apply ApisixRoute")
380+
var apisixRoute apiv2.ApisixRoute
381+
for _, id := range []string{"11111", "1111", "111", "11", "1"} {
382+
name := fmt.Sprintf("route-%s", id)
383+
host := fmt.Sprintf("httpbin-%s", id)
384+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: name}, &apisixRoute, fmt.Sprintf(apisixRouteSpec, name, host))
385+
}
386+
387+
By("verify ApisixRoute works")
388+
for _, id := range []string{"1", "11", "111", "1111", "11111"} {
389+
host := fmt.Sprintf("httpbin-%s", id)
390+
Eventually(func() int {
391+
return s.NewAPISIXClient().GET("/get").WithHost(host).Expect().Raw().StatusCode
392+
}).WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
393+
}
394+
})
359395
})
360396

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

0 commit comments

Comments
 (0)