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 1bb20f6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 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
4 changes: 3 additions & 1 deletion controllers/user/controllers/deleterequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"time"

"sigs.k8s.io/controller-runtime/pkg/builder"

"github.com/go-logr/logr"

userv1 "github.com/labring/sealos/controllers/user/api/v1"
Expand Down Expand Up @@ -174,7 +176,7 @@ func (r *DeleteRequestReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.expirationTime = time.Minute * 10
r.retentionTime = time.Minute * 30
return ctrl.NewControllerManagedBy(mgr).
For(&userv1.DeleteRequest{}).
For(&userv1.DeleteRequest{}, builder.WithPredicates(namespaceOnlyPredicate(config.GetUserSystemNamespace()))).
Complete(r)
}

Expand Down
20 changes: 8 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 Down Expand Up @@ -112,6 +114,12 @@ 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()
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 +206,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 1bb20f6

Please sign in to comment.