func()

in pkg/controllers/hub/endpointsliceexport/controller.go [363:407]


func (r *Reconciler) scanForEndpointSliceImports(
	ctx context.Context,
	endpointSliceExport *fleetnetv1alpha1.EndpointSliceExport,
	svcInUseBy *fleetnetv1alpha1.ServiceInUseBy,
) (endpointSliceImportsToWithdraw, endpointSliceImportsToCreateOrUpdate []*fleetnetv1alpha1.EndpointSliceImport, err error) {
	// List all EndpointSlices distributed as EndpointSliceImports.
	endpointSliceImportList := &fleetnetv1alpha1.EndpointSliceImportList{}
	listOpts := client.MatchingFields{
		endpointSliceImportNameFieldKey: endpointSliceExport.Name,
	}
	if err := r.HubClient.List(ctx, endpointSliceImportList, listOpts); err != nil {
		klog.ErrorS(err, "Failed to list EndpointSliceImports by a specific name",
			"endpointSliceImportName", endpointSliceExport.Name,
			"endpointSliceExport", klog.KObj(endpointSliceExport))
		return endpointSliceImportsToWithdraw, endpointSliceImportsToCreateOrUpdate, err
	}

	// Match the EndpointSliceImports with the member clusters that have requested the EndpointSlice.
	for idx := range endpointSliceImportList.Items {
		endpointSliceImport := endpointSliceImportList.Items[idx]
		nsKey := fleetnetv1alpha1.ClusterNamespace(endpointSliceImport.Namespace)
		if _, ok := svcInUseBy.MemberClusters[nsKey]; ok {
			// A member cluster has requested the EndpointSlice and an EndpointSlice has been distributed to the
			// cluster; the EndpointSliceImport should be updated.
			endpointSliceImportsToCreateOrUpdate = append(endpointSliceImportsToCreateOrUpdate, &endpointSliceImport)
			delete(svcInUseBy.MemberClusters, nsKey)
		} else {
			// No member cluster has imported the EndpointSlice yet an EndpointSlice has been distributed to the cluster;
			// the EndpointSliceImport should be withdrawn.
			endpointSliceImportsToWithdraw = append(endpointSliceImportsToWithdraw, &endpointSliceImport)
		}
	}
	// A member cluster has requested the EndpointSlice but no EndpointSlice has been distributed to the cluster;
	// an EndpointSliceImport should be created.
	for ns := range svcInUseBy.MemberClusters {
		endpointSliceImport := &fleetnetv1alpha1.EndpointSliceImport{
			ObjectMeta: metav1.ObjectMeta{
				Namespace: string(ns),
				Name:      endpointSliceExport.Name,
			},
		}
		endpointSliceImportsToCreateOrUpdate = append(endpointSliceImportsToCreateOrUpdate, endpointSliceImport)
	}
	return endpointSliceImportsToWithdraw, endpointSliceImportsToCreateOrUpdate, nil
}