Skip to content

Commit

Permalink
issue-528-handling-user-errors-for-Cassandra
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksiienkoMykyta committed Oct 3, 2023
1 parent 3d59a9a commit 60319ce
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
48 changes: 36 additions & 12 deletions controllers/clusterresources/cassandrauser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,29 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if event == models.CreatingEvent {
l.Info("Creating user", "user", u, "cluster ID", clusterID)

err = r.API.CreateUser(u.ToInstAPI(username, password), clusterID, instaclustr.CassandraBundleUser)
exists, err := CheckIfUserExistsOnInstaclustrAPI(username, clusterID, instaclustr.CassandraBundleUser, r.API)
if err != nil {
l.Error(err, "Cannot create a user for the Cassandra cluster",
"cluster ID", clusterID,
"username", username)
r.EventRecorder.Eventf(u, models.Warning, models.CreatingEvent,
"Cannot create user. Reason: %v", err)

l.Error(err, "Cannot check if user exists ")
r.EventRecorder.Eventf(
u, models.Warning, models.CreationFailed,
"Cannot check if user exists. Reason: %v", err,
)
return models.ReconcileRequeue, nil
}

if !exists {
err = r.API.CreateUser(u.ToInstAPI(username, password), clusterID, instaclustr.CassandraBundleUser)
if err != nil {
l.Error(err, "Cannot create a user for the Cassandra cluster",
"cluster ID", clusterID,
"username", username)
r.EventRecorder.Eventf(u, models.Warning, models.CreatingEvent,
"Cannot create user. Reason: %v", err)

return models.ReconcileRequeue, nil
}
}

event = models.Created
u.Status.ClustersEvents[clusterID] = event

Expand All @@ -159,15 +171,27 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if event == models.DeletingEvent {
l.Info("Deleting user from a cluster", "cluster ID", clusterID)

err = r.API.DeleteUser(username, clusterID, instaclustr.CassandraBundleUser)
exists, err := CheckIfUserExistsOnInstaclustrAPI(username, clusterID, instaclustr.CassandraBundleUser, r.API)
if err != nil {
l.Error(err, "Cannot delete Cassandra user")
r.EventRecorder.Eventf(u, models.Warning, models.DeletingEvent,
"Cannot delete user. Reason: %v", err)

l.Error(err, "Cannot check if user exists ")
r.EventRecorder.Eventf(
u, models.Warning, models.CreationFailed,
"Cannot check if user exists. Reason: %v", err,
)
return models.ReconcileRequeue, nil
}

if exists {
err = r.API.DeleteUser(username, clusterID, instaclustr.CassandraBundleUser)
if err != nil {
l.Error(err, "Cannot delete Cassandra user")
r.EventRecorder.Eventf(u, models.Warning, models.DeletingEvent,
"Cannot delete user. Reason: %v", err)

return models.ReconcileRequeue, nil
}
}

l.Info("User has been deleted for cluster", "username", username,
"cluster ID", clusterID)
r.EventRecorder.Eventf(u, models.Normal, models.Deleted,
Expand Down
4 changes: 2 additions & 2 deletions controllers/tests/cassandra_plus_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ var _ = Describe("Basic Cassandra User controller + Basic Cassandra cluster cont
return false
}

for i, useRef := range cassandra1.Spec.UserRefs {
if user2.Name == useRef.Name && user2.Namespace == useRef.Namespace {
for i, userRef := range cassandra1.Spec.UserRefs {
if user2.Name == userRef.Name && user2.Namespace == userRef.Namespace {
cassandra1.Spec.UserRefs = removeUserByIndex(cassandra1.Spec.UserRefs, i)
Expect(k8sClient.Patch(ctx, &cassandra1, patch)).Should(Succeed())
}
Expand Down

0 comments on commit 60319ce

Please sign in to comment.