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

refactor: K8 refactor #1202

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
26581a8
fix: resolve merge conflict introduced into the main branch
nathanklick Jan 22, 2025
a97b10d
compiling version with TK8 interface
jeromy-cannon Jan 22, 2025
3b8ca97
renamed vars and method to remove underscore and make private
jeromy-cannon Jan 22, 2025
83cce37
make exec/copy utility functions private and remove from interface
jeromy-cannon Jan 22, 2025
5abf679
removed init from tk8 and added todos
jeromy-cannon Jan 22, 2025
c4cb302
made logger in k8 private
jeromy-cannon Jan 22, 2025
fcb9159
made applyMetadataFilter private and removed from interface
jeromy-cannon Jan 22, 2025
d8248fd
made filterItem private and removed from interface
jeromy-cannon Jan 22, 2025
d285710
made getNodeLog private and removed from interface
jeromy-cannon Jan 22, 2025
bb9a836
made getNodeState private and removed from interface
jeromy-cannon Jan 22, 2025
e393444
made handleKubernetesClientError private and removed from interface
jeromy-cannon Jan 22, 2025
64706aa
removed unused imports
jeromy-cannon Jan 22, 2025
a012067
chore: example changes for refactor
nathanklick Jan 22, 2025
ae04fbd
added todos
jeromy-cannon Jan 22, 2025
89947c4
removed getCurrentCluster()
jeromy-cannon Jan 22, 2025
762fb88
removed getClusterIP
jeromy-cannon Jan 22, 2025
5106cfe
removed warnings that IntelliJ was flagging as errors
jeromy-cannon Jan 22, 2025
aecd264
added k8 cluster interface
jeromy-cannon Jan 22, 2025
2cb9338
made getContexts() private
jeromy-cannon Jan 22, 2025
7c4709b
removed warning that IntelliJ said was an error
jeromy-cannon Jan 22, 2025
48bd6cb
made getContexts() private
jeromy-cannon Jan 22, 2025
b9930d9
removed unused imports
jeromy-cannon Jan 22, 2025
f85aaa8
add context interface
jeromy-cannon Jan 22, 2025
9e4335f
added check interface
jeromy-cannon Jan 22, 2025
189ec9c
add todos for discussion with Nathan
jeromy-cannon Jan 23, 2025
c9ac750
remove double licenses
jeromy-cannon Jan 23, 2025
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
22 changes: 9 additions & 13 deletions src/commands/cluster/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {SoloError} from '../../core/errors.js';
import {RemoteConfigManager} from '../../core/config/remote/remote_config_manager.js';
import type {RemoteConfigDataWrapper} from '../../core/config/remote/remote_config_data_wrapper.js';
import type {K8} from '../../core/k8.js';
import type {Cluster} from '@kubernetes/client-node/dist/config_types.js';
import type {SoloListrTask, SoloListrTaskWrapper} from '../../types/index.js';
import type {SelectClusterContextContext} from './configs.js';
import type {Namespace} from '../../core/config/remote/types.js';
Expand Down Expand Up @@ -72,7 +71,7 @@ export class ClusterCommandTasks {

validateRemoteConfigForCluster(
cluster: string,
currentCluster: Cluster,
currentClusterName: string,
localConfig: LocalConfig,
currentRemoteConfig: RemoteConfigDataWrapper,
) {
Expand All @@ -84,7 +83,7 @@ export class ClusterCommandTasks {
self.parent.getK8().setCurrentContext(context);
const remoteConfigFromOtherCluster = await self.parent.getRemoteConfigManager().get();
if (!RemoteConfigManager.compare(currentRemoteConfig, remoteConfigFromOtherCluster)) {
throw new SoloError(ErrorMessages.REMOTE_CONFIGS_DO_NOT_MATCH(currentCluster.name, cluster));
throw new SoloError(ErrorMessages.REMOTE_CONFIGS_DO_NOT_MATCH(currentClusterName, cluster));
}
},
};
Expand All @@ -96,7 +95,6 @@ export class ClusterCommandTasks {
title: 'Read clusters from remote config',
task: async (ctx, task) => {
const localConfig = this.parent.getLocalConfig();
const currentCluster = this.parent.getK8().getCurrentCluster();
const currentClusterName = this.parent.getK8().getCurrentClusterName();
const currentRemoteConfig: RemoteConfigDataWrapper = await this.parent.getRemoteConfigManager().get();
const subTasks = [];
Expand All @@ -110,7 +108,9 @@ export class ClusterCommandTasks {

// Pull and validate RemoteConfigs from the other clusters
for (const cluster of otherRemoteConfigClusters) {
subTasks.push(self.validateRemoteConfigForCluster(cluster, currentCluster, localConfig, currentRemoteConfig));
subTasks.push(
self.validateRemoteConfigForCluster(cluster, currentClusterName, localConfig, currentRemoteConfig),
);
}

return task.newListr(subTasks, {
Expand Down Expand Up @@ -196,12 +196,8 @@ export class ClusterCommandTasks {
}

private async promptForContext(task: SoloListrTaskWrapper<SelectClusterContextContext>, cluster: string) {
const kubeContexts = this.parent.getK8().getContexts();
return flags.context.prompt(
task,
kubeContexts.map(c => c.name),
cluster,
);
const kubeContexts = this.parent.getK8().getContextNames();
return flags.context.prompt(task, kubeContexts, cluster);
}

private async selectContextForFirstCluster(
Expand Down Expand Up @@ -366,8 +362,8 @@ export class ClusterCommandTasks {
getClusterInfo() {
return new Task('Get cluster info', async (ctx: any, task: ListrTaskWrapper<any, any, any>) => {
try {
const cluster = this.parent.getK8().getCurrentCluster();
this.parent.logger.showJSON(`Cluster Information (${cluster.name})`, cluster);
const clusterName = this.parent.getK8().getCurrentClusterName();
this.parent.logger.showUser(`Cluster Name (${clusterName})`);
this.parent.logger.showUser('\n');
} catch (e: Error | unknown) {
this.parent.logger.showUserError(e);
Expand Down
13 changes: 7 additions & 6 deletions src/commands/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ export class DeploymentCommand extends BaseCommand {
private async create(argv: any): Promise<boolean> {
const self = this;

interface Config {
interface DeploymentCreateConfig {
context: string;
namespace: Namespace;
contextClusterUnparsed: string;
contextCluster: ContextClusterStructure;
}
interface Context {
config: Config;

interface DeploymentCreateContext {
config: DeploymentCreateConfig;
}

const tasks = new Listr<Context>(
const tasks = new Listr<DeploymentCreateContext>(
[
{
title: 'Initialize',
Expand All @@ -80,7 +81,7 @@ export class DeploymentCommand extends BaseCommand {
ctx.config = {
contextClusterUnparsed: self.configManager.getFlag<string>(flags.contextClusterUnparsed),
namespace: self.configManager.getFlag<Namespace>(flags.namespace),
} as Config;
} as DeploymentCreateConfig;

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

Expand Down Expand Up @@ -110,7 +111,7 @@ export class DeploymentCommand extends BaseCommand {
{
title: 'Validate cluster connections',
task: async (ctx, task) => {
const subTasks: SoloListrTask<Context>[] = [];
const subTasks: SoloListrTask<DeploymentCreateContext>[] = [];

for (const context of Object.keys(ctx.config.contextCluster)) {
const cluster = ctx.config.contextCluster[context];
Expand Down
6 changes: 3 additions & 3 deletions src/commands/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as constants from '../core/constants.js';
import {type ProfileManager} from '../core/profile_manager.js';
import {BaseCommand} from './base.js';
import {Flags as flags} from './flags.js';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {ListrRemoteConfig} from '../core/config/remote/listr_config_tasks.js';
import {type CommandBuilder} from '../types/aliases.js';
import {type Opts} from '../types/command_types.js';
import {ListrLease} from '../core/lease/listr_lease.js';
Expand Down Expand Up @@ -191,7 +191,7 @@ export class ExplorerCommand extends BaseCommand {
return ListrLease.newAcquireLeaseTask(lease, task);
},
},
RemoteConfigTasks.loadRemoteConfig.bind(this)(argv),
ListrRemoteConfig.loadRemoteConfig.bind(this)(argv),
{
title: 'Upgrade solo-setup chart',
task: async ctx => {
Expand Down Expand Up @@ -355,7 +355,7 @@ export class ExplorerCommand extends BaseCommand {
return ListrLease.newAcquireLeaseTask(lease, task);
},
},
RemoteConfigTasks.loadRemoteConfig.bind(this)(argv),
ListrRemoteConfig.loadRemoteConfig.bind(this)(argv),
{
title: 'Destroy explorer',
task: async ctx => {
Expand Down
26 changes: 13 additions & 13 deletions src/commands/mirror_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ interface MirrorNodeDeployConfigClass {
storageBucket: string;
}

interface Context {
interface MirrorNodeDeployContext {
config: MirrorNodeDeployConfigClass;
addressBook: string;
}

interface MirrorNodeDestroyContext {
config: {
namespace: string;
isChartInstalled: boolean;
};
}

export class MirrorNodeCommand extends BaseCommand {
private readonly accountManager: AccountManager;
private readonly profileManager: ProfileManager;
Expand Down Expand Up @@ -142,7 +149,7 @@ export class MirrorNodeCommand extends BaseCommand {
const self = this;
const lease = await self.leaseManager.create();

const tasks = new Listr<Context>(
const tasks = new Listr<MirrorNodeDeployContext>(
[
{
title: 'Initialize',
Expand Down Expand Up @@ -222,7 +229,7 @@ export class MirrorNodeCommand extends BaseCommand {
{
title: 'Enable mirror-node',
task: (_, parentTask) => {
return parentTask.newListr<Context>(
return parentTask.newListr<MirrorNodeDeployContext>(
[
{
title: 'Prepare address book',
Expand Down Expand Up @@ -425,14 +432,7 @@ export class MirrorNodeCommand extends BaseCommand {
const self = this;
const lease = await self.leaseManager.create();

interface Context {
config: {
namespace: string;
isChartInstalled: boolean;
};
}

const tasks = new Listr<Context>(
const tasks = new Listr<MirrorNodeDestroyContext>(
[
{
title: 'Initialize',
Expand Down Expand Up @@ -570,7 +570,7 @@ export class MirrorNodeCommand extends BaseCommand {
}

/** Removes the mirror node components from remote config. */
public removeMirrorNodeComponents(): SoloListrTask<object> {
public removeMirrorNodeComponents(): SoloListrTask<MirrorNodeDestroyContext> {
return {
title: 'Remove mirror node from remote config',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
Expand All @@ -583,7 +583,7 @@ export class MirrorNodeCommand extends BaseCommand {
}

/** Adds the mirror node components to remote config. */
public addMirrorNodeComponents(): SoloListrTask<{config: {namespace: Namespace}}> {
public addMirrorNodeComponents(): SoloListrTask<{config: MirrorNodeDeployConfigClass; addressBook: string}> {
return {
title: 'Add mirror node to remote config',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
Expand Down
12 changes: 6 additions & 6 deletions src/commands/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export interface NetworkDeployConfigClass {
googleCredential: string;
}

interface NetworkDeployContext {
config: NetworkDeployConfigClass;
}

export class NetworkCommand extends BaseCommand {
private readonly keyManager: KeyManager;
private readonly platformInstaller: PlatformInstaller;
Expand Down Expand Up @@ -495,11 +499,7 @@ export class NetworkCommand extends BaseCommand {
const self = this;
const lease = await self.leaseManager.create();

interface Context {
config: NetworkDeployConfigClass;
}

const tasks = new Listr<Context>(
const tasks = new Listr<NetworkDeployContext>(
[
{
title: 'Initialize',
Expand Down Expand Up @@ -952,7 +952,7 @@ export class NetworkCommand extends BaseCommand {
}

/** Adds the consensus node, envoy and haproxy components to remote config. */
public addNodesAndProxies(): SoloListrTask<{config: {namespace: Namespace; nodeAliases: NodeAliases}}> {
public addNodesAndProxies(): SoloListrTask<NetworkDeployContext> {
return {
title: 'Add node and proxies to remote config',
skip: (): boolean => !this.remoteConfigManager.isLoaded(),
Expand Down
8 changes: 2 additions & 6 deletions src/core/config/local_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,8 @@ export class LocalConfig implements LocalConfigData {
if (!isQuiet) {
const promptedContexts: string[] = [];
for (const cluster of parsedClusters) {
const kubeContexts = k8.getContexts();
const context: string = await flags.context.prompt(
task,
kubeContexts.map(c => c.name),
cluster,
);
const kubeContexts = k8.getContextNames();
const context: string = await flags.context.prompt(task, kubeContexts, cluster);
self.clusterContextMapping[cluster] = context;
promptedContexts.push(context);

Expand Down
7 changes: 1 addition & 6 deletions src/core/config/remote/remote_config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@ 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 Optional} 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';
import {ErrorMessages} from '../../error_messages.js';

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

/**
* Uses Kubernetes ConfigMaps to manage the remote configuration data by creating, loading, modifying,
* and saving the configuration data to and from a Kubernetes cluster.
Expand Down
Loading