Skip to content

Commit

Permalink
added error handling in port allocation helper
Browse files Browse the repository at this point in the history
Signed-off-by: kon3m <[email protected]>
  • Loading branch information
kon3m committed Mar 19, 2024
1 parent 363f2a5 commit 21449f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 5 additions & 2 deletions controllers/slicegateway/slicegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,13 @@ func (r *SliceGwReconciler) ReconcileGatewayDeployments(ctx context.Context, sli
// It is valid only if the list of remoteNodePorts in the slicegw object contains the portInUse.
if !checkIfNodePortIsValid(sliceGw.Status.Config.SliceGatewayRemoteNodePorts, nodePortInUse) {
// Get a valid port number for this deployment
portNumToUpdate := allocateNodePortToClient(sliceGw.Status.Config.SliceGatewayRemoteNodePorts, &gwClientToRemotePortMap)
portNumToUpdate, err := allocateNodePortToClient(sliceGw.Status.Config.SliceGatewayRemoteNodePorts, deployment.Name, &gwClientToRemotePortMap)
if err != nil {
return ctrl.Result{}, err, true
}
// Update the port map
gwClientToRemotePortMap.Store(deployment.Name, portNumToUpdate)
err := r.updateGatewayDeploymentNodePort(ctx, r.Client, sliceGw, &deployment, portNumToUpdate)
err = r.updateGatewayDeploymentNodePort(ctx, r.Client, sliceGw, &deployment, portNumToUpdate)
if err != nil {
return ctrl.Result{}, err, true
}
Expand Down
11 changes: 8 additions & 3 deletions controllers/slicegateway/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,20 @@ func containsWithIndex[T comparable](s []T, e T) (bool, int) {
}

// a helper to assign distinct port to each client deployment
func allocateNodePortToClient(correctNodePorts []int, nodePortsMap *sync.Map) int {
func allocateNodePortToClient(correctNodePorts []int, depName string, nodePortsMap *sync.Map) (int, error) {
nodePortsMap.Range(func(k, v interface{}) bool {
if ok, index := containsWithIndex(correctNodePorts, v.(int)); ok {
correctNodePorts = append(correctNodePorts[:index], correctNodePorts[index+1:]...)
}
return true
})
if len(correctNodePorts) > 0 {
return correctNodePorts[0]
return correctNodePorts[0], nil
} else {
port, ok := nodePortsMap.Load(depName)
if ok {
return port.(int), nil
}
return 0, errors.New("could not allocate a port")
}
return 0
}

0 comments on commit 21449f3

Please sign in to comment.