error handling for wrong resources
Return error instead of logging if failed to process given resources
MODIFY
pkg/controller/pxc/backup.go
pkg/controller/pxc/backup.go
@@ -27,8 +27,11 @@ func (r *ReconcilePerconaXtraDBCluster) reconcileBackups(cr *api.PerconaXtraDBCl
return fmt.Errorf("storage %s doesn't exist", bcp.StorageName)
}
- bcpjob := bcpObj.Scheduled(&bcp, strg, cr)
- err := setControllerReference(cr, bcpjob, r.scheme)
+ bcpjob, err := bcpObj.Scheduled(&bcp, strg, cr)
+ if err != nil {
+ return fmt.Errorf("unable to schedule backup: %w", err)
+ }
+ err = setControllerReference(cr, bcpjob, r.scheme)
if err != nil {
return fmt.Errorf("set owner ref to backup %s: %v", bcp.Name, err)
}

MODIFY
pkg/controller/pxcbackup/controller.go
pkg/controller/pxcbackup/controller.go
@@ -137,7 +137,10 @@ func (r *ReconcilePerconaXtraDBClusterBackup) Reconcile(request reconcile.Reques
bcp := backup.New(cluster)
job := bcp.Job(instance, cluster)
- job.Spec = bcp.JobSpec(instance.Spec, cluster.Spec, job)
+ job.Spec, err = bcp.JobSpec(instance.Spec, cluster.Spec, job)
+ if err != nil {
+ return reconcile.Result{}, fmt.Errorf("can't create job spec: %w", err)
+ }
var destination string
var s3status *api.BackupStorageS3Spec

MODIFY
pkg/controller/pxcrestore/restore.go
pkg/controller/pxcrestore/restore.go
@@ -34,16 +34,22 @@ func (r *ReconcilePerconaXtraDBClusterRestore) restore(cr *api.PerconaXtraDBClus
func (r *ReconcilePerconaXtraDBClusterRestore) restorePVC(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBClusterBackup, pvcName string, cluster api.PerconaXtraDBClusterSpec) error {
svc := backup.PVCRestoreService(cr, bcp)
k8s.SetControllerReference(cr, svc, r.scheme)
- pod := backup.PVCRestorePod(cr, bcp, pvcName, cluster)
+ pod, err := backup.PVCRestorePod(cr, bcp, pvcName, cluster)
+ if err != nil {
+ return errors.Wrap(err, "restore pod")
+ }
k8s.SetControllerReference(cr, pod, r.scheme)
- job := backup.PVCRestoreJob(cr, bcp, cluster)
+ job, err := backup.PVCRestoreJob(cr, bcp, cluster)
+ if err != nil {
+ return errors.Wrap(err, "restore job")
+ }
k8s.SetControllerReference(cr, job, r.scheme)
r.client.Delete(context.TODO(), svc)
r.client.Delete(context.TODO(), pod)
- err := r.client.Create(context.TODO(), svc)
+ err = r.client.Create(context.TODO(), svc)
if err != nil {
return errors.Wrap(err, "create service")
}

MODIFY
pkg/pxc/backup/job.go
pkg/pxc/backup/job.go
package backup
import (
+ "fmt"
"net/url"
"strings"
@@ -37,10 +38,10 @@ func (*Backup) Job(cr *api.PerconaXtraDBClusterBackup, cluster *api.PerconaXtraD
}
}
-func (bcp *Backup) JobSpec(spec api.PXCBackupSpec, cluster api.PerconaXtraDBClusterSpec, job *batchv1.Job) batchv1.JobSpec {
+func (bcp *Backup) JobSpec(spec api.PXCBackupSpec, cluster api.PerconaXtraDBClusterSpec, job *batchv1.Job) (batchv1.JobSpec, error) {
resources, err := app.CreateResources(cluster.Backup.Storages[spec.StorageName].Resources)
if err != nil {
- log.Info("cannot parse Backup resources: ", err)
+ return batchv1.JobSpec{}, fmt.Errorf("cannot parse Backup resources: %w", err)
}
manualSelector := true
@@ -93,7 +94,7 @@ func (bcp *Backup) JobSpec(spec api.PXCBackupSpec, cluster api.PerconaXtraDBClus
PriorityClassName: cluster.Backup.Storages[spec.StorageName].PriorityClassName,
},
},
- }
+ }, nil
}
func appendStorageSecret(job *batchv1.JobSpec, cr *api.PerconaXtraDBCluster) error {

MODIFY
pkg/pxc/backup/restore.go
pkg/pxc/backup/restore.go
package backup
import (
+ "fmt"
"strconv"
"strings"
@@ -42,7 +43,7 @@ func PVCRestoreService(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtra
}
}
-func PVCRestorePod(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBClusterBackup, pvcName string, cluster api.PerconaXtraDBClusterSpec) *corev1.Pod {
+func PVCRestorePod(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBClusterBackup, pvcName string, cluster api.PerconaXtraDBClusterSpec) (*corev1.Pod, error) {
if _, ok := cluster.Backup.Storages[bcp.Spec.StorageName]; !ok {
log.Info("storage " + bcp.Spec.StorageName + " doesn't exist")
if len(cluster.Backup.Storages) == 0 {
@@ -53,7 +54,7 @@ func PVCRestorePod(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBCl
resources, err := app.CreateResources(cluster.Backup.Storages[bcp.Status.StorageName].Resources)
if err != nil {
- log.Info("cannot parse backup resources: ", err)
+ return nil, fmt.Errorf("cannot parse backup resources: %w", err)
}
// Copy from the original labels to the restore labels
@@ -125,13 +126,13 @@ func PVCRestorePod(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBCl
SchedulerName: cluster.Backup.Storages[bcp.Status.StorageName].SchedulerName,
PriorityClassName: cluster.Backup.Storages[bcp.Status.StorageName].PriorityClassName,
},
- }
+ }, nil
}
-func PVCRestoreJob(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBClusterBackup, cluster api.PerconaXtraDBClusterSpec) *batchv1.Job {
+func PVCRestoreJob(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBClusterBackup, cluster api.PerconaXtraDBClusterSpec) (*batchv1.Job, error) {
resources, err := app.CreateResources(cluster.PXC.Resources)
if err != nil {
- log.Info("cannot parse PXC resources: ", err)
+ return nil, fmt.Errorf("cannot parse PXC resources: %w", err)
}
jobPVC := corev1.Volume{
@@ -230,14 +231,14 @@ func PVCRestoreJob(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBCl
}
}
- return job
+ return job, nil
}
// S3RestoreJob returns restore job object for s3
func S3RestoreJob(cr *api.PerconaXtraDBClusterRestore, bcp *api.PerconaXtraDBClusterBackup, s3dest string, cluster api.PerconaXtraDBClusterSpec) (*batchv1.Job, error) {
resources, err := app.CreateResources(cluster.PXC.Resources)
if err != nil {
- log.Info("cannot parse PXC resources: ", err)
+ return nil, fmt.Errorf("cannot parse PXC resources: %w", err)
}
if bcp.Status.S3 == nil {

MODIFY
pkg/pxc/backup/scheduled.go
pkg/pxc/backup/scheduled.go
package backup
import (
+ "fmt"
+
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
@@ -10,7 +12,7 @@ import (
"github.com/percona/percona-xtradb-cluster-operator/pkg/pxc/app"
)
-func (bcp *Backup) Scheduled(spec *api.PXCScheduledBackupSchedule, strg *api.BackupStorageSpec, cr *api.PerconaXtraDBCluster) *batchv1beta1.CronJob {
+func (bcp *Backup) Scheduled(spec *api.PXCScheduledBackupSchedule, strg *api.BackupStorageSpec, cr *api.PerconaXtraDBCluster) (*batchv1beta1.CronJob, error) {
// Copy from the original labels to the backup labels
labels := make(map[string]string)
for key, value := range strg.Labels {
@@ -20,6 +22,10 @@ func (bcp *Backup) Scheduled(spec *api.PXCScheduledBackupSchedule, strg *api.Bac
labels["cluster"] = bcp.cluster
labels["schedule"] = genScheduleLabel(spec.Schedule)
+ cjts, err := bcp.scheduledJob(spec, strg, labels)
+ if err != nil {
+ return nil, fmt.Errorf("scheduled job: %w", err)
+ }
jb := &batchv1beta1.CronJob{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1beta1",
@@ -35,7 +41,7 @@ func (bcp *Backup) Scheduled(spec *api.PXCScheduledBackupSchedule, strg *api.Bac
Schedule: spec.Schedule,
SuccessfulJobsHistoryLimit: func(i int32) *int32 { return &i }(1),
JobTemplate: batchv1beta1.JobTemplateSpec{
- Spec: bcp.scheduledJob(spec, strg, labels),
+ Spec: cjts,
},
},
}
@@ -51,13 +57,13 @@ func (bcp *Backup) Scheduled(spec *api.PXCScheduledBackupSchedule, strg *api.Bac
),
)
- return jb
+ return jb, nil
}
-func (bcp *Backup) scheduledJob(spec *api.PXCScheduledBackupSchedule, strg *api.BackupStorageSpec, labels map[string]string) batchv1.JobSpec {
+func (bcp *Backup) scheduledJob(spec *api.PXCScheduledBackupSchedule, strg *api.BackupStorageSpec, labels map[string]string) (batchv1.JobSpec, error) {
resources, err := app.CreateResources(strg.Resources)
if err != nil {
- log.Info("cannot parse backup resources: ", err)
+ return batchv1.JobSpec{}, fmt.Errorf("cannot parse backup resources: %w", err)
}
return batchv1.JobSpec{
@@ -115,5 +121,5 @@ func (bcp *Backup) scheduledJob(spec *api.PXCScheduledBackupSchedule, strg *api.
PriorityClassName: strg.PriorityClassName,
},
},
- }
+ }, nil
}

CLOUD-474: error handling for wrong resources

Return error instead of logging if failed to process given resources