Skip to content

Commit

Permalink
ws-manager: Improve the peformance of reconcile handlerr
Browse files Browse the repository at this point in the history
  • Loading branch information
utam0k authored and roboquat committed Dec 28, 2022
1 parent 808d58c commit 67d810e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
3 changes: 3 additions & 0 deletions components/ws-manager/pkg/manager/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ const (

// attemptingToCreatePodAnnotation is set when ws-manager is trying to create pod and is removed when pod is successfully scheduled on the node
attemptingToCreatePodAnnotation = "gitpod.io/attemptingToCreate"

// alreadyInitializingAnnotation is set when initializing is done
alreadyInitializingAnnotation = "gitpod.io/alreadyInitializing"
)

// markWorkspaceAsReady adds annotations to a workspace pod
Expand Down
68 changes: 43 additions & 25 deletions components/ws-manager/pkg/manager/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
"k8s.io/apimachinery/pkg/watch"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// PodReconciler reconciles a Pod object
Expand All @@ -32,41 +33,58 @@ type PodReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var pod corev1.Pod
err := r.Client.Get(context.Background(), req.NamespacedName, &pod)
if errors.IsNotFound(err) {
// pod is gone - that's ok
if pod, ok := r.Pods[req.NamespacedName]; ok {
delete(r.Pods, req.NamespacedName)
queue := pod.Annotations[workspaceIDAnnotation]
if queue == "" {
return ctrl.Result{}, nil
go func() {
var pod corev1.Pod
err := r.Client.Get(context.Background(), req.NamespacedName, &pod)
if errors.IsNotFound(err) {
// pod is gone - that's ok
if pod, ok := r.Pods[req.NamespacedName]; ok {
delete(r.Pods, req.NamespacedName)
queue := pod.Annotations[workspaceIDAnnotation]
if queue == "" {
return
}
r.Monitor.eventpool.Add(queue, watch.Event{
Type: watch.Deleted,
Object: &pod,
})
}
r.Monitor.eventpool.Add(queue, watch.Event{
Type: watch.Deleted,
Object: &pod,
})
return
}
return reconcile.Result{}, nil
}
r.Pods[req.NamespacedName] = pod
r.Pods[req.NamespacedName] = pod

queue := pod.Annotations[workspaceIDAnnotation]
if queue == "" {
return ctrl.Result{}, nil
}
queue := pod.Annotations[workspaceIDAnnotation]
if queue == "" {
return
}

r.Monitor.eventpool.Add(queue, watch.Event{
Type: watch.Modified,
Object: &pod,
})
r.Monitor.eventpool.Add(queue, watch.Event{
Type: watch.Modified,
Object: &pod,
})
}()

return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
WithEventFilter(
predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
_, ok := e.ObjectNew.GetAnnotations()[workspaceIDAnnotation]
return ok
},
CreateFunc: func(e event.CreateEvent) bool {
_, ok := e.Object.GetAnnotations()[workspaceIDAnnotation]
return ok
},
DeleteFunc: func(e event.DeleteEvent) bool {
_, ok := e.Object.GetAnnotations()[workspaceIDAnnotation]
return ok
},
}).
For(&corev1.Pod{}).
Complete(r)
}

0 comments on commit 67d810e

Please sign in to comment.