Skip to content

Commit

Permalink
cluster & node: expose keep_existing option on port allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti committed Oct 10, 2024
1 parent b0144a4 commit 6b86f2c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ allocated_range = agent.tasks.run(
data={
'ports': 1,
'module_id': dmid + '_rsync',
'protocol': 'tcp'
'protocol': 'tcp',
'keep_existing': False
},
endpoint="redis://cluster-leader",
progress_callback=agent.get_progress_callback(26,40),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ allocated_range = agent.tasks.run(
data={
'ports': 1,
'module_id': dmid + '_rsync',
'protocol': 'tcp'
'protocol': 'tcp',
'keep_existing': False
},
endpoint="redis://cluster-leader",
progress_callback=agent.get_progress_callback(26,40),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ if module_env != "" and module_env != f"module/{request['module_id']}":
print(agent.SD_ERR + f" Agent {module_env} does not have permission to change the port allocation for {request['module_id']}.", file=sys.stderr)
sys.exit(1)

range = node.ports_manager.allocate_ports(int(request['ports']), request['module_id'], request['protocol'])
range = node.ports_manager.allocate_ports(int(request['ports']), request['module_id'], request['protocol'], request['keep_existing'])

json.dump(range, fp=sys.stdout)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"required": [
"ports",
"module_id",
"protocol"
"protocol",
"keep_existing"
],
"properties": {
"ports": {
Expand All @@ -24,6 +25,11 @@
"type": "string",
"title": "Ports protocol",
"enum": ["tcp", "udp"]
},
"keep_existing": {
"type": "boolean",
"title": "Keep existing ports",
"description": "If false, remove remove existing ports before allocating a new range"
}
}
}
17 changes: 17 additions & 0 deletions docs/modules/port_allocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,42 @@ Imagine an application module that initially requires only one TCP port. Later,
If ports are already allocated for this module, the previous allocation will be deallocated, and the new requested range of ports will be allocated. Here’s how this can be done:

```python
import agent

# Allocate 4 TCP ports for the module that is calling the function
allocated_ports = agent.allocate_ports(4, "tcp")
```
or
```python
import agent

# Allocate 4 UDP ports for "my_module" module
allocated_ports = agent.allocate_ports(4, "udp", "my_module")
```

If you want to preserve the previously allocated ports and add a new range of ports, you can use the `keep_existing` parameter:

```python
import agent

# Allocate 4 more TCP ports for the module that is calling the function
allocated_ports = agent.allocate_ports(4, "tcp", keep_existing=True)
```

### Deallocate ports

If the module no longer needs the allocated ports, such as when a feature is removed or disabled, the ports can be easily deallocated:

```python
import agent

# Deallocate TCP ports for the module that is calling the function
deallocated_ports = agent.deallocate_ports("tcp")
```
or
```python
import agent

# Deallocate UDP ports for the "my_module" module
deallocated_ports = agent.deallocate_ports("udp", "my_module")
```
Expand Down

0 comments on commit 6b86f2c

Please sign in to comment.