Repository: vllm-project/aibrix
File: pkg/controller/modeladapter/modeladapter_controller.go (Go)

Code before:
	for _, condition := range conditions {
		if meta.SetStatusCondition(&instance.Status.Conditions, condition) {
			changed = true
		}
	}
	// TODO: sort the conditions based on LastTransitionTime if needed.
	klog.InfoS("model adapter reconcile", "Update CR status", instance.Name, "changed", changed, "status", instance.Status, "conditions", conditions)
	return r.Status().Update(ctx, instance)
}

func (r *ModelAdapterReconciler) clearModelAdapterInstanceList(ctx context.Context, instance *modelv1alpha1.ModelAdapter, stalePodName string) error {
	instance.Status.Instances = RemoveInstanceFromList(instance.Status.Instances, stalePodName)
	// remove instance means the lora has not targets at this moment.
	instance.Status.Phase = modelv1alpha1.ModelAdapterPending
	condition := NewCondition(string(modelv1alpha1.ModelAdapterFailed), metav1.ConditionTrue,
		StableInstanceFoundReason,
		fmt.Sprintf("Pod (%s/%s) is stale or invalid for model adapter (%s/%s), clean up the list", instance.GetNamespace(), stalePodName, instance.GetNamespace(), instance.Name))

	// We also need to update the scheduling and ready status to false
	// When the pod get migrated, we need to update the status with latest LastTransitionTime.
	// However, meta.SetStatusCondition won't update the status unless there's other change like Status as well.
	// Here it also help trigger time update in later updates.
	scheduleCondition := meta.FindStatusCondition(instance.Status.Conditions, string(modelv1alpha1.ModelAdapterConditionTypeScheduled))
	if scheduleCondition == nil {
		return fmt.Errorf("status condition %q not found in ModelAdapter %q", modelv1alpha1.ModelAdapterConditionTypeScheduled, klog.KObj(instance).String())
	}
	scheduleCondition.Status = metav1.ConditionFalse
	scheduleCondition.LastTransitionTime = metav1.Now()

	readyCondition := meta.FindStatusCondition(instance.Status.Conditions, string(modelv1alpha1.ModelAdapterConditionReady))
	if readyCondition == nil {
		return fmt.Errorf("status condition %q not found in ModelAdapter %q", modelv1alpha1.ModelAdapterConditionReady, klog.KObj(instance).String())
	}
	readyCondition.Status = metav1.ConditionFalse
	readyCondition.LastTransitionTime = metav1.Now()

	if err := r.updateStatus(ctx, instance, condition, *scheduleCondition, *readyCondition); err != nil {
		return err
	}

	return nil
}

// getActivePodsForModelAdapter retrieves all pods matching the selector and filters them to only include active ones
func (r *ModelAdapterReconciler) getActivePodsForModelAdapter(ctx context.Context, instance *modelv1alpha1.ModelAdapter) ([]corev1.Pod, error) {
	podList := &corev1.PodList{}
	listOpts := []client.ListOption{
		client.InNamespace(instance.GetNamespace()),
		client.MatchingLabels(instance.Spec.PodSelector.MatchLabels),
	}


Review comment (bug):
if we can not find the condition, should we return error here? I have concern that the state machine might not be correct so it never create this condition even we return errors and re-enter the reconciliation loop.

Another approach is to create the new scheduleCondition to unblock the next steps. 

Could you help double check the state machine and see which ways is better?

Code after:
func (r *ModelAdapterReconciler) updateStatus(ctx context.Context, instance *modelv1alpha1.ModelAdapter, conditions ...metav1.Condition) error {
	changed := false
	for _, condition := range conditions {
		if meta.SetStatusCondition(&instance.Status.Conditions, condition) {
			changed = true
		}
	}
	// TODO: sort the conditions based on LastTransitionTime if needed.
	klog.InfoS("model adapter reconcile", "Update CR status", instance.Name, "changed", changed, "status", instance.Status, "conditions", conditions)
	return r.Status().Update(ctx, instance)
}

func (r *ModelAdapterReconciler) clearModelAdapterInstanceList(ctx context.Context, instance *modelv1alpha1.ModelAdapter, stalePodName string) error {
	instance.Status.Instances = RemoveInstanceFromList(instance.Status.Instances, stalePodName)
	// remove instance means the lora has not targets at this moment.
	instance.Status.Phase = modelv1alpha1.ModelAdapterPending
	condition := NewCondition(string(modelv1alpha1.ModelAdapterFailed), metav1.ConditionTrue,
		StableInstanceFoundReason,
		fmt.Sprintf("Pod (%s/%s) is stale or invalid for model adapter (%s/%s), clean up the list", instance.GetNamespace(), stalePodName, instance.GetNamespace(), instance.Name))

	// We also need to update the scheduling and ready status to false
	// When the pod get migrated, we need to update the status with latest LastTransitionTime.
	// However, meta.SetStatusCondition won't update the status unless there's other change like Status as well.
	// Here it also help trigger time update in later updates.
	scheduleCondition := meta.FindStatusCondition(instance.Status.Conditions, string(modelv1alpha1.ModelAdapterConditionTypeScheduled))
	if scheduleCondition == nil {
		// if not found, we add one
		scheduleCondition = &metav1.Condition{
			Type:               string(modelv1alpha1.ModelAdapterConditionTypeScheduled),
			Status:             metav1.ConditionFalse,
			LastTransitionTime: metav1.Now(),
			Reason:             ConditionNotReason,
			Message:            "Condition was missing and initialized by controller",
		}
	} else {
		scheduleCondition.Status = metav1.ConditionFalse
		scheduleCondition.LastTransitionTime = metav1.Now()
	}

	readyCondition := meta.FindStatusCondition(instance.Status.Conditions, string(modelv1alpha1.ModelAdapterConditionReady))
	if readyCondition == nil {
		// if not found, we add one
		readyCondition = &metav1.Condition{
			Type:               string(modelv1alpha1.ModelAdapterConditionReady),
			Status:             metav1.ConditionFalse,
			LastTransitionTime: metav1.Now(),
			Reason:             ConditionNotReason,
			Message:            "Condition was missing and initialized by controller",
		}
	} else {
		readyCondition.Status = metav1.ConditionFalse