Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReconcileRequeue for cluster resources was implemented #612

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions controllers/clusterresources/awsencryptionkey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/instaclustr/operator/apis/clusterresources/v1beta1"
"github.com/instaclustr/operator/pkg/instaclustr"
"github.com/instaclustr/operator/pkg/models"
"github.com/instaclustr/operator/pkg/ratelimiter"
"github.com/instaclustr/operator/pkg/scheduler"
)

Expand Down Expand Up @@ -70,36 +71,36 @@ func (r *AWSEncryptionKeyReconciler) Reconcile(ctx context.Context, req ctrl.Req
l.Info("AWS encryption key resource is not found",
"resource name", req.NamespacedName,
)
return models.ExitReconcile, nil
return ctrl.Result{}, nil
}

l.Error(err, "Unable to fetch AWS encryption key")
return models.ReconcileRequeue, err
return ctrl.Result{}, err
}

switch encryptionKey.Annotations[models.ResourceStateAnnotation] {
case models.CreatingEvent:
return r.handleCreate(ctx, encryptionKey, &l), nil
return r.handleCreate(ctx, encryptionKey, &l)
case models.DeletingEvent:
return r.handleDelete(ctx, encryptionKey, &l), nil
return r.handleDelete(ctx, encryptionKey, &l)
case models.GenericEvent:
l.Info("AWS encryption key event isn't handled",
"alias", encryptionKey.Spec.Alias,
"arn", encryptionKey.Spec.ARN,
"provider account name", encryptionKey.Spec.ProviderAccountName,
"request", req,
"event", encryptionKey.Annotations[models.ResourceStateAnnotation])
return models.ExitReconcile, nil
return ctrl.Result{}, nil
}

return models.ExitReconcile, nil
return ctrl.Result{}, nil
}

