Skip to content
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
37 changes: 20 additions & 17 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
*-----------------------------------------------------------------------------------------------*/

import {
TreeDataProvider,
TreeItem,
Disposable,
Event,
EventEmitter,
Disposable,
ThemeIcon,
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
TreeView,
window,
Uri,
commands,
extensions,
version,
commands,
Uri,
TreeItemCollapsibleState,
ThemeIcon,
window,
} from 'vscode';

import * as path from 'path';
import * as fs from 'fs';
import * as path from 'path';
import { Platform } from './util/platform';

import { WatchUtil, FileContentChangeNotifier } from './util/watch';
import { KubeConfigUtils } from './util/kubeUtils';
import { vsCommand } from './vscommand';
import { KubernetesObject, Context } from '@kubernetes/client-node';
import { Context, KubernetesObject } from '@kubernetes/client-node';
import { CliChannel } from './cli';
import { Command } from './odo/command';
import { newInstance, Odo3 } from './odo3';
import { Odo3, newInstance } from './odo3';
import { KubeConfigUtils } from './util/kubeUtils';
import { Progress } from './util/progress';
import { FileContentChangeNotifier, WatchUtil } from './util/watch';
import { vsCommand } from './vscommand';

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

Expand Down Expand Up @@ -198,22 +198,25 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
// * example is sandbox context created when login to sandbox first time
// (3) there is namespace set in context and namespace exists in the cluster
// (4) there is namespace set in context and namespace does not exist in the cluster
const pOrNs = await this.odo3.getNamespaces();
const namespaces = await this.odo3.getNamespaces();
Comment thread
rgrunber marked this conversation as resolved.
if (this.kubeContext.namespace) {
if (pOrNs.find(item => item?.metadata.name === this.kubeContext.namespace)) {
if (namespaces.find(item => item?.metadata.name === this.kubeContext.namespace)) {
result = [{
kind: 'project',
metadata: {
name: this.kubeContext.namespace,
},
} as KubernetesObject]
} else if (namespaces.length >= 1) {
// switch to first accessible namespace
await this.odo3.setNamespace(namespaces[0].metadata.name);
} else {
result = [CREATE_OR_SET_PROJECT_ITEM]
}
} else {
// get list of projects or namespaces
// find default namespace
if (pOrNs.find(item => item?.metadata.name === 'default')) {
if (namespaces.find(item => item?.metadata.name === 'default')) {
result = [{
kind: 'project',
metadata: {
Expand Down
4 changes: 4 additions & 0 deletions src/odo/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,8 @@ export class Command {
static analyze(): CommandText {
return new CommandText('odo analyze -o json');
}

static setNamespace(namespace: string) {
return new CommandText('odo set namespace', namespace);
}
}
13 changes: 11 additions & 2 deletions src/odo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import { KubernetesObject } from '@kubernetes/client-node';
import { CommandText } from './base/command';
import { CliChannel } from './cli';
import { Command as CommonCommand, loadItems } from './k8s/common';
import { DeploymentConfig } from './k8s/deploymentConfig';
import { Command as DeploymentCommand } from './k8s/deployment';
import { ComponentDescription } from './odo/componentTypeDescription';
import { DeploymentConfig } from './k8s/deploymentConfig';
import { Command } from './odo/command';
import { ComponentDescription } from './odo/componentTypeDescription';

export interface Odo3 {
getNamespaces(): Promise<KubernetesObject[]>;
getDeployments(): Promise<KubernetesObject[]>;
getDeploymentConfigs(): Promise<KubernetesObject[]>;

setNamespace(newNamespace: string): Promise<void>;

describeComponent(contextPath: string): Promise<ComponentDescription | undefined>;
}

Expand Down Expand Up @@ -50,6 +53,12 @@ export class Odo3Impl implements Odo3 {
]);
}

async setNamespace(newNamespace: string): Promise<void> {
await CliChannel.getInstance().executeTool(
Command.setNamespace(newNamespace), undefined, true
);
}

public async describeComponent(contextPath: string): Promise<ComponentDescription | undefined> {
try {
const describeCmdResult = await CliChannel.getInstance().executeTool(
Expand Down
4 changes: 2 additions & 2 deletions test/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as fs from 'fs';
import * as glob from 'glob';
import * as paths from 'path';
import { TestRunnerOptions, CoverageRunner } from '../coverage';
import { CoverageRunner, TestRunnerOptions } from '../coverage';

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

Expand Down Expand Up @@ -35,7 +35,7 @@ export function run(): Promise<void> {
return new Promise((resolve, reject) => {
const testsRoot = paths.resolve(__dirname);
const coverageRunner = loadCoverageRunner(testsRoot);
glob('**/odo.test.js', { cwd: testsRoot }, (error, files): void => {
glob('**/odo*.test.js', { cwd: testsRoot }, (error, files): void => {
if (error) {
reject(error);
} else {
Expand Down
46 changes: 46 additions & 0 deletions test/integration/odo3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import { expect } from 'chai';
import { CommandText } from '../../src/base/command';
import * as Odo from '../../src/odo';
import { Command } from '../../src/odo/command';
import * as Odo3 from '../../src/odo3';

suite('odo 3 integration', () => {

const clusterUrl = process.env.CLUSTER_URL || 'https://192.168.42.80:8443';
const username = process.env.CLUSTER_USER || 'developer';
const password = process.env.CLUSTER_PASSWORD || 'developer';

const odo = Odo.getInstance();
const odo3 = Odo3.newInstance();

let project1: Odo.OpenShiftObject;
let project2: Odo.OpenShiftObject;

setup(async function() {
await odo.execute(Command.odoLoginWithUsernamePassword(clusterUrl, username, password));
project1 = await odo.createProject('myproject1');
project2 = await odo.createProject('myproject2');
});

teardown(async function() {
await odo.deleteProject(project1);
await odo.deleteProject(project2);
await odo.execute(Command.odoLogout());
});

test('get and set namespaces', async function () {
const namespaces = await odo3.getNamespaces();
const namespaceNames = namespaces.map(kObj => kObj.metadata.name);
expect(namespaceNames).to.contain('myproject1');
expect(namespaceNames).to.contain('myproject2');
await odo3.setNamespace('myproject2');
const exitData = await odo.execute(new CommandText('oc status'));
expect(exitData.stdout).to.contain('myproject2');
});

});