Skip to content

Commit eceeafa

Browse files
Update nimservice status when deployment fails
Signed-off-by: Vishesh Tanksale <[email protected]>
1 parent 49bb5cf commit eceeafa

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

internal/conditions/conditions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (u *updater) SetConditionsFailed(ctx context.Context, cr *appsv1alpha1.NIMS
121121
Reason: reason,
122122
Message: message,
123123
})
124-
cr.Status.State = v1alpha1.NIMServiceStatusNotReady
124+
cr.Status.State = v1alpha1.NIMServiceStatusFailed
125125
return u.client.Status().Update(ctx, cr)
126126
}
127127

internal/controller/platform/standalone/nimservice.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,17 @@ func (r *NIMServiceReconciler) reconcileNIMService(ctx context.Context, nimServi
203203
}
204204

205205
// Wait for deployment
206-
ready, err := r.isDeploymentReady(ctx, &namespacedName)
206+
msg, ready, err := r.isDeploymentReady(ctx, &namespacedName)
207207
if err != nil {
208208
return ctrl.Result{}, err
209209
}
210210

211211
if !ready {
212212
// Update status as NotReady
213-
err = r.updater.SetConditionsNotReady(ctx, nimService, conditions.NotReady, "Deployment is not ready")
213+
err = r.updater.SetConditionsNotReady(ctx, nimService, conditions.NotReady, msg)
214214
} else {
215215
// Update status as ready
216-
err = r.updater.SetConditionsReady(ctx, nimService, conditions.Ready, "Deployment is ready")
216+
err = r.updater.SetConditionsReady(ctx, nimService, conditions.Ready, msg)
217217
}
218218

219219
if err != nil {
@@ -278,22 +278,40 @@ func (r *NIMServiceReconciler) renderAndSyncResource(ctx context.Context, nimSer
278278
}
279279

280280
// CheckDeploymentReadiness checks if the Deployment is ready
281-
func (r *NIMServiceReconciler) isDeploymentReady(ctx context.Context, namespacedName *types.NamespacedName) (bool, error) {
282-
dep := &appsv1.Deployment{}
283-
err := r.Get(ctx, client.ObjectKey{Name: namespacedName.Name, Namespace: namespacedName.Namespace}, dep)
281+
func (r *NIMServiceReconciler) isDeploymentReady(ctx context.Context, namespacedName *types.NamespacedName) (string, bool, error) {
282+
deployment := &appsv1.Deployment{}
283+
err := r.Get(ctx, client.ObjectKey{Name: namespacedName.Name, Namespace: namespacedName.Namespace}, deployment)
284284
if err != nil {
285285
if errors.IsNotFound(err) {
286-
return false, nil
286+
return "", false, nil
287287
}
288-
return false, err
288+
return "", false, err
289289
}
290290

291-
for _, cond := range dep.Status.Conditions {
292-
if cond.Type == appsv1.DeploymentAvailable && cond.Status == corev1.ConditionTrue {
293-
return true, nil
291+
cond := getDeploymentCondition(deployment.Status, appsv1.DeploymentProgressing)
292+
if cond != nil && cond.Reason == "ProgressDeadlineExceeded" {
293+
return fmt.Sprintf("deployment %q exceeded its progress deadline", deployment.Name), false, fmt.Errorf("deployment %q exceeded its progress deadline", deployment.Name)
294+
}
295+
if deployment.Spec.Replicas != nil && deployment.Status.UpdatedReplicas < *deployment.Spec.Replicas {
296+
return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d out of %d new replicas have been updated...\n", deployment.Name, deployment.Status.UpdatedReplicas, *deployment.Spec.Replicas), false, nil
297+
}
298+
if deployment.Status.Replicas > deployment.Status.UpdatedReplicas {
299+
return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d old replicas are pending termination...\n", deployment.Name, deployment.Status.Replicas-deployment.Status.UpdatedReplicas), false, nil
300+
}
301+
if deployment.Status.AvailableReplicas < deployment.Status.UpdatedReplicas {
302+
return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d of %d updated replicas are available...\n", deployment.Name, deployment.Status.AvailableReplicas, deployment.Status.UpdatedReplicas), false, nil
303+
}
304+
return fmt.Sprintf("deployment %q successfully rolled out\n", deployment.Name), true, nil
305+
}
306+
307+
func getDeploymentCondition(status appsv1.DeploymentStatus, condType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
308+
for i := range status.Conditions {
309+
c := status.Conditions[i]
310+
if c.Type == condType {
311+
return &c
294312
}
295313
}
296-
return false, nil
314+
return nil
297315
}
298316

299317
func (r *NIMServiceReconciler) syncResource(ctx context.Context, obj client.Object, desired client.Object, namespacedName types.NamespacedName) error {

internal/controller/platform/standalone/standalone.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ func (s *Standalone) Sync(ctx context.Context, r shared.Reconciler, resource cli
101101
reconciler := NewNIMServiceReconciler(r)
102102
reconciler.renderer = render.NewRenderer(ManifestsDir)
103103
logger.Info("Reconciling NIMService instance")
104-
return reconciler.reconcileNIMService(ctx, nimService)
104+
result, err := reconciler.reconcileNIMService(ctx, nimService)
105+
if err != nil {
106+
errConditionUpdate := reconciler.updater.SetConditionsFailed(ctx, nimService, conditions.Failed, err.Error())
107+
if errConditionUpdate != nil {
108+
logger.Error(err, "Unable to update status")
109+
return result, errConditionUpdate
110+
}
111+
}
112+
return result, err
105113
}
106114
return ctrl.Result{}, errors.NewBadRequest("invalid resource type")
107115
}

0 commit comments

Comments
 (0)