Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add new command solo deployment list that lists deployments on that specific cluster #1220

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 94 additions & 5 deletions src/commands/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as constants from '../core/constants.js';
import chalk from 'chalk';
import {ListrRemoteConfig} from '../core/config/remote/listr_config_tasks.js';
import {ClusterCommandTasks} from './cluster/tasks.js';
import {type Namespace} from '../core/config/remote/types.js';
import {type Namespace, type Cluster} from '../core/config/remote/types.js';
import {type CommandFlag} from '../types/flag_types.js';
import {type CommandBuilder} from '../types/aliases.js';
import {type SoloListrTask} from '../types/index.js';
Expand All @@ -35,6 +35,10 @@ export class DeploymentCommand extends BaseCommand {
];
}

private static get LIST_DEPLOYMENTS_FLAGS_LIST(): CommandFlag[] {
return [flags.quiet, flags.clusterName];
}

private async create(argv: any): Promise<boolean> {
const self = this;

Expand Down Expand Up @@ -106,7 +110,7 @@ export class DeploymentCommand extends BaseCommand {
}

return task.newListr(subTasks, {
concurrent: false,
concurrent: true,
rendererOptions: {collapseSubtasks: false},
});
},
Expand All @@ -128,18 +132,82 @@ export class DeploymentCommand extends BaseCommand {
return true;
}

private async list(argv: any): Promise<boolean> {
const self = this;

interface Config {
clusterName: Cluster;
}

interface Context {
config: Config;
}

const tasks = new Listr<Context>(
[
{
title: 'Initialize',
task: async (ctx, task) => {
self.configManager.update(argv);
self.logger.debug('Updated config with argv', {config: self.configManager.config});

await self.configManager.executePrompt(task, [flags.clusterName]);

ctx.config = {
clusterName: self.configManager.getFlag<Cluster>(flags.clusterName),
} as Config;

self.logger.debug('Prepared config', {config: ctx.config, cachedConfig: self.configManager.config});
},
},
{
title: 'Validate context',
task: async ctx => {
const clusterName = ctx.config.clusterName;

const context = self.localConfig.clusterContextMapping[clusterName];

self.k8.setCurrentContext(context);

const namespaces = await self.k8.getNamespaces();
const namespacesWithRemoteConfigs: Namespace[] = [];

for (const namespace of namespaces) {
const isFound = await self.k8.isRemoteConfigPresentInNamespace(namespace);
if (isFound) namespacesWithRemoteConfigs.push(namespace);
}

self.logger.showList(`Deployments inside cluster: ${chalk.cyan(clusterName)}`, namespacesWithRemoteConfigs);
},
},
],
{
concurrent: false,
rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION,
},
);

try {
await tasks.run();
} catch (e: Error | unknown) {
throw new SoloError(`Error installing chart ${constants.SOLO_DEPLOYMENT_CHART}`, e);
}

return true;
}

public getCommandDefinition(): {command: string; desc: string; builder: CommandBuilder} {
const self = this;
return {
command: 'deployment',
desc: 'Manage solo network deployment',
builder: (yargs: any): any => {
builder: yargs => {
return yargs
.command({
command: 'create',
desc: 'Creates solo deployment',
builder: (y: any) => flags.setCommandFlags(y, ...DeploymentCommand.DEPLOY_FLAGS_LIST),
handler: (argv: any) => {
builder: y => flags.setCommandFlags(y, ...DeploymentCommand.DEPLOY_FLAGS_LIST),
handler: argv => {
self.logger.info("==== Running 'deployment create' ===");
self.logger.info(argv);

Expand All @@ -156,6 +224,27 @@ export class DeploymentCommand extends BaseCommand {
});
},
})
.command({
command: 'list',
desc: 'List solo deployments inside a cluster',
builder: y => flags.setCommandFlags(y, ...DeploymentCommand.LIST_DEPLOYMENTS_FLAGS_LIST),
handler: argv => {
self.logger.info("==== Running 'deployment list' ===");
self.logger.info(argv);

self
.list(argv)
.then(r => {
self.logger.info('==== Finished running `deployment list`====');

if (!r) process.exit(1);
})
.catch(err => {
self.logger.showUserError(err);
process.exit(1);
});
},
})
.demandCommand(1, 'Select a chart command');
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/core/kube/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ export interface K8 {

isRemoteConfigPresentInAnyNamespace(): Promise<boolean>;

isRemoteConfigPresentInNamespace(namespace: Namespace): Promise<boolean>;

isPrometheusInstalled(namespace: Namespace): Promise<boolean>;

/**
Expand Down
25 changes: 25 additions & 0 deletions src/core/kube/k8_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,31 @@
}
}

/**
* Searches specific namespace for remote config's config map
*
* @param namespace - namespace where to search
* @returns true if found else false
*/
public async isRemoteConfigPresentInNamespace(namespace: Namespace): Promise<boolean> {
try {
const configmaps = await this.kubeClient.listNamespacedConfigMap(
namespace,
undefined,
undefined,
undefined,
undefined,
constants.SOLO_REMOTE_CONFIGMAP_LABEL_SELECTOR,
);

return configmaps.body.items.length > 0;
} catch (e) {
this.logger.error('Failed to find remote config:', e);

return false;
}
}

Check warning on line 1400 in src/core/kube/k8_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/kube/k8_client.ts#L1384-L1400

Added lines #L1384 - L1400 were not covered by tests

/* ------------- Utilities ------------- */

/**
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export function main(argv: any) {
(command === 'cluster' && subCommand === 'connect') ||
(command === 'cluster' && subCommand === 'info') ||
(command === 'cluster' && subCommand === 'list') ||
(command === 'deployment' && subCommand === 'create');
(command === 'deployment' && subCommand === 'create') ||
(command === 'deployment' && subCommand === 'list');

if (!skip) {
await remoteConfigManager.loadAndValidate(argv);
Expand Down
Loading