Skip to content

Commit 74edacd

Browse files
committed
feat: refactor components data handling to support node aliases by cluster
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
1 parent acf2032 commit 74edacd

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

src/core/config/remote/components_data_wrapper.ts

+18-20
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {EnvoyProxyComponent} from './components/envoy_proxy_component.js';
1111
import {ConsensusNodeComponent} from './components/consensus_node_component.js';
1212
import {MirrorNodeExplorerComponent} from './components/mirror_node_explorer_component.js';
1313
import {
14+
type ClusterRef,
1415
type Component,
16+
type ComponentName,
1517
type ComponentsDataStructure,
1618
type IConsensusNodeComponent,
1719
type IRelayComponent,
18-
type ComponentName,
1920
type NamespaceNameAsString,
2021
} from './types.js';
2122
import {type ToObject, type Validate} from '../../../types/index.js';
23+
import type {NodeAlias} from '../../../types/aliases.js';
24+
import {Templates} from '../../templates.js';
2225

2326
/**
2427
* Represent the components in the remote config and handles:
@@ -232,29 +235,24 @@ export class ComponentsDataWrapper implements Validate, ToObject<ComponentsDataS
232235
}
233236

234237
public static initializeWithNodes(
235-
nodeAliases: string[],
236-
cluster: string,
238+
nodeAliasesByCluster: Record<ClusterRef, NodeAlias[]>,
237239
namespace: NamespaceNameAsString,
238240
): ComponentsDataWrapper {
239241
const consensusNodeComponents: Record<ComponentName, ConsensusNodeComponent> = {};
240-
nodeAliases.forEach((alias, index) => {
241-
consensusNodeComponents[alias] = new ConsensusNodeComponent(
242-
alias,
243-
cluster,
244-
namespace,
245-
ConsensusNodeStates.REQUESTED,
246-
index,
247-
);
242+
243+
Object.entries(nodeAliasesByCluster).forEach(([clusterRef, nodeAliases]) => {
244+
nodeAliases.forEach(nodeAlias => {
245+
consensusNodeComponents[nodeAlias] = new ConsensusNodeComponent(
246+
nodeAlias,
247+
clusterRef,
248+
namespace,
249+
ConsensusNodeStates.REQUESTED,
250+
Templates.nodeIdFromNodeAlias(nodeAlias),
251+
);
252+
});
248253
});
249-
const componentDataWrapper = new ComponentsDataWrapper(
250-
undefined,
251-
undefined,
252-
undefined,
253-
undefined,
254-
consensusNodeComponents,
255-
undefined,
256-
);
257-
return componentDataWrapper;
254+
255+
return new ComponentsDataWrapper(undefined, undefined, undefined, undefined, consensusNodeComponents, undefined);
258256
}
259257

260258
/** checks if component exists in the respective group */

src/core/config/remote/remote_config_manager.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {inject, injectable} from 'tsyringe-neo';
2222
import {patchInject} from '../../dependency_injection/container_helper.js';
2323
import {ErrorMessages} from '../../error_messages.js';
2424
import {CommonFlagsDataWrapper} from './common_flags_data_wrapper.js';
25-
import {type AnyObject} from '../../../types/aliases.js';
25+
import {type AnyObject, type NodeAlias} from '../../../types/aliases.js';
2626
import {NamespaceName} from '../../kube/resources/namespace/namespace_name.js';
2727
import {ResourceNotFoundError} from '../../kube/errors/resource_operation_errors.js';
2828
import {InjectTokens} from '../../dependency_injection/inject_tokens.js';
@@ -113,7 +113,10 @@ export class RemoteConfigManager {
113113
);
114114

115115
// temporary workaround until we can have `solo deployment add` command
116-
const nodeAliases: string[] = helpers.splitFlagInput(this.configManager.getFlag(flags.nodeAliasesUnparsed));
116+
const nodeAliasesByCluster: Record<ClusterRef, NodeAlias[]> = helpers.getNodeAliasesByClusterFromNodeAliasesFlag(
117+
this.configManager.getFlag(flags.nodeAliasesUnparsed),
118+
Object.keys(clusters),
119+
);
117120

118121
this.remoteConfig = new RemoteConfigDataWrapper({
119122
metadata: new RemoteConfigMetadata(
@@ -126,11 +129,7 @@ export class RemoteConfigManager {
126129
clusters,
127130
commandHistory: ['deployment create'],
128131
lastExecutedCommand: 'deployment create',
129-
components: ComponentsDataWrapper.initializeWithNodes(
130-
nodeAliases,
131-
this.configManager.getFlag(flags.deploymentClusters),
132-
this.getNamespace().name,
133-
),
132+
components: ComponentsDataWrapper.initializeWithNodes(nodeAliasesByCluster, this.getNamespace().name),
134133
flags: await CommonFlagsDataWrapper.initialize(this.configManager, argv),
135134
});
136135

src/core/helpers.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {type Duration} from './time/duration.js';
1818
import {type NodeAddConfigClass} from '../commands/node/node_add_config.js';
1919
import {type ConsensusNode} from './model/consensus_node.js';
2020
import {type Optional} from '../types/index.js';
21-
import {type Version} from './config/remote/types.js';
21+
import {type ClusterRef, type ClusterRefs, type Version} from './config/remote/types.js';
2222
import {fileURLToPath} from 'url';
2323
import {type NamespaceName} from './kube/resources/namespace/namespace_name.js';
2424

@@ -74,6 +74,32 @@ export function splitFlagInput(input: string, separator = ',') {
7474
.filter(Boolean);
7575
}
7676

77+
export function getNodeAliasesByClusterFromNodeAliasesFlag(
78+
flagValue: string,
79+
clusterRefs: ClusterRef[],
80+
): Record<ClusterRef, NodeAlias[]> {
81+
const nodeAliasesByCluster: Record<ClusterRef, NodeAlias[]> = {};
82+
clusterRefs.forEach(cluster => {
83+
nodeAliasesByCluster[cluster] = [];
84+
});
85+
86+
const parameterPairs: string[] = splitFlagInput(flagValue);
87+
for (const parameterPair of parameterPairs) {
88+
if (parameterPair.includes('=')) {
89+
const [cluster, nodeAlias] = splitFlagInput(parameterPair, '=');
90+
if (nodeAliasesByCluster[cluster]) {
91+
nodeAliasesByCluster[cluster].push(nodeAlias as NodeAlias);
92+
} else {
93+
clusterRefs.forEach(cluster => {
94+
nodeAliasesByCluster[cluster].push(nodeAlias as NodeAlias);
95+
});
96+
}
97+
}
98+
}
99+
100+
return nodeAliasesByCluster;
101+
}
102+
77103
/**
78104
* @param arr - The array to be cloned
79105
* @returns a new array with the same elements as the input array

0 commit comments

Comments
 (0)