Skip to content

Commit 571fbc8

Browse files
datho7561rgrunber
authored andcommitted
Switch to first available namespace
When the currently configured namespace doesn't exist, switch to the first available namespace. Closes #2180 Signed-off-by: David Thompson <[email protected]>
1 parent 47d69dd commit 571fbc8

File tree

5 files changed

+83
-21
lines changed

5 files changed

+83
-21
lines changed

src/explorer.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@
44
*-----------------------------------------------------------------------------------------------*/
55

66
import {
7-
TreeDataProvider,
8-
TreeItem,
7+
Disposable,
98
Event,
109
EventEmitter,
11-
Disposable,
10+
ThemeIcon,
11+
TreeDataProvider,
12+
TreeItem,
13+
TreeItemCollapsibleState,
1214
TreeView,
13-
window,
15+
Uri,
16+
commands,
1417
extensions,
1518
version,
16-
commands,
17-
Uri,
18-
TreeItemCollapsibleState,
19-
ThemeIcon,
19+
window,
2020
} from 'vscode';
2121

22-
import * as path from 'path';
2322
import * as fs from 'fs';
23+
import * as path from 'path';
2424
import { Platform } from './util/platform';
2525

26-
import { WatchUtil, FileContentChangeNotifier } from './util/watch';
27-
import { KubeConfigUtils } from './util/kubeUtils';
28-
import { vsCommand } from './vscommand';
29-
import { KubernetesObject, Context } from '@kubernetes/client-node';
26+
import { Context, KubernetesObject } from '@kubernetes/client-node';
3027
import { CliChannel } from './cli';
3128
import { Command } from './odo/command';
32-
import { newInstance, Odo3 } from './odo3';
29+
import { Odo3, newInstance } from './odo3';
30+
import { KubeConfigUtils } from './util/kubeUtils';
3331
import { Progress } from './util/progress';
32+
import { FileContentChangeNotifier, WatchUtil } from './util/watch';
33+
import { vsCommand } from './vscommand';
3434

3535
const kubeConfigFolder: string = path.join(Platform.getUserHomePath(), '.kube');
3636

