Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a nil check in endpointslicecache #82393

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions pkg/proxy/endpointslicecache.go
Expand Up @@ -59,6 +59,10 @@ type endpointInfo struct {
Topology map[string]string
}

// spToEndpointMap stores groups Endpoint objects by ServicePortName and
// EndpointSlice name.
type spToEndpointMap map[ServicePortName]map[string]Endpoint

// NewEndpointSliceCache initializes an EndpointSliceCache.
func NewEndpointSliceCache(hostname string, isIPv6Mode *bool, recorder record.EventRecorder, makeEndpointInfo makeEndpointFunc) *EndpointSliceCache {
if makeEndpointInfo == nil {
Expand Down Expand Up @@ -121,10 +125,15 @@ func (cache *EndpointSliceCache) EndpointsMap(serviceNN types.NamespacedName) En
}

// endpointInfoByServicePort groups endpoint info by service port name and address.
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName) map[ServicePortName]map[string]Endpoint {
endpointInfoBySP := map[ServicePortName]map[string]Endpoint{}
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName) spToEndpointMap {
endpointInfoBySP := spToEndpointMap{}
sliceInfoByName, ok := cache.sliceByServiceMap[serviceNN]
robscott marked this conversation as resolved.
Show resolved Hide resolved

if !ok {
return endpointInfoBySP
}

for _, sliceInfo := range cache.sliceByServiceMap[serviceNN] {
for _, sliceInfo := range sliceInfoByName {
for _, port := range sliceInfo.Ports {
if port.Name == nil {
klog.Warningf("ignoring port with nil name %v", port)
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/endpointslicecache_test.go
Expand Up @@ -163,15 +163,15 @@ func TestEndpointInfoByServicePort(t *testing.T) {
namespacedName types.NamespacedName
endpointSlices []*discovery.EndpointSlice
hostname string
expectedMap map[ServicePortName]map[string]Endpoint
expectedMap spToEndpointMap
}{
"simple use case with 3 endpoints": {
namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"},
hostname: "host1",
endpointSlices: []*discovery.EndpointSlice{
generateEndpointSlice("svc1", "ns1", 1, 3, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80)}),
},
expectedMap: map[ServicePortName]map[string]Endpoint{
expectedMap: spToEndpointMap{
{NamespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, Port: "port-0"}: {
"10.0.1.1": &BaseEndpointInfo{Endpoint: "10.0.1.1:80", IsLocal: false},
"10.0.1.2": &BaseEndpointInfo{Endpoint: "10.0.1.2:80", IsLocal: true},
Expand All @@ -190,7 +190,7 @@ func TestEndpointInfoByServicePort(t *testing.T) {

got := esCache.endpointInfoByServicePort(tc.namespacedName)
if !reflect.DeepEqual(got, tc.expectedMap) {
t.Errorf("[%s] endpointInfoByServicePort does not match. Want: %v, Got: %v", name, tc.expectedMap, got)
t.Errorf("[%s] endpointInfoByServicePort does not match. Want: %+v, Got: %+v", name, tc.expectedMap, got)
}

}
Expand Down