From c87a16d0149cf754836a84e238f181e32d6fa50a Mon Sep 17 00:00:00 2001 From: David Kwon Date: Fri, 20 Jun 2025 12:38:26 -0400 Subject: [PATCH 1/4] Update cleanup job node affinity logic Signed-off-by: David Kwon --- pkg/provision/storage/cleanup.go | 44 +++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/pkg/provision/storage/cleanup.go b/pkg/provision/storage/cleanup.go index fe6f17dbc..dec340537 100644 --- a/pkg/provision/storage/cleanup.go +++ b/pkg/provision/storage/cleanup.go @@ -30,7 +30,9 @@ import ( k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -120,7 +122,7 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste pvcName = workspace.Config.Workspace.PVCName } - targetNode, err := getCommonPVCTargetNode(workspace, clusterAPI) + targetNode, err := getTargetNodeName(workspace, clusterAPI) if err != nil { clusterAPI.Logger.Info("Error getting target node for PVC", "PVC", fmt.Sprintf("%s/%s", workspace.Namespace, workspace.Config.Workspace.PVCName), "error", err) } else if targetNode == "" { @@ -253,21 +255,39 @@ func commonPVCExists(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.C return true, nil } -func getCommonPVCTargetNode(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.ClusterAPI) (string, error) { - namespacedName := types.NamespacedName{ - Name: workspace.Config.Workspace.PVCName, - Namespace: workspace.Namespace, - } - pvc := &corev1.PersistentVolumeClaim{} - err := clusterAPI.Client.Get(clusterAPI.Ctx, namespacedName, pvc) +// If there is a running devworkspace pod that already mounts the PVC, this function +// gets the node name of the node the devworkspace pod is running in. +// Returns an empty string if no such pod exists. +func getTargetNodeName(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.ClusterAPI) (string, error) { + + labelSelector, err := labels.Parse(constants.DevWorkspaceIDLabel) if err != nil { return "", err } - targetNode := "" - if pvc.Annotations != nil { - targetNode = pvc.Annotations[constants.SelectedNodeAnnotation] + listOptions := &client.ListOptions{ + Namespace: workspace.Namespace, + LabelSelector: labelSelector, + } + + found := &corev1.PodList{} + err = clusterAPI.Client.List(clusterAPI.Ctx, found, listOptions) + if err != nil { + return "", err } - return targetNode, nil + return getNodeNameWithPVC(found, workspace.Config.Workspace.PVCName), nil +} + +func getNodeNameWithPVC(list *corev1.PodList, pvcName string) string { + for _, pod := range list.Items { + if pod.Status.Phase == corev1.PodRunning { + for _, volume := range pod.Spec.Volumes { + if volume.PersistentVolumeClaim != nil && volume.PersistentVolumeClaim.ClaimName == pvcName { + return pod.Spec.NodeName + } + } + } + } + return "" } From 1775c8f3c42f560549b83e6834c450890fbc2409 Mon Sep 17 00:00:00 2001 From: David Kwon Date: Tue, 24 Jun 2025 14:18:39 -0400 Subject: [PATCH 2/4] Update doc Signed-off-by: David Kwon --- pkg/provision/storage/cleanup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/provision/storage/cleanup.go b/pkg/provision/storage/cleanup.go index dec340537..d9bfee2ad 100644 --- a/pkg/provision/storage/cleanup.go +++ b/pkg/provision/storage/cleanup.go @@ -255,8 +255,8 @@ func commonPVCExists(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.C return true, nil } -// If there is a running devworkspace pod that already mounts the PVC, this function -// gets the node name of the node the devworkspace pod is running in. +// getTargetNodeName returns the node name of the node a running devworkspace pod that already mounts the +// common PVC is running in. // Returns an empty string if no such pod exists. func getTargetNodeName(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.ClusterAPI) (string, error) { From 74abc271d1e5594e5531fe84556141e502f40509 Mon Sep 17 00:00:00 2001 From: David Kwon Date: Tue, 24 Jun 2025 19:50:23 -0400 Subject: [PATCH 3/4] Update log message Signed-off-by: David Kwon --- pkg/provision/storage/cleanup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/provision/storage/cleanup.go b/pkg/provision/storage/cleanup.go index d9bfee2ad..4a041a80c 100644 --- a/pkg/provision/storage/cleanup.go +++ b/pkg/provision/storage/cleanup.go @@ -124,9 +124,9 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste targetNode, err := getTargetNodeName(workspace, clusterAPI) if err != nil { - clusterAPI.Logger.Info("Error getting target node for PVC", "PVC", fmt.Sprintf("%s/%s", workspace.Namespace, workspace.Config.Workspace.PVCName), "error", err) + clusterAPI.Logger.Error(err, "Error getting target node for cleanup job") } else if targetNode == "" { - clusterAPI.Logger.Info("PVC does not have a target node annotation", "PVC", fmt.Sprintf("%s/%s", workspace.Namespace, workspace.Config.Workspace.PVCName)) + clusterAPI.Logger.Info("No target node for cleanup job") } jobLabels := map[string]string{ From e6748d6eec45bb3fc661f1443b666744516a1dec Mon Sep 17 00:00:00 2001 From: David Kwon Date: Tue, 24 Jun 2025 22:40:45 -0400 Subject: [PATCH 4/4] Update log message Signed-off-by: David Kwon --- pkg/provision/storage/cleanup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/provision/storage/cleanup.go b/pkg/provision/storage/cleanup.go index 4a041a80c..0d170603b 100644 --- a/pkg/provision/storage/cleanup.go +++ b/pkg/provision/storage/cleanup.go @@ -126,7 +126,7 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste if err != nil { clusterAPI.Logger.Error(err, "Error getting target node for cleanup job") } else if targetNode == "" { - clusterAPI.Logger.Info("No target node for cleanup job") + clusterAPI.Logger.Info("No target node for cleanup job, NodeAffinity will not be defined") } jobLabels := map[string]string{