func (r *AWSEncryptionKeyReconciler) handleCreate(
ctx context.Context,
encryptionKey *v1beta1.AWSEncryptionKey,
l *logr.Logger,
) reconcile.Result {
) (ctrl.Result, error) {
if encryptionKey.Status.ID == "" {
l.Info(
"Creating AWS encryption key",
Expand All @@ -120,7 +121,7 @@ func (r *AWSEncryptionKeyReconciler) handleCreate(
"Resource creation on the Instaclustr is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
return ctrl.Result{}, err
}

r.EventRecorder.Eventf(
Expand All @@ -138,7 +139,7 @@ func (r *AWSEncryptionKeyReconciler) handleCreate(
"Resource status patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
return ctrl.Result{}, err
}

encryptionKey.Annotations[models.ResourceStateAnnotation] = models.CreatedEvent
Expand All @@ -154,7 +155,7 @@ func (r *AWSEncryptionKeyReconciler) handleCreate(
"Resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
return ctrl.Result{}, err
}

l.Info(
Expand All @@ -173,22 +174,22 @@ func (r *AWSEncryptionKeyReconciler) handleCreate(
"Resource status job creation is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
return ctrl.Result{}, err
}

r.EventRecorder.Eventf(
encryptionKey, models.Normal, models.Created,
"Resource status check job is started",
)

return models.ExitReconcile
return ctrl.Result{}, nil
}

func (r *AWSEncryptionKeyReconciler) handleDelete(
ctx context.Context,
encryptionKey *v1beta1.AWSEncryptionKey,
l *logr.Logger,
) reconcile.Result {
) (ctrl.Result, error) {
status, err := r.API.GetEncryptionKeyStatus(encryptionKey.Status.ID, instaclustr.AWSEncryptionKeyEndpoint)
if err != nil && !errors.Is(err, instaclustr.NotFound) {
l.Error(
Expand All @@ -202,7 +203,7 @@ func (r *AWSEncryptionKeyReconciler) handleDelete(
"Fetch resource from the Instaclustr API is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
return ctrl.Result{}, err
}

if status != nil {
Expand All @@ -219,7 +220,7 @@ func (r *AWSEncryptionKeyReconciler) handleDelete(
"Resource deletion on the Instaclustr is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
return ctrl.Result{}, err
}
r.EventRecorder.Eventf(
encryptionKey, models.Normal, models.DeletionStarted,
Expand All @@ -244,7 +245,7 @@ func (r *AWSEncryptionKeyReconciler) handleDelete(
"Resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
return ctrl.Result{}, err
}

l.Info("AWS encryption key has been deleted",
Expand All @@ -258,7 +259,7 @@ func (r *AWSEncryptionKeyReconciler) handleDelete(
"Resource is deleted",
)

return models.ExitReconcile
return ctrl.Result{}, nil
}

func (r *AWSEncryptionKeyReconciler) startEncryptionKeyStatusJob(encryptionKey *v1beta1.AWSEncryptionKey) error {
Expand Down Expand Up @@ -340,6 +341,8 @@ func (r *AWSEncryptionKeyReconciler) handleExternalDelete(ctx context.Context, k
// SetupWithManager sets up the controller with the Manager.
func (r *AWSEncryptionKeyReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(controller.Options{
RateLimiter: ratelimiter.NewItemExponentialFailureRateLimiterWithMaxTries(ratelimiter.DefaultBaseDelay, ratelimiter.DefaultMaxDelay)}).
For(&v1beta1.AWSEncryptionKey{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(event event.CreateEvent) bool {
if event.Object.GetDeletionTimestamp() != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import (
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

clusterresourcesv1beta1 "github.com/instaclustr/operator/apis/clusterresources/v1beta1"
"github.com/instaclustr/operator/pkg/instaclustr"
"github.com/instaclustr/operator/pkg/models"
"github.com/instaclustr/operator/pkg/ratelimiter"
"github.com/instaclustr/operator/pkg/scheduler"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have unsorted imports in the PR, please sort one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

)

Expand Down Expand Up @@ -65,46 +67,34 @@ func (r *AWSEndpointServicePrincipalReconciler) Reconcile(ctx context.Context, r
}, principal)
if err != nil {
if k8serrors.IsNotFound(err) {
return models.ExitReconcile, nil
return ctrl.Result{}, nil
}

l.Error(err, "Unable to fetch an AWS endpoint service principal resource")

return models.ReconcileRequeue, err
return ctrl.Result{}, err
}

// Handle resource deletion
if principal.DeletionTimestamp != nil {
err = r.handleDelete(ctx, l, principal)
if err != nil {
return models.ReconcileRequeue, err
}

return models.ExitReconcile, nil
return r.handleDelete(ctx, l, principal)
}

// Handle resource creation
if principal.Status.ID == "" {
err = r.handleCreate(ctx, l, principal)
if err != nil {
return models.ReconcileRequeue, nil
}

return models.ExitReconcile, nil
return r.handleCreate(ctx, l, principal)
}

return models.ExitReconcile, nil
return ctrl.Result{}, nil
}

func (r *AWSEndpointServicePrincipalReconciler) handleCreate(ctx context.Context, l logr.Logger, principal *clusterresourcesv1beta1.AWSEndpointServicePrincipal) error {
func (r *AWSEndpointServicePrincipalReconciler) handleCreate(ctx context.Context, l logr.Logger, principal *clusterresourcesv1beta1.AWSEndpointServicePrincipal) (ctrl.Result, error) {
b, err := r.API.CreateAWSEndpointServicePrincipal(principal.Spec)
if err != nil {
l.Error(err, "failed to create an AWS endpoint service principal resource on Instaclustr")
r.EventRecorder.Eventf(principal, models.Warning, models.CreationFailed,
"Failed to create an AWS endpoint service principal on Instaclustr. Reason: %v", err,
)

return err
return ctrl.Result{}, err
}

patch := principal.NewPatch()
Expand All @@ -115,7 +105,7 @@ func (r *AWSEndpointServicePrincipalReconciler) handleCreate(ctx context.Context
"Failed to parse an AWS endpoint service principal resource response from Instaclustr. Reason: %v", err,
)

return err
return ctrl.Result{}, err
}

err = r.Status().Patch(ctx, principal, patch)
Expand All @@ -125,7 +115,7 @@ func (r *AWSEndpointServicePrincipalReconciler) handleCreate(ctx context.Context
"Failed to patch an AWS endpoint service principal resource with its ID. Reason: %v", err,
)

return err
return ctrl.Result{}, err
}

controllerutil.AddFinalizer(principal, models.DeletionFinalizer)
Expand All @@ -136,7 +126,7 @@ func (r *AWSEndpointServicePrincipalReconciler) handleCreate(ctx context.Context
"Failed to patch an AWS endpoint service principal resource with finalizer. Reason: %v", err,
)

return err
return ctrl.Result{}, err
}

l.Info("AWS endpoint service principal resource has been created")
Expand All @@ -151,24 +141,24 @@ func (r *AWSEndpointServicePrincipalReconciler) handleCreate(ctx context.Context
"Failed to start status checker job. Reason: %w", err,
)

return err
return ctrl.Result{}, err
}
r.EventRecorder.Eventf(principal, models.Normal, models.Created,
"Status check job %s has been started", principal.GetJobID(scheduler.StatusChecker),
)

return nil
return ctrl.Result{}, nil
}

func (r *AWSEndpointServicePrincipalReconciler) handleDelete(ctx context.Context, logger logr.Logger, resource *clusterresourcesv1beta1.AWSEndpointServicePrincipal) error {
func (r *AWSEndpointServicePrincipalReconciler) handleDelete(ctx context.Context, logger logr.Logger, resource *clusterresourcesv1beta1.AWSEndpointServicePrincipal) (ctrl.Result, error) {
err := r.API.DeleteAWSEndpointServicePrincipal(resource.Status.ID)
if err != nil && !errors.Is(err, instaclustr.NotFound) {
logger.Error(err, "failed to delete an AWS endpoint service principal resource on Instaclustr")
r.EventRecorder.Eventf(resource, models.Warning, models.DeletionFailed,
"Failed to delete an AWS endpoint service principal on Instaclustr. Reason: %v", err,
)

return err
return ctrl.Result{}, err
}

patch := resource.NewPatch()
Expand All @@ -180,12 +170,12 @@ func (r *AWSEndpointServicePrincipalReconciler) handleDelete(ctx context.Context
"Failed to delete finalizer from an AWS endpoint service principal resource. Reason: %v", err,
)

return err
return ctrl.Result{}, err
}

logger.Info("AWS endpoint service principal resource has been deleted")

return nil
return ctrl.Result{}, nil
}

func (r *AWSEndpointServicePrincipalReconciler) startWatchStatusJob(ctx context.Context, resource *clusterresourcesv1beta1.AWSEndpointServicePrincipal) error {
Expand Down Expand Up @@ -247,6 +237,8 @@ func (r *AWSEndpointServicePrincipalReconciler) handleExternalDelete(ctx context
// SetupWithManager sets up the controller with the Manager.
func (r *AWSEndpointServicePrincipalReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(controller.Options{
RateLimiter: ratelimiter.NewItemExponentialFailureRateLimiterWithMaxTries(ratelimiter.DefaultBaseDelay, ratelimiter.DefaultMaxDelay)}).
For(&clusterresourcesv1beta1.AWSEndpointServicePrincipal{}).
Complete(r)
}
Loading