Skip to content

Commit fd9a2b9

Browse files
fix: Handle AlreadyExists and Conflict as non-errors for cluster RBAC
The controller-runtime cached client can return stale NotFound for cluster-scoped resources when the informer cache has not yet synced. This causes CreateOrUpdate to attempt a Create that fails with AlreadyExists. Retrying does not help because the cache remains stale during the short retry window. Treat AlreadyExists and Conflict as non-errors since the resource exists in the desired state. The next reconcile cycle will update its contents once the cache has synced. This follows the standard Kubernetes operator eventual-consistency pattern. Signed-off-by: ntkathole <nikhilkathole2683@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 7911f4d commit fd9a2b9

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

  • infra/feast-operator/internal/controller/authz

‎infra/feast-operator/internal/controller/authz/authz.go‎

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ import (
1212
apimeta "k8s.io/apimachinery/pkg/api/meta"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
"k8s.io/apimachinery/pkg/labels"
15-
"k8s.io/client-go/util/retry"
1615
"sigs.k8s.io/controller-runtime/pkg/client"
1716
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1817
"sigs.k8s.io/controller-runtime/pkg/log"
1918
)
2019

21-
func isRetriableClusterResourceError(err error) bool {
22-
return apierrors.IsConflict(err) || apierrors.IsAlreadyExists(err)
23-
}
24-
2520
const (
2621
authenticationAPIGroup = "authentication.k8s.io"
2722
verbCreate = "create"
@@ -151,17 +146,21 @@ func (authz *FeastAuthorization) createFeastRole() error {
151146

152147
func (authz *FeastAuthorization) createFeastClusterRole() error {
153148
logger := log.FromContext(authz.Handler.Context)
154-
return retry.OnError(retry.DefaultRetry, isRetriableClusterResourceError, func() error {
155-
clusterRole := authz.initFeastClusterRole()
156-
if op, err := controllerutil.CreateOrUpdate(authz.Handler.Context, authz.Handler.Client, clusterRole, controllerutil.MutateFn(func() error {
157-
return authz.setFeastClusterRole(clusterRole)
158-
})); err != nil {
159-
return err
160-
} else if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
161-
logger.Info("Successfully reconciled", "ClusterRole", clusterRole.Name, "operation", op)
162-
}
149+
clusterRole := authz.initFeastClusterRole()
150+
op, err := controllerutil.CreateOrUpdate(authz.Handler.Context, authz.Handler.Client, clusterRole, controllerutil.MutateFn(func() error {
151+
return authz.setFeastClusterRole(clusterRole)
152+
}))
153+
if apierrors.IsAlreadyExists(err) || apierrors.IsConflict(err) {
154+
logger.Info("ClusterRole conflict or already exists, will reconcile on next cycle", "ClusterRole", clusterRole.Name, "error", err)
163155
return nil
164-
})
156+
}
157+
if err != nil {
158+
return err
159+
}
160+
if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
161+
logger.Info("Successfully reconciled", "ClusterRole", clusterRole.Name, "operation", op)
162+
}
163+
return nil
165164
}
166165

167166
func (authz *FeastAuthorization) initFeastClusterRole() *rbacv1.ClusterRole {
@@ -233,17 +232,21 @@ func (authz *FeastAuthorization) setFeastClusterRoleBinding(clusterRoleBinding *
233232
// Create ClusterRoleBinding
234233
func (authz *FeastAuthorization) createFeastClusterRoleBinding() error {
235234
logger := log.FromContext(authz.Handler.Context)
236-
return retry.OnError(retry.DefaultRetry, isRetriableClusterResourceError, func() error {
237-
clusterRoleBinding := authz.initFeastClusterRoleBinding()
238-
if op, err := controllerutil.CreateOrUpdate(authz.Handler.Context, authz.Handler.Client, clusterRoleBinding, controllerutil.MutateFn(func() error {
239-
return authz.setFeastClusterRoleBinding(clusterRoleBinding)
240-
})); err != nil {
241-
return err
242-
} else if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
243-
logger.Info("Successfully reconciled", "ClusterRoleBinding", clusterRoleBinding.Name, "operation", op)
244-
}
235+
clusterRoleBinding := authz.initFeastClusterRoleBinding()
236+
op, err := controllerutil.CreateOrUpdate(authz.Handler.Context, authz.Handler.Client, clusterRoleBinding, controllerutil.MutateFn(func() error {
237+
return authz.setFeastClusterRoleBinding(clusterRoleBinding)
238+
}))
239+
if apierrors.IsAlreadyExists(err) || apierrors.IsConflict(err) {
240+
logger.Info("ClusterRoleBinding conflict or already exists, will reconcile on next cycle", "ClusterRoleBinding", clusterRoleBinding.Name, "error", err)
245241
return nil
246-
})
242+
}
243+
if err != nil {
244+
return err
245+
}
246+
if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
247+
logger.Info("Successfully reconciled", "ClusterRoleBinding", clusterRoleBinding.Name, "operation", op)
248+
}
249+
return nil
247250
}
248251

249252
func (authz *FeastAuthorization) initFeastRole() *rbacv1.Role {

0 commit comments

Comments
 (0)