-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/google/uuid" | ||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/validation" | ||
|
@@ -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 { | ||
kaovilai marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// Generate UUID suffix | ||
uuidSuffix := uuid.New().String() | ||
|
||
// Build the initial backup name based on the presence of namespace and nabName | ||
veleroBackupName := uuidSuffix | ||
if len(nabName) > 0 { | ||
veleroBackupName = nabName + constant.BackupNameDelimiter + veleroBackupName | ||
} | ||
if len(namespace) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
With current implementation we skip entirely point 2 making it easier within reconcile. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
veleroBackupName = namespace + constant.BackupNameDelimiter + veleroBackupName | ||
} | ||
|
||
// Ensure the name is within the character limit | ||
maxLength := validation.DNS1123SubdomainMaxLength | ||
|
||
if len(veleroBackupName) > maxLength { | ||
// Calculate remaining length after UUID | ||
remainingLength := maxLength - len(uuidSuffix) | ||
|
||
delimeterLength := len(constant.BackupNameDelimiter) | ||
|
||
// 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] | ||
veleroBackupName = namespace + constant.BackupNameDelimiter + uuidSuffix | ||
} else { | ||
remainingLength = remainingLength - len(namespace) - delimeterLength - delimeterLength | ||
nabName = nabName[:remainingLength] | ||
veleroBackupName = uuidSuffix | ||
if len(nabName) > 0 { | ||
veleroBackupName = nabName + constant.BackupNameDelimiter + veleroBackupName | ||
} | ||
if len(namespace) > 0 { | ||
veleroBackupName = namespace + constant.BackupNameDelimiter + veleroBackupName | ||
} | ||
} | ||
} | ||
|
||
return veleroBackupName | ||
} | ||
|
||
// CheckVeleroBackupMetadata return true if Velero Backup object has required Non Admin labels and annotations, false otherwise | ||
func CheckVeleroBackupMetadata(obj client.Object) bool { | ||
labels := obj.GetLabels() | ||
|
There was a problem hiding this comment.
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