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

Related 89: Function to generate custom UUID for NAC objects #98

Closed
wants to merge 2 commits into from

Conversation

mpryc
Copy link
Collaborator

@mpryc mpryc commented Oct 3, 2024

This function will be used to link between NAB and Backup and possibly other parts of the objects within Non-Admin-Backup.

Why the changes were made

Function that will be used to fix #89

How to test the changes made

Run unit tests and ensure many corner cases are handled.

This function will be used to link between NAB and Backup
and possibly other parts of the objects within Non-Admin-Backup.

Signed-off-by: Michal Pryc <[email protected]>
Copy link

openshift-ci bot commented Oct 3, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mpryc

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved label Oct 3, 2024
@mpryc
Copy link
Collaborator Author

mpryc commented Oct 3, 2024

@mateusoliveira43 note this function is separate to currently used one so we can safely move to this new one without breaking working current controller code.

@@ -39,6 +39,9 @@ const (
// EmptyString defines a constant for the empty string
const EmptyString = ""

// BackupNameDelimiter defines character that is used to separate name aprts
const BackupNameDelimiter = "-"
Copy link
Member

Choose a reason for hiding this comment

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

NIT: We may re-use this later e.g: restore etc
Rename it to NameDelimiter

if len(nabName) > 0 {
veleroBackupName = nabName + constant.BackupNameDelimiter + veleroBackupName
}
if len(namespace) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can drop using namespace in VeleroBackupName. It will anyways be part of metadata/spec ?

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 it's useful to quickly filter the namespace from which Backup was created. So if we have multiple users with admin rights that have separate namesapces it allows to quickly sort/identify which Backups are from which ns. Of course this will be also available as additional info via annotation or label, but here is just another step to not have one list of unreadable by human backup names (uuid's only as example).

Copy link
Contributor

Choose a reason for hiding this comment

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

If we are using a constant prefix in NAB names, this function needs to change

way it is, we call it and then check if result is valid; if not, we will retry

but on retry, only thing that will change is name suffix, which has the UUID

Correct way should be to create a function that generates the prefix (in this case <nab-name>-<nab-namespace>-, truncated) if needed

then, inside retry loop, we create UUID and add result of previous function and try to create velero backup. If it fails, we just retry with new UUID, prefix was already created and will not change

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's essentially the same, just a different approach to writing the function. I prefer to encapsulate everything in a single function to generate the full name. If we go with your approach, we'd need to:

  1. Create function to get <nab-name>-<nab-namespace>- with additional parameter of max length (we do not know if UUID will always have static length)
  2. Make logic to merge above with generated UUID
  3. Make logic to retry if the name exists inside reconcile

With current implementation we skip entirely point 2 making it easier within reconcile.

Copy link
Contributor

Choose a reason for hiding this comment

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

from this https://pkg.go.dev/github.com/google/uuid#Parse it is always 36 chars length

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct if we take RFC 4122, then is always 36 but really it should not be split between two functions to create string.

Address comment from @shubham-pampattiwar to rename
BackupNameDelimiter, because it may be used in the future
in other parts of the NAC then just Backup.

Signed-off-by: Michal Pryc <[email protected]>
@kaovilai
Copy link
Member

kaovilai commented Oct 7, 2024

/retest

@mateusoliveira43
Copy link
Contributor

@mpryc I do not like this approach, we would be merging unused code

We already have integration tests in place, when adding new way of generating NAB name, we should replace old way in same PR (and tests can tell us if new implementation works as expected)

@@ -139,6 +140,51 @@ func GenerateVeleroBackupName(namespace, nabName string) string {
return veleroBackupName
}

// GenerateVeleroBackupNameWithUUID generates a Velero backup name based on the provided namespace and NonAdminBackup name.
// It includes a UUID suffix. If the name exceeds the maximum length, it truncates nabName first, then namespace.
func GenerateVeleroBackupNameWithUUID(namespace, nabName string) string {
Copy link
Member

Choose a reason for hiding this comment

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

I think we are gonna use a similar function for NorAdmin Restore Objects ,right ? Would it not make sense to modify this function to just GenerateNameWithUUID ? (Refactor it so that there is not context of Backup/Restore, just namespace and NacObjectName as parameters)

Copy link
Member

@kaovilai kaovilai Oct 10, 2024

Choose a reason for hiding this comment

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

We can always follow up with a refactor where this func calls a more generic func

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 am closing this PR, new function is generic and will be included in other PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

// GenerateNacObjectNameWithUUID generates a unique name based on the provided namespace and object origin name.
// It includes a UUID suffix. If the name exceeds the maximum length, it truncates nacName first, then namespace.
func GenerateNacObjectNameWithUUID(namespace, nacName string) string {
	// Generate UUID suffix
	uuidSuffix := uuid.New().String()

	// Build the initial name based on the presence of namespace and nacName
	nacObjectName := uuidSuffix
	if len(nacName) > 0 {
		nacObjectName = nacName + constant.NameDelimiter + nacObjectName
	}
	if len(namespace) > 0 {
		nacObjectName = namespace + constant.NameDelimiter + nacObjectName
	}

	if len(nacObjectName) > constant.MaximumNacObjectNameLength {
		// Calculate remaining length after UUID
		remainingLength := constant.MaximumNacObjectNameLength - len(uuidSuffix)

		delimeterLength := len(constant.NameDelimiter)

		// Subtract two delimiter lengths to avoid a corner case where the namespace
		// and delimiters leave no space for any part of nabName
		if len(namespace) > remainingLength-delimeterLength-delimeterLength {
			namespace = namespace[:remainingLength-delimeterLength-delimeterLength]
			nacObjectName = namespace + constant.NameDelimiter + uuidSuffix
		} else {
			remainingLength = remainingLength - len(namespace) - delimeterLength - delimeterLength
			nacName = nacName[:remainingLength]
			nacObjectName = uuidSuffix
			if len(nacName) > 0 {
				nacObjectName = nacName + constant.NameDelimiter + nacObjectName
			}
			if len(namespace) > 0 {
				nacObjectName = namespace + constant.NameDelimiter + nacObjectName
			}
		}
	}

	return nacObjectName
}

@openshift-merge-robot
Copy link
Collaborator

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@mpryc
Copy link
Collaborator Author

mpryc commented Oct 10, 2024

Closing as I am preparing new PR with this change included.

@mpryc mpryc closed this Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use custom UUID for NAB Object to Velero Backup Object mapping instead of the Kube UUID
5 participants