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

issue-528-handling-user-errors-for-Cassandra #585

Merged
merged 1 commit into from
Oct 4, 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
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