Skip to content

Commit

Permalink
add new constants for resource prefixes
Browse files Browse the repository at this point in the history
Signed-off-by: Ryotaro Banno <[email protected]>
  • Loading branch information
ushitora-anqou committed Jan 7, 2025
1 parent 7ad40e7 commit 2f52044
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
22 changes: 15 additions & 7 deletions internal/controller/mantlebackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -1060,35 +1068,35 @@ 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 {
return fmt.Sprintf("%s-%s.bin", name, uid)
}

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 {
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/mantlebackup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions test/e2e/multik8s/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -142,22 +142,22 @@ 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")
secondaryPVList, err := getObjectList[corev1.PersistentVolumeList](secondaryK8sCluster, "pv", cephClusterNamespace)
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")
Expand Down
15 changes: 8 additions & 7 deletions test/e2e/singlek8s/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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())
}
})

Expand Down

0 comments on commit 2f52044

Please sign in to comment.