Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

fix(resource-monitor): get DW Namespace name from ENV variable #1269

Merged
merged 2 commits into from
Dec 8, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class CheWorkspaceMainImpl implements CheWorkspaceMain {
);
}

$getCurrentNamespace(): Promise<string> {
return this.workspaceService.getCurrentNamespace();
}

async $getById(workspaceId: string): Promise<cheApi.workspace.Workspace> {
return this.workspaceService.getWorkspaceById(workspaceId).then(
workspace => workspace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface CheWorkspace {

export interface CheWorkspaceMain {
$getCurrentWorkspace(): Promise<cheApi.workspace.Workspace>;
$getCurrentNamespace(): Promise<string>;
// getAll(): Promise<Workspace[]>;
// getAllByNamespace(namespace: string): Promise<Workspace[]>;
$getById(workspaceId: string): Promise<cheApi.workspace.Workspace>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export function createAPIFactory(rpc: RPCProtocol): CheApiFactory {
getCurrentWorkspace(): Promise<cheApi.workspace.Workspace> {
return cheWorkspaceImpl.getCurrentWorkspace();
},
getCurrentNamespace(): Promise<string> {
return cheWorkspaceImpl.getCurrentNamespace();
},
getAll(): Promise<cheApi.workspace.Workspace[]> {
return cheWorkspaceImpl.getAll();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export class CheWorkspaceImpl implements CheWorkspace {
throw new Error('Method not implemented.');
}

getCurrentNamespace(): Promise<string> {
return this.workspaceMain.$getCurrentNamespace();
}

getAll(): Promise<cheApi.workspace.Workspace[]> {
throw new Error('Method not implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare module '@eclipse-che/plugin' {

export namespace workspace {
export function getCurrentWorkspace(): Promise<cheApi.workspace.Workspace>;
export function getCurrentNamespace(): Promise<string>;
export function getAll(): Promise<cheApi.workspace.Workspace[]>;
export function getAllByNamespace(namespace: string): Promise<cheApi.workspace.Workspace[]>;
export function getById(workspaceId: string): Promise<cheApi.workspace.Workspace>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface WorkspaceSettings {

export const WorkspaceService = Symbol('WorkspaceService');
export interface WorkspaceService {
getCurrentNamespace(): Promise<string>;
getCurrentWorkspaceId(): Promise<string>;
currentWorkspace(): Promise<Workspace>;
getWorkspaceById(workspaceId: string): Promise<Workspace>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class CheServerWorkspaceServiceImpl implements WorkspaceService {
@inject(CheServerRemoteApiImpl)
private cheServerRemoteApiImpl: CheServerRemoteApiImpl;

private INFRASTRUCTURE_NAMESPACE = 'infrastructureNamespace';

/**
* Workspace client based variables.
*
Expand Down Expand Up @@ -52,6 +54,11 @@ export class CheServerWorkspaceServiceImpl implements WorkspaceService {
return this.workspaceId;
}

public async getCurrentNamespace(): Promise<string> {
const workspace = await this.currentWorkspace();
return workspace.attributes?.[this.INFRASTRUCTURE_NAMESPACE] || workspace.namespace || '';
}

public async currentWorkspace(): Promise<Workspace> {
return this.cheServerRemoteApiImpl.getAPI().getById<Workspace>(this.workspaceId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* @author Valerii Svydenko
*/
const che: any = {};
che.devfile = {};
che.devfile.metadata = {};
che.workspace = {};
che.k8s = {};
module.exports = che;
4 changes: 1 addition & 3 deletions plugins/resource-monitor-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ export async function start(context: theia.PluginContext): Promise<void> {
}

export async function getNamespace(): Promise<string> {
// get namespace from devfile service
const devfile = await che.devfile.get();
return devfile.metadata?.attributes ? devfile.metadata.attributes.infrastructureNamespace : '';
return await che.workspace.getCurrentNamespace();
}
20 changes: 3 additions & 17 deletions plugins/resource-monitor-plugin/tests/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,14 @@ const context: theia.PluginContext = {

describe('Test Plugin', () => {
jest.mock('../src/inversify-binding');
const devfileMock = jest.fn();
const getCurrentNamespace = jest.fn();
let oldBindings: any;
let initBindings: jest.Mock;

beforeEach(() => {
// Prepare Namespace
che.devfile.get = devfileMock;
const attributes = { infrastructureNamespace: 'che-namespace' };
const devfile = {
metadata: {
attributes,
},
};
devfileMock.mockReturnValue(devfile);
che.workspace.getCurrentNamespace = getCurrentNamespace;
getCurrentNamespace.mockReturnValue('che-namespace');
oldBindings = InversifyBinding.prototype.initBindings;
initBindings = jest.fn();
InversifyBinding.prototype.initBindings = initBindings;
Expand All @@ -107,13 +101,5 @@ describe('Test Plugin', () => {
const namespace = await plugin.getNamespace();
expect(namespace).toBe('che-namespace');
});
test('read che namespace from workspace service if no infrastructureNamespace attribute in devile metadata', async () => {
const devfile = {
metadata: {},
};
devfileMock.mockReturnValue(devfile);
const namespace = await plugin.getNamespace();
expect(namespace).toBe('');
});
});
});