Skip to content

Commit

Permalink
Add resource labels; Coordinate when sub-resource changes (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
powerfooI authored Feb 28, 2024
1 parent 9fc0492 commit bc62619
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 6 deletions.
11 changes: 9 additions & 2 deletions api/v1alpha1/obtenant_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/obtenantbackuppolicy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
})
Expand Down
8 changes: 8 additions & 0 deletions api/v1alpha1/obtenantoperation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand All @@ -90,6 +95,7 @@ func (r *OBTenantOperation) Default() {
Name: firstMeta.GetName(),
UID: firstMeta.GetUID(),
})
labels[oceanbaseconst.LabelTenantName] = firstMeta.GetName()
}

if secondaryTenantName != "" {
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions internal/const/oceanbase/oceanbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions internal/controller/obcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions internal/controller/obzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bc62619

Please sign in to comment.