@@ -16,6 +16,7 @@ import (
16
16
"context"
17
17
"fmt"
18
18
"path"
19
+ "time"
19
20
20
21
"github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
21
22
"github.com/devfile/devworkspace-operator/controllers/workspace/provision"
@@ -68,10 +69,33 @@ func (r *DevWorkspaceReconciler) finalize(ctx context.Context, log logr.Logger,
68
69
if wait {
69
70
return reconcile.Result {Requeue : true }, nil
70
71
}
72
+
73
+ terminating , err := r .namespaceIsTerminating (ctx , workspace .Namespace )
74
+ if err != nil {
75
+ return reconcile.Result {}, err
76
+ } else if terminating {
77
+ //Namespace is terminating, it's redundant to clean PVC files since it's going to be removed
78
+ log .Info ("Namespace is terminating; clearing storage finalizer" )
79
+ clearFinalizer (workspace )
80
+ return reconcile.Result {}, r .Update (ctx , workspace )
81
+ }
82
+
83
+ pvcExists , err := r .pvcExists (ctx , workspace )
84
+ if err != nil {
85
+ return reconcile.Result {}, err
86
+ } else if ! pvcExists {
87
+ //PVC does not exist. nothing to clean up
88
+ log .Info ("PVC does not exit; clearing storage finalizer" )
89
+ clearFinalizer (workspace )
90
+ // job will be clean up by k8s garbage collector
91
+ return reconcile.Result {}, r .Update (ctx , workspace )
92
+ }
93
+
71
94
specJob , err := r .getSpecCleanupJob (workspace )
72
95
if err != nil {
73
96
return reconcile.Result {}, err
74
97
}
98
+
75
99
clusterJob , err := r .getClusterCleanupJob (ctx , workspace )
76
100
if err != nil {
77
101
return reconcile.Result {}, err
@@ -111,7 +135,8 @@ func (r *DevWorkspaceReconciler) finalize(ctx context.Context, log logr.Logger,
111
135
return r .updateWorkspaceStatus (workspace , r .Log , failedStatus , reconcile.Result {}, nil )
112
136
}
113
137
}
114
- return reconcile.Result {}, nil
138
+ // Requeue at least each 10 seconds to check if PVC is not removed by someone else
139
+ return reconcile.Result {RequeueAfter : 10 * time .Second }, nil
115
140
}
116
141
117
142
func (r * DevWorkspaceReconciler ) getSpecCleanupJob (workspace * v1alpha2.DevWorkspace ) (* batchv1.Job , error ) {
@@ -222,3 +247,33 @@ func clearFinalizer(workspace *v1alpha2.DevWorkspace) {
222
247
}
223
248
workspace .SetFinalizers (newFinalizers )
224
249
}
250
+
251
+ func (r * DevWorkspaceReconciler ) namespaceIsTerminating (ctx context.Context , namespace string ) (bool , error ) {
252
+ namespacedName := types.NamespacedName {
253
+ Name : namespace ,
254
+ }
255
+ n := & corev1.Namespace {}
256
+
257
+ err := r .Get (ctx , namespacedName , n )
258
+ if err != nil {
259
+ return false , err
260
+ }
261
+
262
+ return n .Status .Phase == corev1 .NamespaceTerminating , nil
263
+ }
264
+
265
+ func (r * DevWorkspaceReconciler ) pvcExists (ctx context.Context , workspace * v1alpha2.DevWorkspace ) (bool , error ) {
266
+ namespacedName := types.NamespacedName {
267
+ Name : config .ControllerCfg .GetWorkspacePVCName (),
268
+ Namespace : workspace .Namespace ,
269
+ }
270
+ err := r .Get (ctx , namespacedName , & corev1.PersistentVolumeClaim {})
271
+ if err != nil {
272
+ if k8sErrors .IsNotFound (err ) {
273
+ return false , nil
274
+ }
275
+
276
+ return false , err
277
+ }
278
+ return true , nil
279
+ }
0 commit comments