Skip to content

Commit

Permalink
use the built-in RetryOnConflict function (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealak12 authored Nov 18, 2023
1 parent 161c945 commit e8709bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
14 changes: 5 additions & 9 deletions internal/controllers/s3bucket/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package s3bucket

import (
"context"
"strings"

genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"

"github.com/opdev/subreconciler"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand Down Expand Up @@ -67,12 +65,10 @@ func (r *Reconciler) updateBucketStatus(ctx context.Context,

if !apiequality.Semantic.DeepEqual(r.s3Bucket.Status, status) {
r.s3Bucket.Status = status
if err := r.Status().Update(ctx, r.s3Bucket); err != nil {
if strings.Contains(err.Error(), genericregistry.OptimisticLockErrorMsg) {
r.logger.Info("re-queuing item due to optimistic locking on resource", "error", err.Error())
} else {
r.logger.Error(err, "failed to update s3 bucket")
}
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return r.Status().Update(ctx, r.s3Bucket)
}); err != nil {
r.logger.Error(err, "failed to update s3 bucket")
return subreconciler.Requeue()
}
}
Expand Down
33 changes: 13 additions & 20 deletions internal/controllers/s3userclaim/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
goerrors "errors"
"fmt"
"strings"

"github.com/ceph/go-ceph/rgw/admin"
"github.com/opdev/subreconciler"
Expand All @@ -15,8 +14,8 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/client-go/tools/reference"
"k8s.io/client-go/util/retry"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -241,12 +240,10 @@ func (r *Reconciler) updateS3UserClaimStatus(ctx context.Context) (*ctrl.Result,

if !apiequality.Semantic.DeepEqual(r.s3UserClaim.Status, status) {
r.s3UserClaim.Status = status
if err := r.Status().Update(ctx, r.s3UserClaim); err != nil {
if strings.Contains(err.Error(), genericregistry.OptimisticLockErrorMsg) {
r.logger.Info("re-queuing item due to optimistic locking on resource", "error", err.Error())
} else {
r.logger.Error(err, "failed to update s3 user claim")
}
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return r.Status().Update(ctx, r.s3UserClaim)
}); err != nil {
r.logger.Error(err, "failed to update s3 user claim")
return subreconciler.Requeue()
}
}
Expand Down Expand Up @@ -285,12 +282,10 @@ func (r *Reconciler) updateNamespaceQuotaStatus(ctx context.Context, addCurrentQ

if !apiequality.Semantic.DeepEqual(quota.Status, *status) {
quota.Status = *status
if err := r.Status().Update(ctx, &quota); err != nil {
if strings.Contains(err.Error(), genericregistry.OptimisticLockErrorMsg) {
r.logger.Info("re-queuing item due to optimistic locking on resource", "error", err.Error())
} else {
r.logger.Error(err, "failed to update namespace quota status")
}
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return r.Status().Update(ctx, &quota)
}); err != nil {
r.logger.Error(err, "failed to update namespace quota status")
return subreconciler.Requeue()
}
}
Expand Down Expand Up @@ -335,12 +330,10 @@ func (r *Reconciler) updateClusterQuotaStatus(ctx context.Context, addCurrentQuo

if !apiequality.Semantic.DeepEqual(clusterQuota.Status, *status) {
clusterQuota.Status = *status
if err := r.Status().Update(ctx, clusterQuota); err != nil {
if strings.Contains(err.Error(), genericregistry.OptimisticLockErrorMsg) {
r.logger.Info("re-queuing item due to optimistic locking on resource", "error", err.Error())
} else {
r.logger.Error(err, "failed to update cluster resource quota status")
}
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return r.Status().Update(ctx, clusterQuota)
}); err != nil {
r.logger.Error(err, "failed to update cluster resource quota status")
return subreconciler.Requeue()
}
}
Expand Down

0 comments on commit e8709bf

Please sign in to comment.