mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-13 03:24:39 +00:00
validate schedule-derived backup names don't exceed length limit
Signed-off-by: samay43 <samayrbhat43@gmail.com>
This commit is contained in:
@@ -216,6 +216,17 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
|
||||
return fmt.Errorf("invalid backup name %q: %s", o.Name, strings.Join(errs, "; "))
|
||||
}
|
||||
}
|
||||
// When a backup name will be generated from the schedule (i.e. FromSchedule
|
||||
// is set and no explicit name was given), ensure the schedule name leaves
|
||||
// enough room for the generated timestamp suffix ("-" + 14-digit timestamp,
|
||||
// 15 characters total) within the DNS1123 subdomain length limit.
|
||||
if o.FromSchedule != "" && o.Name == "" {
|
||||
const timestampSuffixLen = 15 // "-" + "20060102150405"
|
||||
maxScheduleNameLen := validation.DNS1123SubdomainMaxLength - timestampSuffixLen
|
||||
if len(o.FromSchedule) > maxScheduleNameLen {
|
||||
return fmt.Errorf("schedule name %q is too long: must be %d characters or fewer to leave room for the generated timestamp suffix", o.FromSchedule, maxScheduleNameLen)
|
||||
}
|
||||
}
|
||||
errs := collections.ValidateNamespaceIncludesExcludes(o.IncludeNamespaces, o.ExcludeNamespaces)
|
||||
if len(errs) > 0 {
|
||||
return kubeerrs.NewAggregate(errs)
|
||||
|
||||
@@ -538,6 +538,20 @@ func TestCreateOptions_Validate(t *testing.T) {
|
||||
args: []string{},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "should pass when schedule name leaves room for timestamp suffix",
|
||||
optName: "",
|
||||
fromSchedule: strings.Repeat("a", 238), // exactly at the 238-char limit
|
||||
args: []string{},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "should error when schedule name is too long to leave room for timestamp suffix",
|
||||
optName: "",
|
||||
fromSchedule: strings.Repeat("a", 239), // one over the 238-char limit
|
||||
args: []string{},
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
||||
Reference in New Issue
Block a user