func()

in pkg/controller/integration/monitor.go [70:157]


func (action *monitorAction) Handle(ctx context.Context, integration *v1.Integration) (*v1.Integration, error) {
	// When in InitializationFailed condition a kit is not available for the integration
	// so handle it differently from the rest
	if isInInitializationFailed(integration.Status) {
		// Only check if the Integration requires a rebuild
		return action.checkDigestAndRebuild(ctx, integration, nil)
	}

	var kit *v1.IntegrationKit
	var err error
	if integration.Status.IntegrationKit == nil && integration.Status.Image == "" {
		return nil, fmt.Errorf("no kit nor container image set on integration %s", integration.Name)
	}

	if integration.Status.IntegrationKit != nil {
		// Managed Integration
		kit, err = kubernetes.GetIntegrationKit(ctx, action.client,
			integration.Status.IntegrationKit.Name, integration.Status.IntegrationKit.Namespace)
		if err != nil {
			return nil, fmt.Errorf("unable to find integration kit %s/%s: %w",
				integration.Status.IntegrationKit.Namespace, integration.Status.IntegrationKit.Name, err)
		}

		// If integration is in error and its kit is also in error then integration does not change
		if isInIntegrationKitFailed(integration.Status) &&
			kit.Status.Phase == v1.IntegrationKitPhaseError {
			return nil, nil
		}
	}

	// Check if the Integration requires a rebuild
	if changed, err := action.checkDigestAndRebuild(ctx, integration, kit); err != nil {
		return nil, err
	} else if changed != nil {
		return changed, nil
	}

	if kit != nil {
		// Check if an IntegrationKit with higher priority is ready
		priority, ok := kit.Labels[v1.IntegrationKitPriorityLabel]
		if !ok {
			priority = "0"
		}
		withHigherPriority, err := labels.NewRequirement(v1.IntegrationKitPriorityLabel,
			selection.GreaterThan, []string{priority})
		if err != nil {
			return nil, err
		}

		kits, err := lookupKitsForIntegration(ctx, action.client, integration, ctrl.MatchingLabelsSelector{
			Selector: labels.NewSelector().Add(*withHigherPriority),
		})
		if err != nil {
			return nil, err
		}
		priorityReadyKit, err := findHighestPriorityReadyKit(kits)
		if err != nil {
			return nil, err
		}
		if priorityReadyKit != nil {
			integration.SetIntegrationKit(priorityReadyKit)
		}
	}
	// Run traits that are enabled for the phase
	environment, err := trait.Apply(ctx, action.client, integration, kit)
	if err != nil {
		integration.Status.Phase = v1.IntegrationPhaseError
		integration.SetReadyCondition(corev1.ConditionFalse,
			v1.IntegrationConditionInitializationFailedReason, err.Error())
		return integration, err
	}
	// If the platform is not in ready status (it may happen when a new IntegrationPlatform is created), then, we may not be able to
	// properly apply all the traits. We must set the phase in an unknown status which should be periodically reconciled in order to make sure that
	// we eventually return in a ready phase (likely once the platform is done)
	if environment.Platform == nil || environment.Platform.Status.Phase != v1.IntegrationPlatformPhaseReady {
		integration.Status.Phase = v1.IntegrationPhaseUnknown
		integration.Status.SetCondition(
			v1.IntegrationConditionPlatformAvailable,
			corev1.ConditionFalse,
			"PlatformMissing",
			"IntegrationPlatform is missing or not yet ready. If the problem persist, make sure to fix the IntegrationPlatform error or create a new one.",
		)
		return integration, nil
	}
	action.checkTraitAnnotationsDeprecatedNotice(integration)

	return action.monitorPods(ctx, environment, integration)
}