From e4afc19da80fb7f97697bf72f49968f183391f1c Mon Sep 17 00:00:00 2001 From: jiahui Date: Fri, 18 Oct 2024 16:52:41 +0800 Subject: [PATCH] operationrequest controller add user lock && add crd printcolumn: creationTimestamp --- .../user/api/v1/deleterequest_types.go | 1 + .../user/api/v1/operationrequest_types.go | 1 + .../bases/user.sealos.io_deleterequests.yaml | 3 +++ .../user.sealos.io_operationrequests.yaml | 3 +++ .../operationrequest_controller.go | 22 +++++++++---------- .../user/deploy/manifests/deploy.yaml.tmpl | 6 +++++ 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/controllers/user/api/v1/deleterequest_types.go b/controllers/user/api/v1/deleterequest_types.go index 387badd52a2..7b4ca3dd1fb 100644 --- a/controllers/user/api/v1/deleterequest_types.go +++ b/controllers/user/api/v1/deleterequest_types.go @@ -36,6 +36,7 @@ type DeleteRequestStatus struct { // +kubebuilder:resource:scope=Cluster //+kubebuilder:printcolumn:name="User",type="string",JSONPath=".spec.user" //+kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase" +//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" // DeleteRequest is the Schema for the deleterequests API type DeleteRequest struct { diff --git a/controllers/user/api/v1/operationrequest_types.go b/controllers/user/api/v1/operationrequest_types.go index 19893830b6d..8b0d2adf294 100644 --- a/controllers/user/api/v1/operationrequest_types.go +++ b/controllers/user/api/v1/operationrequest_types.go @@ -62,6 +62,7 @@ const ( //+kubebuilder:printcolumn:name="User",type="string",JSONPath=".spec.user" //+kubebuilder:printcolumn:name="Role",type="string",JSONPath=".spec.role" //+kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase" +//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" //+kubebuilder:object:root=true //+kubebuilder:subresource:status diff --git a/controllers/user/config/crd/bases/user.sealos.io_deleterequests.yaml b/controllers/user/config/crd/bases/user.sealos.io_deleterequests.yaml index 51777d6ca58..575311e7219 100644 --- a/controllers/user/config/crd/bases/user.sealos.io_deleterequests.yaml +++ b/controllers/user/config/crd/bases/user.sealos.io_deleterequests.yaml @@ -35,6 +35,9 @@ spec: - jsonPath: .status.phase name: Phase type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date name: v1 schema: openAPIV3Schema: diff --git a/controllers/user/config/crd/bases/user.sealos.io_operationrequests.yaml b/controllers/user/config/crd/bases/user.sealos.io_operationrequests.yaml index b30058a00b9..cfabdc44c85 100644 --- a/controllers/user/config/crd/bases/user.sealos.io_operationrequests.yaml +++ b/controllers/user/config/crd/bases/user.sealos.io_operationrequests.yaml @@ -44,6 +44,9 @@ spec: - jsonPath: .status.phase name: Phase type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date name: v1 schema: openAPIV3Schema: diff --git a/controllers/user/controllers/operationrequest_controller.go b/controllers/user/controllers/operationrequest_controller.go index df60723f235..1691fd51436 100644 --- a/controllers/user/controllers/operationrequest_controller.go +++ b/controllers/user/controllers/operationrequest_controller.go @@ -19,6 +19,7 @@ package controllers import ( "context" "fmt" + "sync" "time" "sigs.k8s.io/controller-runtime/pkg/builder" @@ -52,6 +53,7 @@ type OperationReqReconciler struct { Logger logr.Logger Scheme *runtime.Scheme Recorder record.EventRecorder + userLock map[string]*sync.Mutex // expirationTime is the time duration of the request is expired expirationTime time.Duration @@ -72,6 +74,7 @@ func (r *OperationReqReconciler) SetupWithManager(mgr ctrl.Manager, opts util.Ra r.Scheme = mgr.GetScheme() r.expirationTime = expTime r.retentionTime = retTime + r.userLock = make(map[string]*sync.Mutex) r.Logger.V(1).Info("init reconcile operationrequest controller") return ctrl.NewControllerManagedBy(mgr). For(&userv1.Operationrequest{}, builder.WithPredicates(namespaceOnlyPredicate(config.GetUserSystemNamespace()))). @@ -112,6 +115,13 @@ func (r *OperationReqReconciler) Reconcile(ctx context.Context, req ctrl.Request } func (r *OperationReqReconciler) reconcile(ctx context.Context, request *userv1.Operationrequest) (ctrl.Result, error) { + userLock, ok := r.userLock[request.Spec.User] + if !ok { + userLock = &sync.Mutex{} + r.userLock[request.Spec.User] = userLock + } + userLock.Lock() + defer userLock.Unlock() r.Logger.V(1).Info("start reconcile controller operationRequest", getLog(request)...) // count the time cost of handling the request startTime := time.Now() @@ -198,18 +208,6 @@ func (r *OperationReqReconciler) reconcile(ctx context.Context, request *userv1. r.Recorder.Eventf(request, v1.EventTypeWarning, "Failed to delete rolebinding", "Failed to delete rolebinding %s/%s", rolebinding.Namespace, rolebinding.Name) return ctrl.Result{}, err } - //err := wait.PollUntilContextTimeout(ctx, time.Second, time.Minute, true, func(ct context.Context) (bool, error) { - // rb := &rbacv1.RoleBinding{} - // err := r.Get(ct, types.NamespacedName{Name: rolebinding.Name, Namespace: rolebinding.Namespace}, rb) - // if apierrors.IsNotFound(err) { - // return true, nil - // } - // return false, nil - //}) - //if err != nil { - // r.Recorder.Eventf(request, v1.EventTypeWarning, "Failed to delete rolebinding", "Failed to delete rolebinding %s/%s", rolebinding.Namespace, rolebinding.Name) - // return ctrl.Result{}, err - //} if err := r.Create(ctx, rolebinding); err != nil { r.Recorder.Eventf(request, v1.EventTypeWarning, "Failed to create rolebinding", "Failed to create rolebinding %s/%s", rolebinding.Namespace, rolebinding.Name) return ctrl.Result{}, err diff --git a/controllers/user/deploy/manifests/deploy.yaml.tmpl b/controllers/user/deploy/manifests/deploy.yaml.tmpl index f89529874a3..c9a83881014 100644 --- a/controllers/user/deploy/manifests/deploy.yaml.tmpl +++ b/controllers/user/deploy/manifests/deploy.yaml.tmpl @@ -27,6 +27,9 @@ spec: - jsonPath: .status.phase name: Phase type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date name: v1 schema: openAPIV3Schema: @@ -103,6 +106,9 @@ spec: - jsonPath: .status.phase name: Phase type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date name: v1 schema: openAPIV3Schema: