chore(source/service): add resource label without looping over (#5528)

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
This commit is contained in:
Ivan Ka 2025-06-15 22:28:58 +01:00 committed by GitHub
parent c522e45bca
commit 7aca4dc6a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 10 deletions

View File

@ -215,7 +215,6 @@ func (sc *serviceSource) Endpoints(_ context.Context) ([]*endpoint.Endpoint, err
}
log.Debugf("Endpoints generated from service: %s/%s: %v", svc.Namespace, svc.Name, svcEndpoints)
sc.setResourceLabel(svc, svcEndpoints)
endpoints = append(endpoints, svcEndpoints...)
}
@ -390,6 +389,7 @@ func (sc *serviceSource) extractHeadlessEndpoints(svc *v1.Service, hostname stri
}
if ep != nil {
ep.WithLabel(endpoint.ResourceLabelKey, fmt.Sprintf("service/%s/%s", svc.Namespace, svc.Name))
endpoints = append(endpoints, ep)
}
}
@ -474,12 +474,6 @@ func (sc *serviceSource) filterByServiceType(services []*v1.Service) []*v1.Servi
return result
}
func (sc *serviceSource) setResourceLabel(service *v1.Service, endpoints []*endpoint.Endpoint) {
for _, ep := range endpoints {
ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("service/%s/%s", service.Namespace, service.Name)
}
}
func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, providerSpecific endpoint.ProviderSpecific, setIdentifier string, useClusterIP bool) (endpoints []*endpoint.Endpoint) {
hostname = strings.TrimSuffix(hostname, ".")
@ -745,6 +739,7 @@ func (sc *serviceSource) extractNodePortEndpoints(svc *v1.Service, hostname stri
}
if ep != nil {
ep.WithLabel(endpoint.ResourceLabelKey, fmt.Sprintf("service/%s/%s", svc.Namespace, svc.Name))
endpoints = append(endpoints, ep)
}
}

View File

@ -3556,7 +3556,7 @@ func TestHeadlessServicesHostIP(t *testing.T) {
t.Parallel()
// Create a Kubernetes testing client
kubernetes := fake.NewSimpleClientset()
kubernetes := fake.NewClientset()
service := &v1.Service{
Spec: v1.ServiceSpec{
@ -3654,6 +3654,11 @@ func TestHeadlessServicesHostIP(t *testing.T) {
// Validate returned endpoints against desired endpoints.
validateEndpoints(t, endpoints, tc.expected)
// TODO; when all resources have the resource label, we could add this check to the validateEndpoints function.
for _, ep := range endpoints {
require.Contains(t, ep.Labels, endpoint.ResourceLabelKey)
}
})
}
}
@ -3785,7 +3790,7 @@ func TestExternalServices(t *testing.T) {
t.Parallel()
// Create a Kubernetes testing client
kubernetes := fake.NewSimpleClientset()
kubernetes := fake.NewClientset()
service := &v1.Service{
Spec: v1.ServiceSpec{
@ -3834,12 +3839,17 @@ func TestExternalServices(t *testing.T) {
// Validate returned endpoints against desired endpoints.
validateEndpoints(t, endpoints, tc.expected)
// TODO; when all resources have the resource label, we could add this check to the validateEndpoints function.
for _, ep := range endpoints {
require.Contains(t, ep.Labels, endpoint.ResourceLabelKey)
}
})
}
}
func BenchmarkServiceEndpoints(b *testing.B) {
kubernetes := fake.NewSimpleClientset()
kubernetes := fake.NewClientset()
service := &v1.Service{
ObjectMeta: metav1.ObjectMeta{