-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathremote_config_manager.ts
351 lines (295 loc) · 12.4 KB
/
remote_config_manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import * as constants from '../../constants.js';
import {MissingArgumentError, SoloError} from '../../errors.js';
import {RemoteConfigDataWrapper} from './remote_config_data_wrapper.js';
import chalk from 'chalk';
import {RemoteConfigMetadata} from './metadata.js';
import {Flags as flags} from '../../../commands/flags.js';
import * as yaml from 'yaml';
import {ComponentsDataWrapper} from './components_data_wrapper.js';
import {RemoteConfigValidator} from './remote_config_validator.js';
import {K8} from '../../k8.js';
import type {Cluster, Context, Namespace} from './types.js';
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';
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.
*/
@injectable()
export class RemoteConfigManager {
/** Stores the loaded remote configuration data. */
private remoteConfig: Optional<RemoteConfigDataWrapper>;
/**
* @param k8 - The Kubernetes client used for interacting with ConfigMaps.
* @param logger - The logger for recording activity and errors.
* @param localConfig - Local configuration for the remote config.
* @param configManager - Manager to retrieve application flags and settings.
*/
public constructor(
@inject(K8) private readonly k8?: K8,
@inject(SoloLogger) private readonly logger?: SoloLogger,
@inject(LocalConfig) private readonly localConfig?: LocalConfig,
@inject(ConfigManager) private readonly configManager?: ConfigManager,
) {
this.k8 = patchInject(k8, K8, this.constructor.name);
this.logger = patchInject(logger, SoloLogger, this.constructor.name);
this.localConfig = patchInject(localConfig, LocalConfig, this.constructor.name);
this.configManager = patchInject(configManager, ConfigManager, this.constructor.name);
}
/* ---------- Getters ---------- */
public get currentCluster(): Cluster {
return this.localConfig.currentDeploymentName as Cluster;
}
/** @returns the components data wrapper cloned */
public get components(): ComponentsDataWrapper {
return this.remoteConfig.components.clone();
}
/* ---------- Readers and Modifiers ---------- */
/**
* Modifies the loaded remote configuration data using a provided callback function.
* The callback operates on the configuration data, which is then saved to the cluster.
*
* @param callback - an async function that modifies the remote configuration data.
* @throws {@link SoloError} if the configuration is not loaded before modification.
*/
public async modify(callback: (remoteConfig: RemoteConfigDataWrapper) => Promise<void>): Promise<void> {
if (!this.remoteConfig) {
return;
// TODO see if this should be disabled to make it an optional feature
// throw new SoloError('Attempting to modify remote config without loading it first')
}
await callback(this.remoteConfig);
await this.save();
}
/**
* Creates a new remote configuration in the Kubernetes cluster.
* Gathers data from the local configuration and constructs a new ConfigMap
* entry in the cluster with initial command history and metadata.
*/
private async create(): Promise<void> {
const clusters: Record<Cluster, Namespace> = {};
Object.entries(this.localConfig.deployments).forEach(
([namespace, deployment]: [Namespace, DeploymentStructure]) => {
deployment.clusters.forEach(cluster => (clusters[cluster] = namespace));
},
);
this.remoteConfig = new RemoteConfigDataWrapper({
metadata: new RemoteConfigMetadata(this.getNamespace(), new Date(), this.localConfig.userEmailAddress),
clusters,
components: ComponentsDataWrapper.initializeEmpty(),
lastExecutedCommand: 'deployment create',
commandHistory: ['deployment create'],
});
await this.createConfigMap();
}
/**
* Saves the currently loaded remote configuration data to the Kubernetes cluster.
* @throws {@link SoloError} if there is no remote configuration data to save.
*/
private async save(): Promise<void> {
if (!this.remoteConfig) {
throw new SoloError('Attempted to save remote config without data');
}
await this.replaceConfigMap();
}
/**
* Loads the remote configuration from the Kubernetes cluster if it exists.
* @returns true if the configuration is loaded successfully.
*/
private async load(): Promise<boolean> {
if (this.remoteConfig) return true;
const configMap = await this.getConfigMap();
if (!configMap) return false;
this.remoteConfig = RemoteConfigDataWrapper.fromConfigmap(configMap);
return true;
}
/**
* Loads the remote configuration, performs a validation and returns it
* @returns RemoteConfigDataWrapper
*/
public async get(): Promise<RemoteConfigDataWrapper> {
await this.load();
try {
await RemoteConfigValidator.validateComponents(this.remoteConfig.components, this.k8);
} catch (e) {
throw new SoloError(ErrorMessages.REMOTE_CONFIG_IS_INVALID(this.k8.getKubeConfig().getCurrentCluster().name));
}
return this.remoteConfig;
}
public static compare(remoteConfig1: RemoteConfigDataWrapper, remoteConfig2: RemoteConfigDataWrapper): boolean {
// Compare clusters
const clusters1 = Object.keys(remoteConfig1.clusters);
const clusters2 = Object.keys(remoteConfig2.clusters);
if (clusters1.length !== clusters2.length) return false;
for (const i in clusters1) {
if (clusters1[i] !== clusters2[i]) {
return false;
}
}
return true;
}
/* ---------- Listr Task Builders ---------- */
/**
* Builds a task for loading the remote configuration, intended for use with Listr task management.
* Checks if the configuration is already loaded, otherwise loads and adds the command to history.
*
* @param argv - arguments containing command input for historical reference.
* @returns a Listr task which loads the remote configuration.
*/
public buildLoadTask(argv: {_: string[]}): SoloListrTask<EmptyContextConfig> {
const self = this;
return {
title: 'Load remote config',
task: async (_, task): Promise<void> => {
try {
self.setDefaultNamespaceIfNotSet();
self.setDefaultContextIfNotSet();
} catch {
return; // TODO
}
if (!(await self.load())) {
task.title = `${task.title} - ${chalk.red('remote config not found')}`;
// TODO see if this should be disabled to make it an optional feature
return;
// throw new SoloError('Failed to load remote config')
}
await RemoteConfigValidator.validateComponents(self.remoteConfig.components, self.k8);
const currentCommand = argv._.join(' ');
self.remoteConfig!.addCommandToHistory(currentCommand);
await self.save();
},
};
}
/**
* Builds a task for creating a new remote configuration, intended for use with Listr task management.
* Merges cluster mappings from the provided context into the local configuration, then creates the remote config.
*
* @returns a Listr task which creates the remote configuration.
*/
public buildCreateTask(cluster: Cluster, context: Context, namespace: Namespace): SoloListrTask<ListrContext> {
const self = this;
return {
title: `Create remote config in cluster: ${cluster}`,
task: async (_, task): Promise<void> => {
self.k8.setCurrentContext(context);
if (!(await self.k8.hasNamespace(namespace))) {
await self.k8.createNamespace(namespace);
}
const localConfigExists = this.localConfig.configFileExists();
if (!localConfigExists) {
throw new SoloError("Local config doesn't exist");
}
self.unload();
if (await self.load()) {
task.title = `${task.title} - ${chalk.red('Remote config already exists')}}`;
throw new SoloError('Remote config already exists');
}
await self.create();
},
};
}
/* ---------- Utilities ---------- */
/** Empties the component data inside the remote config */
public async deleteComponents() {
await this.modify(async remoteConfig => {
remoteConfig.components = ComponentsDataWrapper.initializeEmpty();
});
}
public isLoaded(): boolean {
return !!this.remoteConfig;
}
public unload() {
delete this.remoteConfig;
}
/**
* Retrieves the ConfigMap containing the remote configuration from the Kubernetes cluster.
*
* @returns the remote configuration data.
* @throws {@link SoloError} if the ConfigMap could not be read and the error is not a 404 status.
*/
public async getConfigMap(): Promise<k8s.V1ConfigMap> {
try {
return await this.k8.getNamespacedConfigMap(constants.SOLO_REMOTE_CONFIGMAP_NAME);
} catch (error: any) {
if (error.meta?.statusCode !== StatusCodes.NOT_FOUND) {
throw new SoloError('Failed to read remote config from cluster', error);
}
return null;
}
}
/**
* Creates a new ConfigMap entry in the Kubernetes cluster with the remote configuration data.
*/
private async createConfigMap(): Promise<void> {
await this.k8.createNamespacedConfigMap(
constants.SOLO_REMOTE_CONFIGMAP_NAME,
constants.SOLO_REMOTE_CONFIGMAP_LABELS,
{'remote-config-data': yaml.stringify(this.remoteConfig.toObject())},
);
}
/** Replaces an existing ConfigMap in the Kubernetes cluster with the current remote configuration data. */
private async replaceConfigMap(): Promise<void> {
await this.k8.replaceNamespacedConfigMap(
constants.SOLO_REMOTE_CONFIGMAP_NAME,
constants.SOLO_REMOTE_CONFIGMAP_LABELS,
{'remote-config-data': yaml.stringify(this.remoteConfig.toObject() as any)},
);
}
private setDefaultNamespaceIfNotSet(): void {
if (this.configManager.hasFlag(flags.namespace)) return;
if (!this.localConfig?.currentDeploymentName) {
this.logger.error('Current deployment name is not set in local config', this.localConfig);
throw new SoloError('Current deployment name is not set in local config');
}
// TODO: Current quick fix for commands where namespace is not passed
const namespace = this.localConfig.currentDeploymentName.replace(/^kind-/, '');
this.configManager.setFlag(flags.namespace, namespace);
}
private setDefaultContextIfNotSet(): void {
if (this.configManager.hasFlag(flags.context)) return;
const context = this.k8.getKubeConfig().currentContext;
if (!context) {
this.logger.error("Context is not passed and default one can't be acquired", this.localConfig);
throw new SoloError("Context is not passed and default one can't be acquired");
}
this.configManager.setFlag(flags.context, context);
}
// cluster will be retrieved from LocalConfig based the context to cluster mapping
/**
* Retrieves the namespace value from the configuration manager's flags.
* @returns string - The namespace value if set.
*/
private getNamespace(): string {
const ns = this.configManager.getFlag<string>(flags.namespace) as string;
if (!ns) throw new MissingArgumentError('namespace is not set');
return ns;
}
}