Skip to content

Commit

Permalink
fix: the warning when trying to start an empty workspace (eclipse-che…
Browse files Browse the repository at this point in the history
…#768)

* fix: warning when tries to start an empty workspace

Signed-off-by: Oleksii Orel <oorel@redhat.com>
  • Loading branch information
olexii4 authored and ScrewTSW committed Jul 14, 2023
1 parent 0bc2f09 commit f316254
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
describe('DEVWORKSPACE_METADATA_ANNOTATION attribute', () => {
test('add the attribute with annotation', () => {
const devfile = {
schemaVersion: '2.1.0',
metadata: {
generateName: 'wksp-',
},
Expand All @@ -42,7 +43,7 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {

const newDevfile = prepareDevfile(devfile, factoryId, undefined, false);

expect(newDevfile.metadata.attributes).toEqual({
expect(newDevfile.attributes).toEqual({
[DEVWORKSPACE_METADATA_ANNOTATION]: factorySource,
});
});
Expand All @@ -65,18 +66,18 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
metadata: {
name: 'asdf',
generateName: 'wksp-',
attributes: {
[DEVWORKSPACE_METADATA_ANNOTATION]: customAnnotation,
},
},
attributes: {
[DEVWORKSPACE_METADATA_ANNOTATION]: customAnnotation,
},
} as devfileApi.Devfile;

const newDevfile = prepareDevfile(devfile, factoryId, undefined, false);

expect(newDevfile.metadata.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(
expect(newDevfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(
expect.objectContaining(customAnnotation),
);
expect(newDevfile.metadata.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(
expect(newDevfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(
expect.objectContaining(factorySource),
);
});
Expand All @@ -94,6 +95,7 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
const badMetadataAnnotation = 'bad-metadata-annotation';

const devfile = {
schemaVersion: '2.1.0',
metadata: {
generateName: 'wksp-',
attributes: {
Expand All @@ -104,12 +106,10 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {

const newDevfile = prepareDevfile(devfile, factoryId, undefined, false);

expect(newDevfile.metadata.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).not.toContain(
expect(newDevfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).not.toContain(
badMetadataAnnotation,
);
expect(newDevfile.metadata.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(
factorySource,
);
expect(newDevfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(factorySource);
});

test('update the attribute with annotation - bad DEVWORKSPACE_DEVFILE_SOURCE', () => {
Expand All @@ -127,6 +127,7 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
};

const devfile = {
schemaVersion: '2.1.0',
metadata: {
generateName: 'wksp-',
attributes: {
Expand All @@ -137,19 +138,18 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {

const newDevfile = prepareDevfile(devfile, factoryId, undefined, false);

expect(newDevfile.metadata.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).not.toContain(
expect(newDevfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).not.toContain(
expect.objectContaining(badDevworkspaceDevfileSource),
);
expect(newDevfile.metadata.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(
factorySource,
);
expect(newDevfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]).toEqual(factorySource);
});
});

describe('DevWorkspace name', () => {
it('should not change the name', () => {
const factoryId = 'url=https://devfile-location';
const devfile = {
schemaVersion: '2.1.0',
metadata: {
name: 'wksp-test',
},
Expand All @@ -163,6 +163,7 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
it('should append a suffix to the name', () => {
const factoryId = 'url=https://devfile-location';
const devfile = {
schemaVersion: '2.1.0',
metadata: {
name: 'wksp-test',
},
Expand All @@ -176,6 +177,7 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
it('should generate a new name #1', () => {
const factoryId = 'url=https://devfile-location';
const devfile = {
schemaVersion: '2.1.0',
metadata: {
generateName: 'wksp-',
},
Expand All @@ -189,6 +191,7 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
it('should generate a new name #2', () => {
const factoryId = 'url=https://devfile-location';
const devfile = {
schemaVersion: '2.1.0',
metadata: {
generateName: 'wksp-',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
DEVWORKSPACE_DEVFILE_SOURCE,
DEVWORKSPACE_METADATA_ANNOTATION,
} from '../../../../../../services/workspace-client/devworkspace/devWorkspaceClient';
import { DevfileAdapter } from '../../../../../../services/devfile/adapter';

export type FactorySource = { factory?: { params: string } };

Expand All @@ -30,26 +31,21 @@ export function prepareDevfile(
appendSuffix: boolean,
): devfileApi.Devfile {
const devfile = cloneDeep(_devfile);

// set factory ID
if (!devfile.metadata.attributes) {
devfile.metadata.attributes = {};
}
const attributes = DevfileAdapter.getAttributesFromDevfileV2(devfile);
if (
!devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION] ||
typeof devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION] !== 'object'
!attributes[DEVWORKSPACE_METADATA_ANNOTATION] ||
typeof attributes[DEVWORKSPACE_METADATA_ANNOTATION] !== 'object'
) {
devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION] = {};
attributes[DEVWORKSPACE_METADATA_ANNOTATION] = {};
}
const dwMetadataAnnotations = devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION];
const dwMetadataAnnotations = attributes[DEVWORKSPACE_METADATA_ANNOTATION];
const devfileSourceYaml = dwMetadataAnnotations[DEVWORKSPACE_DEVFILE_SOURCE];
let devfileSource = devfileSourceYaml ? load(devfileSourceYaml) : {};
if (typeof devfileSource !== 'object') {
devfileSource = {};
}
(devfileSource as FactorySource).factory = { params: factoryId };
devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION][DEVWORKSPACE_DEVFILE_SOURCE] =
dump(devfileSource);
attributes[DEVWORKSPACE_METADATA_ANNOTATION][DEVWORKSPACE_DEVFILE_SOURCE] = dump(devfileSource);

// update `metadata.name` in accordance to the policy
if (devfile.metadata.generateName) {
Expand All @@ -63,15 +59,7 @@ export function prepareDevfile(

// propagate storage type
if (storageType === 'ephemeral') {
if (devfile.schemaVersion === '2.0.0') {
devfile.metadata.attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR] = 'ephemeral';
} else {
// for devfiles version 2.1.0 and above
if (!devfile.attributes) {
devfile.attributes = {};
}
devfile.attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR] = 'ephemeral';
}
attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR] = 'ephemeral';
}

return devfile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,49 @@ import { DevfileBuilder } from '../../../store/__mocks__/devfile';
import { DEVWORKSPACE_STORAGE_TYPE_ATTR } from '../../devfileApi/devWorkspace/spec/template';
import { DevfileAdapter } from '../adapter';
import { convertDevfileV1toDevfileV2 } from '../converters';
import devfileApi from '../../devfileApi';

describe('DevfileAdapter Service', () => {
describe('getAttributesFromDevfileV2', () => {
it('should returns attributes from the devfile v2.0.0', async () => {
const devfileV2 = {
schemaVersion: '2.0.0',
metadata: {
name: 'wksp-test',
},
} as devfileApi.Devfile;
const attributes = DevfileAdapter.getAttributesFromDevfileV2(devfileV2);

expect(attributes === devfileV2.metadata.attributes).toBeTruthy();
expect(attributes === devfileV2.attributes).toBeFalsy();
});

it('should returns attributes from the devfile v2.1.0', async () => {
const devfileV2 = {
schemaVersion: '2.1.0',
metadata: {
name: 'wksp-test',
},
} as devfileApi.Devfile;
const attributes = DevfileAdapter.getAttributesFromDevfileV2(devfileV2);

expect(attributes === devfileV2.metadata.attributes).toBeFalsy();
expect(attributes === devfileV2.attributes).toBeTruthy();
});

it('should returns attributes from the devfile v2.2.0', async () => {
const devfileV2 = {
schemaVersion: '2.2.0',
metadata: {
name: 'wksp-test',
},
} as devfileApi.Devfile;
const attributes = DevfileAdapter.getAttributesFromDevfileV2(devfileV2);

expect(attributes === devfileV2.metadata.attributes).toBeFalsy();
expect(attributes === devfileV2.attributes).toBeTruthy();
});
});
describe('update storageType', () => {
describe('devfile V2', () => {
describe('setting the "ephemeral" storage', () => {
Expand Down
47 changes: 27 additions & 20 deletions packages/dashboard-frontend/src/services/devfile/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,43 @@ export class DevfileAdapter {
this._devfile = devfile;
}

public static getAttributesFromDevfileV2(devfile: devfileApi.Devfile) {
let attributes = {};
if (devfile.schemaVersion?.startsWith('2.0')) {
if (!devfile.metadata.attributes) {
devfile.metadata.attributes = attributes;
} else {
attributes = devfile.metadata.attributes;
}
} else {
if (!devfile.attributes) {
devfile.attributes = attributes;
} else {
attributes = devfile.attributes;
}
}

return attributes;
}

get devfile(): Devfile {
return this._devfile;
}

set storageType(type: che.WorkspaceStorageType) {
if (isDevfileV2(this._devfile)) {
const attributes = DevfileAdapter.getAttributesFromDevfileV2(this._devfile);
if (type && type !== 'persistent') {
if (this._devfile.schemaVersion === '2.0.0') {
if (!this._devfile.metadata.attributes) {
this._devfile.metadata.attributes = {};
}
this._devfile.metadata.attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR] = type;
} else {
// for devfiles version 2.1.0 and above
if (!this._devfile.attributes) {
this._devfile.attributes = {};
}
this._devfile.attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR] = type;
}
attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR] = type;
} else {
if (this._devfile.metadata.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]) {
delete this._devfile.metadata.attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR];
if (Object.keys(this._devfile.metadata.attributes).length === 0) {
delete this._devfile.metadata.attributes;
}
if (attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR]) {
delete attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR];
}
if (this._devfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]) {
delete this._devfile.attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR];
if (Object.keys(this._devfile.attributes).length === 0) {
if (Object.keys(attributes).length === 0) {
if (this._devfile.attributes === attributes) {
delete this._devfile.attributes;
} else if (this._devfile.metadata.attributes === attributes) {
delete this._devfile.metadata.attributes;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '../../services/workspace-client/devworkspace/devWorkspaceClient';
import { generateWorkspaceName } from '../../services/helpers/generateName';
import { FactoryParams } from '../../containers/Loader/buildFactoryParams';
import { DevfileAdapter } from '../../services/devfile/adapter';

/**
* Returns a devfile from the FactoryResolver object.
Expand Down Expand Up @@ -110,14 +111,13 @@ export default function normalizeDevfileV2(
} else if (location) {
devfileSource = dump({ url: { location } });
}
if (!devfile.metadata.attributes) {
devfile.metadata.attributes = {};
}
if (!devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION]) {
devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION] = {};

const attributes = DevfileAdapter.getAttributesFromDevfileV2(devfile);

if (!attributes[DEVWORKSPACE_METADATA_ANNOTATION]) {
attributes[DEVWORKSPACE_METADATA_ANNOTATION] = {};
}
devfile.metadata.attributes[DEVWORKSPACE_METADATA_ANNOTATION][DEVWORKSPACE_DEVFILE_SOURCE] =
devfileSource;
attributes[DEVWORKSPACE_METADATA_ANNOTATION][DEVWORKSPACE_DEVFILE_SOURCE] = devfileSource;

return devfile;
}

0 comments on commit f316254

Please sign in to comment.