Skip to content

Commit

Permalink
add ip validation across compute peers
Browse files Browse the repository at this point in the history
  • Loading branch information
shamsartem committed Dec 31, 2024
1 parent 6ff73ae commit 9e5327d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,15 @@ function getDefault(args: ProviderConfigArgs) {

const computePeerEntries: [string, ComputePeer][] = [];

for (const i of times(numberOfPeers)) {
const name = `peer-${numToStr(i)}`;
for (const index of times(numberOfPeers)) {
const name = `peer-${numToStr(index)}`;

computePeerEntries.push([
name,
defaultComputePeerConfig({
computeUnits: DEFAULT_NUMBER_OF_COMPUTE_UNITS_ON_PEER,
name,
index,
}),
] as const);
}
Expand Down
49 changes: 39 additions & 10 deletions packages/cli/package/src/lib/configs/project/provider/provider4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,15 +552,18 @@ export default {
async migrate({ computePeers, capacityCommitments, offers, providerName }) {
const newComputePeers = Object.fromEntries(
Object.entries(computePeers).map(
([
name,
{
computeUnits,
kubeconfigPath,
resources,
nox: { vm: { network: { vmIp } = {} } = {} } = {},
},
]) => {
(
[
name,
{
computeUnits,
kubeconfigPath,
resources,
nox: { vm: { network: { vmIp } = {} } = {} } = {},
},
],
index,
) => {
return [
name,
defaultComputePeerConfig({
Expand All @@ -572,6 +575,7 @@ export default {
(vmIp === undefined
? undefined
: { ip: { supply: [{ start: vmIp }] } }),
index,
}),
] as const;
},
Expand Down Expand Up @@ -677,6 +681,7 @@ type DefaultComputePeerConfigArgs = {
ip: PrevIp;
}
| undefined;
index: number;
};

const DEFAULT_CPU_DETAILS: PeerCPUDetails = {
Expand Down Expand Up @@ -741,11 +746,14 @@ export function getDefaultOfferResources(): ResourcePrices {
};
}

const START_IP = 16843009; // 1.1.1.1

export function defaultComputePeerConfig({
kubeconfigPath,
name,
computeUnits,
resources,
index,
}: DefaultComputePeerConfigArgs): ComputePeer {
return {
kubeconfigPath: kubeconfigPath ?? `./path-to-${name}-kubeconfig`,
Expand All @@ -770,7 +778,9 @@ export function defaultComputePeerConfig({
bandwidth: { name: BANDWIDTH_RESOURCE_NAME, supply: 1 },
ip: {
name: IP_RESOURCE_NAME,
...(resources?.ip ?? { supply: [{ start: "1.1.1.1" }] }),
...(resources?.ip ?? {
supply: [{ start: ipNumToIpStr(START_IP + index) }],
}),
},
},
};
Expand Down Expand Up @@ -1083,6 +1093,9 @@ function validateComputePeerIPs({
}: {
computePeers: ComputePeers;
}) {
const allIPs = new Set<string>();
const duplicateIPs = new Set<string>();

const errors = Object.entries(computePeers).reduce<string[]>(
(acc, [peerName, { resources }]) => {
const res = ipSupplyToIndividualIPs(resources.ip.supply);
Expand All @@ -1091,13 +1104,29 @@ function validateComputePeerIPs({
acc.push(
`Compute peer ${color.yellow(peerName)} has invalid IPs in the supply:\n${res.error}`,
);

return acc;
}

res.result.forEach((ip) => {
if (allIPs.has(ip)) {
duplicateIPs.add(ip);
} else {
allIPs.add(ip);
}
});

return acc;
},
[],
);

if (duplicateIPs.size > 0) {
errors.push(
`The following IPs are not unique across compute peers:\n${Array.from(duplicateIPs).join("\n")}`,
);
}

return errors.length === 0 ? true : errors.join("\n");
}

Expand Down

0 comments on commit 9e5327d

Please sign in to comment.