Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix(circular-dependency): Remove warning about circular dependency wo…
Browse files Browse the repository at this point in the history
…rkspaceService-->devFileService --> workspaceService

Change-Id: I4e22ca592de046578221956e63638186507202a0
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf authored and monaka committed Mar 6, 2021
1 parent 9a6d41f commit 5cac2cd
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { WorkspaceService, cheWorkspaceServicePath } from '@eclipse-che/theia-re
import { ContainerModule } from 'inversify';
import { K8SServiceImpl } from './k8s-service-impl';
import { K8sCertificateServiceImpl } from './k8s-certificate-service-impl';
import { K8sDevWorkspaceEnvVariables } from './k8s-devworkspace-env-variables';
import { K8sDevfileServiceImpl } from './k8s-devfile-service-impl';
import { K8sEndpointServiceImpl } from './k8s-endpoint-service-impl';
import { K8sFactoryServiceImpl } from './k8s-factory-service-impl';
Expand All @@ -52,6 +53,7 @@ export default new ContainerModule(bind => {
bind(K8SServiceImpl).toSelf().inSingletonScope();
bind(K8sDevfileServiceImpl).toSelf().inSingletonScope();
bind(K8sEndpointServiceImpl).toSelf().inSingletonScope();
bind(K8sDevWorkspaceEnvVariables).toSelf().inSingletonScope();

bind(CertificateService).to(K8sCertificateServiceImpl).inSingletonScope();
bind(FactoryService).to(K8sFactoryServiceImpl).inSingletonScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import {
import { inject, injectable } from 'inversify';

import { K8SServiceImpl } from './k8s-service-impl';
import { K8sDevWorkspaceEnvVariables } from './k8s-devworkspace-env-variables';
import { V1Pod } from '@kubernetes/client-node';
import { WorkspaceService } from '@eclipse-che/theia-remote-api/lib/common/workspace-service';

@injectable()
export class K8sDevfileServiceImpl implements DevfileService {
@inject(K8SServiceImpl)
private k8SService: K8SServiceImpl;

@inject(WorkspaceService)
private workspaceService: WorkspaceService;
@inject(K8sDevWorkspaceEnvVariables)
private env: K8sDevWorkspaceEnvVariables;

async getRaw(): Promise<string> {
const devfile = await this.get();
Expand All @@ -41,10 +41,13 @@ export class K8sDevfileServiceImpl implements DevfileService {
const customObjectsApi = this.k8SService.makeApiClient(k8s.CustomObjectsApi);
const group = 'workspace.devfile.io';
const version = 'v1alpha2';
const workspace = await this.workspaceService.currentWorkspace();
const namespace = workspace.namespace || '';
const name = workspace.name || '';
const response = await customObjectsApi.getNamespacedCustomObject(group, version, namespace, 'devworkspaces', name);
const response = await customObjectsApi.getNamespacedCustomObject(
group,
version,
this.env.getWorkspaceNamespace(),
'devworkspaces',
this.env.getWorkspaceName()
);

// devfile is stored inside the dev workspace inside spec.template object
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -54,19 +57,15 @@ export class K8sDevfileServiceImpl implements DevfileService {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async getWorkspaceRouting(): Promise<any> {
// grab current workspace
const workspace = await this.workspaceService.currentWorkspace();

// get workspace pod
const group = 'controller.devfile.io';
const version = 'v1alpha1';
const namespace = workspace.namespace || '';
const customObjectsApi = this.k8SService.makeApiClient(k8s.CustomObjectsApi);
const labelSelector = `controller.devfile.io/workspace_id=${workspace.id}`;
const labelSelector = `controller.devfile.io/workspace_id=${this.env.getWorkspaceId()}`;
const response = await customObjectsApi.listNamespacedCustomObject(
group,
version,
namespace,
this.env.getWorkspaceNamespace(),
'workspaceroutings',
undefined,
undefined,
Expand Down Expand Up @@ -95,15 +94,11 @@ export class K8sDevfileServiceImpl implements DevfileService {
}

async getWorkspacePod(): Promise<V1Pod> {
// grab current workspace
const workspace = await this.workspaceService.currentWorkspace();

// get workspace pod
const namespace = workspace.namespace || '';
const k8sCoreV1Api = this.k8SService.makeApiClient(k8s.CoreV1Api);
const labelSelector = `controller.devfile.io/workspace_id=${workspace.id}`;
const labelSelector = `controller.devfile.io/workspace_id=${this.env.getWorkspaceId()}`;
const { body } = await k8sCoreV1Api.listNamespacedPod(
namespace,
this.env.getWorkspaceNamespace(),
undefined,
undefined,
undefined,
Expand Down Expand Up @@ -186,9 +181,6 @@ export class K8sDevfileServiceImpl implements DevfileService {
const customObjectsApi = this.k8SService.makeApiClient(k8s.CustomObjectsApi);
const group = 'workspace.devfile.io';
const version = 'v1alpha2';
const workspace = await this.workspaceService.currentWorkspace();
const namespace = workspace.namespace || '';
const name = workspace.name || '';

const patch = [
{
Expand All @@ -205,9 +197,9 @@ export class K8sDevfileServiceImpl implements DevfileService {
await customObjectsApi.patchNamespacedCustomObject(
group,
version,
namespace,
this.env.getWorkspaceNamespace(),
'devworkspaces',
name,
this.env.getWorkspaceName(),
patch,
undefined,
undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**********************************************************************
* Copyright (c) 2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import { injectable } from 'inversify';

/**
* Manage access to env variables defined by a dev workspace
*/
@injectable()
export class K8sDevWorkspaceEnvVariables {
/**
* workspaceId - workspace ID taken from environment variable, always the same at workspace lifecycle
*/
private readonly workspaceId: string;

/**
* workspaceName - workspace name taken from environment variable, always the same at workspace lifecycle
*/
private readonly workspaceName: string;

/**
* workspaceNamespace - workspace namespace taken from environment variable, always the same at workspace lifecycle
*/
private readonly workspaceNamespace: string;

constructor() {
if (process.env.DEVWORKSPACE_ID === undefined) {
console.error('Environment variable DEVWORKSPACE_ID is not set');
} else {
this.workspaceId = process.env.DEVWORKSPACE_ID;
}
if (process.env.DEVWORKSPACE_NAMESPACE === undefined) {
console.error('Environment variable DEVWORKSPACE_NAMESPACE is not set');
} else {
this.workspaceNamespace = process.env.DEVWORKSPACE_NAMESPACE;
}
if (process.env.DEVWORKSPACE_NAME === undefined) {
console.error('Environment variable DEVWORKSPACE_NAME is not set');
} else {
this.workspaceName = process.env.DEVWORKSPACE_NAME;
}
}

getWorkspaceId(): string {
return this.workspaceId;
}

getWorkspaceName(): string {
return this.workspaceName;
}

getWorkspaceNamespace(): string {
return this.workspaceNamespace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,29 @@ import {
import { inject, injectable } from 'inversify';

import { DevfileService } from '@eclipse-che/theia-remote-api/lib/common/devfile-service';
import { K8sDevWorkspaceEnvVariables } from './k8s-devworkspace-env-variables';

@injectable()
export class K8sWorkspaceServiceImpl implements WorkspaceService {
@inject(DevfileService)
private devfileService: DevfileService;

/**
* workspaceId - workspace ID taken from environment variable, always the same at workspace lifecycle
*/
private readonly workspaceId: string;

/**
* workspaceName - workspace name taken from environment variable, always the same at workspace lifecycle
*/
private readonly workspaceName: string;

/**
* workspaceNamespace - workspace namespace taken from environment variable, always the same at workspace lifecycle
*/
private readonly workspaceNamespace: string;

constructor() {
if (process.env.DEVWORKSPACE_ID === undefined) {
console.error('Environment variable DEVWORKSPACE_ID is not set');
} else {
this.workspaceId = process.env.DEVWORKSPACE_ID;
}
if (process.env.DEVWORKSPACE_NAMESPACE === undefined) {
console.error('Environment variable DEVWORKSPACE_NAMESPACE is not set');
} else {
this.workspaceNamespace = process.env.DEVWORKSPACE_NAMESPACE;
}
if (process.env.DEVWORKSPACE_NAME === undefined) {
console.error('Environment variable DEVWORKSPACE_NAME is not set');
} else {
this.workspaceName = process.env.DEVWORKSPACE_NAME;
}
}
@inject(K8sDevWorkspaceEnvVariables)
private env: K8sDevWorkspaceEnvVariables;

public async getCurrentNamespace(): Promise<string> {
return this.workspaceNamespace;
return this.env.getWorkspaceNamespace();
}

public async getCurrentWorkspaceId(): Promise<string> {
return this.workspaceId;
return this.env.getWorkspaceId();
}

public async currentWorkspace(): Promise<Workspace> {
return {
id: this.workspaceId,
name: this.workspaceName,
namespace: this.workspaceNamespace,
id: this.env.getWorkspaceId(),
name: this.env.getWorkspaceName(),
namespace: this.env.getWorkspaceNamespace(),
// running as we're in the pod
status: 'RUNNING',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { Devfile, DevfileComponent, DevfileProject } from '@eclipse-che/theia-re

import { Container } from 'inversify';
import { K8SServiceImpl } from '../..//src/node/k8s-service-impl';
import { K8sDevWorkspaceEnvVariables } from '../../src/node/k8s-devworkspace-env-variables';
import { K8sDevfileServiceImpl } from '../../src/node/k8s-devfile-service-impl';
import { WorkspaceService } from '@eclipse-che/theia-remote-api/lib/common/workspace-service';

describe('Test K8sDevfileServiceImpl', () => {
let container: Container;
Expand All @@ -38,31 +38,34 @@ describe('Test K8sDevfileServiceImpl', () => {
listNamespacedCustomObject: listNamespacedMockCustomObjectMethod,
};

const workspaceServiceCurrentWorkspaceMethod = jest.fn();
const workspaceService = {
currentWorkspace: workspaceServiceCurrentWorkspaceMethod,
const workspaceIdEnvVariablesMethod = jest.fn();
const workspaceNameEnvVariablesMethod = jest.fn();
const workspaceNamespaceEnvVariablesMethod = jest.fn();
const k8sDevWorkspaceEnvVariables = {
getWorkspaceId: workspaceIdEnvVariablesMethod,
getWorkspaceName: workspaceNameEnvVariablesMethod,
getWorkspaceNamespace: workspaceNamespaceEnvVariablesMethod,
} as any;

beforeEach(() => {
jest.restoreAllMocks();
jest.resetAllMocks();
container = new Container();
container.bind(WorkspaceService).toConstantValue(workspaceService);
container.bind(K8sDevWorkspaceEnvVariables).toConstantValue(k8sDevWorkspaceEnvVariables);
container.bind(K8sDevfileServiceImpl).toSelf().inSingletonScope();
container.bind(K8SServiceImpl).toConstantValue(k8sServiceMock);
k8sServiceMakeApiClientMethod.mockReturnValueOnce(customObjectsApiMock);
k8sDevfileServiceImpl = container.get(K8sDevfileServiceImpl);
workspaceNameEnvVariablesMethod.mockReturnValue('fake-workspace-name');
workspaceNamespaceEnvVariablesMethod.mockReturnValue('fake-workspace-namespace');
});

test('get', async () => {
const devWorkspaceJsonPath = path.resolve(__dirname, '..', '_data', 'get-devworkspace-response-body.json');
const devWorkspaceJsonContent = await fs.readFile(devWorkspaceJsonPath, 'utf-8');
const devWorkspaceJson = JSON.parse(devWorkspaceJsonContent);
workspaceServiceCurrentWorkspaceMethod.mockResolvedValue({
name: 'fake-workspace-name',
namespace: 'fake-workspace-namespace',
});

workspaceNameEnvVariablesMethod.mockReturnValue('fake-workspace-name');
workspaceNamespaceEnvVariablesMethod.mockReturnValue('fake-workspace-namespace');
customObjectsApiMockGetNamespacedCustomObjectMethod.mockReturnValue({ body: devWorkspaceJson });

const devfile = await k8sDevfileServiceImpl.get();
Expand Down Expand Up @@ -106,10 +109,6 @@ describe('Test K8sDevfileServiceImpl', () => {
const devWorkspaceJsonPath = path.resolve(__dirname, '..', '_data', 'get-devworkspace-response-body.json');
const devWorkspaceJsonContent = await fs.readFile(devWorkspaceJsonPath, 'utf-8');
const devWorkspaceJson = JSON.parse(devWorkspaceJsonContent);
workspaceServiceCurrentWorkspaceMethod.mockResolvedValue({
name: 'fake-workspace-name',
namespace: 'fake-workspace-namespace',
});

customObjectsApiMockGetNamespacedCustomObjectMethod.mockReturnValue({ body: devWorkspaceJson });

Expand All @@ -133,10 +132,6 @@ describe('Test K8sDevfileServiceImpl', () => {
);
const workspaceRoutingJsonContent = await fs.readFile(workspaceRoutingJsonPath, 'utf-8');
const workspaceRoutingJson = JSON.parse(workspaceRoutingJsonContent);
workspaceServiceCurrentWorkspaceMethod.mockResolvedValue({
name: 'fake-workspace-name',
namespace: 'fake-workspace-namespace',
});

listNamespacedMockCustomObjectMethod.mockReturnValue({ body: workspaceRoutingJson });

Expand Down

0 comments on commit 5cac2cd

Please sign in to comment.