Skip to content

Commit 3e93314

Browse files
committed
fix: common PVC cleanup job to be assigned to a correct node
Signed-off-by: Oleksii Kurinnyi <[email protected]>
1 parent 799c0c4 commit 3e93314

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pkg/constants/constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ const (
9696
// ProjectCloneDisable specifies that project cloning should be disabled.
9797
ProjectCloneDisable = "disable"
9898
)
99+
100+
const (
101+
// This annotation is added to a PVC that is triggered by a scheduler to be dynamically provisioned. Its value is the name of the selected node.
102+
SelectedNodeAnnotation = "volume.kubernetes.io/selected-node"
103+
)

pkg/provision/storage/cleanup.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
120120
pvcName = workspace.Config.Workspace.PVCName
121121
}
122122

123+
targetNode, err := getCommonPVCTargetNode(workspace, clusterAPI)
124+
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)
126+
} 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))
128+
}
129+
123130
jobLabels := map[string]string{
124131
constants.DevWorkspaceIDLabel: workspaceId,
125132
constants.DevWorkspaceNameLabel: workspace.Name,
@@ -189,11 +196,30 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
189196
},
190197
},
191198
},
199+
Affinity: &corev1.Affinity{},
192200
},
193201
},
194202
},
195203
}
196204

205+
if targetNode != "" {
206+
job.Spec.Template.Spec.Affinity.NodeAffinity = &corev1.NodeAffinity{
207+
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
208+
NodeSelectorTerms: []corev1.NodeSelectorTerm{
209+
{
210+
MatchExpressions: []corev1.NodeSelectorRequirement{
211+
{
212+
Key: "kubernetes.io/hostname",
213+
Operator: corev1.NodeSelectorOpIn,
214+
Values: []string{targetNode},
215+
},
216+
},
217+
},
218+
},
219+
},
220+
}
221+
}
222+
197223
podTolerations, nodeSelector, err := nsconfig.GetNamespacePodTolerationsAndNodeSelector(workspace.Namespace, clusterAPI)
198224
if err != nil {
199225
return nil, err
@@ -226,3 +252,22 @@ func commonPVCExists(workspace *common.DevWorkspaceWithConfig, clusterAPI sync.C
226252
}
227253
return true, nil
228254
}
255+
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)
263+
if err != nil {
264+
return "", err
265+
}
266+
267+
targetNode := ""
268+
if pvc.Annotations != nil {
269+
targetNode = pvc.Annotations[constants.SelectedNodeAnnotation]
270+
}
271+
272+
return targetNode, nil
273+
}

0 commit comments

Comments
 (0)