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

fix: remove k8.getKubeConfig #1182

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 9 additions & 8 deletions src/commands/cluster/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
if (!context) {
const isQuiet = self.parent.getConfigManager().getFlag(flags.quiet);
if (isQuiet) {
context = self.parent.getK8().getKubeConfig().currentContext;
context = self.parent.getK8().getCurrentContext();

Check warning on line 58 in src/commands/cluster/tasks.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/cluster/tasks.ts#L58

Added line #L58 was not covered by tests
} else {
context = await self.promptForContext(parentTask, cluster);
}
Expand Down Expand Up @@ -96,11 +96,12 @@
title: 'Read clusters from remote config',
task: async (ctx, task) => {
const localConfig = this.parent.getLocalConfig();
const currentCluster = this.parent.getK8().getKubeConfig().getCurrentCluster();
const currentCluster = this.parent.getK8().getCurrentCluster();
const currentClusterName = this.parent.getK8().getCurrentClusterName();
const currentRemoteConfig: RemoteConfigDataWrapper = await this.parent.getRemoteConfigManager().get();
const subTasks = [];
const remoteConfigClusters = Object.keys(currentRemoteConfig.clusters);
const otherRemoteConfigClusters: string[] = remoteConfigClusters.filter(c => c !== currentCluster.name);
const otherRemoteConfigClusters: string[] = remoteConfigClusters.filter(c => c !== currentClusterName);

// Validate connections for the other clusters
for (const cluster of otherRemoteConfigClusters) {
Expand Down Expand Up @@ -163,7 +164,7 @@
} else if (!localConfig.clusterContextMapping[cluster]) {
// In quiet mode, use the currently selected context to update the mapping
if (isQuiet) {
localConfig.clusterContextMapping[cluster] = this.parent.getK8().getKubeConfig().getCurrentContext();
localConfig.clusterContextMapping[cluster] = this.parent.getK8().getCurrentContext();
}

// Prompt the user to select a context if mapping value is missing
Expand All @@ -186,7 +187,7 @@
) {
let selectedContext;
if (isQuiet) {
selectedContext = this.parent.getK8().getKubeConfig().getCurrentContext();
selectedContext = this.parent.getK8().getCurrentContext();
} else {
selectedContext = await this.promptForContext(task, selectedCluster);
localConfig.clusterContextMapping[selectedCluster] = selectedContext;
Expand Down Expand Up @@ -304,8 +305,8 @@
else {
// Add the deployment to the LocalConfig with the currently selected cluster and context in KubeConfig
if (isQuiet) {
selectedContext = this.parent.getK8().getKubeConfig().getCurrentContext();
selectedCluster = this.parent.getK8().getKubeConfig().getCurrentCluster().name;
selectedContext = this.parent.getK8().getCurrentContext();
selectedCluster = this.parent.getK8().getCurrentClusterName();
localConfig.deployments[deploymentName] = {
clusters: [selectedCluster],
};
Expand Down Expand Up @@ -364,7 +365,7 @@
getClusterInfo() {
return new Task('Get cluster info', async (ctx: any, task: ListrTaskWrapper<any, any, any>) => {
try {
const cluster = this.parent.getK8().getKubeConfig().getCurrentCluster();
const cluster = this.parent.getK8().getCurrentCluster();
this.parent.logger.showJSON(`Cluster Information (${cluster.name})`, cluster);
this.parent.logger.showUser('\n');
} catch (e: Error | unknown) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/config/local_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@

if (!deploymentClusters) {
if (isQuiet) {
deploymentClusters = k8.getKubeConfig().getCurrentCluster().name;
deploymentClusters = k8.getCurrentClusterName();

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

View check run for this annotation

Codecov / codecov/patch

src/core/config/local_config.ts#L192

Added line #L192 was not covered by tests
} else {
deploymentClusters = await flags.deploymentClusters.prompt(task, deploymentClusters);
}
Expand Down Expand Up @@ -221,7 +221,7 @@
}
self.configManager.setFlag(flags.context, promptedContexts.join(','));
} else {
const context = k8.getKubeConfig().getCurrentContext();
const context = k8.getCurrentContext();

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

View check run for this annotation

Codecov / codecov/patch

src/core/config/local_config.ts#L224

Added line #L224 was not covered by tests
for (const cluster of parsedClusters) {
self.clusterContextMapping[cluster] = context;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/config/remote/remote_config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
try {
await RemoteConfigValidator.validateComponents(this.remoteConfig.components, this.k8);
} catch (e) {
throw new SoloError(ErrorMessages.REMOTE_CONFIG_IS_INVALID(this.k8.getKubeConfig().getCurrentCluster().name));
throw new SoloError(ErrorMessages.REMOTE_CONFIG_IS_INVALID(this.k8.getCurrentClusterName()));

Check warning on line 164 in src/core/config/remote/remote_config_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/remote_config_manager.ts#L164

Added line #L164 was not covered by tests
}
return this.remoteConfig;
}
Expand Down Expand Up @@ -338,7 +338,7 @@
private setDefaultContextIfNotSet(): void {
if (this.configManager.hasFlag(flags.context)) return;

const context = this.k8.getKubeConfig().currentContext;
const context = this.k8.getCurrentContext();

Check warning on line 341 in src/core/config/remote/remote_config_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/remote_config_manager.ts#L341

Added line #L341 was not covered by tests

if (!context) {
this.logger.error("Context is not passed and default one can't be acquired", this.localConfig);
Expand Down
25 changes: 20 additions & 5 deletions src/core/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import {inject, injectable} from 'tsyringe-neo';
import {patchInject} from './container_helper.js';
import type {Namespace} from './config/remote/types.js';
import {type Cluster} from '@kubernetes/client-node/dist/config_types.js';

interface TDirectoryData {
directory: boolean;
Expand Down Expand Up @@ -78,10 +79,6 @@
this.init();
}

getKubeConfig() {
return this.kubeConfig;
}

init() {
this.kubeConfig = new k8s.KubeConfig();
this.kubeConfig.loadFromDefault();
Expand Down Expand Up @@ -1729,11 +1726,29 @@
this.logger.debug(`getNodeState(${pod.metadata.name}): ...end`);
}

setCurrentContext(context: string) {
public setCurrentContext(context: string) {
this.kubeConfig.setCurrentContext(context);

// Reinitialize clients
this.kubeClient = this.kubeConfig.makeApiClient(k8s.CoreV1Api);
this.coordinationApiClient = this.kubeConfig.makeApiClient(k8s.CoordinationV1Api);
}

public getCurrentContext(): string {
return this.kubeConfig.getCurrentContext();
}

Check warning on line 1739 in src/core/k8.ts

View check run for this annotation

Codecov / codecov/patch

src/core/k8.ts#L1738-L1739

Added lines #L1738 - L1739 were not covered by tests

public getCurrentContextObject(): Context {
return this.kubeConfig.getContextObject(this.getCurrentContext());
}

Check warning on line 1743 in src/core/k8.ts

View check run for this annotation

Codecov / codecov/patch

src/core/k8.ts#L1742-L1743

Added lines #L1742 - L1743 were not covered by tests

public getCurrentCluster(): Cluster {
return this.kubeConfig.getCurrentCluster();
}

public getCurrentClusterName(): string {
const currentCluster = this.kubeConfig.getCurrentCluster();
if (!currentCluster) return '';
return currentCluster.name;
}

Check warning on line 1753 in src/core/k8.ts

View check run for this annotation

Codecov / codecov/patch

src/core/k8.ts#L1750-L1753

Added lines #L1750 - L1753 were not covered by tests
}
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ export function main(argv: any) {

// set cluster and namespace in the global configManager from kubernetes context
// so that we don't need to prompt the user
const kubeConfig = k8.getKubeConfig();
const context = kubeConfig.getContextObject(kubeConfig.getCurrentContext());
const cluster = kubeConfig.getCurrentCluster();
const context = k8.getCurrentContextObject();
const currentClusterName = k8.getCurrentClusterName();

const opts: Opts = {
logger,
Expand All @@ -106,7 +105,7 @@ export function main(argv: any) {
configManager.reset();
}

const clusterName = configManager.getFlag(flags.clusterName) || cluster.name;
const clusterName = configManager.getFlag(flags.clusterName) || currentClusterName;

if (context.namespace) {
configManager.setFlag(flags.namespace, context.namespace);
Expand Down
14 changes: 9 additions & 5 deletions test/unit/commands/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,28 @@ describe('ClusterCommand unit tests', () => {
k8Stub.testClusterConnection.resolves(true);
}

const kubeConfigStub = sandbox.createStubInstance(KubeConfig);
kubeConfigStub.getCurrentContext.returns('context-from-kubeConfig');
kubeConfigStub.getCurrentCluster.returns({
const kubeConfigClusterObject = {
name: 'cluster-3',
caData: 'caData',
caFile: 'caFile',
server: 'server-3',
skipTLSVerify: true,
tlsServerName: 'tls-3',
} as Cluster);
} as Cluster;

const kubeConfigStub = sandbox.createStubInstance(KubeConfig);
kubeConfigStub.getCurrentContext.returns('context-from-kubeConfig');
kubeConfigStub.getCurrentCluster.returns(kubeConfigClusterObject);

remoteConfigManagerStub = sandbox.createStubInstance(RemoteConfigManager);
remoteConfigManagerStub.modify.callsFake(async callback => {
await callback(remoteConfig);
});
remoteConfigManagerStub.get.resolves(remoteConfig);

k8Stub.getKubeConfig.returns(kubeConfigStub);
k8Stub.getCurrentClusterName.returns(kubeConfigClusterObject.name);
k8Stub.getCurrentCluster.returns(kubeConfigClusterObject);
k8Stub.getCurrentContext.returns('context-from-kubeConfig');

const configManager = sandbox.createStubInstance(ConfigManager);

Expand Down
Loading