Skip to content

Commit

Permalink
[Entitystore] Use updater to set status conditions (#289)
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Ramachandra Sekar <[email protected]>
Co-authored-by: Varun Ramachandra Sekar <[email protected]>
  • Loading branch information
varunrsekar and varunrsekar authored Jan 18, 2025
1 parent 3458a2c commit 850b403
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 96 deletions.
129 changes: 104 additions & 25 deletions internal/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
NotReady = "NotReady"
// Failed indicates that the service has failed
Failed = "Failed"
// ValidationFailed indicates that the service CR has failed validations
ValidationFailed = "ValidationFailed"
// ReasonServiceAccountFailed indicates that the creation of serviceaccount has failed
ReasonServiceAccountFailed = "ServiceAccountFailed"
// ReasonRoleFailed indicates that the creation of serviceaccount has failed
Expand Down Expand Up @@ -75,12 +77,16 @@ func NewUpdater(c client.Client) Updater {
}

func (u *updater) SetConditionsReady(ctx context.Context, obj client.Object, reason, message string) error {
if cr, ok := obj.(*appsv1alpha1.NIMService); ok {
switch cr := obj.(type) {
case *appsv1alpha1.NIMService:
return u.SetConditionsReadyNIMService(ctx, cr, reason, message)
} else if gr, ok := obj.(*appsv1alpha1.NemoGuardrail); ok {
return u.SetConditionsReadyNemoGuardrail(ctx, gr, reason, message)
case *appsv1alpha1.NemoGuardrail:
return u.SetConditionsReadyNemoGuardrail(ctx, cr, reason, message)
case *appsv1alpha1.NemoEntitystore:
return u.SetConditionsReadyNemoEntitystore(ctx, cr, reason, message)
default:
return fmt.Errorf("unknown CRD type for %v", obj)
}
return fmt.Errorf("unknown CRD type for %v", obj)
}

func (u *updater) SetConditionsReadyNIMService(ctx context.Context, cr *appsv1alpha1.NIMService, reason, message string) error {
Expand All @@ -100,21 +106,51 @@ func (u *updater) SetConditionsReadyNIMService(ctx context.Context, cr *appsv1al
return u.updateNIMServiceStatus(ctx, cr)
}

func (u *updater) SetConditionsReadyNemoGuardrail(ctx context.Context, gr *appsv1alpha1.NemoGuardrail, reason, message string) error {
meta.SetStatusCondition(&gr.Status.Conditions, metav1.Condition{
func (u *updater) SetConditionsReadyNemoGuardrail(ctx context.Context, cr *appsv1alpha1.NemoGuardrail, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&gr.Status.Conditions, metav1.Condition{
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Failed,
Status: metav1.ConditionFalse,
Reason: Ready,
})
cr.Status.State = v1alpha1.NemoGuardrailStatusReady
return u.updateNemoGuardrailStatus(ctx, cr)
}

func (u *updater) SetConditionsReadyNemoEntitystore(ctx context.Context, cr *appsv1alpha1.NemoEntitystore, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Failed,
Status: metav1.ConditionFalse,
Reason: Ready,
})
gr.Status.State = v1alpha1.NIMServiceStatusReady
return u.updateNemoGuardrailStatus(ctx, gr)
cr.Status.State = v1alpha1.NemoEntitystoreStatusReady
return u.updateNemoEntitystoreStatus(ctx, cr)
}

func (u *updater) SetConditionsNotReady(ctx context.Context, obj client.Object, reason, message string) error {
switch cr := obj.(type) {
case *appsv1alpha1.NIMService:
return u.SetConditionsNotReadyNIMService(ctx, cr, reason, message)
case *appsv1alpha1.NemoGuardrail:
return u.SetConditionsNotReadyNemoGuardrail(ctx, cr, reason, message)
case *appsv1alpha1.NemoEntitystore:
return u.SetConditionsNotReadyNemoEntitystore(ctx, cr, reason, message)
default:
return fmt.Errorf("unknown CRD type for %v", obj)
}
}

func (u *updater) SetConditionsNotReadyNIMService(ctx context.Context, cr *appsv1alpha1.NIMService, reason, message string) error {
Expand All @@ -135,40 +171,53 @@ func (u *updater) SetConditionsNotReadyNIMService(ctx context.Context, cr *appsv
return u.updateNIMServiceStatus(ctx, cr)
}

func (u *updater) SetConditionsNotReadyNemoGuardrail(ctx context.Context, gr *appsv1alpha1.NemoGuardrail, reason, message string) error {
meta.SetStatusCondition(&gr.Status.Conditions, metav1.Condition{
func (u *updater) SetConditionsNotReadyNemoGuardrail(ctx context.Context, cr *appsv1alpha1.NemoGuardrail, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionFalse,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&gr.Status.Conditions, metav1.Condition{
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Failed,
Status: metav1.ConditionFalse,
Reason: Ready,
Message: message,
})
gr.Status.State = v1alpha1.NemoGuardrailStatusNotReady
return u.updateNemoGuardrailStatus(ctx, gr)
cr.Status.State = v1alpha1.NemoGuardrailStatusNotReady
return u.updateNemoGuardrailStatus(ctx, cr)
}

func (u *updater) SetConditionsNotReady(ctx context.Context, obj client.Object, reason, message string) error {
if cr, ok := obj.(*appsv1alpha1.NIMService); ok {
return u.SetConditionsNotReadyNIMService(ctx, cr, reason, message)
} else if gr, ok := obj.(*appsv1alpha1.NemoGuardrail); ok {
return u.SetConditionsNotReadyNemoGuardrail(ctx, gr, reason, message)
}
return fmt.Errorf("unknown CRD type for %v", obj)
func (u *updater) SetConditionsNotReadyNemoEntitystore(ctx context.Context, cr *appsv1alpha1.NemoEntitystore, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionFalse,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Failed,
Status: metav1.ConditionFalse,
Reason: Ready,
Message: message,
})
cr.Status.State = v1alpha1.NemoEntitystoreStatusNotReady
return u.updateNemoEntitystoreStatus(ctx, cr)
}

func (u *updater) SetConditionsFailed(ctx context.Context, obj client.Object, reason, message string) error {
if cr, ok := obj.(*appsv1alpha1.NIMService); ok {
switch cr := obj.(type) {
case *appsv1alpha1.NIMService:
return u.SetConditionsFailedNIMService(ctx, cr, reason, message)
} else if gr, ok := obj.(*appsv1alpha1.NemoGuardrail); ok {
return u.SetConditionsFailedNemoGuardrail(ctx, gr, reason, message)
case *appsv1alpha1.NemoGuardrail:
return u.SetConditionsFailedNemoGuardrail(ctx, cr, reason, message)
case *appsv1alpha1.NemoEntitystore:
return u.SetConditionsFailedNemoEntitystore(ctx, cr, reason, message)
default:
return fmt.Errorf("unknown CRD type for %v", obj)
}
return fmt.Errorf("unknown CRD type for %v", obj)
}

func (u *updater) SetConditionsFailedNIMService(ctx context.Context, cr *appsv1alpha1.NIMService, reason, message string) error {
Expand Down Expand Up @@ -205,6 +254,23 @@ func (u *updater) SetConditionsFailedNemoGuardrail(ctx context.Context, cr *apps
return u.updateNemoGuardrailStatus(ctx, cr)
}

func (u *updater) SetConditionsFailedNemoEntitystore(ctx context.Context, cr *appsv1alpha1.NemoEntitystore, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Ready,
Status: metav1.ConditionFalse,
Reason: Failed,
})

meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: Failed,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})
cr.Status.State = v1alpha1.NemoEntitystoreStatusFailed
return u.updateNemoEntitystoreStatus(ctx, cr)
}

func (u *updater) updateNIMServiceStatus(ctx context.Context, cr *appsv1alpha1.NIMService) error {

obj := &appsv1alpha1.NIMService{}
Expand Down Expand Up @@ -232,6 +298,19 @@ func (u *updater) updateNemoGuardrailStatus(ctx context.Context, cr *appsv1alpha
return nil
}

func (u *updater) updateNemoEntitystoreStatus(ctx context.Context, cr *appsv1alpha1.NemoEntitystore) error {
obj := &appsv1alpha1.NemoEntitystore{}
errGet := u.client.Get(ctx, types.NamespacedName{Name: cr.Name, Namespace: cr.GetNamespace()}, obj)
if errGet != nil {
return errGet
}
obj.Status = cr.Status
if err := u.client.Status().Update(ctx, obj); err != nil {
return err
}
return nil
}

// UpdateCondition updates the given condition into the conditions list
func UpdateCondition(conditions *[]metav1.Condition, conditionType string, status metav1.ConditionStatus, reason, message string) {
for i := range *conditions {
Expand Down
77 changes: 6 additions & 71 deletions internal/controller/nemo_entitystore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -278,6 +277,10 @@ func (r *NemoEntitystoreReconciler) reconcileNemoEntitystore(ctx context.Context
NemoEntitystore.Normalize()
err = NemoEntitystore.Validate()
if err != nil {
statusError := r.updater.SetConditionsFailed(ctx, NemoEntitystore, conditions.ValidationFailed, err.Error())
if statusError != nil {
logger.Error(statusError, "failed to update status", "NemoEntitystore", NemoEntitystore.GetName())
}
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -384,12 +387,12 @@ func (r *NemoEntitystoreReconciler) reconcileNemoEntitystore(ctx context.Context

if !ready {
// Update status as NotReady
err = r.SetConditionsNotReady(ctx, NemoEntitystore, conditions.NotReady, msg)
err = r.updater.SetConditionsNotReady(ctx, NemoEntitystore, conditions.NotReady, msg)
r.GetEventRecorder().Eventf(NemoEntitystore, corev1.EventTypeNormal, conditions.NotReady,
"NemoEntitystore %s not ready yet, msg: %s", NemoEntitystore.Name, msg)
} else {
// Update status as ready
err = r.SetConditionsReady(ctx, NemoEntitystore, conditions.Ready, msg)
err = r.updater.SetConditionsReady(ctx, NemoEntitystore, conditions.Ready, msg)
r.GetEventRecorder().Eventf(NemoEntitystore, corev1.EventTypeNormal, conditions.Ready,
"NemoEntitystore %s ready, msg: %s", NemoEntitystore.Name, msg)
}
Expand Down Expand Up @@ -540,71 +543,3 @@ func (r *NemoEntitystoreReconciler) cleanupResource(ctx context.Context, obj cli
logger.V(2).Info("NIM Service object changed, deleting ", "obj", obj)
return nil
}

func (r *NemoEntitystoreReconciler) SetConditionsReady(ctx context.Context, cr *appsv1alpha1.NemoEntitystore, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: conditions.Ready,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: conditions.Failed,
Status: metav1.ConditionFalse,
Reason: conditions.Ready,
})

cr.Status.State = appsv1alpha1.NemoEntitystoreStatusReady
return r.updateNemoEntitystoreStatus(ctx, cr)
}

func (r *NemoEntitystoreReconciler) SetConditionsNotReady(ctx context.Context, cr *appsv1alpha1.NemoEntitystore, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: conditions.Ready,
Status: metav1.ConditionFalse,
Reason: reason,
Message: message,
})

meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: conditions.Failed,
Status: metav1.ConditionFalse,
Reason: conditions.Ready,
Message: message,
})

cr.Status.State = appsv1alpha1.NemoEntitystoreStatusNotReady
return r.updateNemoEntitystoreStatus(ctx, cr)
}

func (r *NemoEntitystoreReconciler) SetConditionsFailed(ctx context.Context, cr *appsv1alpha1.NemoEntitystore, reason, message string) error {
meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: conditions.Ready,
Status: metav1.ConditionFalse,
Reason: conditions.Failed,
})

meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: conditions.Failed,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
})
cr.Status.State = appsv1alpha1.NemoEntitystoreStatusFailed
return r.updateNemoEntitystoreStatus(ctx, cr)
}

func (r *NemoEntitystoreReconciler) updateNemoEntitystoreStatus(ctx context.Context, cr *appsv1alpha1.NemoEntitystore) error {

obj := &appsv1alpha1.NemoEntitystore{}
errGet := r.Get(ctx, types.NamespacedName{Name: cr.Name, Namespace: cr.GetNamespace()}, obj)
if errGet != nil {
return errGet
}
obj.Status = cr.Status
if err := r.Status().Update(ctx, obj); err != nil {
return err
}
return nil
}

0 comments on commit 850b403

Please sign in to comment.