From 24edf80ceb1c4799f4f8b61e77a54378cbeaf3cb Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 9 Oct 2023 15:48:43 +0800 Subject: [PATCH] fix: make magic numbers & strings constants; move type definitions in api constants to a specific package --- api/constants/backup.go | 67 ++++++++------------ api/constants/restore.go | 16 ++--- api/constants/tenant.go | 26 ++++---- api/types/types.go | 30 +++++++++ api/v1alpha1/obtenant_types.go | 14 ++-- api/v1alpha1/obtenantbackup_types.go | 28 ++++---- api/v1alpha1/obtenantbackuppolicy_types.go | 22 +++---- api/v1alpha1/obtenantoperation_types.go | 18 +++--- api/v1alpha1/obtenantrestore_types.go | 21 +++--- pkg/const/oceanbase/oceanbase.go | 5 ++ pkg/oceanbase/operation/backup.go | 7 +- pkg/oceanbase/test/backup_test.go | 3 +- pkg/resource/obtenantbackuppolicy_manager.go | 5 +- pkg/resource/obtenantbackuppolicy_task.go | 12 ++-- pkg/resource/obtenantoperation_manager.go | 5 +- pkg/resource/obtenantoperation_task.go | 13 ++-- pkg/resource/obtenantrestore_manager.go | 5 +- 17 files changed, 161 insertions(+), 136 deletions(-) create mode 100644 api/types/types.go diff --git a/api/constants/backup.go b/api/constants/backup.go index 55daa085f..97b8164a1 100644 --- a/api/constants/backup.go +++ b/api/constants/backup.go @@ -12,62 +12,47 @@ See the Mulan PSL v2 for more details. package constants -type BackupJobType string +import "github.com/oceanbase/ob-operator/api/types" const ( - BackupJobTypeFull BackupJobType = "FULL" - BackupJobTypeIncr BackupJobType = "INC" - BackupJobTypeClean BackupJobType = "CLEAN" - BackupJobTypeArchive BackupJobType = "ARCHIVE" + BackupJobTypeFull types.BackupJobType = "FULL" + BackupJobTypeIncr types.BackupJobType = "INC" + BackupJobTypeClean types.BackupJobType = "CLEAN" + BackupJobTypeArchive types.BackupJobType = "ARCHIVE" ) -type BackupJobStatus string - const ( - BackupJobStatusRunning BackupJobStatus = "RUNNING" - BackupJobStatusInitializing BackupJobStatus = "INITIALIZING" - BackupJobStatusSuccessful BackupJobStatus = "SUCCESSFUL" - BackupJobStatusFailed BackupJobStatus = "FAILED" - BackupJobStatusCanceled BackupJobStatus = "CANCELED" - BackupJobStatusStopped BackupJobStatus = "STOPPED" - BackupJobStatusSuspend BackupJobStatus = "SUSPEND" + BackupJobStatusRunning types.BackupJobStatus = "RUNNING" + BackupJobStatusInitializing types.BackupJobStatus = "INITIALIZING" + BackupJobStatusSuccessful types.BackupJobStatus = "SUCCESSFUL" + BackupJobStatusFailed types.BackupJobStatus = "FAILED" + BackupJobStatusCanceled types.BackupJobStatus = "CANCELED" + BackupJobStatusStopped types.BackupJobStatus = "STOPPED" + BackupJobStatusSuspend types.BackupJobStatus = "SUSPEND" ) -type BackupPolicyStatusType string - const ( - BackupPolicyStatusPreparing BackupPolicyStatusType = "PREPARING" - BackupPolicyStatusPrepared BackupPolicyStatusType = "PREPARED" - BackupPolicyStatusRunning BackupPolicyStatusType = "RUNNING" - BackupPolicyStatusFailed BackupPolicyStatusType = "FAILED" - BackupPolicyStatusPausing BackupPolicyStatusType = "PAUSING" - BackupPolicyStatusPaused BackupPolicyStatusType = "PAUSED" - BackupPolicyStatusStopped BackupPolicyStatusType = "STOPPED" - BackupPolicyStatusResuming BackupPolicyStatusType = "RESUMING" + BackupPolicyStatusPreparing types.BackupPolicyStatusType = "PREPARING" + BackupPolicyStatusPrepared types.BackupPolicyStatusType = "PREPARED" + BackupPolicyStatusRunning types.BackupPolicyStatusType = "RUNNING" + BackupPolicyStatusFailed types.BackupPolicyStatusType = "FAILED" + BackupPolicyStatusPausing types.BackupPolicyStatusType = "PAUSING" + BackupPolicyStatusPaused types.BackupPolicyStatusType = "PAUSED" + BackupPolicyStatusStopped types.BackupPolicyStatusType = "STOPPED" + BackupPolicyStatusResuming types.BackupPolicyStatusType = "RESUMING" ) -type BackupDestination struct { - Type BackupDestType `json:"type,omitempty"` - Path string `json:"path,omitempty"` -} - -type BackupDestType string - const ( - BackupDestTypeOSS BackupDestType = "OSS" - BackupDestTypeNFS BackupDestType = "NFS" + BackupDestTypeOSS types.BackupDestType = "OSS" + BackupDestTypeNFS types.BackupDestType = "NFS" ) -type LogArchiveDestState string - const ( - LogArchiveDestStateEnable LogArchiveDestState = "ENABLE" - LogArchiveDestStateDefer LogArchiveDestState = "DEFER" + LogArchiveDestStateEnable types.LogArchiveDestState = "ENABLE" + LogArchiveDestStateDefer types.LogArchiveDestState = "DEFER" ) -type ArchiveBinding string - const ( - ArchiveBindingOptional = "Optional" - ArchiveBindingMandatory = "Mandatory" + ArchiveBindingOptional types.ArchiveBinding = "Optional" + ArchiveBindingMandatory types.ArchiveBinding = "Mandatory" ) diff --git a/api/constants/restore.go b/api/constants/restore.go index fe58e66ea..5702d4fd3 100644 --- a/api/constants/restore.go +++ b/api/constants/restore.go @@ -12,15 +12,15 @@ See the Mulan PSL v2 for more details. package constants -type RestoreJobStatus string +import "github.com/oceanbase/ob-operator/api/types" const ( - RestoreJobStarting RestoreJobStatus = "STARTING" - RestoreJobRunning RestoreJobStatus = "RUNNING" - RestoreJobFailed RestoreJobStatus = "FAILED" - RestoreJobSuccessful RestoreJobStatus = "SUCCESSFUL" - RestoreJobCanceled RestoreJobStatus = "CANCELED" + RestoreJobStarting types.RestoreJobStatus = "STARTING" + RestoreJobRunning types.RestoreJobStatus = "RUNNING" + RestoreJobFailed types.RestoreJobStatus = "FAILED" + RestoreJobSuccessful types.RestoreJobStatus = "SUCCESSFUL" + RestoreJobCanceled types.RestoreJobStatus = "CANCELED" - RestoreJobStatusActivating RestoreJobStatus = "ACTIVATING" - RestoreJobStatusReplaying RestoreJobStatus = "REPLAYING" + RestoreJobStatusActivating types.RestoreJobStatus = "ACTIVATING" + RestoreJobStatusReplaying types.RestoreJobStatus = "REPLAYING" ) diff --git a/api/constants/tenant.go b/api/constants/tenant.go index fe28a7da5..edc043a20 100644 --- a/api/constants/tenant.go +++ b/api/constants/tenant.go @@ -12,27 +12,23 @@ See the Mulan PSL v2 for more details. package constants -type TenantRole string +import "github.com/oceanbase/ob-operator/api/types" const ( - TenantRolePrimary TenantRole = "PRIMARY" - TenantRoleStandby TenantRole = "STANDBY" + TenantRolePrimary types.TenantRole = "PRIMARY" + TenantRoleStandby types.TenantRole = "STANDBY" ) -type TenantOperationType string - const ( - TenantOpSwitchover TenantOperationType = "SWITCHOVER" - TenantOpFailover TenantOperationType = "FAILOVER" - TenantOpChangePwd TenantOperationType = "CHANGE_PASSWORD" + TenantOpSwitchover types.TenantOperationType = "SWITCHOVER" + TenantOpFailover types.TenantOperationType = "FAILOVER" + TenantOpChangePwd types.TenantOperationType = "CHANGE_PASSWORD" ) -type TenantOperationStatus string - const ( - TenantOpStarting TenantOperationStatus = "STARTING" - TenantOpRunning TenantOperationStatus = "RUNNING" - TenantOpSuccessful TenantOperationStatus = "SUCCESSFUL" - TenantOpFailed TenantOperationStatus = "FAILED" - TenantOpReverting TenantOperationStatus = "REVERTING" + TenantOpStarting types.TenantOperationStatus = "STARTING" + TenantOpRunning types.TenantOperationStatus = "RUNNING" + TenantOpSuccessful types.TenantOperationStatus = "SUCCESSFUL" + TenantOpFailed types.TenantOperationStatus = "FAILED" + TenantOpReverting types.TenantOperationStatus = "REVERTING" ) diff --git a/api/types/types.go b/api/types/types.go new file mode 100644 index 000000000..549009494 --- /dev/null +++ b/api/types/types.go @@ -0,0 +1,30 @@ +/* +Copyright (c) 2023 OceanBase +ob-operator is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. +*/ + +package types + +type BackupJobType string +type BackupJobStatus string +type BackupPolicyStatusType string +type BackupDestType string +type LogArchiveDestState string +type ArchiveBinding string +type BackupDestination struct { + Type BackupDestType `json:"type,omitempty"` + Path string `json:"path,omitempty"` +} + +type RestoreJobStatus string + +type TenantRole string +type TenantOperationStatus string +type TenantOperationType string diff --git a/api/v1alpha1/obtenant_types.go b/api/v1alpha1/obtenant_types.go index c32dfeb73..8be9d51bc 100644 --- a/api/v1alpha1/obtenant_types.go +++ b/api/v1alpha1/obtenant_types.go @@ -20,7 +20,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" ) // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! @@ -46,9 +46,9 @@ type OBTenantSpec struct { Pools []ResourcePoolSpec `json:"pools"` //+kubebuilder:default=PRIMARY - TenantRole constants.TenantRole `json:"tenantRole,omitempty"` - Source *TenantSourceSpec `json:"source,omitempty"` - Credentials TenantCredentials `json:"credentials"` + TenantRole apitypes.TenantRole `json:"tenantRole,omitempty"` + Source *TenantSourceSpec `json:"source,omitempty"` + Credentials TenantCredentials `json:"credentials"` } type TenantCredentials struct { @@ -113,9 +113,9 @@ type OBTenantStatus struct { OperationContext *OperationContext `json:"operationContext,omitempty"` TenantRecordInfo TenantRecordInfo `json:"tenantRecordInfo,omitempty"` - TenantRole constants.TenantRole `json:"tenantRole,omitempty"` - Source *TenantSourceStatus `json:"source,omitempty"` - Credentials TenantCredentials `json:"credentials,omitempty"` + TenantRole apitypes.TenantRole `json:"tenantRole,omitempty"` + Source *TenantSourceStatus `json:"source,omitempty"` + Credentials TenantCredentials `json:"credentials,omitempty"` } type TenantSourceStatus struct { diff --git a/api/v1alpha1/obtenantbackup_types.go b/api/v1alpha1/obtenantbackup_types.go index 18a4a0deb..524773684 100644 --- a/api/v1alpha1/obtenantbackup_types.go +++ b/api/v1alpha1/obtenantbackup_types.go @@ -17,7 +17,7 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - constants "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" "github.com/oceanbase/ob-operator/pkg/oceanbase/model" ) @@ -30,11 +30,11 @@ type OBTenantBackupSpec struct { // Important: Run "make" to regenerate code after modifying this file // Foo is an example field of OBTenantBackup. Edit obtenantbackup_types.go to remove/update - Type constants.BackupJobType `json:"type"` - TenantName string `json:"tenantName"` - TenantSecret string `json:"tenantSecret"` - ObClusterName string `json:"obClusterName"` - Path string `json:"path,omitempty"` + Type apitypes.BackupJobType `json:"type"` + TenantName string `json:"tenantName"` + TenantSecret string `json:"tenantSecret"` + ObClusterName string `json:"obClusterName"` + Path string `json:"path,omitempty"` } // +kubebuilder:object:generate=false @@ -42,14 +42,14 @@ type OBTenantBackupSpec struct { type OBTenantBackupStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file - Status constants.BackupJobStatus `json:"status"` - Progress string `json:"progress,omitempty"` - OperationContext *OperationContext `json:"operationContext,omitempty"` - StartedAt string `json:"startedAt,omitempty"` - EndedAt string `json:"endedAt,omitempty"` - BackupJob *model.OBBackupJob `json:"backupJob,omitempty"` - ArchiveLogJob *model.OBArchiveLogJob `json:"archiveLogJob,omitempty"` - DataCleanJob *model.OBBackupCleanJob `json:"dataCleanJob,omitempty"` + Status apitypes.BackupJobStatus `json:"status"` + Progress string `json:"progress,omitempty"` + OperationContext *OperationContext `json:"operationContext,omitempty"` + StartedAt string `json:"startedAt,omitempty"` + EndedAt string `json:"endedAt,omitempty"` + BackupJob *model.OBBackupJob `json:"backupJob,omitempty"` + ArchiveLogJob *model.OBArchiveLogJob `json:"archiveLogJob,omitempty"` + DataCleanJob *model.OBBackupCleanJob `json:"dataCleanJob,omitempty"` } // fix: implementation of DeepCopyInto needed by zz_generated.deepcopy.go diff --git a/api/v1alpha1/obtenantbackuppolicy_types.go b/api/v1alpha1/obtenantbackuppolicy_types.go index ac8616234..9542fc750 100644 --- a/api/v1alpha1/obtenantbackuppolicy_types.go +++ b/api/v1alpha1/obtenantbackuppolicy_types.go @@ -17,7 +17,7 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - constants "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" "github.com/oceanbase/ob-operator/pkg/oceanbase/model" ) @@ -42,8 +42,8 @@ type OBTenantBackupPolicySpec struct { // +kubebuilder:object:generate=false // OBTenantBackupPolicyStatus defines the observed state of OBTenantBackupPolicy type OBTenantBackupPolicyStatus struct { - Status constants.BackupPolicyStatusType `json:"status"` - OperationContext *OperationContext `json:"operationContext,omitempty"` + Status apitypes.BackupPolicyStatusType `json:"status"` + OperationContext *OperationContext `json:"operationContext,omitempty"` NextFull string `json:"nextFull,omitempty"` NextIncremental string `json:"nextIncremental,omitempty"` @@ -127,18 +127,18 @@ func init() { // LogArchiveConfig contains the configuration for log archive progress type LogArchiveConfig struct { - Destination constants.BackupDestination `json:"destination"` - SwitchPieceInterval string `json:"switchPieceInterval"` - Binding constants.ArchiveBinding `json:"binding,omitempty"` - DestDisabled bool `json:"destDisabled,omitempty"` - Concurrency int `json:"concurrency,omitempty"` + Destination apitypes.BackupDestination `json:"destination"` + SwitchPieceInterval string `json:"switchPieceInterval"` + Binding apitypes.ArchiveBinding `json:"binding,omitempty"` + DestDisabled bool `json:"destDisabled,omitempty"` + Concurrency int `json:"concurrency,omitempty"` } // DataBackupConfig contains the configuration for data backup progress type DataBackupConfig struct { - Destination constants.BackupDestination `json:"destination"` - FullCrontab string `json:"fullCrontab,omitempty"` - IncrementalCrontab string `json:"incrementalCrontab,omitempty"` + Destination apitypes.BackupDestination `json:"destination"` + FullCrontab string `json:"fullCrontab,omitempty"` + IncrementalCrontab string `json:"incrementalCrontab,omitempty"` } type CleanPolicy struct { diff --git a/api/v1alpha1/obtenantoperation_types.go b/api/v1alpha1/obtenantoperation_types.go index e94dcfc22..558fd9a23 100644 --- a/api/v1alpha1/obtenantoperation_types.go +++ b/api/v1alpha1/obtenantoperation_types.go @@ -19,7 +19,7 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" ) // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! @@ -30,10 +30,10 @@ type OBTenantOperationSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file - Type constants.TenantOperationType `json:"type"` - Switchover *OBTenantOpSwitchoverSpec `json:"switchover,omitempty"` - Failover *OBTenantOpFailoverSpec `json:"failover,omitempty"` - ChangePwd *OBTenantOpChangePwdSpec `json:"changePwd,omitempty"` + Type apitypes.TenantOperationType `json:"type"` + Switchover *OBTenantOpSwitchoverSpec `json:"switchover,omitempty"` + Failover *OBTenantOpFailoverSpec `json:"failover,omitempty"` + ChangePwd *OBTenantOpChangePwdSpec `json:"changePwd,omitempty"` } type OBTenantOpSwitchoverSpec struct { @@ -54,10 +54,10 @@ type OBTenantOpChangePwdSpec struct { type OBTenantOperationStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file - Status constants.TenantOperationStatus `json:"status"` - OperationContext *OperationContext `json:"operationContext,omitempty"` - PrimaryTenant *OBTenant `json:"primaryTenant,omitempty"` - SecondaryTenant *OBTenant `json:"secondaryTenant,omitempty"` + Status apitypes.TenantOperationStatus `json:"status"` + OperationContext *OperationContext `json:"operationContext,omitempty"` + PrimaryTenant *OBTenant `json:"primaryTenant,omitempty"` + SecondaryTenant *OBTenant `json:"secondaryTenant,omitempty"` } //+kubebuilder:object:root=true diff --git a/api/v1alpha1/obtenantrestore_types.go b/api/v1alpha1/obtenantrestore_types.go index bfee40c36..8ee76b881 100644 --- a/api/v1alpha1/obtenantrestore_types.go +++ b/api/v1alpha1/obtenantrestore_types.go @@ -15,7 +15,8 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" + "github.com/oceanbase/ob-operator/pkg/oceanbase/model" ) @@ -27,12 +28,12 @@ type OBTenantRestoreSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file - TargetTenant string `json:"targetTenant"` - TargetCluster string `json:"targetCluster"` - RestoreRole constants.TenantRole `json:"restoreRole"` - Source RestoreSourceSpec `json:"source"` - Option string `json:"restoreOption"` - PrimaryTenant *string `json:"primaryTenant,omitempty"` + TargetTenant string `json:"targetTenant"` + TargetCluster string `json:"targetCluster"` + RestoreRole apitypes.TenantRole `json:"restoreRole"` + Source RestoreSourceSpec `json:"source"` + Option string `json:"restoreOption"` + PrimaryTenant *string `json:"primaryTenant,omitempty"` } // +kubebuilder:object:generate=false @@ -40,9 +41,9 @@ type OBTenantRestoreSpec struct { type OBTenantRestoreStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file - Status constants.RestoreJobStatus `json:"status"` - RestoreProgress *model.RestoreHistory `json:"restoreProgress,omitempty"` - OperationContext *OperationContext `json:"operationContext,omitempty"` + Status apitypes.RestoreJobStatus `json:"status"` + RestoreProgress *model.RestoreHistory `json:"restoreProgress,omitempty"` + OperationContext *OperationContext `json:"operationContext,omitempty"` } func (in *OBTenantRestoreStatus) DeepCopyInto(out *OBTenantRestoreStatus) { diff --git a/pkg/const/oceanbase/oceanbase.go b/pkg/const/oceanbase/oceanbase.go index ec00925ea..f44f8b50d 100644 --- a/pkg/const/oceanbase/oceanbase.go +++ b/pkg/const/oceanbase/oceanbase.go @@ -120,3 +120,8 @@ const ( const ( OceanbaseAllScope = "oceanbase.*" ) + +const ( + TenantOpRetryTimes = 9 + TenantOpRetryGapSeconds = 9 +) diff --git a/pkg/oceanbase/operation/backup.go b/pkg/oceanbase/operation/backup.go index 9b618936a..1c5d5d6ec 100644 --- a/pkg/oceanbase/operation/backup.go +++ b/pkg/oceanbase/operation/backup.go @@ -16,6 +16,7 @@ import ( "github.com/pkg/errors" "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" "github.com/oceanbase/ob-operator/pkg/oceanbase/const/sql" "github.com/oceanbase/ob-operator/pkg/oceanbase/model" ) @@ -56,7 +57,7 @@ func (m *OceanbaseOperationManager) CreateBackupIncr() error { return m.ExecWithDefaultTimeout(sql.CreateBackupIncr) } -func (m *OceanbaseOperationManager) CreateAndReturnBackupJob(jobType constants.BackupJobType) (*model.OBBackupJob, error) { +func (m *OceanbaseOperationManager) CreateAndReturnBackupJob(jobType apitypes.BackupJobType) (*model.OBBackupJob, error) { var err error if jobType == constants.BackupJobTypeFull { err = m.ExecWithDefaultTimeout(sql.CreateBackupFull) @@ -197,11 +198,11 @@ func (m *OceanbaseOperationManager) listBackupTaskOrHistory(statement string) ([ return tasks, nil } -func (m *OceanbaseOperationManager) GetLatestBackupJobOfType(jobType constants.BackupJobType) (*model.OBBackupJob, error) { +func (m *OceanbaseOperationManager) GetLatestBackupJobOfType(jobType apitypes.BackupJobType) (*model.OBBackupJob, error) { return m.getLatestBackupJob([]string{sql.QueryLatestBackupJobOfType, sql.QueryLatestBackupJobHistoryOfType}, jobType) } -func (m *OceanbaseOperationManager) GetLatestBackupJobOfTypeAndPath(jobType constants.BackupJobType, path string) (*model.OBBackupJob, error) { +func (m *OceanbaseOperationManager) GetLatestBackupJobOfTypeAndPath(jobType apitypes.BackupJobType, path string) (*model.OBBackupJob, error) { return m.getLatestBackupJob([]string{sql.QueryLatestBackupJobOfTypeAndPath, sql.QueryLatestBackupJobHistoryOfTypeAndPath}, jobType, path) } diff --git a/pkg/oceanbase/test/backup_test.go b/pkg/oceanbase/test/backup_test.go index b416affc7..2c4b3fe73 100644 --- a/pkg/oceanbase/test/backup_test.go +++ b/pkg/oceanbase/test/backup_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/gomega" "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" "github.com/oceanbase/ob-operator/pkg/oceanbase/connector" "github.com/oceanbase/ob-operator/pkg/oceanbase/operation" ) @@ -121,7 +122,7 @@ var _ = Describe("Test Backup Operation", Label("backup"), func() { It("Create and return full type BackupJob", func() { Skip("This test will create a backup job, which will take a long time") - var t constants.BackupJobType + var t apitypes.BackupJobType timeNow := time.Now().Unix() if timeNow%2 == 0 { t = constants.BackupJobTypeFull diff --git a/pkg/resource/obtenantbackuppolicy_manager.go b/pkg/resource/obtenantbackuppolicy_manager.go index f53cdfa13..f1e2ba102 100644 --- a/pkg/resource/obtenantbackuppolicy_manager.go +++ b/pkg/resource/obtenantbackuppolicy_manager.go @@ -28,6 +28,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" v1alpha1 "github.com/oceanbase/ob-operator/api/v1alpha1" "github.com/oceanbase/ob-operator/pkg/oceanbase/operation" "github.com/oceanbase/ob-operator/pkg/task" @@ -109,7 +110,7 @@ func (m *ObTenantBackupPolicyManager) ClearTaskInfo() { } func (m *ObTenantBackupPolicyManager) FinishTask() { - m.BackupPolicy.Status.Status = constants.BackupPolicyStatusType(m.BackupPolicy.Status.OperationContext.TargetStatus) + m.BackupPolicy.Status.Status = apitypes.BackupPolicyStatusType(m.BackupPolicy.Status.OperationContext.TargetStatus) m.BackupPolicy.Status.OperationContext = nil } @@ -268,7 +269,7 @@ func (m *ObTenantBackupPolicyManager) HandleFailure() { case "": fallthrough case strategy.StartOver: - m.BackupPolicy.Status.Status = constants.BackupPolicyStatusType(failureRule.NextTryStatus) + m.BackupPolicy.Status.Status = apitypes.BackupPolicyStatusType(failureRule.NextTryStatus) m.BackupPolicy.Status.OperationContext = nil case strategy.RetryFromCurrent: operationContext.TaskStatus = taskstatus.Pending diff --git a/pkg/resource/obtenantbackuppolicy_task.go b/pkg/resource/obtenantbackuppolicy_task.go index 83e8b8d79..ef7386c6c 100644 --- a/pkg/resource/obtenantbackuppolicy_task.go +++ b/pkg/resource/obtenantbackuppolicy_task.go @@ -27,6 +27,8 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + apitypes "github.com/oceanbase/ob-operator/api/types" + constants "github.com/oceanbase/ob-operator/api/constants" "github.com/oceanbase/ob-operator/api/v1alpha1" oceanbaseconst "github.com/oceanbase/ob-operator/pkg/const/oceanbase" @@ -433,7 +435,7 @@ func (m *ObTenantBackupPolicyManager) syncLatestJobs() error { return nil } -func (m *ObTenantBackupPolicyManager) getLatestBackupJob(jobType constants.BackupJobType) (*model.OBBackupJob, error) { +func (m *ObTenantBackupPolicyManager) getLatestBackupJob(jobType apitypes.BackupJobType) (*model.OBBackupJob, error) { con, err := m.getOperationManager() if err != nil { return nil, err @@ -441,7 +443,7 @@ func (m *ObTenantBackupPolicyManager) getLatestBackupJob(jobType constants.Backu return con.GetLatestBackupJobOfType(jobType) } -func (m *ObTenantBackupPolicyManager) getLatestBackupJobOfTypeAndPath(jobType constants.BackupJobType, path string) (*model.OBBackupJob, error) { +func (m *ObTenantBackupPolicyManager) getLatestBackupJobOfTypeAndPath(jobType apitypes.BackupJobType, path string) (*model.OBBackupJob, error) { con, err := m.getOperationManager() if err != nil { return nil, err @@ -508,7 +510,7 @@ func (m *ObTenantBackupPolicyManager) getBackupDestPath() string { return targetDest.Path } -func (m *ObTenantBackupPolicyManager) createBackupJob(jobType constants.BackupJobType) error { +func (m *ObTenantBackupPolicyManager) createBackupJob(jobType apitypes.BackupJobType) error { m.Logger.Info("Create Backup Job", "type", jobType) var path string switch jobType { @@ -552,7 +554,7 @@ func (m *ObTenantBackupPolicyManager) createBackupJob(jobType constants.BackupJo return m.Client.Create(m.Ctx, backupJob) } -func (m *ObTenantBackupPolicyManager) createBackupJobIfNotExists(jobType constants.BackupJobType) error { +func (m *ObTenantBackupPolicyManager) createBackupJobIfNotExists(jobType apitypes.BackupJobType) error { noRunningJobs, err := m.noRunningJobs(jobType) if err != nil { m.Logger.Error(err, "Failed to check if there is running backup job") @@ -564,7 +566,7 @@ func (m *ObTenantBackupPolicyManager) createBackupJobIfNotExists(jobType constan return nil } -func (m *ObTenantBackupPolicyManager) noRunningJobs(jobType constants.BackupJobType) (bool, error) { +func (m *ObTenantBackupPolicyManager) noRunningJobs(jobType apitypes.BackupJobType) (bool, error) { var runningJobs v1alpha1.OBTenantBackupList err := m.Client.List(m.Ctx, &runningJobs, client.MatchingLabels{ diff --git a/pkg/resource/obtenantoperation_manager.go b/pkg/resource/obtenantoperation_manager.go index b709dee7a..a30501617 100644 --- a/pkg/resource/obtenantoperation_manager.go +++ b/pkg/resource/obtenantoperation_manager.go @@ -25,6 +25,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" v1alpha1 "github.com/oceanbase/ob-operator/api/v1alpha1" "github.com/oceanbase/ob-operator/pkg/oceanbase/operation" "github.com/oceanbase/ob-operator/pkg/task" @@ -124,7 +125,7 @@ func (m *ObTenantOperationManager) HandleFailure() { case "": fallthrough case strategy.StartOver: - m.Resource.Status.Status = constants.TenantOperationStatus(failureRule.NextTryStatus) + m.Resource.Status.Status = apitypes.TenantOperationStatus(failureRule.NextTryStatus) m.Resource.Status.OperationContext = nil case strategy.RetryFromCurrent: operationContext.TaskStatus = taskstatus.Pending @@ -134,7 +135,7 @@ func (m *ObTenantOperationManager) HandleFailure() { } func (m *ObTenantOperationManager) FinishTask() { - m.Resource.Status.Status = constants.TenantOperationStatus(m.Resource.Status.OperationContext.TargetStatus) + m.Resource.Status.Status = apitypes.TenantOperationStatus(m.Resource.Status.OperationContext.TargetStatus) m.Resource.Status.OperationContext = nil } diff --git a/pkg/resource/obtenantoperation_task.go b/pkg/resource/obtenantoperation_task.go index b2bb7783f..0ab4da0ac 100644 --- a/pkg/resource/obtenantoperation_task.go +++ b/pkg/resource/obtenantoperation_task.go @@ -22,6 +22,7 @@ import ( "github.com/oceanbase/ob-operator/api/constants" "github.com/oceanbase/ob-operator/api/v1alpha1" + oceanbaseconst "github.com/oceanbase/ob-operator/pkg/const/oceanbase" "github.com/oceanbase/ob-operator/pkg/oceanbase/operation" "github.com/oceanbase/ob-operator/pkg/oceanbase/param" ) @@ -35,7 +36,7 @@ func (m *ObTenantOperationManager) ChangeTenantRootPassword() error { if err != nil { return err } - err = con.ChangeTenantUserPassword("root", pwd) + err = con.ChangeTenantUserPassword(oceanbaseconst.RootUser, pwd) if err != nil { return err } @@ -85,7 +86,7 @@ func (m *ObTenantOperationManager) CreateUsersForActivatedStandby() error { } // Wait for the tenant to be ready - maxRetry := 9 + maxRetry := oceanbaseconst.TenantOpRetryTimes counter := 0 for counter < maxRetry { tenants, err := con.ListTenantWithName(m.Resource.Status.PrimaryTenant.Spec.TenantName) @@ -99,7 +100,7 @@ func (m *ObTenantOperationManager) CreateUsersForActivatedStandby() error { if t.TenantType == "USER" && t.TenantRole == "PRIMARY" && t.SwitchoverStatus == "NORMAL" { break } - time.Sleep(9 * time.Second) + time.Sleep(oceanbaseconst.TenantOpRetryGapSeconds * time.Second) counter++ } if counter >= maxRetry { @@ -137,7 +138,7 @@ func (m *ObTenantOperationManager) SwitchTenantsRole() error { if err != nil { return err } - maxRetry := 5 + maxRetry := oceanbaseconst.TenantOpRetryTimes counter := 0 for counter < maxRetry { primary, err := con.ListTenantWithName(m.Resource.Status.PrimaryTenant.Spec.TenantName) @@ -149,7 +150,7 @@ func (m *ObTenantOperationManager) SwitchTenantsRole() error { } p := primary[0] if p.TenantRole != "STANDBY" || p.SwitchoverStatus != "NORMAL" { - time.Sleep(9 * time.Second) + time.Sleep(oceanbaseconst.TenantOpRetryGapSeconds * time.Second) counter++ } else { break @@ -177,7 +178,7 @@ func (m *ObTenantOperationManager) SwitchTenantsRole() error { } s := standby[0] if s.TenantRole != "PRIMARY" || s.SwitchoverStatus != "NORMAL" { - time.Sleep(9 * time.Second) + time.Sleep(oceanbaseconst.TenantOpRetryGapSeconds * time.Second) counter++ } else { break diff --git a/pkg/resource/obtenantrestore_manager.go b/pkg/resource/obtenantrestore_manager.go index e7fa94d53..b06018c28 100644 --- a/pkg/resource/obtenantrestore_manager.go +++ b/pkg/resource/obtenantrestore_manager.go @@ -24,6 +24,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/oceanbase/ob-operator/api/constants" + apitypes "github.com/oceanbase/ob-operator/api/types" v1alpha1 "github.com/oceanbase/ob-operator/api/v1alpha1" "github.com/oceanbase/ob-operator/pkg/oceanbase/model" "github.com/oceanbase/ob-operator/pkg/oceanbase/operation" @@ -72,7 +73,7 @@ func (m ObTenantRestoreManager) ClearTaskInfo() { } func (m ObTenantRestoreManager) FinishTask() { - m.Resource.Status.Status = constants.RestoreJobStatus(m.Resource.Status.OperationContext.TargetStatus) + m.Resource.Status.Status = apitypes.RestoreJobStatus(m.Resource.Status.OperationContext.TargetStatus) m.Resource.Status.OperationContext = nil } @@ -86,7 +87,7 @@ func (m ObTenantRestoreManager) HandleFailure() { case "": fallthrough case strategy.StartOver: - m.Resource.Status.Status = constants.RestoreJobStatus(failureRule.NextTryStatus) + m.Resource.Status.Status = apitypes.RestoreJobStatus(failureRule.NextTryStatus) m.Resource.Status.OperationContext = nil case strategy.RetryFromCurrent: operationContext.TaskStatus = taskstatus.Pending