Repository: prometheus/prometheus
File: discovery/kubernetes/kubernetes.go (Go)

Code before:
				if addr.TargetRef != nil {
					switch addr.TargetRef.Kind {
					case "Pod":
						if addr.NodeName != nil {
							nodes = append(nodes, *addr.NodeName)
						}
					case "Node":
						nodes = append(nodes, addr.TargetRef.Name)
					}
				}
			}
		}
		return nodes, nil
	}

	return d.mustNewSharedIndexInformer(plw, &apiv1.Endpoints{}, resyncDisabled, indexers)
}

func (d *Discovery) newEndpointSlicesByNodeInformer(plw *cache.ListWatch, object runtime.Object) cache.SharedIndexInformer {
	indexers := make(map[string]cache.IndexFunc)
	indexers[serviceIndex] = func(obj interface{}) ([]string, error) {
		var services []string
		switch e := obj.(type) {
		case *disv1.EndpointSlice:
			if serviceName, exists := e.ObjectMeta.Labels[disv1.LabelServiceName]; exists {
				return []string{serviceName}, nil
			}
		default:
			return nil, errors.New("object is not an endpointslice")
		}

		return services, nil
	}
	if !d.attachMetadata.Node {
		return d.mustNewSharedIndexInformer(plw, object, resyncDisabled, indexers)
	}

	indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
		var nodes []string
		switch e := obj.(type) {
		case *disv1.EndpointSlice:
			for _, target := range e.Endpoints {
				if target.TargetRef != nil {
					switch target.TargetRef.Kind {
					case "Pod":
						if target.NodeName != nil {
							nodes = append(nodes, *target.NodeName)
						}
					case "Node":
						nodes = append(nodes, target.TargetRef.Name)
					}

Review comment (suggestion):
the service name in the label doesn't seem to be namespaced.
we should use `namespacedName()` to ensure uniqueness (two services in two different namespaces can have the same name)

if you could add add a `TestEndpointSliceDiscoveryWithUnrelatedServiceUpdate` where we update an unrelated service with the same name, in another namespace and check the no-op, it'd be a great regression test.

Code after:
					}
				}
			}
		}
		return nodes, nil
	}

	return d.mustNewSharedIndexInformer(plw, &apiv1.Endpoints{}, resyncDisabled, indexers)
}

func (d *Discovery) newEndpointSlicesByNodeInformer(plw *cache.ListWatch, object runtime.Object) cache.SharedIndexInformer {
	indexers := make(map[string]cache.IndexFunc)
	indexers[serviceIndex] = func(obj interface{}) ([]string, error) {
		e, ok := obj.(*disv1.EndpointSlice)
		if !ok {
			return nil, errors.New("object is not an endpointslice")
		}

		svcName, exists := e.Labels[disv1.LabelServiceName]
		if !exists {
			return nil, nil
		}

		return []string{namespacedName(e.Namespace, svcName)}, nil
	}
	if !d.attachMetadata.Node {
		return d.mustNewSharedIndexInformer(plw, object, resyncDisabled, indexers)
	}

	indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
		e, ok := obj.(*disv1.EndpointSlice)
		if !ok {
			return nil, errors.New("object is not an endpointslice")
		}

		var nodes []string
		for _, target := range e.Endpoints {
			if target.TargetRef != nil {
				switch target.TargetRef.Kind {
				case "Pod":
					if target.NodeName != nil {
						nodes = append(nodes, *target.NodeName)
					}
				case "Node":
					nodes = append(nodes, target.TargetRef.Name)
				}
			}
		}

		return nodes, nil
	}