func()

in pkg/controllers/hub/internalserviceimport/controller.go [226:269]


func (r *Reconciler) withdrawServiceImport(ctx context.Context,
	svcImport *fleetnetv1alpha1.ServiceImport,
	internalSvcImport *fleetnetv1alpha1.InternalServiceImport) (ctrl.Result, error) {
	// The cluster namespace of the member cluster which imports the Service.
	clusterNamespace := fleetnetv1alpha1.ClusterNamespace(internalSvcImport.Namespace)

	// Update the annotated ServiceInUseBy information.
	svcInUseBy := extractServiceInUseByInfoFromServiceImport(svcImport)
	if _, ok := svcInUseBy.MemberClusters[clusterNamespace]; ok {
		delete(svcInUseBy.MemberClusters, clusterNamespace)
		switch {
		case len(svcInUseBy.MemberClusters) > 0:
			// There are still member clusters importing the Service after the withdrawal; the ServiceInUseBy
			// annotation will be updated. Note that with current semantics (one import only across the fleet)
			// this branch should not run.
			if err := r.annotateServiceImportWithServiceInUseByInfo(ctx, svcImport, svcInUseBy); err != nil {
				klog.ErrorS(err, "Failed to annotate ServiceImport with ServiceInUseBy info",
					"serviceImport", klog.KObj(svcImport),
					"serviceInUseBy", svcInUseBy)
				return ctrl.Result{}, err
			}
		case len(svcInUseBy.MemberClusters) == 0:
			// No member cluster imports the Service after the withdrawal; the ServiceInUseBy annotation (and
			// the cleanup finalizer) on ServiceImport will be cleared.
			if err := r.clearServiceInUseByInfoFromServiceImport(ctx, svcImport); err != nil {
				klog.ErrorS(err, "Failed to clear ServiceImport ServiceInUseBy annotation",
					"serviceImport", klog.KObj(svcImport),
					"serviceInUseBy", svcInUseBy)
				return ctrl.Result{}, err
			}
		}
	}
	// A rare occurrence as it is, it could happen that the InternalServiceImport has the cleanup finalizer,
	// yet the import is not annotated on the ServiceImport. This is usually caused by data corruption, or
	// direct ServiceInUseBy annotation manipulation by the user; and in this case the controller will skip
	// the updating.

	// Remove the cleanup finalizer.
	if err := r.removeInternalServiceImportCleanupFinalizer(ctx, internalSvcImport); err != nil {
		klog.ErrorS(err, "Failed to remove cleanup finalizer from InternalServiceImport", "internalServiceImport", klog.KObj(internalSvcImport))
		return ctrl.Result{}, err
	}
	return ctrl.Result{}, nil
}