Skip to content

fix: common PVC cleanup job to be assigned to a correct node #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ const (
// ProjectCloneDisable specifies that project cloning should be disabled.
ProjectCloneDisable = "disable"
)

const (
// SelectedNodeAnnotation 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.
SelectedNodeAnnotation = "volume.kubernetes.io/selected-node"
)
45 changes: 45 additions & 0 deletions pkg/provision/storage/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
pvcName = workspace.Config.Workspace.PVCName
}

targetNode, err := getCommonPVCTargetNode(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 == "" {
clusterAPI.Logger.Info("PVC does not have a target node annotation", "PVC", fmt.Sprintf("%s/%s", workspace.Namespace, workspace.Config.Workspace.PVCName))
}

jobLabels := map[string]string{
constants.DevWorkspaceIDLabel: workspaceId,
constants.DevWorkspaceNameLabel: workspace.Name,
Expand Down Expand Up @@ -189,11 +196,30 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
},
},
},
Affinity: &corev1.Affinity{},
},
},
},
}

if targetNode != "" {
job.Spec.Template.Spec.Affinity.NodeAffinity = &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: "kubernetes.io/hostname",
Operator: corev1.NodeSelectorOpIn,
Values: []string{targetNode},
},
},
},
},
},
}
}

podTolerations, nodeSelector, err := nsconfig.GetNamespacePodTolerationsAndNodeSelector(workspace.Namespace, clusterAPI)
if err != nil {
return nil, err
Expand Down Expand Up @@ -226,3 +252,22 @@ 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 err != nil {
return "", err
}

targetNode := ""
if pvc.Annotations != nil {
targetNode = pvc.Annotations[constants.SelectedNodeAnnotation]
}

return targetNode, nil
}
Loading