@@ -8,11 +8,11 @@ import (
8
8
"context"
9
9
"fmt"
10
10
"strings"
11
+ "sync"
11
12
"time"
12
13
13
14
corev1 "k8s.io/api/core/v1"
14
15
"k8s.io/apimachinery/pkg/api/equality"
15
- "k8s.io/apimachinery/pkg/api/errors"
16
16
apierrors "k8s.io/apimachinery/pkg/api/errors"
17
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
18
"k8s.io/apimachinery/pkg/runtime"
@@ -95,7 +95,7 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
95
95
96
96
var workspace workspacev1.Workspace
97
97
if err := r .Get (ctx , req .NamespacedName , & workspace ); err != nil {
98
- if ! errors .IsNotFound (err ) {
98
+ if ! apierrors .IsNotFound (err ) {
99
99
log .Error (err , "unable to fetch workspace" )
100
100
}
101
101
// we'll ignore not-found errors, since they can't be fixed by an immediate
@@ -184,7 +184,7 @@ func (r *WorkspaceReconciler) actOnStatus(ctx context.Context, workspace *worksp
184
184
}
185
185
186
186
err = r .Create (ctx , pod )
187
- if errors .IsAlreadyExists (err ) {
187
+ if apierrors .IsAlreadyExists (err ) {
188
188
// pod exists, we're good
189
189
} else if err != nil {
190
190
log .Error (err , "unable to create Pod for Workspace" , "pod" , pod )
@@ -257,7 +257,7 @@ func (r *WorkspaceReconciler) actOnStatus(ctx context.Context, workspace *worksp
257
257
err := r .Client .Delete (ctx , pod , & client.DeleteOptions {
258
258
GracePeriodSeconds : gracePeriodSeconds ,
259
259
})
260
- if errors .IsNotFound (err ) {
260
+ if apierrors .IsNotFound (err ) {
261
261
// pod is gone - nothing to do here
262
262
} else {
263
263
return ctrl.Result {Requeue : true }, err
@@ -282,7 +282,7 @@ func (r *WorkspaceReconciler) actOnStatus(ctx context.Context, workspace *worksp
282
282
// Workspace was requested to be deleted, propagate by deleting the Pod.
283
283
// The Pod deletion will then trigger workspace disposal steps.
284
284
err := r .Client .Delete (ctx , pod )
285
- if errors .IsNotFound (err ) {
285
+ if apierrors .IsNotFound (err ) {
286
286
// pod is gone - nothing to do here
287
287
} else {
288
288
return ctrl.Result {Requeue : true }, err
@@ -396,7 +396,7 @@ func (r *WorkspaceReconciler) deleteWorkspacePod(ctx context.Context, pod *corev
396
396
// Workspace was requested to be deleted, propagate by deleting the Pod.
397
397
// The Pod deletion will then trigger workspace disposal steps.
398
398
err := r .Client .Delete (ctx , pod )
399
- if errors .IsNotFound (err ) {
399
+ if apierrors .IsNotFound (err ) {
400
400
// pod is gone - nothing to do here
401
401
} else {
402
402
return ctrl.Result {Requeue : true }, err
@@ -441,7 +441,7 @@ func (r *WorkspaceReconciler) deleteSecret(ctx context.Context, name, namespace
441
441
}, func (ctx context.Context ) (bool , error ) {
442
442
var secret corev1.Secret
443
443
err := r .Client .Get (ctx , types.NamespacedName {Name : name , Namespace : namespace }, & secret )
444
- if errors .IsNotFound (err ) {
444
+ if apierrors .IsNotFound (err ) {
445
445
// nothing to delete
446
446
return true , nil
447
447
}
@@ -452,7 +452,7 @@ func (r *WorkspaceReconciler) deleteSecret(ctx context.Context, name, namespace
452
452
}
453
453
454
454
err = r .Client .Delete (ctx , & secret )
455
- if err != nil && ! errors .IsNotFound (err ) {
455
+ if err != nil && ! apierrors .IsNotFound (err ) {
456
456
log .Error (err , "cannot delete secret" , "secret" , name )
457
457
return false , nil
458
458
}
@@ -468,7 +468,7 @@ func (r *WorkspaceReconciler) deleteSecret(ctx context.Context, name, namespace
468
468
// For conflicts, instead a result with `Requeue: true` is returned, which has the same requeuing
469
469
// behaviour as returning an error.
470
470
func errorResultLogConflict (log logr.Logger , err error ) (ctrl.Result , error ) {
471
- if errors .IsConflict (err ) {
471
+ if apierrors .IsConflict (err ) {
472
472
return ctrl.Result {RequeueAfter : 100 * time .Millisecond }, nil
473
473
} else {
474
474
return ctrl.Result {}, err
@@ -482,26 +482,6 @@ var (
482
482
483
483
// SetupWithManager sets up the controller with the Manager.
484
484
func (r * WorkspaceReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
485
- idx := func (rawObj client.Object ) []string {
486
- // grab the job object, extract the owner...
487
- job := rawObj .(* corev1.Pod )
488
- owner := metav1 .GetControllerOf (job )
489
- if owner == nil {
490
- return nil
491
- }
492
- // ...make sure it's a workspace...
493
- if owner .APIVersion != apiGVStr || owner .Kind != "Workspace" {
494
- return nil
495
- }
496
-
497
- // ...and if so, return it
498
- return []string {owner .Name }
499
- }
500
- err := mgr .GetFieldIndexer ().IndexField (context .Background (), & corev1.Pod {}, wsOwnerKey , idx )
501
- if err != nil {
502
- return err
503
- }
504
-
505
485
return ctrl .NewControllerManagedBy (mgr ).
506
486
Named ("workspace" ).
507
487
WithOptions (controller.Options {
@@ -538,3 +518,29 @@ func (r *WorkspaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
538
518
}).
539
519
Complete (r )
540
520
}
521
+
522
+ func SetupIndexer (mgr ctrl.Manager ) error {
523
+ var err error
524
+ var once sync.Once
525
+ once .Do (func () {
526
+ idx := func (rawObj client.Object ) []string {
527
+ // grab the job object, extract the owner...
528
+ job := rawObj .(* corev1.Pod )
529
+ owner := metav1 .GetControllerOf (job )
530
+ if owner == nil {
531
+ return nil
532
+ }
533
+ // ...make sure it's a workspace...
534
+ if owner .APIVersion != apiGVStr || owner .Kind != "Workspace" {
535
+ return nil
536
+ }
537
+
538
+ // ...and if so, return it
539
+ return []string {owner .Name }
540
+ }
541
+
542
+ err = mgr .GetFieldIndexer ().IndexField (context .Background (), & corev1.Pod {}, wsOwnerKey , idx )
543
+ })
544
+
545
+ return err
546
+ }
0 commit comments