func()

in pkg/controllers/serviceexport_controller.go [217:260]


func (r *ServiceExportReconciler) extractEndpoints(ctx context.Context, svc *v1.Service) ([]*model.Endpoint, error) {
	result := make([]*model.Endpoint, 0)

	endpointSlices := discovery.EndpointSliceList{}
	err := r.Client.List(ctx, &endpointSlices,
		client.InNamespace(svc.Namespace), client.MatchingLabels{discovery.LabelServiceName: svc.Name})

	if err != nil {
		return nil, err
	}

	servicePortMap := make(map[string]model.Port)
	for _, svcPort := range svc.Spec.Ports {
		servicePortMap[svcPort.Name] = ServicePortToPort(svcPort)
	}

	for _, slice := range endpointSlices.Items {
		if slice.AddressType != discovery.AddressTypeIPv4 {
			return nil, fmt.Errorf("unsupported address type %s for service %s", slice.AddressType, svc.Name)
		}
		for _, endpointPort := range slice.Ports {
			for _, endpoint := range slice.Endpoints {
				for _, IP := range endpoint.Addresses {
					attributes := make(map[string]string)
					if version.GetVersion() != "" {
						attributes[K8sVersionAttr] = version.PackageName + " " + version.GetVersion()
					}
					// TODO extract attributes - pod, node and other useful details if possible

					port := EndpointPortToPort(endpointPort)
					result = append(result, &model.Endpoint{
						Id:           model.EndpointIdFromIPAddressAndPort(IP, port),
						IP:           IP,
						EndpointPort: port,
						ServicePort:  servicePortMap[*endpointPort.Name],
						Attributes:   attributes,
					})
				}
			}
		}
	}

	return result, nil
}