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-439, Support batch of users for OpenSearch cluster CRD #471

Merged
merged 1 commit into from
Jul 17, 2023

Conversation

worryg0d
Copy link
Collaborator

Batch of users support

Added support for a batch of users for OpenSearch CRD.

Creation flow

To create a user on the specified cluster a customer should add a reference to a OpenSearchUser CRD to the cluster spec.

Deletion flow

To delete a user from a specified cluster a customer should delete a reference for the OpenSearchUser CRD from the cluster spec.

A customer cannot delete OpenSearchUser CRD until at least one cluster references the user CRD.

Cluster deletion

If a cluster is deleted, the cluster resource sets to the CRD ClusterDeletingEvent that indicates OpenSearchUser CRD to delete the reference to the deleted cluster. Also, if the event happened, OpenSearchUser reconciler doesn't make an API call to Instaclurt API.

@worryg0d worryg0d force-pushed the issue-439-opensearch-users-batch-support branch 2 times, most recently from 6835c7e to 398e351 Compare July 13, 2023 13:45
DeletedEvent = "deleted"
GenericEvent = "generic"
SecretEvent = "secret"
ClusterDeletingEvent = "cluster deleting"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't use deleting event?

Copy link
Collaborator Author

@worryg0d worryg0d Jul 14, 2023

Choose a reason for hiding this comment

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

Because DeletingEvent indicates that it deletes a user from the specified cluster by its username. But ClusterDeletingEvent indicates that the operator deletes cluster, so it hasn't to make an API call to Instaclustr API to delete the user. but we still need to delete finalizers from the user CRD.
Maybe the name of the variable is not appropriate. But if you have any idea how to call or enhance one you are welcome

}

patch := user.NewPatch()
user.Status.State = models.Created

if controllerutil.AddFinalizer(user, models.DeletionUserFinalizer+clusterID) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets use generator for finalizer as you did before

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

err,
)
return models.ReconcileRequeue, nil
errorOccurred := false
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove this variable
You can check the error existence and if it exists, re-run the reconciler

Copy link
Collaborator Author

@worryg0d worryg0d Jul 14, 2023

Choose a reason for hiding this comment

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

The variable indicates that error has occurred in at least one of user creating or deleting events, so we have to re-run the reconciler. If we check the error existence and immediately re-run the reconciler then it stops processing other events until the next reconciliation.

Comment on lines 241 to 244
patch = user.NewPatch()

user.Status.ClustersEvents[clusterID] = models.Created

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove spaces from here also, check all similar cases, please

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed

Comment on lines 224 to 225
patch := user.NewPatch()
user.Status.State = models.Created

if controllerutil.AddFinalizer(user, models.DeletionUserFinalizer+clusterID) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove the space here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed

)
r.EventRecorder.Eventf(
user, models.Normal, models.Deleted,
"OpenSearchUser resource has been deleted deleted",
"OpenSearchUser resource has been deleted deleted from the cluster (clusterID: %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.

Remove one of deleted words, please

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed

@@ -306,13 +306,19 @@ func (r *OpenSearchReconciler) HandleUpdateCluster(
}
}

if o.Spec.UserRef != nil {
err = r.handleCreateUser(ctx, o, logger)
errorOccurred := false
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove this variable and just check the error

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've answered this in the comment above

@worryg0d worryg0d force-pushed the issue-439-opensearch-users-batch-support branch 3 times, most recently from 1190d32 to 3e53c67 Compare July 14, 2023 07:20
Comment on lines 230 to 233
if o.Spec.UserRefs != nil {
patch := o.NewPatch()
o.Annotations[models.ResourceStateAnnotation] = models.UpdatingEvent
err = r.Patch(ctx, o, patch)
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest you refactor this logic, as I did. Please take a look at this in the Cassandra controller.

Copy link
Contributor

Choose a reason for hiding this comment

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

you may do this in another PR

Comment on lines 309 to 320
errorOccurred := false

for _, ref := range o.Spec.UserRefs {
err = r.createUser(ctx, logger, o, ref)
if err != nil {
return models.ReconcileRequeue
errorOccurred = true
}
}

if errorOccurred {
return models.ReconcileRequeue
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to remove it all and just keep user creation/deletion logic in the predicate

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed

Comment on lines 871 to 859
err := r.Get(ctx, req, u)
if err != nil {
if k8serrors.IsNotFound(err) {
l.Error(err, "OpenSearch user is not found", "request", req)
r.EventRecorder.Eventf(c, models.Warning, models.NotFound,
"User is not found, create a new one or provide correct userRef."+
"Current provided reference: %v", uRef)
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

if it's a delete event, there's no reason to create a new user for you to delete. Is that make sense?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it makes sense. Fixed

if k8serrors.IsNotFound(err) {
l.Error(err, "OpenSearch user is not found", "request", req)
r.EventRecorder.Eventf(c, models.Warning, models.NotFound,
"User is not found, create a new one or provide correct userRef."+
Copy link
Contributor

Choose a reason for hiding this comment

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

The same goes for a detach event.

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

@worryg0d worryg0d force-pushed the issue-439-opensearch-users-batch-support branch from 3e53c67 to bde8496 Compare July 14, 2023 09:31
@worryg0d worryg0d requested a review from ribaraka July 14, 2023 09:32
@worryg0d worryg0d self-assigned this Jul 14, 2023
@taaraora taaraora merged commit 4574689 into main Jul 17, 2023
@testisnullus testisnullus added the enhancement New feature or request label Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement credentials lifecycle for OpenSearch
5 participants