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, handle the user errors #547

Merged
merged 1 commit into from
Sep 14, 2023

Conversation

OleksiienkoMykyta
Copy link
Collaborator

No description provided.

@@ -134,7 +136,7 @@ func (r *RedisUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
for clusterID, event := range user.Status.ClustersEvents {
if event == models.CreatingEvent {
_, err = r.API.CreateRedisUser(user.Spec.ToInstAPI(password, clusterID, username))
if err != nil {
if err != nil && !strings.Contains(fmt.Sprint(err), "username already in use") {
Copy link
Contributor

Choose a reason for hiding this comment

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

!strings.Contains(fmt.Sprint(err) - doesn't seem like the best approach for error handling. If you don't get the HTTP status code AlreadyExist, I recommend implementing the following alternative strategy:

  1. Retrieve a list of users from the Instaclustr API.
  2. Iterate through this list and check for any duplicate users.
  3. Only proceed with sending a request for user creation if no duplicates are found in the user list.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@OleksiienkoMykyta OleksiienkoMykyta force-pushed the issue-528-handle-the-user-errors branch 2 times, most recently from 0ddcd3e to 6f03bf7 Compare September 7, 2023 15:45
@OleksiienkoMykyta OleksiienkoMykyta force-pushed the issue-528-handle-the-user-errors branch from 6f03bf7 to e960266 Compare September 7, 2023 15:47
@@ -339,6 +368,8 @@ func (r *RedisUserReconciler) handleDeleteUser(
return err
}

l.Info("CR Redis User was deleted")
Copy link
Collaborator

Choose a reason for hiding this comment

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

l.Info("Redis User resource/CR was deleted")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

if event == models.CreatingEvent {
_, err = r.API.CreateRedisUser(user.Spec.ToInstAPI(password, clusterID, username))
if err != nil {
err = r.API.GetRedisUser(userID)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's strange that the Get method returns nothing. Let's return the user from the GetRedisUser method and validate that usernames are the same, wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that we should return entity, but name validation unnecessary, because user ID contains user name, we cant get user if its not equal
user ID example: "id": "c1af59c6-ba0e-4cc2-a0f3-65cee17a5f37_myRedisUser"
User ID equals clusterID + "_" + username

Comment on lines 12 to 25
name: "Username-redis"
name: "Username-test"
version: "7.0.12"
slaTier: "NON_PRODUCTION"
clientEncryption: false
passwordAndUserAuth: true
privateNetworkCluster: false
userRefs:
# - name: redisuser-sample-1
# namespace: default
# - name: redisuser-sample-2
# namespace: default
# - name: redisuser-sample-3
# namespace: default
- name: redisuser-sample-1
namespace: default
- name: redisuser-sample-2
namespace: default
- name: redisuser-sample-3
namespace: default
# twoFactorDelete:
Copy link
Contributor

Choose a reason for hiding this comment

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

please leave it unchanged

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 139 to 142
_, err = r.API.GetRedisUser(userID)

if err != nil && !errors.Is(err, instaclustr.NotFound) {
l.Error(err, "Cannot create a user for the Redis cluster",
Copy link
Contributor

Choose a reason for hiding this comment

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

"Cannot create a user..." -> "Cannot get a user..."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought that it would be strange when customer try to create entity and got message that they cannot get user from the cluster, wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, write a more detailed error message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

"Cannot create a user for the Redis cluster" --> "Cannot check if the user already exists on the cluster"

err = r.API.UpdateRedisUser(user.ToInstAPIUpdate(password, userID))
if err != nil {
if err != nil && !errors.Is(instaclustr.NotFound, err) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please switch the parameters in !errors.Is. Fix in similar cases

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 246 to 256
if errors.Is(instaclustr.NotFound, err) {
l.Info("Cannot update redis user password",
"secret name", user.Spec.SecretRef.Name,
"secret namespace", user.Spec.SecretRef.Namespace,
"username", username,
"cluster ID", clusterID,
"user ID", userID,
)

r.EventRecorder.Eventf(user, models.Warning, models.UpdateFailed,
"Given user doesn`t exist on the cluster ID: %v", clusterID)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe you should verbose these logs a bit because they are the same as if there is no NotFound error.
Like:
Cannot update redis user password -> Cannot update redis user password, the user doesn't exist on the given cluster

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@testisnullus testisnullus added the refactor Code improvements or refactorings label Sep 12, 2023
@OleksiienkoMykyta OleksiienkoMykyta force-pushed the issue-528-handle-the-user-errors branch 2 times, most recently from 09f7de9 to fbc2eb7 Compare September 13, 2023 07:57
@@ -339,6 +368,8 @@ func (r *RedisUserReconciler) handleDeleteUser(
return err
}

l.Info("Redis User resource/CR was deleted")
Copy link
Collaborator

Choose a reason for hiding this comment

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

l.Info("Redis User resource/CR was deleted") -> l.Info("Redis User resource was deleted")

I meant that you can choose between 2 options and use only one of them

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@OleksiienkoMykyta OleksiienkoMykyta force-pushed the issue-528-handle-the-user-errors branch from fbc2eb7 to c207c5b Compare September 14, 2023 08:04
@OleksiienkoMykyta OleksiienkoMykyta force-pushed the issue-528-handle-the-user-errors branch from c207c5b to ca16854 Compare September 14, 2023 10:28
@OleksiienkoMykyta OleksiienkoMykyta force-pushed the issue-528-handle-the-user-errors branch from ca16854 to a938766 Compare September 14, 2023 10:30
@testisnullus testisnullus merged commit 7c0608c into main Sep 14, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Code improvements or refactorings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants