From 8fc0b7c3e8bd4c544bba2aaa77e30aa1238b3d6b Mon Sep 17 00:00:00 2001 From: raaizik <132667934+raaizik@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:47:24 +0200 Subject: [PATCH] Adds status to DRClusterConfig Adds a basic status to DRClusterConfig reflecting success or failure of reconciliation Signed-off-by: raaizik <132667934+raaizik@users.noreply.github.com> --- api/v1alpha1/drclusterconfig_types.go | 13 ++++++++++ ...ramendr.openshift.io_drclusterconfigs.yaml | 8 +++++++ .../controller/drclusterconfig_controller.go | 24 +++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/api/v1alpha1/drclusterconfig_types.go b/api/v1alpha1/drclusterconfig_types.go index 9113b4898..652143feb 100644 --- a/api/v1alpha1/drclusterconfig_types.go +++ b/api/v1alpha1/drclusterconfig_types.go @@ -29,12 +29,25 @@ type DRClusterConfigSpec struct { // TODO: PeerClusters []ClusterID; to decide if we really need this! } +type DRClusterConfigState string + +const ( + DRClusterConfigFailed = DRClusterConfigState("Failed") + DRClusterConfigReady = DRClusterConfigState("Ready") +) + // DRClusterConfigStatus defines the observed state of DRClusterConfig type DRClusterConfigStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file // TODO: handle no status for this resource, and remove required RBAC/kubebuilder artifacts for the same + + Phase DRClusterConfigState `json:"phase,omitempty"` + + // ObservedGeneration is the last generation change the operator has dealt with + //+optional + ObservedGeneration int64 `json:"observedGeneration,omitempty"` } //+kubebuilder:object:root=true diff --git a/config/crd/bases/ramendr.openshift.io_drclusterconfigs.yaml b/config/crd/bases/ramendr.openshift.io_drclusterconfigs.yaml index 6980852ce..97fcae894 100644 --- a/config/crd/bases/ramendr.openshift.io_drclusterconfigs.yaml +++ b/config/crd/bases/ramendr.openshift.io_drclusterconfigs.yaml @@ -65,6 +65,14 @@ spec: type: object status: description: DRClusterConfigStatus defines the observed state of DRClusterConfig + properties: + observedGeneration: + description: ObservedGeneration is the last generation change the + operator has dealt with + format: int64 + type: integer + phase: + type: string type: object type: object served: true diff --git a/internal/controller/drclusterconfig_controller.go b/internal/controller/drclusterconfig_controller.go index abed094c6..2088c960e 100644 --- a/internal/controller/drclusterconfig_controller.go +++ b/internal/controller/drclusterconfig_controller.go @@ -77,11 +77,22 @@ func (r *DRClusterConfigReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } + var res reconcile.Result + var err error + if util.ResourceIsDeleted(drCConfig) { - return r.processDeletion(ctx, log, drCConfig) + res, err = r.processDeletion(ctx, log, drCConfig) + } else { + res, err = r.processCreateOrUpdate(ctx, log, drCConfig) + } + + // Apply status changes to the DRClusterConfig + statusError := r.Client.Status().Update(ctx, drCConfig) + if statusError != nil { + log.Error(statusError, "Failed to update DRClusterConfig status.") } - return r.processCreateOrUpdate(ctx, log, drCConfig) + return res, err } func (r *DRClusterConfigReconciler) GetDRClusterConfig(ctx context.Context) (*ramen.DRClusterConfig, error) { @@ -110,6 +121,7 @@ func (r *DRClusterConfigReconciler) processDeletion( ) (ctrl.Result, error) { if err := r.pruneClusterClaims(ctx, log, []string{}); err != nil { log.Info("Reconcile error", "error", err) + drCConfig.Status.Phase = ramen.DRClusterConfigFailed return ctrl.Result{Requeue: true}, err } @@ -118,11 +130,13 @@ func (r *DRClusterConfigReconciler) processDeletion( RemoveFinalizer(drCConfigFinalizerName). Update(ctx, r.Client); err != nil { log.Info("Reconcile error", "error", err) + drCConfig.Status.Phase = ramen.DRClusterConfigFailed return ctrl.Result{Requeue: true}, fmt.Errorf("failed to remove finalizer for DRClusterConfig resource, %w", err) } + drCConfig.Status.Phase = ramen.DRClusterConfigReady return ctrl.Result{}, nil } @@ -168,23 +182,29 @@ func (r *DRClusterConfigReconciler) processCreateOrUpdate( AddFinalizer(drCConfigFinalizerName). Update(ctx, r.Client); err != nil { log.Info("Reconcile error", "error", err) + drCConfig.Status.Phase = ramen.DRClusterConfigFailed return ctrl.Result{Requeue: true}, fmt.Errorf("failed to add finalizer for DRClusterConfig resource, %w", err) } allSurvivors, err := r.CreateClassClaims(ctx, log) if err != nil { + drCConfig.Status.Phase = ramen.DRClusterConfigFailed log.Info("Reconcile error", "error", err) return ctrl.Result{Requeue: true}, err } if err := r.pruneClusterClaims(ctx, log, allSurvivors); err != nil { + drCConfig.Status.Phase = ramen.DRClusterConfigFailed log.Info("Reconcile error", "error", err) return ctrl.Result{Requeue: true}, err } + drCConfig.Status.Phase = ramen.DRClusterConfigReady + drCConfig.Status.ObservedGeneration = drCConfig.Generation + return ctrl.Result{}, nil }