validate schedule-derived backup names don't exceed length limit

Signed-off-by: samay43 <samayrbhat43@gmail.com>
This commit is contained in:
samay43
2026-08-14 10:37:57 +05:30
parent e2c3b4b269
commit efe1279d49
2 changed files with 25 additions and 0 deletions
+11
View File
@@ -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)
+14
View File
@@ -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 {