Merge pull request #13408 from machine424/save-cycles

chore(kubernetes): check preconditions earlier and avoid unnecessary checks or iterations
pull/13423/head
Julien Pivotto 2024-01-18 12:07:07 +01:00 committed by GitHub
commit ee3edc4a70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 26 deletions

View File

@ -369,6 +369,11 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
// For all seen pods, check all container ports. If they were not covered // For all seen pods, check all container ports. If they were not covered
// by one of the service endpoints, generate targets for them. // by one of the service endpoints, generate targets for them.
for _, pe := range seenPods { for _, pe := range seenPods {
// PodIP can be empty when a pod is starting or has been evicted.
if len(pe.pod.Status.PodIP) == 0 {
continue
}
for _, c := range pe.pod.Spec.Containers { for _, c := range pe.pod.Spec.Containers {
for _, cport := range c.Ports { for _, cport := range c.Ports {
hasSeenPort := func() bool { hasSeenPort := func() bool {
@ -383,8 +388,6 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
continue continue
} }
// PodIP can be empty when a pod is starting or has been evicted.
if len(pe.pod.Status.PodIP) != 0 {
a := net.JoinHostPort(pe.pod.Status.PodIP, strconv.FormatUint(uint64(cport.ContainerPort), 10)) a := net.JoinHostPort(pe.pod.Status.PodIP, strconv.FormatUint(uint64(cport.ContainerPort), 10))
ports := strconv.FormatUint(uint64(cport.ContainerPort), 10) ports := strconv.FormatUint(uint64(cport.ContainerPort), 10)
@ -400,7 +403,6 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
} }
} }
} }
}
return tg return tg
} }

View File

@ -405,6 +405,11 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
// For all seen pods, check all container ports. If they were not covered // For all seen pods, check all container ports. If they were not covered
// by one of the service endpoints, generate targets for them. // by one of the service endpoints, generate targets for them.
for _, pe := range seenPods { for _, pe := range seenPods {
// PodIP can be empty when a pod is starting or has been evicted.
if len(pe.pod.Status.PodIP) == 0 {
continue
}
for _, c := range pe.pod.Spec.Containers { for _, c := range pe.pod.Spec.Containers {
for _, cport := range c.Ports { for _, cport := range c.Ports {
hasSeenPort := func() bool { hasSeenPort := func() bool {
@ -422,8 +427,6 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
continue continue
} }
// PodIP can be empty when a pod is starting or has been evicted.
if len(pe.pod.Status.PodIP) != 0 {
a := net.JoinHostPort(pe.pod.Status.PodIP, strconv.FormatUint(uint64(cport.ContainerPort), 10)) a := net.JoinHostPort(pe.pod.Status.PodIP, strconv.FormatUint(uint64(cport.ContainerPort), 10))
ports := strconv.FormatUint(uint64(cport.ContainerPort), 10) ports := strconv.FormatUint(uint64(cport.ContainerPort), 10)
@ -439,7 +442,6 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
} }
} }
} }
}
return tg return tg
} }