From 2f520442cd43ff2b682302596bce6c65fe83ef84 Mon Sep 17 00:00:00 2001 From: Ryotaro Banno Date: Tue, 7 Jan 2025 02:27:49 +0000 Subject: [PATCH] add new constants for resource prefixes Signed-off-by: Ryotaro Banno --- .../controller/mantlebackup_controller.go | 22 +++++++++++++------ .../mantlebackup_controller_test.go | 6 ++--- test/e2e/multik8s/suite_test.go | 14 ++++++------ test/e2e/singlek8s/backup_test.go | 15 +++++++------ 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/internal/controller/mantlebackup_controller.go b/internal/controller/mantlebackup_controller.go index 8080e1be..c1dde3b8 100644 --- a/internal/controller/mantlebackup_controller.go +++ b/internal/controller/mantlebackup_controller.go @@ -52,6 +52,14 @@ const ( annotRetainIfExpired = "mantle.cybozu.io/retain-if-expired" annotSyncMode = "mantle.cybozu.io/sync-mode" + MantleExportJobPrefix = "mantle-export-" + MantleUploadJobPrefix = "mantle-upload-" + MantleExportDataPVCPrefix = "mantle-export-" + MantleImportJobPrefix = "mantle-import-" + MantleDiscardJobPrefix = "mantle-discard-" + MantleDiscardPVCPrefix = "mantle-discard-" + MantleDiscardPVPrefix = "mantle-discard-" + syncModeFull = "full" syncModeIncremental = "incremental" ) @@ -1060,15 +1068,15 @@ func (r *MantleBackupReconciler) createOrUpdateExportDataPVC(ctx context.Context } func makeExportJobName(target *mantlev1.MantleBackup) string { - return fmt.Sprintf("mantle-export-%s", target.GetUID()) + return MantleExportJobPrefix + string(target.GetUID()) } func makeUploadJobName(target *mantlev1.MantleBackup) string { - return fmt.Sprintf("mantle-upload-%s", target.GetUID()) + return MantleUploadJobPrefix + string(target.GetUID()) } func makeExportDataPVCName(target *mantlev1.MantleBackup) string { - return fmt.Sprintf("mantle-export-%s", target.GetUID()) + return MantleExportDataPVCPrefix + string(target.GetUID()) } func makeObjectNameOfExportedData(name, uid string) string { @@ -1076,19 +1084,19 @@ func makeObjectNameOfExportedData(name, uid string) string { } func makeImportJobName(target *mantlev1.MantleBackup) string { - return fmt.Sprintf("mantle-import-%s", target.GetUID()) + return MantleImportJobPrefix + string(target.GetUID()) } func makeDiscardJobName(target *mantlev1.MantleBackup) string { - return fmt.Sprintf("mantle-discard-%s", target.GetUID()) + return MantleDiscardJobPrefix + string(target.GetUID()) } func makeDiscardPVCName(target *mantlev1.MantleBackup) string { - return fmt.Sprintf("mantle-discard-%s", target.GetUID()) + return MantleDiscardPVCPrefix + string(target.GetUID()) } func makeDiscardPVName(target *mantlev1.MantleBackup) string { - return fmt.Sprintf("mantle-discard-%s", target.GetUID()) + return MantleDiscardPVPrefix + string(target.GetUID()) } func (r *MantleBackupReconciler) createOrUpdateExportJob(ctx context.Context, target *mantlev1.MantleBackup, sourceBackupNamePtr *string) error { diff --git a/internal/controller/mantlebackup_controller_test.go b/internal/controller/mantlebackup_controller_test.go index 3a235c13..624c2280 100644 --- a/internal/controller/mantlebackup_controller_test.go +++ b/internal/controller/mantlebackup_controller_test.go @@ -532,7 +532,7 @@ var _ = Describe("MantleBackup controller", func() { err = k8sClient.Get( ctx, types.NamespacedName{ - Name: fmt.Sprintf("mantle-upload-%s", backup.GetUID()), + Name: makeUploadJobName(backup), Namespace: resMgr.ClusterID, }, &jobUpload, @@ -551,7 +551,7 @@ var _ = Describe("MantleBackup controller", func() { err = k8sClient.Get( ctx, types.NamespacedName{ - Name: fmt.Sprintf("mantle-upload-%s", backup.GetUID()), + Name: makeUploadJobName(backup), Namespace: resMgr.ClusterID, }, &jobUpload, @@ -622,7 +622,7 @@ var _ = Describe("MantleBackup controller", func() { err = k8sClient.Get( ctx, types.NamespacedName{ - Name: fmt.Sprintf("mantle-export-%s", backup2.GetUID()), + Name: makeExportJobName(backup2), Namespace: resMgr.ClusterID, }, &jobExport2, diff --git a/test/e2e/multik8s/suite_test.go b/test/e2e/multik8s/suite_test.go index c7dacd83..da6f350c 100644 --- a/test/e2e/multik8s/suite_test.go +++ b/test/e2e/multik8s/suite_test.go @@ -125,15 +125,15 @@ func ensureTemporaryResourcesDeleted(ctx context.Context) { Expect(err).NotTo(HaveOccurred()) Expect(slices.ContainsFunc(primaryJobList.Items, func(job batchv1.Job) bool { n := job.GetName() - return strings.HasPrefix(n, "mantle-export-") || - strings.HasPrefix(n, "mantle-upload-") + return strings.HasPrefix(n, controller.MantleExportJobPrefix) || + strings.HasPrefix(n, controller.MantleUploadJobPrefix) })).To(BeFalse()) secondaryJobList, err := getObjectList[batchv1.JobList](secondaryK8sCluster, "job", cephClusterNamespace) Expect(err).NotTo(HaveOccurred()) Expect(slices.ContainsFunc(secondaryJobList.Items, func(job batchv1.Job) bool { n := job.GetName() - return strings.HasPrefix(n, "mantle-import-") || - strings.HasPrefix(n, "mantle-discard-") + return strings.HasPrefix(n, controller.MantleImportJobPrefix) || + strings.HasPrefix(n, controller.MantleDiscardJobPrefix) })).To(BeFalse()) By("checking all temporary PVCs related to export and import of RBD images are removed") @@ -142,14 +142,14 @@ func ensureTemporaryResourcesDeleted(ctx context.Context) { Expect(err).NotTo(HaveOccurred()) Expect(slices.ContainsFunc(primaryPVCList.Items, func(pvc corev1.PersistentVolumeClaim) bool { n := pvc.GetName() - return strings.HasPrefix(n, "mantle-export-") + return strings.HasPrefix(n, controller.MantleExportDataPVCPrefix) })).To(BeFalse()) secondaryPVCList, err := getObjectList[corev1.PersistentVolumeClaimList]( secondaryK8sCluster, "pvc", cephClusterNamespace) Expect(err).NotTo(HaveOccurred()) Expect(slices.ContainsFunc(secondaryPVCList.Items, func(pvc corev1.PersistentVolumeClaim) bool { n := pvc.GetName() - return strings.HasPrefix(n, "mantle-discard-") + return strings.HasPrefix(n, controller.MantleDiscardPVCPrefix) })).To(BeFalse()) By("checking all temporary PVs related to export and import of RBD images are removed") @@ -157,7 +157,7 @@ func ensureTemporaryResourcesDeleted(ctx context.Context) { Expect(err).NotTo(HaveOccurred()) Expect(slices.ContainsFunc(secondaryPVList.Items, func(pv corev1.PersistentVolume) bool { n := pv.GetName() - return strings.HasPrefix(n, "mantle-discard-") + return strings.HasPrefix(n, controller.MantleDiscardPVPrefix) })).To(BeFalse()) By("checking all temporary objects in the object storage related to export and import of RBD images are removed") diff --git a/test/e2e/singlek8s/backup_test.go b/test/e2e/singlek8s/backup_test.go index b2d4c3bf..18eccf95 100644 --- a/test/e2e/singlek8s/backup_test.go +++ b/test/e2e/singlek8s/backup_test.go @@ -7,6 +7,7 @@ import ( "strings" mantlev1 "github.com/cybozu-go/mantle/api/v1" + "github.com/cybozu-go/mantle/internal/controller" "github.com/cybozu-go/mantle/test/util" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -176,10 +177,10 @@ func (test *backupTest) testCase1() { err = json.Unmarshal(stdout, &jobs) Expect(err).NotTo(HaveOccurred()) for _, job := range jobs.Items { - Expect(strings.HasPrefix(job.GetName(), "mantle-export-")).To(BeFalse()) - Expect(strings.HasPrefix(job.GetName(), "mantle-upload-")).To(BeFalse()) - Expect(strings.HasPrefix(job.GetName(), "mantle-import-")).To(BeFalse()) - Expect(strings.HasPrefix(job.GetName(), "mantle-discard-")).To(BeFalse()) + Expect(strings.HasPrefix(job.GetName(), controller.MantleExportJobPrefix)).To(BeFalse()) + Expect(strings.HasPrefix(job.GetName(), controller.MantleUploadJobPrefix)).To(BeFalse()) + Expect(strings.HasPrefix(job.GetName(), controller.MantleImportJobPrefix)).To(BeFalse()) + Expect(strings.HasPrefix(job.GetName(), controller.MantleDiscardJobPrefix)).To(BeFalse()) } // Check that export and discard PVC are not created. @@ -189,8 +190,8 @@ func (test *backupTest) testCase1() { err = json.Unmarshal(stdout, &pvcs) Expect(err).NotTo(HaveOccurred()) for _, pvc := range pvcs.Items { - Expect(strings.HasPrefix(pvc.GetName(), "mantle-export-")).To(BeFalse()) - Expect(strings.HasPrefix(pvc.GetName(), "mantle-discard-")).To(BeFalse()) + Expect(strings.HasPrefix(pvc.GetName(), controller.MantleExportDataPVCPrefix)).To(BeFalse()) + Expect(strings.HasPrefix(pvc.GetName(), controller.MantleDiscardPVCPrefix)).To(BeFalse()) } // Check that discard PV is not created. @@ -200,7 +201,7 @@ func (test *backupTest) testCase1() { err = json.Unmarshal(stdout, &pvs) Expect(err).NotTo(HaveOccurred()) for _, pv := range pvs.Items { - Expect(strings.HasPrefix(pv.GetName(), "mantle-discard-")).To(BeFalse()) + Expect(strings.HasPrefix(pv.GetName(), controller.MantleDiscardPVPrefix)).To(BeFalse()) } })