Skip to content

Commit

Permalink
Aggregate conditions in CR
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Kalenyuk <akalenyu@redhat.com>
  • Loading branch information
akalenyu committed Mar 19, 2023
1 parent f3cc58c commit 69b30de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
23 changes: 11 additions & 12 deletions pkg/sdk/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/go-logr/logr"
conditions "github.com/openshift/custom-resource-status/conditions/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -465,6 +464,7 @@ func (r *Reconciler) ReconcileError(cr client.Object, message string) (reconcile
// CheckDegraded checks whether the deployment is degraded and updates CR status conditions accordingly
func (r *Reconciler) CheckDegraded(logger logr.Logger, cr client.Object) (bool, error) {
degraded := false
requiresIntervention := false

deployments, err := r.GetAllDeployments(cr)
if err != nil {
Expand All @@ -478,6 +478,10 @@ func (r *Reconciler) CheckDegraded(logger logr.Logger, cr client.Object) (bool,
return true, err
}

if !sdk.CheckDeploymentRequiresIntervention(deployment) {
requiresIntervention = true
}

if !sdk.CheckDeploymentReady(deployment) {
degraded = true
break
Expand All @@ -486,18 +490,13 @@ func (r *Reconciler) CheckDegraded(logger logr.Logger, cr client.Object) (bool,

logger.Info("Degraded check", "Degraded", degraded)

// If deployed and degraded, mark degraded, otherwise we are still deploying or not degraded.
// If not upgrading, aggregate deployment conditions in CR
status := r.status(cr)
if degraded && status.Phase == sdkapi.PhaseDeployed {
conditions.SetStatusCondition(&status.Conditions, conditions.Condition{
Type: conditions.ConditionDegraded,
Status: corev1.ConditionTrue,
})
} else {
conditions.SetStatusCondition(&status.Conditions, conditions.Condition{
Type: conditions.ConditionDegraded,
Status: corev1.ConditionFalse,
})
if degraded && requiresIntervention && !sdk.IsUpgrading(status) {
sdk.MarkCrFailed(cr, status, "DeploymentError", "One or more of the deployments require intervention to progress", r.recorder)
if err := r.CrUpdateStatus(status.Phase, cr); err != nil {
return true, err
}
}

logger.Info("Finished degraded check", "conditions", status.Conditions)
Expand Down
22 changes: 22 additions & 0 deletions pkg/sdk/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ func CheckDeploymentReady(deployment *appsv1.Deployment) bool {
return true
}

func CheckDeploymentRequiresIntervention(deployment *appsv1.Deployment) bool {
var progressing, available bool
if cond := GetDeploymentCondition(deployment.Status, appsv1.DeploymentAvailable); cond != nil && cond.Status == v1.ConditionTrue {
available = true
}
if cond := GetDeploymentCondition(deployment.Status, appsv1.DeploymentProgressing); cond != nil && cond.Status == v1.ConditionTrue {
progressing = true
}

return !progressing && !available
}

func GetDeploymentCondition(status appsv1.DeploymentStatus, condType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}

func NewDefaultInstance(obj client.Object) client.Object {
typ := reflect.ValueOf(obj).Elem().Type()
return reflect.New(typ).Interface().(client.Object)
Expand Down

0 comments on commit 69b30de

Please sign in to comment.