@@ -30,7 +30,9 @@ import (
30
30
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
31
31
"k8s.io/apimachinery/pkg/api/resource"
32
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
+ "k8s.io/apimachinery/pkg/labels"
33
34
"k8s.io/apimachinery/pkg/types"
35
+ "sigs.k8s.io/controller-runtime/pkg/client"
34
36
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
35
37
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
36
38
@@ -120,11 +122,11 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
120
122
pvcName = workspace .Config .Workspace .PVCName
121
123
}
122
124
123
- targetNode , err := getCommonPVCTargetNode (workspace , clusterAPI )
125
+ targetNode , err := getTargetNodeName (workspace , clusterAPI )
124
126
if err != nil {
125
- clusterAPI .Logger .Info ( "Error getting target node for PVC" , "PVC" , fmt . Sprintf ( "%s/%s" , workspace . Namespace , workspace . Config . Workspace . PVCName ), "error" , err )
127
+ clusterAPI .Logger .Error ( err , "Error getting target node for cleanup job" )
126
128
} else if targetNode == "" {
127
- clusterAPI .Logger .Info ("PVC does not have a target node annotation" , "PVC" , fmt . Sprintf ( "%s/%s" , workspace . Namespace , workspace . Config . Workspace . PVCName ) )
129
+ clusterAPI .Logger .Info ("No target node for cleanup job, NodeAffinity will not be defined" )
128
130
}
129
131
130
132
jobLabels := map [string ]string {
@@ -253,21 +255,39 @@ func commonPVCExists(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.C
253
255
return true , nil
254
256
}
255
257
256
- func getCommonPVCTargetNode (workspace * common.DevWorkspaceWithConfig , clusterAPI sync.ClusterAPI ) (string , error ) {
257
- namespacedName := types.NamespacedName {
258
- Name : workspace .Config .Workspace .PVCName ,
259
- Namespace : workspace .Namespace ,
260
- }
261
- pvc := & corev1.PersistentVolumeClaim {}
262
- err := clusterAPI .Client .Get (clusterAPI .Ctx , namespacedName , pvc )
258
+ // getTargetNodeName returns the node name of the node a running devworkspace pod that already mounts the
259
+ // common PVC is running in.
260
+ // Returns an empty string if no such pod exists.
261
+ func getTargetNodeName (workspace * common.DevWorkspaceWithConfig , clusterAPI sync.ClusterAPI ) (string , error ) {
262
+
263
+ labelSelector , err := labels .Parse (constants .DevWorkspaceIDLabel )
263
264
if err != nil {
264
265
return "" , err
265
266
}
266
267
267
- targetNode := ""
268
- if pvc .Annotations != nil {
269
- targetNode = pvc .Annotations [constants .SelectedNodeAnnotation ]
268
+ listOptions := & client.ListOptions {
269
+ Namespace : workspace .Namespace ,
270
+ LabelSelector : labelSelector ,
271
+ }
272
+
273
+ found := & corev1.PodList {}
274
+ err = clusterAPI .Client .List (clusterAPI .Ctx , found , listOptions )
275
+ if err != nil {
276
+ return "" , err
270
277
}
271
278
272
- return targetNode , nil
279
+ return getNodeNameWithPVC (found , workspace .Config .Workspace .PVCName ), nil
280
+ }
281
+
282
+ func getNodeNameWithPVC (list * corev1.PodList , pvcName string ) string {
283
+ for _ , pod := range list .Items {
284
+ if pod .Status .Phase == corev1 .PodRunning {
285
+ for _ , volume := range pod .Spec .Volumes {
286
+ if volume .PersistentVolumeClaim != nil && volume .PersistentVolumeClaim .ClaimName == pvcName {
287
+ return pod .Spec .NodeName
288
+ }
289
+ }
290
+ }
291
+ }
292
+ return ""
273
293
}
0 commit comments