Skip to content

Commit 36c751a

Browse files
committed
Bount the source of subscribers to an informer
1 parent 3371869 commit 36c751a

File tree

3 files changed

+106
-10
lines changed

3 files changed

+106
-10
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package controllers
6+
7+
import (
8+
"context"
9+
"os"
10+
11+
"k8s.io/apimachinery/pkg/api/errors"
12+
"k8s.io/apimachinery/pkg/runtime"
13+
"k8s.io/client-go/tools/record"
14+
ctrl "sigs.k8s.io/controller-runtime"
15+
"sigs.k8s.io/controller-runtime/pkg/client"
16+
"sigs.k8s.io/controller-runtime/pkg/controller"
17+
"sigs.k8s.io/controller-runtime/pkg/handler"
18+
"sigs.k8s.io/controller-runtime/pkg/log"
19+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
20+
"sigs.k8s.io/controller-runtime/pkg/source"
21+
22+
config "github.com/gitpod-io/gitpod/ws-manager/api/config"
23+
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
24+
)
25+
26+
func NewSubscriberReconciler(c client.Client, scheme *runtime.Scheme, recorder record.EventRecorder, cfg *config.Configuration) (*SubscriberReconciler, error) {
27+
reconciler := &SubscriberReconciler{
28+
Client: c,
29+
Scheme: scheme,
30+
Config: cfg,
31+
Recorder: recorder,
32+
}
33+
34+
return reconciler, nil
35+
}
36+
37+
type SubscriberReconciler struct {
38+
client.Client
39+
Scheme *runtime.Scheme
40+
41+
Config *config.Configuration
42+
Recorder record.EventRecorder
43+
OnReconcile func(ctx context.Context, ws *workspacev1.Workspace)
44+
}
45+
46+
// Reconcile is part of the main kubernetes reconciliation loop which aims to
47+
// move the current state of the cluster closer to the desired state.
48+
// Modify the Reconcile function to compare the state specified by
49+
// the Workspace object against the actual cluster state, and then
50+
// perform operations to make the cluster state reflect the state specified by
51+
// the user.
52+
//
53+
// For more details, check Reconcile and its Result here:
54+
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
55+
func (r *SubscriberReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
56+
log := log.FromContext(ctx)
57+
58+
var workspace workspacev1.Workspace
59+
if err := r.Get(ctx, req.NamespacedName, &workspace); err != nil {
60+
if !errors.IsNotFound(err) {
61+
log.Error(err, "unable to fetch workspace")
62+
}
63+
64+
return ctrl.Result{}, client.IgnoreNotFound(err)
65+
}
66+
67+
if r.OnReconcile != nil {
68+
// Publish to subscribers in a goroutine, to prevent blocking the main reconcile loop.
69+
ws := workspace.DeepCopy()
70+
go func() {
71+
r.OnReconcile(ctx, ws)
72+
}()
73+
}
74+
75+
return reconcile.Result{}, nil
76+
}
77+
78+
// SetupWithManager sets up the controller with the Manager.
79+
func (r *SubscriberReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
80+
c, err := controller.NewUnmanaged("maintenance-controller", mgr, controller.Options{Reconciler: r})
81+
if err != nil {
82+
return err
83+
}
84+
85+
go func() {
86+
err = c.Start(ctx)
87+
if err != nil {
88+
log.FromContext(ctx).Error(err, "cannot start maintenance reconciler")
89+
os.Exit(1)
90+
}
91+
}()
92+
93+
return c.Watch(source.Kind(mgr.GetCache(), &workspacev1.Workspace{}), &handler.EnqueueRequestForObject{})
94+
}

components/ws-manager-mk2/controllers/workspace_controller.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ type WorkspaceReconciler struct {
7171
metrics *controllerMetrics
7272
maintenance maintenance.Maintenance
7373
Recorder record.EventRecorder
74-
OnReconcile func(ctx context.Context, ws *workspacev1.Workspace)
7574
}
7675

7776
//+kubebuilder:rbac:groups=workspace.gitpod.io,resources=workspaces,verbs=get;list;watch;create;update;patch;delete
@@ -107,14 +106,6 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
107106
workspace.Status.Conditions = []metav1.Condition{}
108107
}
109108

110-
if r.OnReconcile != nil {
111-
// Publish to subscribers in a goroutine, to prevent blocking the main reconcile loop.
112-
ws := workspace.DeepCopy()
113-
go func() {
114-
r.OnReconcile(ctx, ws)
115-
}()
116-
}
117-
118109
log.Info("reconciling workspace", "workspace", req.NamespacedName, "phase", workspace.Status.Phase)
119110

120111
var workspacePods corev1.PodList

components/ws-manager-mk2/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,18 @@ func main() {
185185
os.Exit(1)
186186
}
187187

188-
workspaceReconciler.OnReconcile = wsmanService.OnWorkspaceReconcile
188+
subscriberReconciler, err := controllers.NewSubscriberReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetEventRecorderFor("subscribers"), &cfg.Manager)
189+
if err != nil {
190+
setupLog.Error(err, "unable to create timeout controller", "controller", "Timeout")
191+
os.Exit(1)
192+
}
193+
194+
subscriberReconciler.OnReconcile = wsmanService.OnWorkspaceReconcile
195+
196+
if err = subscriberReconciler.SetupWithManager(mgrCtx, mgr); err != nil {
197+
setupLog.Error(err, "unable to setup workspace controller with manager", "controller", "Subscribers")
198+
os.Exit(1)
199+
}
189200

190201
if err = workspaceReconciler.SetupWithManager(mgr); err != nil {
191202
setupLog.Error(err, "unable to setup workspace controller with manager", "controller", "Workspace")

0 commit comments

Comments
 (0)