From 5eafe245d8c720b407ec42a9d387876cf7a621cc Mon Sep 17 00:00:00 2001 From: acekingke Date: Wed, 7 Sep 2022 12:33:26 +0800 Subject: [PATCH] *: fix the bug status always empty till backup complete #687 --- api/v1alpha1/backup_types.go | 4 +++- api/v1alpha1/zz_generated.deepcopy.go | 1 - backup/syncer/job.go | 2 ++ .../crds/mysql.radondb.com_backups.yaml | 3 +++ config/crd/bases/mysql.radondb.com_backups.yaml | 3 +++ controllers/backup_controller.go | 13 ++++++++----- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/api/v1alpha1/backup_types.go b/api/v1alpha1/backup_types.go index 071a31bb..430291a1 100644 --- a/api/v1alpha1/backup_types.go +++ b/api/v1alpha1/backup_types.go @@ -56,7 +56,8 @@ type BackupStatus struct { // Completed indicates whether the backup is in a final state, // no matter whether its' corresponding job failed or succeeded - Completed bool `json:"completed,omitempty"` + // +kubebuilder:default:=false + Completed bool `json:"completed"` // Get the backup path. BackupName string `json:"backupName,omitempty"` // Get the backup Date @@ -104,6 +105,7 @@ const ( BackupComplete BackupConditionType = "Complete" // BackupFailed means backup has failed BackupFailed BackupConditionType = "Failed" + BackupStart BackupConditionType = "Started" ) //+kubebuilder:object:root=true diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 6669bd0a..a41a7223 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -1,4 +1,3 @@ -//go:build !ignore_autogenerated // +build !ignore_autogenerated /* diff --git a/backup/syncer/job.go b/backup/syncer/job.go index b3af2bc0..dde56efc 100644 --- a/backup/syncer/job.go +++ b/backup/syncer/job.go @@ -85,6 +85,8 @@ func (s *jobSyncer) SyncFn() error { func (s *jobSyncer) updateStatus(job *batchv1.Job) { // check for completion condition + s.backup.Status.Completed = false + s.backup.UpdateStatusCondition(v1alpha1.BackupStart, corev1.ConditionTrue, "backup has started", "backup has started") if cond := jobCondition(batchv1.JobComplete, job); cond != nil { s.backup.UpdateStatusCondition(v1alpha1.BackupComplete, cond.Status, cond.Reason, cond.Message) if cond.Status == corev1.ConditionTrue { diff --git a/charts/mysql-operator/crds/mysql.radondb.com_backups.yaml b/charts/mysql-operator/crds/mysql.radondb.com_backups.yaml index 06a454d7..dbe5e0b6 100644 --- a/charts/mysql-operator/crds/mysql.radondb.com_backups.yaml +++ b/charts/mysql-operator/crds/mysql.radondb.com_backups.yaml @@ -89,6 +89,7 @@ spec: description: Get the backup Type type: string completed: + default: false description: Completed indicates whether the backup is in a final state, no matter whether its' corresponding job failed or succeeded type: boolean @@ -124,6 +125,8 @@ spec: - type type: object type: array + required: + - completed type: object type: object served: true diff --git a/config/crd/bases/mysql.radondb.com_backups.yaml b/config/crd/bases/mysql.radondb.com_backups.yaml index 06a454d7..dbe5e0b6 100644 --- a/config/crd/bases/mysql.radondb.com_backups.yaml +++ b/config/crd/bases/mysql.radondb.com_backups.yaml @@ -89,6 +89,7 @@ spec: description: Get the backup Type type: string completed: + default: false description: Completed indicates whether the backup is in a final state, no matter whether its' corresponding job failed or succeeded type: boolean @@ -124,6 +125,8 @@ spec: - type type: object type: array + required: + - completed type: object type: object served: true diff --git a/controllers/backup_controller.go b/controllers/backup_controller.go index 25c61a37..a1e67a61 100644 --- a/controllers/backup_controller.go +++ b/controllers/backup_controller.go @@ -169,11 +169,8 @@ func (r *BackupReconciler) SetupWithManager(mgr ctrl.Manager) error { // Update backup Object and Status. func (r *BackupReconciler) updateBackup(savedBackup *apiv1alpha1.Backup, backup *backup.Backup) error { log := log.Log.WithName("controllers").WithName("Backup") - if !reflect.DeepEqual(savedBackup, backup.Unwrap()) { - if err := r.Update(context.TODO(), backup.Unwrap()); err != nil { - return err - } - } + // When update the backup, backup.Status will lost, and I don't know why, + // Since it updated to go 1.17, it has got this problem. So I put the update status first. if !reflect.DeepEqual(savedBackup.Status, backup.Unwrap().Status) { log.Info("update backup object status") if err := r.Status().Update(context.TODO(), backup.Unwrap()); err != nil { @@ -182,6 +179,12 @@ func (r *BackupReconciler) updateBackup(savedBackup *apiv1alpha1.Backup, backup return err } } + if !reflect.DeepEqual(savedBackup, backup.Unwrap()) { + if err := r.Update(context.TODO(), backup.Unwrap()); err != nil { + return err + } + } + return nil }