Skip to content

Commit f4aa99b

Browse files
SalmaElsoly0oM4R
andauthored
Add duplicate check for node ID assignment in cluster (#603)
* feat: add duplicate check for node ID assignment in cluster * fix: prevent assigning VMs to nodes already assigned within a cluster --------- Co-authored-by: kassem <omarksm09@gmail.com>
1 parent 0fb9120 commit f4aa99b

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

backend/app/deployment_handler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ func (h *Handler) HandleAddNode(c *gin.Context) {
503503
c.JSON(http.StatusConflict, gin.H{"error": "Node with the same name already exists"})
504504
return
505505
}
506+
507+
if node.NodeID == cluster.Nodes[0].NodeID {
508+
c.JSON(http.StatusConflict, gin.H{"error": fmt.Sprintf("node id %d is already assigned to this cluster", node.NodeID)})
509+
return
510+
}
506511
}
507512

508513
wf, err := h.ewfEngine.NewWorkflow(constants.WorkflowAddNode)

backend/kubedeployer/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,17 @@ func (c *Cluster) UnmarshalJSON(data []byte) error {
313313

314314
func (c *Cluster) Validate() error {
315315
nodeNames := make(map[string]struct{})
316+
nodeIDs := make(map[uint32]struct{})
316317
for _, node := range c.Nodes {
317318
if _, exists := nodeNames[node.Name]; exists {
318319
return fmt.Errorf("duplicate node name found: %s", node.Name)
319320
}
320321
nodeNames[node.Name] = struct{}{}
322+
323+
if _, exists := nodeIDs[node.NodeID]; exists {
324+
return fmt.Errorf("duplicate node id found: %d", node.NodeID)
325+
}
326+
nodeIDs[node.NodeID] = struct{}{}
321327
}
322328

323329
return nil

frontend/kubecloud/src/components/dashboard/EditClusterNodesDialog.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ const availableNodes = computed<RawNode[]>(() => {
187187
return nodes.value.filter((node: RawNode) => {
188188
const availRAM = getAvailableRAM(node)
189189
const availStorage = getAvailableStorage(node)
190-
return availRAM > 0 && availStorage > 0
190+
const nodeIsNotInCluster = !clusterNodes.value.some((clusterNode: any) => clusterNode.node_id === node.nodeId)
191+
return availRAM > 0 && availStorage > 0 && nodeIsNotInCluster
191192
})
192193
})
193194

frontend/kubecloud/src/components/deploy/Step2AssignNodes.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ const getAvailableNodesForVM = (vmIndex: number) => {
181181
const requiredStorage = (vm.disk || 0) + vm.rootfs;
182182
183183
return props.availableNodes.filter(node => {
184+
const nodeIsNotInCluster = !props.allVMs.some((vm: any) => vm.node === node.nodeId);
185+
if(!nodeIsNotInCluster && vm.node !== node.nodeId){
186+
return false;
187+
}
184188
const vmResources = vm.node === node.nodeId ? { ram: vm.ram, storage: requiredStorage } : undefined;
185189
const available = getNodeResources(node, vmResources);
186190
return available.cpu >= vm.vcpu && available.ram >= vm.ram && available.storage >= requiredStorage;

0 commit comments

Comments
 (0)