feat: 初始化 controller 状态机
This commit is contained in:
@@ -18,7 +18,9 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
@@ -47,9 +49,29 @@ type PostgreSQLTenantReconciler struct {
|
||||
// For more details, check Reconcile and its Result here:
|
||||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
|
||||
func (r *PostgreSQLTenantReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
_ = logf.FromContext(ctx)
|
||||
logger := logf.FromContext(ctx)
|
||||
tenant := &databasev1alpha1.PostgreSQLTenant{}
|
||||
if err := r.Get(ctx, req.NamespacedName, tenant); err != nil {
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
|
||||
// TODO(user): your logic here
|
||||
before := tenant.DeepCopy()
|
||||
if !tenant.DeletionTimestamp.IsZero() {
|
||||
tenant.Status.Phase = databasev1alpha1.PostgreSQLTenantPhaseDeleting
|
||||
setReconcilingCondition(&tenant.Status.Conditions, tenant.Generation, "tenant deletion is reconciling")
|
||||
} else if tenant.Status.Phase == "" {
|
||||
tenant.Status.Phase = databasev1alpha1.PostgreSQLTenantPhasePending
|
||||
setReconcilingCondition(&tenant.Status.Conditions, tenant.Generation, "tenant is waiting for its instance")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(before.Status, tenant.Status) {
|
||||
if err := r.Status().Patch(ctx, tenant, client.MergeFrom(before)); err != nil {
|
||||
if apierrors.IsConflict(err) {
|
||||
logger.V(1).Info("tenant status changed concurrently; retrying")
|
||||
}
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user