GetEndpointSliceByServiceName returns the EndpointSlice for a given service name in a given namespace
( ctx context.Context, crudClient client.Client, namespace, serviceName string, )
| 40 | |
| 41 | // GetEndpointSliceByServiceName returns the EndpointSlice for a given service name in a given namespace |
| 42 | func GetEndpointSliceByServiceName( |
| 43 | ctx context.Context, |
| 44 | crudClient client.Client, |
| 45 | namespace, serviceName string, |
| 46 | ) (*discoveryv1.EndpointSlice, error) { |
| 47 | endpointSliceList := &discoveryv1.EndpointSliceList{} |
| 48 | |
| 49 | if err := crudClient.List( |
| 50 | ctx, |
| 51 | endpointSliceList, |
| 52 | client.InNamespace(namespace), |
| 53 | client.MatchingLabels{"kubernetes.io/service-name": serviceName}, |
| 54 | ); err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | if len(endpointSliceList.Items) == 0 { |
| 59 | return nil, fmt.Errorf("no endpointslice found for service %s in namespace %s", serviceName, namespace) |
| 60 | } |
| 61 | |
| 62 | if len(endpointSliceList.Items) > 1 { |
| 63 | return nil, fmt.Errorf("multiple endpointslice found for service %s in namespace %s", serviceName, namespace) |
| 64 | } |
| 65 | |
| 66 | return &endpointSliceList.Items[0], nil |
| 67 | } |