fix svc long name (#9245)
Signed-off-by: tombokombo <tombo@sysart.tech> Signed-off-by: tombokombo <tombo@sysart.tech>
This commit is contained in:
parent
3c32413e30
commit
490ecffc52
5 changed files with 115 additions and 1 deletions
|
|
@ -21,6 +21,7 @@ import (
|
|||
"strings"
|
||||
|
||||
discoveryv1 "k8s.io/api/discovery/v1"
|
||||
apiNames "k8s.io/apiserver/pkg/storage/names"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
|
|
@ -32,9 +33,21 @@ type EndpointSliceLister struct {
|
|||
// MatchByKey returns the EndpointsSlices of the Service matching key in the local Endpoint Store.
|
||||
func (s *EndpointSliceLister) MatchByKey(key string) ([]*discoveryv1.EndpointSlice, error) {
|
||||
var eps []*discoveryv1.EndpointSlice
|
||||
keyNsLen := strings.Index(key, "/")
|
||||
if keyNsLen < -1 {
|
||||
keyNsLen = 0
|
||||
} else {
|
||||
// count '/' char
|
||||
keyNsLen += 1
|
||||
}
|
||||
// filter endpointSlices owned by svc
|
||||
for _, listKey := range s.ListKeys() {
|
||||
if !strings.HasPrefix(listKey, key) {
|
||||
if len(key) < (apiNames.MaxGeneratedNameLength+keyNsLen) && !strings.HasPrefix(listKey, key) {
|
||||
continue
|
||||
}
|
||||
// generated endpointslices names has truncated svc name as prefix when svc name is too long, we compare only non truncated part
|
||||
// https://github.com/kubernetes/ingress-nginx/issues/9240
|
||||
if len(key) >= (apiNames.MaxGeneratedNameLength+keyNsLen) && !strings.HasPrefix(listKey, key[:apiNames.MaxGeneratedNameLength+keyNsLen-1]) {
|
||||
continue
|
||||
}
|
||||
epss, exists, err := s.GetByKey(listKey)
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
discoveryv1 "k8s.io/api/discovery/v1"
|
||||
|
|
@ -91,4 +92,42 @@ func TestEndpointSliceLister(t *testing.T) {
|
|||
t.Errorf("expected %v, error, got %v", endpointSlice.GetName(), eps[0].GetName())
|
||||
}
|
||||
})
|
||||
t.Run("svc long name", func(t *testing.T) {
|
||||
el := newEndpointSliceLister(t)
|
||||
ns := "namespace"
|
||||
ns2 := "another-ns"
|
||||
svcName := "test-backend-http-test-http-test-http-test-http-test-http-truncated"
|
||||
svcName2 := "another-long-svc-name-for-test-inhttp-test-http-test-http-truncated"
|
||||
key := fmt.Sprintf("%s/%s", ns, svcName)
|
||||
endpointSlice := &discoveryv1.EndpointSlice{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: ns,
|
||||
Name: "test-backend-http-test-http-test-http-test-http-test-http-bar88",
|
||||
Labels: map[string]string{
|
||||
discoveryv1.LabelServiceName: svcName,
|
||||
},
|
||||
},
|
||||
}
|
||||
el.Add(endpointSlice)
|
||||
endpointSlice2 := &discoveryv1.EndpointSlice{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: ns2,
|
||||
Name: "another-long-svc-name-for-test-inhttp-test-http-test-http-bar88",
|
||||
Labels: map[string]string{
|
||||
discoveryv1.LabelServiceName: svcName2,
|
||||
},
|
||||
},
|
||||
}
|
||||
el.Add(endpointSlice2)
|
||||
eps, err := el.MatchByKey(key)
|
||||
if err != nil {
|
||||
t.Errorf("unexpeted error %v", err)
|
||||
}
|
||||
if len(eps) != 1 {
|
||||
t.Errorf("expected one slice %v, error, got %d slices", endpointSlice, len(eps))
|
||||
}
|
||||
if len(eps) == 1 && eps[0].Labels[discoveryv1.LabelServiceName] != svcName {
|
||||
t.Errorf("expected slice %v, error, got %v slices", endpointSlice, eps[0])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue