Skip to content

Commit

Permalink
refactor: utilize k8.ConfigMaps methods (#1306)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivo Yankov <[email protected]>
  • Loading branch information
Ivo-Yankov authored Feb 7, 2025
1 parent 0274a80 commit 76435a4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 77 deletions.
29 changes: 14 additions & 15 deletions src/core/config/remote/remote_config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {LocalConfig} from '../local_config.js';
import {type DeploymentStructure} from '../local_config_data.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';
Expand Down Expand Up @@ -101,7 +100,7 @@ export class RemoteConfigManager {
);

this.remoteConfig = new RemoteConfigDataWrapper({
metadata: new RemoteConfigMetadata(this.getNamespace(), new Date(), this.localConfig.userEmailAddress),
metadata: new RemoteConfigMetadata(this.getNamespace().name, new Date(), this.localConfig.userEmailAddress),
clusters,
commandHistory: ['deployment create'],
lastExecutedCommand: 'deployment create',
Expand Down Expand Up @@ -260,7 +259,7 @@ export class RemoteConfigManager {
*/
public async getConfigMap(): Promise<k8s.V1ConfigMap> {
try {
return await this.k8.getNamespacedConfigMap(constants.SOLO_REMOTE_CONFIGMAP_NAME);
return await this.k8.configMaps().read(this.getNamespace(), constants.SOLO_REMOTE_CONFIGMAP_NAME);
} catch (error: any) {
if (!(error instanceof ResourceNotFoundError)) {
throw new SoloError('Failed to read remote config from cluster', error);
Expand All @@ -274,20 +273,20 @@ export class RemoteConfigManager {
* 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())},
);
await this.k8
.configMaps()
.create(this.getNamespace(), 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)},
);
await this.k8
.configMaps()
.replace(this.getNamespace(), constants.SOLO_REMOTE_CONFIGMAP_NAME, constants.SOLO_REMOTE_CONFIGMAP_LABELS, {
'remote-config-data': yaml.stringify(this.remoteConfig.toObject() as any),
});
}

private setDefaultNamespaceIfNotSet(): void {
Expand Down Expand Up @@ -324,9 +323,9 @@ export class RemoteConfigManager {
* Retrieves the namespace value from the configuration manager's flags.
* @returns string - The namespace value if set.
*/
private getNamespace(): NamespaceNameAsString {
private getNamespace(): NamespaceName {
const ns = this.configManager.getFlag<NamespaceName>(flags.namespace);
if (!ns) throw new MissingArgumentError('namespace is not set');
return ns.name;
return ns;
}
}
8 changes: 4 additions & 4 deletions src/core/kube/config_maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ConfigMaps {
name: string,
labels: Record<string, string>,
data: Record<string, string>,
): Promise<boolean>; // TODO was createNamespacedConfigMap
): Promise<boolean>;

/**
* Create or replace a config map. If the config map already exists, it will be replaced.
Expand All @@ -45,7 +45,7 @@ export interface ConfigMaps {
* @param namespace - for the config map
* @param name - for the config name
*/
read(namespace: NamespaceName, name: string): Promise<V1ConfigMap>; // TODO was getNamespacedConfigMap
read(namespace: NamespaceName, name: string): Promise<V1ConfigMap>;

/**
* Replace an existing config map with a new one
Expand All @@ -59,14 +59,14 @@ export interface ConfigMaps {
name: string,
labels: Record<string, string>,
data: Record<string, string>,
): Promise<boolean>; // TODO was replaceNamespacedConfigMap
): Promise<boolean>;

/**
* Delete a config map
* @param namespace - for the config map
* @param name - for the config name
*/
delete(namespace: NamespaceName, name: string): Promise<boolean>; // TODO was deleteNamespacedConfigMap
delete(namespace: NamespaceName, name: string): Promise<boolean>;

/**
* Check if a config map exists
Expand Down
31 changes: 0 additions & 31 deletions src/core/kube/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,37 +302,6 @@ export interface K8 {
*/
deleteSecret(name: string, namespace: NamespaceName): Promise<boolean>;

/**
* @param name - name of the configmap
* @returns the configmap if found
* @throws SoloError - if the response if not found or the response is not OK
*/
getNamespacedConfigMap(name: string): Promise<k8s.V1ConfigMap>;

/**
* @param name - for the config name
* @param labels - for the config metadata
* @param data - to contain in the config
*/
createNamespacedConfigMap(
name: string,
labels: Record<string, string>,
data: Record<string, string>,
): Promise<boolean>;

/**
* @param name - for the config name
* @param labels - for the config metadata
* @param data - to contain in the config
*/
replaceNamespacedConfigMap(
name: string,
labels: Record<string, string>,
data: Record<string, string>,
): Promise<boolean>;

deleteNamespacedConfigMap(name: string, namespace: NamespaceName): Promise<boolean>;

createNamespacedLease(
namespace: NamespaceName,
leaseName: string,
Expand Down
26 changes: 0 additions & 26 deletions src/core/kube/k8_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,32 +365,6 @@ export class K8Client extends K8ClientBase implements K8 {
return resp.response.statusCode === StatusCodes.OK;
}

/* ------------- ConfigMap ------------- */

public async getNamespacedConfigMap(name: string): Promise<k8s.V1ConfigMap> {
return this.configMaps().read(this.getNamespace(), name);
}

public async createNamespacedConfigMap(
name: string,
labels: Record<string, string>,
data: Record<string, string>,
): Promise<boolean> {
return this.configMaps().create(this.getNamespace(), name, labels, data);
}

public async replaceNamespacedConfigMap(
name: string,
labels: Record<string, string>,
data: Record<string, string>,
): Promise<boolean> {
return this.configMaps().replace(this.getNamespace(), name, labels, data);
}

public async deleteNamespacedConfigMap(name: string, namespace: NamespaceName): Promise<boolean> {
return this.configMaps().delete(namespace, name);
}

// --------------------------------------- LEASES --------------------------------------- //

public async createNamespacedLease(
Expand Down
4 changes: 3 additions & 1 deletion test/unit/commands/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {DependencyManager} from '../../../src/core/dependency_managers/index.js'
import {LocalConfig} from '../../../src/core/config/local_config.js';
import {resetForTest} from '../../test_container.js';
import {ClusterChecks} from '../../../src/core/cluster_checks.js';
import {type K8ClientConfigMaps} from '../../../src/core/kube/k8_client/k8_client_config_maps.js';

const testName = 'network-cmd-unit';
const argv = getDefaultArgv();
Expand Down Expand Up @@ -60,7 +61,8 @@ describe('NetworkCommand unit tests', () => {

opts.k8 = sinon.stub() as unknown as K8;
opts.k8.hasNamespace = sinon.stub().returns(true);
opts.k8.getNamespacedConfigMap = sinon.stub().returns(null);
opts.k8.configMaps = sinon.stub() as unknown as K8ClientConfigMaps;
opts.k8.configMaps.read = sinon.stub();
opts.k8.waitForPodReady = sinon.stub();
opts.k8.waitForPods = sinon.stub();
opts.k8.readNamespacedLease = sinon.stub();
Expand Down

0 comments on commit 76435a4

Please sign in to comment.