Skip to content

Commit 57df10a

Browse files
authored
Merge pull request kubernetes#82393 from robscott/endpointslicecache-nil-check
Adding a nil check in endpointslicecache
2 parents 1866891 + a1e3afa commit 57df10a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

pkg/proxy/endpointslicecache.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type endpointInfo struct {
5959
Topology map[string]string
6060
}
6161

62+
// spToEndpointMap stores groups Endpoint objects by ServicePortName and
63+
// EndpointSlice name.
64+
type spToEndpointMap map[ServicePortName]map[string]Endpoint
65+
6266
// NewEndpointSliceCache initializes an EndpointSliceCache.
6367
func NewEndpointSliceCache(hostname string, isIPv6Mode *bool, recorder record.EventRecorder, makeEndpointInfo makeEndpointFunc) *EndpointSliceCache {
6468
if makeEndpointInfo == nil {
@@ -121,10 +125,15 @@ func (cache *EndpointSliceCache) EndpointsMap(serviceNN types.NamespacedName) En
121125
}
122126

123127
// endpointInfoByServicePort groups endpoint info by service port name and address.
124-
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName) map[ServicePortName]map[string]Endpoint {
125-
endpointInfoBySP := map[ServicePortName]map[string]Endpoint{}
128+
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName) spToEndpointMap {
129+
endpointInfoBySP := spToEndpointMap{}
130+
sliceInfoByName, ok := cache.sliceByServiceMap[serviceNN]
131+
132+
if !ok {
133+
return endpointInfoBySP
134+
}
126135

127-
for _, sliceInfo := range cache.sliceByServiceMap[serviceNN] {
136+
for _, sliceInfo := range sliceInfoByName {
128137
for _, port := range sliceInfo.Ports {
129138
if port.Name == nil {
130139
klog.Warningf("ignoring port with nil name %v", port)

pkg/proxy/endpointslicecache_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ func TestEndpointInfoByServicePort(t *testing.T) {
163163
namespacedName types.NamespacedName
164164
endpointSlices []*discovery.EndpointSlice
165165
hostname string
166-
expectedMap map[ServicePortName]map[string]Endpoint
166+
expectedMap spToEndpointMap
167167
}{
168168
"simple use case with 3 endpoints": {
169169
namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"},
170170
hostname: "host1",
171171
endpointSlices: []*discovery.EndpointSlice{
172172
generateEndpointSlice("svc1", "ns1", 1, 3, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80)}),
173173
},
174-
expectedMap: map[ServicePortName]map[string]Endpoint{
174+
expectedMap: spToEndpointMap{
175175
{NamespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, Port: "port-0"}: {
176176
"10.0.1.1": &BaseEndpointInfo{Endpoint: "10.0.1.1:80", IsLocal: false},
177177
"10.0.1.2": &BaseEndpointInfo{Endpoint: "10.0.1.2:80", IsLocal: true},
@@ -190,7 +190,7 @@ func TestEndpointInfoByServicePort(t *testing.T) {
190190

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

196196
}

0 commit comments

Comments
 (0)