Skip to content

Commit 01dda08

Browse files
authored
Update cleanup job node affinity logic (#1455)
Signed-off-by: David Kwon <[email protected]>
1 parent 89c6257 commit 01dda08

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

pkg/provision/storage/cleanup.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import (
3030
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
3131
"k8s.io/apimachinery/pkg/api/resource"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/apimachinery/pkg/labels"
3334
"k8s.io/apimachinery/pkg/types"
35+
"sigs.k8s.io/controller-runtime/pkg/client"
3436
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
3537
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3638

@@ -120,11 +122,11 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
120122
pvcName = workspace.Config.Workspace.PVCName
121123
}
122124

123-
targetNode, err := getCommonPVCTargetNode(workspace, clusterAPI)
125+
targetNode, err := getTargetNodeName(workspace, clusterAPI)
124126
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")
126128
} 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")
128130
}
129131

130132
jobLabels := map[string]string{
@@ -253,21 +255,39 @@ func commonPVCExists(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.C
253255
return true, nil
254256
}
255257

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)
263264
if err != nil {
264265
return "", err
265266
}
266267

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
270277
}
271278

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 ""
273293
}

0 commit comments

Comments
 (0)