mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #82393 from robscott/endpointslicecache-nil-check
Adding a nil check in endpointslicecache
This commit is contained in:
commit
57df10a244
@ -59,6 +59,10 @@ type endpointInfo struct {
|
|||||||
Topology map[string]string
|
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.
|
// NewEndpointSliceCache initializes an EndpointSliceCache.
|
||||||
func NewEndpointSliceCache(hostname string, isIPv6Mode *bool, recorder record.EventRecorder, makeEndpointInfo makeEndpointFunc) *EndpointSliceCache {
|
func NewEndpointSliceCache(hostname string, isIPv6Mode *bool, recorder record.EventRecorder, makeEndpointInfo makeEndpointFunc) *EndpointSliceCache {
|
||||||
if makeEndpointInfo == nil {
|
if makeEndpointInfo == nil {
|
||||||
@ -121,10 +125,15 @@ func (cache *EndpointSliceCache) EndpointsMap(serviceNN types.NamespacedName) En
|
|||||||
}
|
}
|
||||||
|
|
||||||
// endpointInfoByServicePort groups endpoint info by service port name and address.
|
// endpointInfoByServicePort groups endpoint info by service port name and address.
|
||||||
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName) map[ServicePortName]map[string]Endpoint {
|
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName) spToEndpointMap {
|
||||||
endpointInfoBySP := map[ServicePortName]map[string]Endpoint{}
|
endpointInfoBySP := spToEndpointMap{}
|
||||||
|
sliceInfoByName, ok := cache.sliceByServiceMap[serviceNN]
|
||||||
|
|
||||||
for _, sliceInfo := range cache.sliceByServiceMap[serviceNN] {
|
if !ok {
|
||||||
|
return endpointInfoBySP
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sliceInfo := range sliceInfoByName {
|
||||||
for _, port := range sliceInfo.Ports {
|
for _, port := range sliceInfo.Ports {
|
||||||
if port.Name == nil {
|
if port.Name == nil {
|
||||||
klog.Warningf("ignoring port with nil name %v", port)
|
klog.Warningf("ignoring port with nil name %v", port)
|
||||||
|
@ -163,7 +163,7 @@ func TestEndpointInfoByServicePort(t *testing.T) {
|
|||||||
namespacedName types.NamespacedName
|
namespacedName types.NamespacedName
|
||||||
endpointSlices []*discovery.EndpointSlice
|
endpointSlices []*discovery.EndpointSlice
|
||||||
hostname string
|
hostname string
|
||||||
expectedMap map[ServicePortName]map[string]Endpoint
|
expectedMap spToEndpointMap
|
||||||
}{
|
}{
|
||||||
"simple use case with 3 endpoints": {
|
"simple use case with 3 endpoints": {
|
||||||
namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"},
|
namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"},
|
||||||
@ -171,7 +171,7 @@ func TestEndpointInfoByServicePort(t *testing.T) {
|
|||||||
endpointSlices: []*discovery.EndpointSlice{
|
endpointSlices: []*discovery.EndpointSlice{
|
||||||
generateEndpointSlice("svc1", "ns1", 1, 3, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80)}),
|
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"}: {
|
{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.1": &BaseEndpointInfo{Endpoint: "10.0.1.1:80", IsLocal: false},
|
||||||
"10.0.1.2": &BaseEndpointInfo{Endpoint: "10.0.1.2:80", IsLocal: true},
|
"10.0.1.2": &BaseEndpointInfo{Endpoint: "10.0.1.2:80", IsLocal: true},
|
||||||
@ -190,7 +190,7 @@ func TestEndpointInfoByServicePort(t *testing.T) {
|
|||||||
|
|
||||||
got := esCache.endpointInfoByServicePort(tc.namespacedName)
|
got := esCache.endpointInfoByServicePort(tc.namespacedName)
|
||||||
if !reflect.DeepEqual(got, tc.expectedMap) {
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user