Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 01178-save-reusable-s…
Browse files Browse the repository at this point in the history
…olo-flags-in-remoteconfig

# Conflicts:
#	src/core/config/remote/remote_config_manager.ts
  • Loading branch information
instamenta committed Jan 21, 2025
2 parents 95dff03 + 89c557a commit 8b961b3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
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 @@ export class ClusterCommandTasks {
if (!context) {
const isQuiet = self.parent.getConfigManager().getFlag(flags.quiet);
if (isQuiet) {
context = self.parent.getK8().getKubeConfig().currentContext;
context = self.parent.getK8().getCurrentContext();
} else {
context = await self.promptForContext(parentTask, cluster);
}
Expand Down Expand Up @@ -96,11 +96,12 @@ export class ClusterCommandTasks {
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 @@ export class ClusterCommandTasks {
} 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 @@ export class ClusterCommandTasks {
) {
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 @@ export class ClusterCommandTasks {
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 @@ export class ClusterCommandTasks {
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 @@ export class LocalConfig implements LocalConfigData {

if (!deploymentClusters) {
if (isQuiet) {
deploymentClusters = k8.getKubeConfig().getCurrentCluster().name;
deploymentClusters = k8.getCurrentClusterName();
} else {
deploymentClusters = await flags.deploymentClusters.prompt(task, deploymentClusters);
}
Expand Down Expand Up @@ -221,7 +221,7 @@ export class LocalConfig implements LocalConfigData {
}
self.configManager.setFlag(flags.context, promptedContexts.join(','));
} else {
const context = k8.getKubeConfig().getCurrentContext();
const context = k8.getCurrentContext();
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 @@ -164,7 +164,7 @@ export class RemoteConfigManager {
try {
await RemoteConfigValidator.validateComponents(this.remoteConfig.components, this.k8);
} catch {
throw new SoloError(ErrorMessages.REMOTE_CONFIG_IS_INVALID(this.k8.getKubeConfig().getCurrentCluster().name));
throw new SoloError(ErrorMessages.REMOTE_CONFIG_IS_INVALID(this.k8.getCurrentClusterName()));
}
return this.remoteConfig;
}
Expand Down Expand Up @@ -348,7 +348,7 @@ export class RemoteConfigManager {
private setDefaultContextIfNotSet(): void {
if (this.configManager.hasFlag(flags.context)) return;

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

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 {Duration} from './time/duration.js';
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 @@ export class K8 {
this.init();
}

getKubeConfig() {
return this.kubeConfig;
}

init() {
this.kubeConfig = new k8s.KubeConfig();
this.kubeConfig.loadFromDefault();
Expand Down Expand Up @@ -1729,11 +1726,29 @@ export class K8 {
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();
}

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

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

public getCurrentClusterName(): string {
const currentCluster = this.kubeConfig.getCurrentCluster();
if (!currentCluster) return '';
return currentCluster.name;
}
}
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

0 comments on commit 8b961b3

Please sign in to comment.