Skip to content

Commit

Permalink
Checking for free public IPs before deploying the instances (#1185)
Browse files Browse the repository at this point in the history
* Declared a new field that matches the farmId in the response of the grid proxy, created a new method 'checkFarmIps' inside the 'TwinDeploymentHandler' to check if the deployment farm has the requested ips

* Update the type of the farmIps, and changed the error message.

* Enhance the way that we load the farm IPs by it

* Removed unused prop.

* Enhanced the code by adding a continue statements
  • Loading branch information
Mahmoud-Emad authored Oct 9, 2023
1 parent c894f27 commit 03836df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
39 changes: 39 additions & 0 deletions packages/grid_client/src/high_level/twinDeploymentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,43 @@ class TwinDeploymentHandler {
return deployments;
}

async checkFarmIps(twinDeployments: TwinDeployment[]) {
const farmIPs: Map<number, number> = new Map();

for (const twinDeployment of twinDeployments) {
if (twinDeployment.operation !== Operations.deploy) {
continue;
}

if (twinDeployment.publicIps === 0) {
continue;
}

const node = await this.nodes.getNode(twinDeployment.nodeId);
if (!node) {
continue;
}
if (!farmIPs.has(node.farmId)) {
farmIPs.set(node.farmId, twinDeployment.publicIps);
} else {
farmIPs.set(node.farmId, farmIPs.get(node.farmId)! + twinDeployment.publicIps);
}
}

for (const farmId of farmIPs.keys()) {
const _farm = await this.tfclient.farms.get({ id: farmId });
const freeIps = _farm.publicIps.filter(res => res.contractId === 0).length;

if (freeIps < farmIPs.get(farmId)!) {
throw Error(
`Farm ${farmId} doesn't have enough public IPs: requested IPs=${farmIPs.get(
farmId,
)}, available IPs=${freeIps}`,
);
}
}
}

async checkNodesCapacity(twinDeployments: TwinDeployment[]) {
for (const twinDeployment of twinDeployments) {
let workloads: Workload[] = [];
Expand Down Expand Up @@ -439,6 +476,8 @@ class TwinDeploymentHandler {
twinDeployments = await this.merge(twinDeployments);
await this.validate(twinDeployments);
await this.checkNodesCapacity(twinDeployments);
await this.checkFarmIps(twinDeployments);

const contracts = { created: [], updated: [], deleted: [] };
const resultContracts = { created: [], updated: [], deleted: [] };
let nodeExtrinsics: ExtrinsicResult<Contract>[] = [];
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ class FarmFilterOptions {
@Expose() @IsOptional() @IsInt() @Min(1) nodeRentedBy?: number;
@Expose() @IsOptional() @IsInt() page?: number;
@Expose() @IsOptional() @IsInt() size?: number;
@Expose() @IsOptional() @IsInt() farmId?: number;
}

class CalculatorModel {
Expand Down
5 changes: 3 additions & 2 deletions packages/grid_client/src/primitives/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface FarmInfo {
interface PublicIps {
id: string;
ip: string;
contractId: number;
contract_id: number; // Added to match the one in the farm interface || TODO: Should we replace the whole http requests to be done with the gridProxy.
gateway: string;
}

Expand Down Expand Up @@ -176,7 +176,7 @@ class Nodes {
farms = await this.getAllFarms(url);
}
return farms
.filter(farm => farm.publicIps.filter(ip => ip.contractId === 0).length > 0)
.filter(farm => farm.publicIps.filter(ip => ip.contract_id === 0).length > 0)
.map(farm => farm.farmId)
.includes(farmId);
}
Expand Down Expand Up @@ -382,6 +382,7 @@ class Nodes {
node_has_gpu: options.nodeHasGPU,
node_rented_by: options.nodeRentedBy,
node_certified: options.nodeCertified,
farm_id: options.farmId,
};
return Object.entries(params)
.map(param => param.join("="))
Expand Down

0 comments on commit 03836df

Please sign in to comment.