Skip to content

Commit 67d810e

Browse files
utam0kroboquat
utam0k
authored andcommitted
ws-manager: Improve the peformance of reconcile handlerr
1 parent 808d58c commit 67d810e

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

components/ws-manager/pkg/manager/annotations.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ const (
8585

8686
// attemptingToCreatePodAnnotation is set when ws-manager is trying to create pod and is removed when pod is successfully scheduled on the node
8787
attemptingToCreatePodAnnotation = "gitpod.io/attemptingToCreate"
88+
89+
// alreadyInitializingAnnotation is set when initializing is done
90+
alreadyInitializingAnnotation = "gitpod.io/alreadyInitializing"
8891
)
8992

9093
// markWorkspaceAsReady adds annotations to a workspace pod

components/ws-manager/pkg/manager/pod_controller.go

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515
"k8s.io/apimachinery/pkg/watch"
1616
ctrl "sigs.k8s.io/controller-runtime"
1717
"sigs.k8s.io/controller-runtime/pkg/client"
18-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
18+
"sigs.k8s.io/controller-runtime/pkg/event"
19+
"sigs.k8s.io/controller-runtime/pkg/predicate"
1920
)
2021

2122
// PodReconciler reconciles a Pod object
@@ -32,41 +33,58 @@ type PodReconciler struct {
3233
// For more details, check Reconcile and its Result here:
3334
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
3435
func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
35-
var pod corev1.Pod
36-
err := r.Client.Get(context.Background(), req.NamespacedName, &pod)
37-
if errors.IsNotFound(err) {
38-
// pod is gone - that's ok
39-
if pod, ok := r.Pods[req.NamespacedName]; ok {
40-
delete(r.Pods, req.NamespacedName)
41-
queue := pod.Annotations[workspaceIDAnnotation]
42-
if queue == "" {
43-
return ctrl.Result{}, nil
36+
go func() {
37+
var pod corev1.Pod
38+
err := r.Client.Get(context.Background(), req.NamespacedName, &pod)
39+
if errors.IsNotFound(err) {
40+
// pod is gone - that's ok
41+
if pod, ok := r.Pods[req.NamespacedName]; ok {
42+
delete(r.Pods, req.NamespacedName)
43+
queue := pod.Annotations[workspaceIDAnnotation]
44+
if queue == "" {
45+
return
46+
}
47+
r.Monitor.eventpool.Add(queue, watch.Event{
48+
Type: watch.Deleted,
49+
Object: &pod,
50+
})
4451
}
45-
r.Monitor.eventpool.Add(queue, watch.Event{
46-
Type: watch.Deleted,
47-
Object: &pod,
48-
})
52+
return
4953
}
50-
return reconcile.Result{}, nil
51-
}
52-
r.Pods[req.NamespacedName] = pod
54+
r.Pods[req.NamespacedName] = pod
5355

54-
queue := pod.Annotations[workspaceIDAnnotation]
55-
if queue == "" {
56-
return ctrl.Result{}, nil
57-
}
56+
queue := pod.Annotations[workspaceIDAnnotation]
57+
if queue == "" {
58+
return
59+
}
5860

59-
r.Monitor.eventpool.Add(queue, watch.Event{
60-
Type: watch.Modified,
61-
Object: &pod,
62-
})
61+
r.Monitor.eventpool.Add(queue, watch.Event{
62+
Type: watch.Modified,
63+
Object: &pod,
64+
})
65+
}()
6366

6467
return ctrl.Result{}, nil
6568
}
6669

6770
// SetupWithManager sets up the controller with the Manager.
6871
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
6972
return ctrl.NewControllerManagedBy(mgr).
73+
WithEventFilter(
74+
predicate.Funcs{
75+
UpdateFunc: func(e event.UpdateEvent) bool {
76+
_, ok := e.ObjectNew.GetAnnotations()[workspaceIDAnnotation]
77+
return ok
78+
},
79+
CreateFunc: func(e event.CreateEvent) bool {
80+
_, ok := e.Object.GetAnnotations()[workspaceIDAnnotation]
81+
return ok
82+
},
83+
DeleteFunc: func(e event.DeleteEvent) bool {
84+
_, ok := e.Object.GetAnnotations()[workspaceIDAnnotation]
85+
return ok
86+
},
87+
}).
7088
For(&corev1.Pod{}).
7189
Complete(r)
7290
}

0 commit comments

Comments
 (0)