func()

in pkg/controller/integrationkit/integrationkit_controller.go [190:317]


func (r *reconcileIntegrationKit) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
	rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name)
	rlog.Debug("Reconciling IntegrationKit")

	// Make sure the operator is allowed to act on namespace
	if ok, err := platform.IsOperatorAllowedOnNamespace(ctx, r.client, request.Namespace); err != nil {
		log.Debugf("Error occurred when checking whether operator is allowed in namespace %s: %v", request.Namespace, err)
		return reconcile.Result{}, err
	} else if !ok {
		rlog.Info("Ignoring request because namespace is locked")
		return reconcile.Result{}, nil
	}

	var instance v1.IntegrationKit

	// Fetch the IntegrationKit instance
	if err := r.client.Get(ctx, request.NamespacedName, &instance); err != nil {
		if k8serrors.IsNotFound(err) {
			// Request object not found, could have been deleted after reconcile request.
			// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
			// Return and don't requeue
			return reconcile.Result{}, nil
		}
		// Error reading the object - requeue the request.
		return reconcile.Result{}, err
	}

	// Only process resources assigned to the operator
	if !platform.IsOperatorHandlerConsideringLock(ctx, r.client, request.Namespace, &instance) {
		rlog.Info("Ignoring request because resource is not assigned to current operator")
		return reconcile.Result{}, nil
	}

	target := instance.DeepCopy()
	targetLog := rlog.ForIntegrationKit(target)

	//nolint:nestif
	if target.Status.Phase == v1.IntegrationKitPhaseNone || target.Status.Phase == v1.IntegrationKitPhaseWaitingForPlatform {
		rlog.Debug("Preparing to shift integration kit phase")
		//nolint: staticcheck
		if target.IsExternal() || target.IsSynthetic() {
			target.Status.Phase = v1.IntegrationKitPhaseInitialization
			return r.update(ctx, &instance, target)
		}

		// Platform is always local to the kit
		pl, err := platform.GetForResource(ctx, r.client, target)
		if err != nil || pl.Status.Phase != v1.IntegrationPlatformPhaseReady {
			target.Status.Phase = v1.IntegrationKitPhaseWaitingForPlatform
		} else {
			target.Status.Phase = v1.IntegrationKitPhaseInitialization
		}

		if instance.Status.Phase != target.Status.Phase {
			if err != nil {
				rlog.Debugf("Error occurred while searching for platform. Cannot advance phase until cleared: %v", err)
				target.Status.SetErrorCondition(v1.IntegrationKitConditionPlatformAvailable, v1.IntegrationKitConditionPlatformAvailableReason, err)
			}

			if pl != nil {
				target.SetIntegrationPlatform(pl)
			}

			return r.update(ctx, &instance, target)
		}

		return reconcile.Result{}, err
	}

	actions := []Action{
		NewInitializeAction(),
		NewBuildAction(),
		NewMonitorAction(),
		NewErrorAction(),
	}

	targetPhase := instance.Status.Phase

	for _, a := range actions {
		a.InjectClient(r.client)
		a.InjectLogger(targetLog)

		if !a.CanHandle(target) {
			continue
		}

		targetLog.Infof("Invoking action %s", a.Name())

		newTarget, err := a.Handle(ctx, target)

		if err != nil {
			camelevent.NotifyIntegrationKitError(ctx, r.client, r.recorder, &instance, newTarget, err)
			return reconcile.Result{}, err
		}

		if newTarget != nil {
			if res, err := r.update(ctx, &instance, newTarget); err != nil {
				camelevent.NotifyIntegrationKitError(ctx, r.client, r.recorder, &instance, newTarget, err)
				return res, err
			}

			targetPhase = newTarget.Status.Phase

			if targetPhase != instance.Status.Phase {
				targetLog.Info(
					"State transition",
					"phase-from", instance.Status.Phase,
					"phase-to", targetPhase,
				)
			}
		}

		// handle one action at time so the resource
		// is always at its latest state
		camelevent.NotifyIntegrationKitUpdated(ctx, r.client, r.recorder, &instance, newTarget)

		break
	}

	if targetPhase == v1.IntegrationKitPhaseWaitingForCatalog {
		// Requeue
		return reconcile.Result{
			RequeueAfter: requeueAfterDuration,
		}, nil
	}

	return reconcile.Result{}, nil
}