From 8294735609c72a32b6983dd98e051620d56c1276 Mon Sep 17 00:00:00 2001 From: Vladimir Fidunin Date: Sat, 27 Aug 2022 16:35:11 +0300 Subject: [PATCH] Limit backup job name to 63 chars, fix bitpoke#836 --- pkg/internal/mysqlbackup/mysqlbackup.go | 6 +++++- pkg/internal/mysqlbackup/mysqlbackup_test.go | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/internal/mysqlbackup/mysqlbackup.go b/pkg/internal/mysqlbackup/mysqlbackup.go index e95d30d49..bcfc8c472 100644 --- a/pkg/internal/mysqlbackup/mysqlbackup.go +++ b/pkg/internal/mysqlbackup/mysqlbackup.go @@ -79,7 +79,11 @@ func (b *MysqlBackup) composeBackupURL(base string) string { // GetNameForJob returns the name of the job func (b *MysqlBackup) GetNameForJob() string { - return fmt.Sprintf("%s-backup", b.Name) + prefix := b.Name + if len(prefix) >= 56 { + prefix = fmt.Sprintf("%s-%d", prefix[:44], hash(prefix)) + } + return fmt.Sprintf("%s-backup", prefix) } // GetNameForDeletionJob returns the name for the hard deletion job. diff --git a/pkg/internal/mysqlbackup/mysqlbackup_test.go b/pkg/internal/mysqlbackup/mysqlbackup_test.go index 690030a1b..499d8a415 100644 --- a/pkg/internal/mysqlbackup/mysqlbackup_test.go +++ b/pkg/internal/mysqlbackup/mysqlbackup_test.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://wwb.apache.org/licenses/LICENSE-2.0 + http://wwb.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -55,4 +55,22 @@ var _ = Describe("MySQL backup unit tests", func() { Expect(backup.GetNameForDeletionJob()).To(Equal("not-too-long-backup-name-for-testing-cleanup-job-test-cleanup")) Expect(len(backup.GetNameForDeletionJob())).To(BeNumerically("<=", 63)) }) + + It("should generate the correct backup job name", func() { + backup := New(&api.MysqlBackup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "backup-name", + }, + }) + + Expect(backup.GetNameForJob()).To(Equal("backup-name-backup")) + + backup.Name = "super-long-backup-name-for-testing-backup-job-name-generator" + Expect(backup.GetNameForJob()).To(Equal("super-long-backup-name-for-testing-backup-jo-4133418200-backup")) + Expect(len(backup.GetNameForJob())).To(BeNumerically("<=", 63)) + + backup.Name = "not-too-long-backup-name-for-testing-backup-job-test" + Expect(backup.GetNameForJob()).To(Equal("not-too-long-backup-name-for-testing-backup-job-test-backup")) + Expect(len(backup.GetNameForJob())).To(BeNumerically("<=", 63)) + }) })