Skip to content

Commit

Permalink
feat: suggest to deploy manifests when ip changed (#1092)
Browse files Browse the repository at this point in the history
* feat: suggest to deploy manifests when ip changed

* fix circular

* feat: improve provider.yaml (#1094)

* feat: improvements for provider yaml

* Apply automatic changes

* some fixes

* fix and use latest

* Apply automatic changes

* fix

* fix

* remove deprecation warning from tests as well

---------

Co-authored-by: shamsartem <[email protected]>

---------

Co-authored-by: shamsartem <[email protected]>
  • Loading branch information
shamsartem and shamsartem authored Jan 16, 2025
1 parent e6955df commit efbe845
Show file tree
Hide file tree
Showing 22 changed files with 881 additions and 862 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package/bin/dev.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --no-warnings --import tsx
#!/usr/bin/env -S node --no-warnings --no-deprecation --import tsx

/**
* Fluence CLI
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package/bin/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --no-warnings
#!/usr/bin/env -S node --no-warnings --no-deprecation

/**
* Fluence CLI
Expand Down
190 changes: 57 additions & 133 deletions packages/cli/package/docs/configs/provider.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/cli/package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"whatwg-url": "^14.0.0"
},
"dependencies": {
"@fluencelabs/deal-ts-clients": "0.23.2-add-dc-to-subgraph-afdd94a-7473-1.0",
"@fluencelabs/deal-ts-clients": "0.23.2-feat-marketplace-v2-resources-eb89b78-7511-1.0",
"@kubernetes/client-node": "github:fluencelabs/kubernetes-client-javascript#e72ee00a52fec4eb4a8327632895d888ee504f4d",
"@libp2p/crypto": "4.0.1",
"@libp2p/peer-id-factory": "4.0.5",
Expand Down
94 changes: 2 additions & 92 deletions packages/cli/package/src/commands/provider/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { readFile } from "node:fs/promises";
import { isAbsolute, resolve } from "node:path";

import type k8s from "@kubernetes/client-node";

import { BaseCommand } from "../../baseCommand.js";
import { commandObj } from "../../lib/commandObj.js";
import { CHAIN_FLAGS, PEER_AND_OFFER_NAMES_FLAGS } from "../../lib/const.js";
import { initCli } from "../../lib/lifeCycle.js";
import { projectRootDir } from "../../lib/paths.js";
import { resolveComputePeersByNames } from "../../lib/resolveComputePeersByNames.js";
import { deployManifests } from "../../lib/manifestsDeploy.js";

export default class Deploy extends BaseCommand<typeof Deploy> {
static override description = "Deploy manifests";
Expand All @@ -36,89 +29,6 @@ export default class Deploy extends BaseCommand<typeof Deploy> {
};
async run(): Promise<void> {
const { flags } = await initCli(this, await this.parse(Deploy));
const computePeers = await resolveComputePeersByNames(flags);

for (const { kubeconfigPath, name, manifestPath } of computePeers) {
await kubectlApply(kubeconfigPath, manifestPath, name);
}
}
}

/**
* This function comes from https://github.com/kubernetes-client/javascript
* Replicate the functionality of `kubectl apply`. That is, create the resources defined in the `specFile` if they do
* not exist, patch them if they do exist.
*
* @param specPath File system path to a YAML Kubernetes spec.
* @return Array of resources created
*/
async function kubectlApply(
kubeconfigPath: string,
specPath: string,
peerName: string,
): Promise<k8s.KubernetesObject[]> {
const kubeconfigAbsolutePath = isAbsolute(kubeconfigPath)
? kubeconfigPath
: resolve(projectRootDir, kubeconfigPath);

commandObj.log(
`Applying manifest for computePeer ${peerName}\nKubeconfig: ${kubeconfigAbsolutePath}\nManifest: ${specPath}`,
);

const k8s = await import("@kubernetes/client-node");
const kc = new k8s.KubeConfig(kubeconfigAbsolutePath);
kc.loadFromFile(kubeconfigAbsolutePath);

/* eslint-disable */
const client = k8s.KubernetesObjectApi.makeApiClient(kc);

const specString = await readFile(specPath, "utf8");
const specs: k8s.KubernetesObject[] = k8s.loadAllYaml(specString);

const validSpecs = specs.filter((s) => {
return s && s.kind && s.metadata;
});

const created: k8s.KubernetesObject[] = [];

for (const spec of validSpecs) {
// this is to convince the old version of TypeScript that metadata exists even though we already filtered specs
// without metadata out
spec.metadata = spec.metadata || {};
spec.metadata.annotations = spec.metadata.annotations || {};

delete spec.metadata.annotations[
"kubectl.kubernetes.io/last-applied-configuration"
];

spec.metadata.annotations[
"kubectl.kubernetes.io/last-applied-configuration"
] = JSON.stringify(spec);

try {
// try to get the resource, if it does not exist an error will be thrown and we will end up in the catch
// block.
// @ts-expect-error
await client.read(spec);
// we got the resource, so it exists, so patch it
//
// Note that this could fail if the spec refers to a custom resource. For custom resources you may need
// to specify a different patch merge strategy in the content-type header.
//
// See: https://github.com/kubernetes/kubernetes/issues/97423
const response = await client.patch(spec);
created.push(response.body);
} catch (err) {
// if the resource doesnt exist then create it
if (err instanceof k8s.HttpError && err.statusCode === 404) {
const response = await client.create(spec);
created.push(response.body);
} else {
throw err;
}
}
await deployManifests({ flags });
}

return created;
/* eslint-enable */
}
2 changes: 1 addition & 1 deletion packages/cli/package/src/commands/provider/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class Info extends BaseCommand<typeof Info> {

async run(): Promise<void> {
const { flags } = await initCli(this, await this.parse(Info));
const computePeers = await resolveComputePeersByNames(flags);
const computePeers = await resolveComputePeersByNames({ flags });

const infoToPrint = {
...(await formatProvidersInfo(
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package/src/lib/chain/commitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ async function getCommitmentsIds(
);
}

return getComputePeersWithCCIds(await resolveComputePeersByNames(flags));
return getComputePeersWithCCIds(await resolveComputePeersByNames({ flags }));
}

export async function createCommitments(flags: PeerAndOfferNameFlags) {
const computePeers = await resolveComputePeersByNames(flags);
const computePeers = await resolveComputePeersByNames({ flags });
const { contracts } = await getContracts();
const precision = await contracts.diamond.precision();
const { ZeroAddress } = await import("ethers");
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package/src/lib/chain/distributeToNox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { fltFormatWithSymbol, fltParse } from "./currencies.js";
export async function distributeToPeer(
flags: { amount?: string | undefined } & PeerAndOfferNameFlags,
) {
const computePeers = await resolveComputePeersByNames(flags);
const computePeers = await resolveComputePeersByNames({ flags });

const amount =
flags.amount ??
Expand Down Expand Up @@ -71,7 +71,7 @@ export async function withdrawFromPeer(
amount?: string | undefined;
} & PeerAndOfferNameFlags,
) {
const computePeers = await resolveComputePeersByNames(flags);
const computePeers = await resolveComputePeersByNames({ flags });

const amount =
flags.amount ??
Expand Down
Loading

0 comments on commit efbe845

Please sign in to comment.