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): reduce redundancy on context to cluster flags in command deployment create #1156

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 24 additions & 38 deletions src/commands/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,22 @@
* limitations under the License.
*
*/
import {Listr, type ListrTaskWrapper} from 'listr2';
import {Listr} from 'listr2';
import {SoloError} from '../core/errors.js';
import {BaseCommand} from './base.js';
import {Flags as flags} from './flags.js';
import * as constants from '../core/constants.js';
import {Templates} from '../core/templates.js';
import chalk from 'chalk';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {ListrLease} from '../core/lease/listr_lease.js';
import type {Namespace} from '../core/config/remote/types.js';
import {type ContextClusterStructure} from '../types/config_types.js';
import {type CommandFlag} from '../types/flag_types.js';
import {type CommandBuilder} from '../types/aliases.js';
import type {CommandFlag} from '../types/flag_types.js';
import type {CommandBuilder} from '../types/aliases.js';
import type {SoloListrTask} from '../types/index.js';

export class DeploymentCommand extends BaseCommand {
private static get DEPLOY_FLAGS_LIST(): CommandFlag[] {
return [
flags.quiet,
flags.namespace,
flags.userEmailAddress,
flags.deploymentClusters,
flags.contextClusterUnparsed,
];
return [flags.quiet, flags.namespace, flags.userEmailAddress, flags.deploymentClusters];
}

private async create(argv: any): Promise<boolean> {
Expand All @@ -45,8 +38,6 @@ export class DeploymentCommand extends BaseCommand {

interface Config {
namespace: Namespace;
contextClusterUnparsed: string;
contextCluster: ContextClusterStructure;
}
interface Context {
config: Config;
Expand All @@ -56,23 +47,16 @@ export class DeploymentCommand extends BaseCommand {
[
{
title: 'Initialize',
task: async (ctx, task): Promise<Listr<Context, any, any>> => {
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.contextClusterUnparsed,
flags.namespace,
flags.deploymentClusters,
]);
await self.configManager.executePrompt(task, [flags.namespace]);

ctx.config = {
contextClusterUnparsed: self.configManager.getFlag<string>(flags.contextClusterUnparsed),
namespace: self.configManager.getFlag<Namespace>(flags.namespace),
} as Config;

ctx.config.contextCluster = Templates.parseContextCluster(ctx.config.contextClusterUnparsed);

const namespace = ctx.config.namespace;

if (!(await self.k8.hasNamespace(namespace))) {
Expand All @@ -87,20 +71,22 @@ export class DeploymentCommand extends BaseCommand {
this.localConfig.promptLocalConfigTask(self.k8),
{
title: 'Validate cluster connections',
task: async (ctx, task): Promise<Listr<Context, any, any>> => {
const subTasks = [];

for (const cluster of Object.keys(ctx.config.contextCluster)) {
subTasks.push({
title: `Testing connection to cluster: ${chalk.cyan(cluster)}`,
task: async (_: Context, task: ListrTaskWrapper<Context, any, any>) => {
if (!(await self.k8.testClusterConnection(cluster))) {
task.title = `${task.title} - ${chalk.red('Cluster connection failed')}`;

throw new SoloError(`Cluster connection failed for: ${cluster}`);
}
},
});
task: async (_, task) => {
const subTasks: SoloListrTask<Context>[] = [];

for (const deployment of Object.values(self.localConfig.deployments)) {
for (const cluster of deployment.clusters) {
subTasks.push({
title: `Testing connection to cluster: ${chalk.cyan(cluster)}`,
task: async (_, task) => {
if (!(await self.k8.testClusterConnection(cluster))) {
instamenta marked this conversation as resolved.
Show resolved Hide resolved
task.title = `${task.title} - ${chalk.red('Cluster connection failed')}`;

throw new SoloError(`Cluster connection failed for: ${cluster}`);
}
},
});
}
}

return task.newListr(subTasks, {
Expand All @@ -119,7 +105,7 @@ export class DeploymentCommand extends BaseCommand {

try {
await tasks.run();
} catch (e: Error | any) {
} catch (e: Error | unknown) {
throw new SoloError(`Error installing chart ${constants.SOLO_DEPLOYMENT_CHART}`, e);
} finally {
await lease.release();
Expand Down
22 changes: 0 additions & 22 deletions src/commands/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1596,27 +1596,6 @@ export class Flags {
prompt: undefined,
};

static readonly contextClusterUnparsed: CommandFlag = {
constName: 'contextClusterUnparsed',
name: 'context-cluster',
definition: {
describe:
'Context cluster mapping where context is key = value is cluster and comma delimited if more than one, ' +
'(e.g.: --context-cluster kind-solo=kind-solo,kind-solo-2=kind-solo-2)',
type: 'string',
},
prompt: async function promptContextCluster(task: ListrTaskWrapper<any, any, any>, input: any) {
return await Flags.promptText(
task,
input,
null,
'Enter context cluster mapping: ',
'context-cluster cannot be empty',
Flags.contextClusterUnparsed.name,
);
},
};

static readonly haproxyIps: CommandFlag = {
constName: 'haproxyIps',
name: 'haproxy-ips',
Expand Down Expand Up @@ -1745,7 +1724,6 @@ export class Flags {
Flags.clusterName,
Flags.clusterSetupNamespace,
Flags.context,
Flags.contextClusterUnparsed,
Flags.createAmount,
Flags.debugNodeAlias,
Flags.deletePvcs,
Expand Down
2 changes: 1 addition & 1 deletion src/core/config/local_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
title: 'Prompt local configuration',
skip: this.skipPromptTask,
task: async (_: any, task: SoloListrTaskWrapper<any>): Promise<void> => {
if (self.configFileExists) {
if (self.configFileExists()) {

Check warning on line 172 in src/core/config/local_config.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/local_config.ts#L172

Added line #L172 was not covered by tests
self.configManager.setFlag(flags.userEmailAddress, self.userEmailAddress);
}

Expand Down
3 changes: 1 addition & 2 deletions src/core/config/remote/remote_config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ import {SoloLogger} from '../../logging.js';
import {ConfigManager} from '../../config_manager.js';
import {LocalConfig} from '../local_config.js';
import type {DeploymentStructure} from '../local_config_data.js';
import {type ContextClusterStructure} from '../../../types/config_types.js';
import {type EmptyContextConfig, type Optional, type SoloListrTask} from '../../../types/index.js';
import type * as k8s from '@kubernetes/client-node';
import {StatusCodes} from 'http-status-codes';
import {inject, injectable} from 'tsyringe-neo';
import {patchInject} from '../../container_helper.js';

interface ListrContext {
config: {contextCluster: ContextClusterStructure};
config: object;
}

/**
Expand Down
32 changes: 1 addition & 31 deletions src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import * as constants from './constants.js';
import {type AccountId} from '@hashgraph/sdk';
import type {IP, NodeAlias, NodeId, PodName} from '../types/aliases.js';
import {GrpcProxyTlsEnums} from './enumerations.js';
import {type ContextClusterStructure} from '../types/config_types.js';
import type {Cluster, Context, Namespace} from './config/remote/types.js';
import type {Namespace} from './config/remote/types.js';
import {HEDERA_PLATFORM_VERSION} from '../../version.js';

export class Templates {
Expand Down Expand Up @@ -228,35 +227,6 @@ export class Templates {
}
}

/**
* Parsed and validates the unparsed value of flag clusterMappings
*
* @param unparsed - value of flag clusterMappings
*/
public static parseContextCluster(unparsed: string): ContextClusterStructure {
const mapping = {};
const errorMessage =
'Invalid context in context-cluster, expected structure where context' +
' is key = value is cluster and comma delimited if more than one, ' +
'(e.g.: --context-cluster kind-solo=kind-solo,kind-solo-2=kind-solo-2)';

unparsed.split(',').forEach(data => {
const [context, cluster] = data.split('=') as [Context, Cluster];

if (!context || typeof context !== 'string') {
throw new SoloError(errorMessage, null, {data});
}

if (!cluster || typeof cluster !== 'string') {
throw new SoloError(errorMessage, null, {data});
}

mapping[context] = cluster;
});

return mapping;
}

public static renderEnvoyProxyName(nodeAlias: NodeAlias): string {
return `envoy-proxy-${nodeAlias}`;
}
Expand Down
2 changes: 0 additions & 2 deletions src/types/config_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@
*
*/
import type {Cluster, Context} from '../core/config/remote/types.js';

export type ContextClusterStructure = Record<Context, Cluster>;
Loading