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

Add a label to the S3User's Secret to allow label matching and speed up fetching the secret #49

Open
phlg opened this issue Aug 13, 2024 · 0 comments

Comments

@phlg
Copy link
Contributor

phlg commented Aug 13, 2024

As of current version, the user controller lists every secret in a namespace to find the one secret matching a S3User, which is fairly inefficient :

err := r.List(ctx, secretsList, client.InNamespace(userResource.Namespace))
if err != nil {
logger.Error(err, "An error occurred while listing the secrets in user's namespace")
return userSecret, fmt.Errorf("SecretListingFailed")
}
if len(secretsList.Items) == 0 {
logger.Info("The user's namespace doesn't appear to contain any secret")
return userSecret, nil
}
// In all the secrets inside the S3User's namespace, one should have an owner reference
// pointing to the S3User. For that specific secret, we check if its name matches the one from
// the S3User, whether explicit (userResource.Spec.SecretName) or implicit (userResource.Name)
// In case of mismatch, that secret is deleted (and will be recreated) ; if there is a match,
// it will be used for state comparison.
uid := userResource.GetUID()
// cmp.Or takes the first non "zero" value, see https://pkg.go.dev/cmp#Or
effectiveS3UserSecretName := cmp.Or(userResource.Spec.SecretName, userResource.Name)
for _, secret := range secretsList.Items {
for _, ref := range secret.OwnerReferences {
if ref.UID == uid {
if secret.Name != effectiveS3UserSecretName {
return secret, fmt.Errorf("S3UserSecretNameMismatch")
} else {
userSecret = secret
break
}
}
}
}

This could benefit from a dedicated label add to the secret when it's created. This is not difficult in itself, but requires some thought regarding pre-existing secrets (as in : Should the operator reconcile secrets to add labels ? Should this be managed with a small one-shot script to add the label to every S3User secret ?)

EDIT : usage example visible in Operator SDK doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant