func()

in pkg/controllers/serviceexport_controller.go [49:93]


func (r *ServiceExportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	namespace := req.Namespace
	name := req.NamespacedName
	r.Log.Debug("reconciling ServiceExport", "Namespace", namespace, "Name", name)

	serviceExport := v1alpha1.ServiceExport{}
	if err := r.Client.Get(ctx, name, &serviceExport); err != nil {
		if errors.IsNotFound(err) {
			r.Log.Debug("no ServiceExport found",
				"Namespace", namespace, "Name", name)
		} else {
			r.Log.Error(err, "error fetching ServiceExport",
				"Namespace", namespace, "Name", name)
		}
		return ctrl.Result{}, nil
	}

	// Mark ServiceExport to be deleted, which is indicated by the deletion timestamp being set.
	isServiceExportMarkedForDelete := serviceExport.GetDeletionTimestamp() != nil

	service := v1.Service{}
	namespacedName := types.NamespacedName{Namespace: serviceExport.Namespace, Name: serviceExport.Name}
	if err := r.Client.Get(ctx, namespacedName, &service); err != nil {
		if errors.IsNotFound(err) {
			r.Log.Info("no Service found, deleting the ServiceExport",
				"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
			// Mark ServiceExport to be deleted, if the corresponding Service is not found
			isServiceExportMarkedForDelete = true
		} else {
			r.Log.Error(err, "error fetching Service",
				"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
			return ctrl.Result{}, nil
		}
	}

	// Check if the service export is marked to be deleted
	if isServiceExportMarkedForDelete {
		return r.handleDelete(ctx, &serviceExport)
	}

	return r.handleUpdate(ctx, &serviceExport, &service)
}