Skip to content

Commit

Permalink
operationrequest controller add user lock && add crd printcolumn: cre…
Browse files Browse the repository at this point in the history
…ationTimestamp
  • Loading branch information
bxy4543 committed Oct 18, 2024
1 parent 7f10f6b commit e4afc19
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions controllers/user/api/v1/deleterequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions controllers/user/api/v1/operationrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ spec:
- jsonPath: .status.phase
name: Phase
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ spec:
- jsonPath: .status.phase
name: Phase
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
Expand Down
22 changes: 10 additions & 12 deletions controllers/user/controllers/operationrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"sync"
"time"

"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -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
Expand All @@ -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()))).
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions controllers/user/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ spec:
- jsonPath: .status.phase
name: Phase
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -103,6 +106,9 @@ spec:
- jsonPath: .status.phase
name: Phase
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
Expand Down

0 comments on commit e4afc19

Please sign in to comment.