From bc626191e98dd57b374ab050626f3d74830436d1 Mon Sep 17 00:00:00 2001 From: Powerfool Date: Wed, 28 Feb 2024 21:01:01 +0800 Subject: [PATCH] Add resource labels; Coordinate when sub-resource changes (#212) --- api/v1alpha1/obtenant_webhook.go | 11 +++++++++-- api/v1alpha1/obtenantbackuppolicy_webhook.go | 2 +- api/v1alpha1/obtenantoperation_webhook.go | 8 ++++++++ internal/const/oceanbase/oceanbase.go | 5 +++-- internal/controller/obcluster_controller.go | 1 + internal/controller/obzone_controller.go | 1 + .../obtenantbackuppolicy_manager.go | 2 +- 7 files changed, 24 insertions(+), 6 deletions(-) diff --git a/api/v1alpha1/obtenant_webhook.go b/api/v1alpha1/obtenant_webhook.go index 16165f96e..1906efbdf 100644 --- a/api/v1alpha1/obtenant_webhook.go +++ b/api/v1alpha1/obtenant_webhook.go @@ -66,12 +66,19 @@ func (r *OBTenant) Default() { if err != nil { tenantlog.Error(err, "Failed to get cluster") } else { + clusterMeta := cluster.GetObjectMeta() r.SetOwnerReferences([]metav1.OwnerReference{{ APIVersion: cluster.APIVersion, Kind: cluster.Kind, - Name: cluster.GetObjectMeta().GetName(), - UID: cluster.GetObjectMeta().GetUID(), + Name: clusterMeta.GetName(), + UID: clusterMeta.GetUID(), }}) + labels := r.GetLabels() + if labels == nil { + labels = make(map[string]string) + } + labels[oceanbaseconst.LabelRefOBCluster] = clusterMeta.GetName() + r.SetLabels(labels) } if r.Spec.TenantRole == "" { diff --git a/api/v1alpha1/obtenantbackuppolicy_webhook.go b/api/v1alpha1/obtenantbackuppolicy_webhook.go index 9db2dda91..f86691987 100644 --- a/api/v1alpha1/obtenantbackuppolicy_webhook.go +++ b/api/v1alpha1/obtenantbackuppolicy_webhook.go @@ -111,7 +111,7 @@ func (r *OBTenantBackupPolicy) Default() { }}) r.SetLabels(map[string]string{ - oceanbaseconst.LabelTenantName: r.Spec.TenantName, + oceanbaseconst.LabelTenantName: r.Spec.TenantCRName, oceanbaseconst.LabelRefOBCluster: r.Spec.ObClusterName, oceanbaseconst.LabelRefUID: string(tenant.GetObjectMeta().GetUID()), }) diff --git a/api/v1alpha1/obtenantoperation_webhook.go b/api/v1alpha1/obtenantoperation_webhook.go index 373e69b7f..78554cd22 100644 --- a/api/v1alpha1/obtenantoperation_webhook.go +++ b/api/v1alpha1/obtenantoperation_webhook.go @@ -36,6 +36,7 @@ import ( "github.com/oceanbase/ob-operator/api/constants" apitypes "github.com/oceanbase/ob-operator/api/types" + oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase" ) // log is for logging in this package. @@ -73,6 +74,10 @@ func (r *OBTenantOperation) Default() { targetTenantName = *r.Spec.TargetTenant } references := r.GetOwnerReferences() + labels := r.GetLabels() + if labels == nil { + labels = make(map[string]string) + } if targetTenantName != "" { err := clt.Get(context.Background(), types.NamespacedName{ @@ -90,6 +95,7 @@ func (r *OBTenantOperation) Default() { Name: firstMeta.GetName(), UID: firstMeta.GetUID(), }) + labels[oceanbaseconst.LabelTenantName] = firstMeta.GetName() } if secondaryTenantName != "" { @@ -109,9 +115,11 @@ func (r *OBTenantOperation) Default() { Name: secondMeta.GetName(), UID: secondMeta.GetUID(), }) + labels[oceanbaseconst.LabelSecondaryTenant] = secondMeta.GetName() } r.SetOwnerReferences(references) + r.SetLabels(labels) } // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. diff --git a/internal/const/oceanbase/oceanbase.go b/internal/const/oceanbase/oceanbase.go index e97dcd380..8411d8229 100644 --- a/internal/const/oceanbase/oceanbase.go +++ b/internal/const/oceanbase/oceanbase.go @@ -144,8 +144,9 @@ const ( ) const ( - LabelTenantName = "oceanbase.oceanbase.com/tenant-name" - LabelBackupType = "oceanbase.oceanbase.com/backup-type" + LabelTenantName = "oceanbase.oceanbase.com/tenant-name" + LabelSecondaryTenant = "oceanbase.oceanbase.com/secondary-tenant" + LabelBackupType = "oceanbase.oceanbase.com/backup-type" ) const ( diff --git a/internal/controller/obcluster_controller.go b/internal/controller/obcluster_controller.go index dbe2ca88f..cc2f3772c 100644 --- a/internal/controller/obcluster_controller.go +++ b/internal/controller/obcluster_controller.go @@ -97,6 +97,7 @@ func (r *OBClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( func (r *OBClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&v1alpha1.OBCluster{}). + Owns(&v1alpha1.OBZone{}). WithEventFilter(preds). Complete(r) } diff --git a/internal/controller/obzone_controller.go b/internal/controller/obzone_controller.go index 60e639d74..9d959814c 100644 --- a/internal/controller/obzone_controller.go +++ b/internal/controller/obzone_controller.go @@ -87,6 +87,7 @@ func (r *OBZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr func (r *OBZoneReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&v1alpha1.OBZone{}). + Owns(&v1alpha1.OBServer{}). WithEventFilter(preds). Complete(r) } diff --git a/internal/resource/obtenantbackuppolicy/obtenantbackuppolicy_manager.go b/internal/resource/obtenantbackuppolicy/obtenantbackuppolicy_manager.go index eb44a8dff..f2a7eadcb 100644 --- a/internal/resource/obtenantbackuppolicy/obtenantbackuppolicy_manager.go +++ b/internal/resource/obtenantbackuppolicy/obtenantbackuppolicy_manager.go @@ -168,7 +168,7 @@ func (m *ObTenantBackupPolicyManager) UpdateStatus() error { m.BackupPolicy.Status.OperationContext = nil } else if !m.BackupPolicy.Spec.Suspend && m.BackupPolicy.Status.Status == constants.BackupPolicyStatusPaused { m.BackupPolicy.Status.Status = constants.BackupPolicyStatusResuming - } else if m.IsDeleting() && m.BackupPolicy.Status.Status == constants.BackupPolicyStatusRunning { + } else if m.IsDeleting() && (m.BackupPolicy.Status.Status == constants.BackupPolicyStatusRunning || m.BackupPolicy.Status.Status == constants.BackupPolicyStatusPaused) { m.BackupPolicy.Status.Status = constants.BackupPolicyStatusDeleting m.BackupPolicy.Status.OperationContext = nil } else if m.BackupPolicy.Status.Status == constants.BackupPolicyStatusRunning {