@@ -30,9 +30,7 @@ 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"
34
33
"k8s.io/apimachinery/pkg/types"
35
- "sigs.k8s.io/controller-runtime/pkg/client"
36
34
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
37
35
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
38
36
@@ -122,11 +120,11 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
122
120
pvcName = workspace .Config .Workspace .PVCName
123
121
}
124
122
125
- targetNode , err := getTargetNodeName (workspace , clusterAPI )
123
+ targetNode , err := getCommonPVCTargetNode (workspace , clusterAPI )
126
124
if err != nil {
127
- clusterAPI .Logger .Error ( err , "Error getting target node for cleanup job" )
125
+ clusterAPI .Logger .Info ( "Error getting target node for PVC" , "PVC" , fmt . Sprintf ( "%s/%s" , workspace . Namespace , workspace . Config . Workspace . PVCName ), "error" , err )
128
126
} else if targetNode == "" {
129
- clusterAPI .Logger .Info ("No target node for cleanup job, NodeAffinity will not be defined" )
127
+ clusterAPI .Logger .Info ("PVC does not have a target node annotation" , "PVC" , fmt . Sprintf ( "%s/%s" , workspace . Namespace , workspace . Config . Workspace . PVCName ) )
130
128
}
131
129
132
130
jobLabels := map [string ]string {
@@ -255,39 +253,21 @@ func commonPVCExists(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.C
255
253
return true , nil
256
254
}
257
255
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 )
264
- if err != nil {
265
- return "" , err
266
- }
267
-
268
- listOptions := & client.ListOptions {
269
- Namespace : workspace .Namespace ,
270
- LabelSelector : labelSelector ,
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 ,
271
260
}
272
-
273
- found := & corev1.PodList {}
274
- err = clusterAPI .Client .List (clusterAPI .Ctx , found , listOptions )
261
+ pvc := & corev1.PersistentVolumeClaim {}
262
+ err := clusterAPI .Client .Get (clusterAPI .Ctx , namespacedName , pvc )
275
263
if err != nil {
276
264
return "" , err
277
265
}
278
266
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
- }
267
+ targetNode := ""
268
+ if pvc .Annotations != nil {
269
+ targetNode = pvc .Annotations [constants .SelectedNodeAnnotation ]
291
270
}
292
- return ""
271
+
272
+ return targetNode , nil
293
273
}
0 commit comments