@@ -124,21 +124,21 @@ var runCmd = &cobra.Command{
124124 return
125125 }
126126
127- wc , err := NewWorkspaceCountController (mgr .GetClient ())
127+ nsac , err := NewNodeScaledownAnnotationController (mgr .GetClient ())
128128 if err != nil {
129- log .WithError (err ).Fatal ("unable to create workspace count controller" )
129+ log .WithError (err ).Fatal ("unable to create node scaledown annotation controller" )
130130 }
131- err = wc .SetupWithManager (mgr )
131+ err = nsac .SetupWithManager (mgr )
132132 if err != nil {
133- log .WithError (err ).Fatal ("unable to bind workspace count controller" )
133+ log .WithError (err ).Fatal ("unable to bind node scaledown annotation controller" )
134134 }
135135
136136 err = mgr .Add (manager .RunnableFunc (func (context.Context ) error {
137- wc .Stop ()
137+ nsac .Stop ()
138138 return nil
139139 }))
140140 if err != nil {
141- log .WithError (err ).Fatal ("couldn't properly clean up WorkspaceCountController " )
141+ log .WithError (err ).Fatal ("couldn't properly clean up node scaledown annotation controller " )
142142 }
143143
144144 metrics .Registry .MustRegister (NodeLabelerCounterVec )
@@ -282,32 +282,32 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
282282 return reconcile.Result {}, nil
283283}
284284
285- type WorkspaceCountController struct {
285+ type NodeScaledownAnnotationController struct {
286286 client.Client
287287 nodesToReconcile chan string
288288 stopChan chan struct {}
289289}
290290
291- func NewWorkspaceCountController (client client.Client ) (* WorkspaceCountController , error ) {
292- return & WorkspaceCountController {
291+ func NewNodeScaledownAnnotationController (client client.Client ) (* NodeScaledownAnnotationController , error ) {
292+ return & NodeScaledownAnnotationController {
293293 Client : client ,
294294 nodesToReconcile : make (chan string , 1000 ),
295295 stopChan : make (chan struct {}),
296296 }, nil
297297}
298298
299- func (wc * WorkspaceCountController ) SetupWithManager (mgr ctrl.Manager ) error {
299+ func (c * NodeScaledownAnnotationController ) SetupWithManager (mgr ctrl.Manager ) error {
300300 // Start the periodic reconciliation goroutine
301- go wc .periodicReconciliation ()
301+ go c .periodicReconciliation ()
302302
303303 return ctrl .NewControllerManagedBy (mgr ).
304- Named ("workspace-count " ).
304+ Named ("node-scaledown-annotation-controller " ).
305305 For (& workspacev1.Workspace {}).
306- WithEventFilter (wc .workspaceFilter ()).
307- Complete (wc )
306+ WithEventFilter (c .workspaceFilter ()).
307+ Complete (c )
308308}
309309
310- func (wc * WorkspaceCountController ) periodicReconciliation () {
310+ func (c * NodeScaledownAnnotationController ) periodicReconciliation () {
311311 ticker := time .NewTicker (5 * time .Minute )
312312 defer ticker .Stop ()
313313
@@ -316,16 +316,16 @@ func (wc *WorkspaceCountController) periodicReconciliation() {
316316 case <- ticker .C :
317317 log .Info ("starting periodic full reconciliation" )
318318 ctx := context .Background ()
319- if _ , err := wc .reconcileAllNodes (ctx ); err != nil {
319+ if _ , err := c .reconcileAllNodes (ctx ); err != nil {
320320 log .WithError (err ).Error ("periodic reconciliation failed" )
321321 }
322- case <- wc .stopChan :
322+ case <- c .stopChan :
323323 return
324324 }
325325 }
326326}
327327
328- func (wc * WorkspaceCountController ) workspaceFilter () predicate.Predicate {
328+ func (c * NodeScaledownAnnotationController ) workspaceFilter () predicate.Predicate {
329329 return predicate.Funcs {
330330 CreateFunc : func (e event.CreateEvent ) bool {
331331 ws := e .Object .(* workspacev1.Workspace )
@@ -351,7 +351,7 @@ func (wc *WorkspaceCountController) workspaceFilter() predicate.Predicate {
351351 if ws .Status .Runtime != nil && ws .Status .Runtime .NodeName != "" {
352352 // Queue the node for reconciliation
353353 select {
354- case wc .nodesToReconcile <- ws .Status .Runtime .NodeName :
354+ case c .nodesToReconcile <- ws .Status .Runtime .NodeName :
355355 log .WithField ("node" , ws .Status .Runtime .NodeName ).Info ("queued node for reconciliation from delete" )
356356 default :
357357 log .WithField ("node" , ws .Status .Runtime .NodeName ).Warn ("reconciliation queue full" )
@@ -363,21 +363,21 @@ func (wc *WorkspaceCountController) workspaceFilter() predicate.Predicate {
363363 }
364364}
365365
366- func (wc * WorkspaceCountController ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
366+ func (c * NodeScaledownAnnotationController ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
367367 log .WithField ("request" , req .NamespacedName .String ()).Info ("WorkspaceCountController reconciling" )
368368
369369 // Process any queued nodes first, logging errors (not returning)
370370 select {
371- case nodeName := <- wc .nodesToReconcile :
372- if err := wc .reconcileNode (ctx , nodeName ); err != nil {
371+ case nodeName := <- c .nodesToReconcile :
372+ if err := c .reconcileNode (ctx , nodeName ); err != nil {
373373 log .WithError (err ).WithField ("node" , nodeName ).Error ("failed to reconcile node from queue" )
374374 }
375375 default :
376376 // No nodes in queue, continue with regular reconciliation
377377 }
378378
379379 var ws workspacev1.Workspace
380- if err := wc .Get (ctx , req .NamespacedName , & ws ); err != nil {
380+ if err := c .Get (ctx , req .NamespacedName , & ws ); err != nil {
381381 if ! errors .IsNotFound (err ) {
382382 log .WithError (err ).WithField ("workspace" , req .NamespacedName ).Error ("unable to fetch Workspace" )
383383 return ctrl.Result {}, err
@@ -386,7 +386,7 @@ func (wc *WorkspaceCountController) Reconcile(ctx context.Context, req ctrl.Requ
386386 }
387387
388388 if ws .Status .Runtime != nil && ws .Status .Runtime .NodeName != "" {
389- if err := wc .reconcileNode (ctx , ws .Status .Runtime .NodeName ); err != nil {
389+ if err := c .reconcileNode (ctx , ws .Status .Runtime .NodeName ); err != nil {
390390 log .WithError (err ).WithField ("node" , ws .Status .Runtime .NodeName ).Error ("failed to reconcile node" )
391391 return ctrl.Result {}, err
392392 }
@@ -396,12 +396,12 @@ func (wc *WorkspaceCountController) Reconcile(ctx context.Context, req ctrl.Requ
396396}
397397
398398// Cleanup method to be called when shutting down the controller
399- func (wc * WorkspaceCountController ) Stop () {
399+ func (wc * NodeScaledownAnnotationController ) Stop () {
400400 close (wc .stopChan )
401401}
402402
403403// reconcileAllNodes lists all nodes and reconciles each one
404- func (wc * WorkspaceCountController ) reconcileAllNodes (ctx context.Context ) (ctrl.Result , error ) {
404+ func (wc * NodeScaledownAnnotationController ) reconcileAllNodes (ctx context.Context ) (ctrl.Result , error ) {
405405 var nodes corev1.NodeList
406406 if err := wc .List (ctx , & nodes ); err != nil {
407407 log .WithError (err ).Error ("failed to list nodes" )
@@ -419,26 +419,26 @@ func (wc *WorkspaceCountController) reconcileAllNodes(ctx context.Context) (ctrl
419419}
420420
421421// reconcileNode counts the workspaces running on a node and updates the autoscaler annotation accordingly
422- func (wc * WorkspaceCountController ) reconcileNode (ctx context.Context , nodeName string ) error {
422+ func (c * NodeScaledownAnnotationController ) reconcileNode (ctx context.Context , nodeName string ) error {
423423 var workspaceList workspacev1.WorkspaceList
424- if err := wc .List (ctx , & workspaceList , client.MatchingFields {
424+ if err := c .List (ctx , & workspaceList , client.MatchingFields {
425425 "status.runtime.nodeName" : nodeName ,
426426 }); err != nil {
427427 return fmt .Errorf ("failed to list workspaces: %w" , err )
428428 }
429429 log .WithField ("node" , nodeName ).WithField ("count" , len (workspaceList .Items )).Info ("acting on workspaces" )
430430 count := len (workspaceList .Items )
431431
432- return wc .updateNodeAnnotation (ctx , nodeName , count )
432+ return c .updateNodeAnnotation (ctx , nodeName , count )
433433}
434434
435- func (wc * WorkspaceCountController ) updateNodeAnnotation (ctx context.Context , nodeName string , count int ) error {
435+ func (c * NodeScaledownAnnotationController ) updateNodeAnnotation (ctx context.Context , nodeName string , count int ) error {
436436 return retry .RetryOnConflict (retry .DefaultBackoff , func () error {
437437 ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
438438 defer cancel ()
439439
440440 var node corev1.Node
441- err := wc .Get (ctx , types.NamespacedName {Name : nodeName }, & node )
441+ err := c .Get (ctx , types.NamespacedName {Name : nodeName }, & node )
442442 if err != nil {
443443 return fmt .Errorf ("obtaining node %s: %w" , nodeName , err )
444444 }
@@ -455,7 +455,7 @@ func (wc *WorkspaceCountController) updateNodeAnnotation(ctx context.Context, no
455455 log .WithField ("nodeName" , nodeName ).Info ("enabling scale-down for node" )
456456 }
457457
458- return wc .Update (ctx , & node )
458+ return c .Update (ctx , & node )
459459 })
460460}
461461
0 commit comments