func()

in pkg/controllers/route_controller.go [292:378]


func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request, route core.Route) error {
	r.log.Infow(ctx, "reconcile, adding or updating", "name", req.Name)
	r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeNormal,
		k8s.RouteEventReasonReconcile, "Adding/Updating Reconcile")

	if err := r.finalizerManager.AddFinalizers(ctx, route.K8sObject(), routeTypeToFinalizer[r.routeType]); err != nil {
		r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeWarning, k8s.RouteEventReasonFailedAddFinalizer, fmt.Sprintf("Failed add finalizer due to %s", err))
	}

	if err := r.validateRoute(ctx, route); err != nil {
		// TODO: we suppose to stop reconciliation here, but that will create problem when
		// we delete Service and we suppose to delete TargetGroup, this validation will
		// throw error if Service is not found.  For now just update route status and log
		// error.
		r.log.Infof(ctx, "route: %s: %s", route.Name(), err)
	}

	backendRefIPFamiliesErr := r.validateBackendRefsIpFamilies(ctx, route)

	if backendRefIPFamiliesErr != nil {
		httpRouteOld := route.DeepCopy()
		parentRef, err := r.findControlledParentRef(ctx, route)
		if err != nil {
			return err
		}

		route.Status().UpdateParentRefs(parentRef, config.LatticeGatewayControllerName)
		route.Status().UpdateRouteCondition(parentRef, metav1.Condition{
			Type:               string(gwv1.RouteConditionAccepted),
			Status:             metav1.ConditionFalse,
			ObservedGeneration: route.K8sObject().GetGeneration(),
			Reason:             string(gwv1.RouteReasonUnsupportedValue),
			Message:            "Dual stack Service is not supported",
		})

		if err := r.client.Status().Patch(ctx, route.K8sObject(), client.MergeFrom(httpRouteOld.K8sObject())); err != nil {
			return errors.Wrapf(err, "failed to update httproute status")
		}

		return backendRefIPFamiliesErr
	}

	if _, err := r.buildAndDeployModel(ctx, route); err != nil {
		if services.IsConflictError(err) {
			// Stop reconciliation of this route if the route cannot be owned / has conflict
			parentRef, parentRefErr := r.findControlledParentRef(ctx, route)
			if parentRefErr != nil {
				// if parentRef not found, we cannot update route status
				return parentRefErr
			}
			route.Status().UpdateParentRefs(parentRef, config.LatticeGatewayControllerName)
			route.Status().UpdateRouteCondition(parentRef, metav1.Condition{
				Type:               string(gwv1.RouteConditionAccepted),
				Status:             metav1.ConditionFalse,
				ObservedGeneration: route.K8sObject().GetGeneration(),
				Reason:             "Conflicted",
				Message:            err.Error(),
			})
			if err = r.client.Status().Update(ctx, route.K8sObject()); err != nil {
				return fmt.Errorf("failed to update route status for conflict due to err %w", err)
			}
			return nil
		}
		return err
	}

	r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeNormal,
		k8s.RouteEventReasonDeploySucceed, "Adding/Updating reconcile Done!")

	svcName := k8sutils.LatticeServiceName(route.Name(), route.Namespace())
	svc, err := r.cloud.Lattice().FindService(ctx, svcName)
	if err != nil && !services.IsNotFoundError(err) {
		return err
	}

	if svc == nil || svc.DnsEntry == nil || svc.DnsEntry.DomainName == nil {
		r.log.Infof(ctx, "Either service, dns entry, or domain name is not available. Will Retry")
		return errors.New(lattice.LATTICE_RETRY)
	}

	if err := r.updateRouteAnnotation(ctx, *svc.DnsEntry.DomainName, route); err != nil {
		return err
	}

	r.log.Infow(ctx, "reconciled", "name", req.Name)
	return nil
}