Skip to content

Commit

Permalink
refactor: uniform mark as ready logic
Browse files Browse the repository at this point in the history
Signed-off-by: Armando Ruocco <[email protected]>
  • Loading branch information
armru committed Oct 22, 2024
1 parent c7b9af9 commit 8b8d293
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 41 deletions.
7 changes: 7 additions & 0 deletions api/v1/publication_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ func (pub *Publication) SetAsFailed(err error) {
pub.Status.Ready = false
pub.Status.Error = err.Error()
}

// SetAsReady sets the subscription as working correctly
func (pub *Publication) SetAsReady() {
pub.Status.Error = ""
pub.Status.Ready = true
pub.Status.ObservedGeneration = pub.Generation
}
7 changes: 7 additions & 0 deletions api/v1/subscription_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ func (sub *Subscription) SetAsFailed(err error) {
sub.Status.Ready = false
sub.Status.Error = err.Error()
}

// SetAsReady sets the subscription as working correctly
func (sub *Subscription) SetAsReady() {
sub.Status.Error = ""
sub.Status.Ready = true
sub.Status.ObservedGeneration = sub.Generation
}
18 changes: 17 additions & 1 deletion internal/management/controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controller

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -38,3 +37,20 @@ func markAsFailed(
resource.SetAsFailed(err)
return cli.Status().Patch(ctx, resource, client.MergeFrom(oldResource))
}

type markableAsReady interface {
client.Object
SetAsReady()
}

// markAsReady marks the reconciliation as succeeded inside the resource
func markAsReady(
ctx context.Context,
cli client.Client,
resource markableAsReady,
) error {
oldResource := resource.DeepCopyObject().(markableAsReady)
resource.SetAsReady()

return cli.Status().Patch(ctx, resource, client.MergeFrom(oldResource))
}
21 changes: 1 addition & 20 deletions internal/management/controller/publication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (r *PublicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{RequeueAfter: databaseReconciliationInterval}, markAsFailed(ctx, r.Client, &publication, err)
}

return r.succeededReconciliation(ctx, &publication)
return ctrl.Result{RequeueAfter: subscriptionReconciliationInterval}, markAsReady(ctx, r.Client, &publication)
}

func (r *PublicationReconciler) reconcileFinalizer(ctx context.Context, publication apiv1.Publication) error {
Expand Down Expand Up @@ -168,25 +168,6 @@ func (r *PublicationReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

// succeededReconciliation marks the reconciliation as succeeded
func (r *PublicationReconciler) succeededReconciliation(
ctx context.Context,
publication *apiv1.Publication,
) (ctrl.Result, error) {
oldPublication := publication.DeepCopy()
publication.Status.Error = ""
publication.Status.Ready = true
publication.Status.ObservedGeneration = publication.Generation

if err := r.Client.Status().Patch(ctx, publication, client.MergeFrom(oldPublication)); err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{
RequeueAfter: publicationReconciliationInterval,
}, nil
}

// GetCluster gets the managed cluster through the client
func (r *PublicationReconciler) GetCluster(ctx context.Context) (*apiv1.Cluster, error) {
var cluster apiv1.Cluster
Expand Down
22 changes: 2 additions & 20 deletions internal/management/controller/subscription_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ func (r *SubscriptionReconciler) Reconcile(ctx context.Context, req ctrl.Request
)
}

return r.succeededReconciliation(
return ctrl.Result{RequeueAfter: subscriptionReconciliationInterval}, markAsReady(
ctx,
r.Client,
&subscription,
)
}
Expand All @@ -184,25 +185,6 @@ func (r *SubscriptionReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

// succeededReconciliation marks the reconciliation as succeeded
func (r *SubscriptionReconciler) succeededReconciliation(
ctx context.Context,
subscription *apiv1.Subscription,
) (ctrl.Result, error) {
oldSubscription := subscription.DeepCopy()
subscription.Status.Error = ""
subscription.Status.Ready = true
subscription.Status.ObservedGeneration = subscription.Generation

if err := r.Client.Status().Patch(ctx, subscription, client.MergeFrom(oldSubscription)); err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{
RequeueAfter: subscriptionReconciliationInterval,
}, nil
}

// GetCluster gets the managed cluster through the client
func (r *SubscriptionReconciler) GetCluster(ctx context.Context) (*apiv1.Cluster, error) {
var cluster apiv1.Cluster
Expand Down

0 comments on commit 8b8d293

Please sign in to comment.