@@ -199,22 +199,25 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
199199
// * example is sandbox context created when login to sandbox first time
200200
// (3) there is namespace set in context and namespace exists in the cluster
201201
// (4) there is namespace set in context and namespace does not exist in the cluster
202-
const pOrNs = await this.odo3.getNamespaces();
202+
const namespaces = await this.odo3.getNamespaces();
203203
if (this.kubeContext.namespace) {
204-
if (pOrNs.find(item => item?.metadata.name === this.kubeContext.namespace)) {
204+
if (namespaces.find(item => item?.metadata.name === this.kubeContext.namespace)) {
205205
result = [{
206206
kind: 'project',
207207
metadata: {
208208
name: this.kubeContext.namespace,
209209
},
210210
} as KubernetesObject]
211+
} else if (namespaces.length >= 1) {
212+
// switch to first accessible namespace
213+
await this.odo3.setNamespace(namespaces[0].metadata.name);
211214
} else {
212215
result = [CREATE_OR_SET_PROJECT_ITEM]
213216
}
214217
} else {
215218
// get list of projects or namespaces
216219
// find default namespace
217-
if (pOrNs.find(item => item?.metadata.name === 'default')) {
220+
if (namespaces.find(item => item?.metadata.name === 'default')) {
218221
result = [{
219222
kind: 'project',
220223
metadata: {

src/odo/command.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,8 @@ export class Command {
461461
static analyze(): CommandText {
462462
return new CommandText('odo analyze -o json');
463463
}
464+
465+
static setNamespace(namespace: string) {
466+
return new CommandText('odo set namespace', namespace);
467+
}
464468
}

src/odo3.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ import { KubernetesObject } from '@kubernetes/client-node';
77
import { CommandText } from './base/command';
88
import { CliChannel } from './cli';
99
import { Command as CommonCommand, loadItems } from './k8s/common';
10-
import { DeploymentConfig } from './k8s/deploymentConfig';
1110
import { Command as DeploymentCommand } from './k8s/deployment';
12-
import { ComponentDescription } from './odo/componentTypeDescription';
11+
import { DeploymentConfig } from './k8s/deploymentConfig';
1312
import { Command } from './odo/command';
13+
import { ComponentDescription } from './odo/componentTypeDescription';
1414

1515
export interface Odo3 {
1616
getNamespaces(): Promise<KubernetesObject[]>;
1717
getDeployments(): Promise<KubernetesObject[]>;
1818
getDeploymentConfigs(): Promise<KubernetesObject[]>;
19+
20+
setNamespace(newNamespace: string): Promise<void>;
21+
1922
describeComponent(contextPath: string): Promise<ComponentDescription | undefined>;
2023
}
2124

@@ -50,6 +53,12 @@ export class Odo3Impl implements Odo3 {
5053
]);
5154
}
5255

56+
async setNamespace(newNamespace: string): Promise<void> {
57+
await CliChannel.getInstance().executeTool(
58+
Command.setNamespace(newNamespace), undefined, true
59+
);
60+
}
61+
5362
public async describeComponent(contextPath: string): Promise<ComponentDescription | undefined> {
5463
try {
5564
const describeCmdResult = await CliChannel.getInstance().executeTool(

test/integration/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as fs from 'fs';
77
import * as glob from 'glob';
88
import * as paths from 'path';
9-
import { TestRunnerOptions, CoverageRunner } from '../coverage';
9+
import { CoverageRunner, TestRunnerOptions } from '../coverage';
1010

1111
require('source-map-support').install();
1212

@@ -35,7 +35,7 @@ export function run(): Promise<void> {
3535
return new Promise((resolve, reject) => {
3636
const testsRoot = paths.resolve(__dirname);
3737
const coverageRunner = loadCoverageRunner(testsRoot);
38-
glob('**/odo.test.js', { cwd: testsRoot }, (error, files): void => {
38+
glob('**/odo*.test.js', { cwd: testsRoot }, (error, files): void => {
3939
if (error) {
4040
reject(error);
4141
} else {

test/integration/odo3.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
4+
*-----------------------------------------------------------------------------------------------*/
5+
6+
import { expect } from 'chai';
7+
import { CommandText } from '../../src/base/command';
8+
import * as Odo from '../../src/odo';
9+
import { Command } from '../../src/odo/command';
10+
import * as Odo3 from '../../src/odo3';
11+
12+
suite('odo 3 integration', () => {
13+
14+
const clusterUrl = process.env.CLUSTER_URL || 'https://192.168.42.80:8443';
15+
const username = process.env.CLUSTER_USER || 'developer';
16+
const password = process.env.CLUSTER_PASSWORD || 'developer';
17+
18+
const odo = Odo.getInstance();
19+
const odo3 = Odo3.newInstance();
20+
21+
let project1: Odo.OpenShiftObject;
22+
let project2: Odo.OpenShiftObject;
23+
24+
setup(async function() {
25+
await odo.execute(Command.odoLoginWithUsernamePassword(clusterUrl, username, password));
26+
project1 = await odo.createProject('myproject1');
27+
project2 = await odo.createProject('myproject2');
28+
});
29+
30+
teardown(async function() {
31+
await odo.deleteProject(project1);
32+
await odo.deleteProject(project2);
33+
await odo.execute(Command.odoLogout());
34+
});
35+
36+
test('get and set namespaces', async function () {
37+
const namespaces = await odo3.getNamespaces();
38+
const namespaceNames = namespaces.map(kObj => kObj.metadata.name);
39+
expect(namespaceNames).to.contain('myproject1');
40+
expect(namespaceNames).to.contain('myproject2');
41+
await odo3.setNamespace('myproject2');
42+
const exitData = await odo.execute(new CommandText('oc status'));
43+
expect(exitData.stdout).to.contain('myproject2');
44+
});
45+
46+
});

0 commit comments

Comments
 (0)