Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: factory flow with a devfile that uses a parent #1320

Merged
merged 2 commits into from
Feb 25, 2025
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 @@ -11,6 +11,7 @@
*/

import { dump } from 'js-yaml';
import cloneDeep from 'lodash/cloneDeep';

import { prepareDevfile } from '@/components/WorkspaceProgress/CreatingSteps/Apply/Devfile/prepareDevfile';
import devfileApi from '@/services/devfileApi';
Expand Down Expand Up @@ -228,107 +229,140 @@ describe('FactoryLoaderContainer/prepareDevfile', () => {
});

describe('has parent', () => {
describe('with registryUrl', () => {
it('with storage-type attribute', () => {
// mute console logs
console.warn = jest.fn();
const devfile = {
schemaVersion: '2.2.0',
metadata: {
name: 'wksp-test',
},
parent: {
id: 'nodejs',
registryUrl: 'https://registry.devfile.io/',
},
} as devfileApi.Devfile;
let devfile: devfileApi.Devfile;
let parentDevfile: devfileApi.Devfile;
// mute console logs
console.warn = jest.fn();

beforeEach(() => {
devfile = {
schemaVersion: '2.2.2',
metadata: {
generateName: 'nodejs',
},
parent: {},
} as devfileApi.Devfile;

const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
schemaVersion: '2.2.2',
metadata: {
generateName: 'nodejs',
parentDevfile = {
schemaVersion: '2.2.0',
metadata: {
name: 'sample-using-parent',
},
components: [
{
name: 'tools',
container: {
env: [
{
name: 'DEVFILE_ENV_VAR',
value: 'true',
},
],
},
},
attributes: {
'controller.devfile.io/storage-type': 'ephemeral',
],
commands: [
{
id: 'parent-command',
exec: {
label: '2. This command from the parent',
component: 'tools',
commandLine: 'echo "Hello from parent"',
},
},
} as devfileApi.Devfile);
],
} as devfileApi.Devfile;
});

describe('with registryUrl', () => {
it('with storage-type attribute', () => {
devfile.parent!.id = 'nodejs';
devfile.parent!.registryUrl = 'https://registry.devfile.io/';
const _devfile = cloneDeep(devfile);

parentDevfile.attributes = {
'controller.devfile.io/storage-type': 'ephemeral',
};
const _parentDevfile = cloneDeep(parentDevfile);

const newDevfile = prepareDevfile(
_devfile,
factoryId,
'ephemeral',
false,
_parentDevfile,
);

expect(_devfile).toEqual(devfile);
expect(_parentDevfile).toEqual(parentDevfile);
expect(console.warn).toHaveBeenCalledWith(
'Unable to apply controller.devfile.io/storage-type attribute.',
);
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toBeUndefined();
});

it('without storage-type attribute', () => {
const devfile = {
schemaVersion: '2.2.0',
metadata: {
name: 'wksp-test',
},
parent: {
id: 'nodejs',
registryUrl: 'https://registry.devfile.io/',
},
} as devfileApi.Devfile;

const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
schemaVersion: '2.2.2',
metadata: {
generateName: 'nodejs',
},
} as devfileApi.Devfile);
devfile.parent!.id = 'nodejs';
devfile.parent!.registryUrl = 'https://registry.devfile.io/';
const _devfile = cloneDeep(devfile);

const _parentDevfile = cloneDeep(parentDevfile);

const newDevfile = prepareDevfile(
_devfile,
factoryId,
'ephemeral',
false,
_parentDevfile,
);

expect(_devfile).toEqual(devfile);
expect(_parentDevfile).toEqual(parentDevfile);
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toEqual('ephemeral');
});
});
describe('with uri', () => {
it('with storage-type attribute', () => {
// mute console logs
console.warn = jest.fn();
const devfile = {
schemaVersion: '2.2.0',
metadata: {
name: 'wksp-test',
},
parent: {
uri: 'https://raw.githubusercontent.com/test/devfile.yaml',
},
} as devfileApi.Devfile;

const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
schemaVersion: '2.2.2',
metadata: {
generateName: 'nodejs',
},
attributes: {
'controller.devfile.io/storage-type': 'ephemeral',
},
} as devfileApi.Devfile);
devfile.parent!.uri = 'https://raw.githubusercontent.com/test/devfile.yaml';
const _devfile = cloneDeep(devfile);

parentDevfile.attributes = {
'controller.devfile.io/storage-type': 'ephemeral',
};
const _parentDevfile = cloneDeep(parentDevfile);

const newDevfile = prepareDevfile(
_devfile,
factoryId,
'ephemeral',
false,
_parentDevfile,
);

expect(_devfile).toEqual(devfile);
expect(_parentDevfile).toEqual(parentDevfile);
expect(console.warn).toHaveBeenCalledWith(
'Unable to apply controller.devfile.io/storage-type attribute.',
);
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toBeUndefined();
});

it('without storage-type attribute', () => {
const devfile = {
schemaVersion: '2.2.0',
metadata: {
name: 'wksp-test',
},
parent: {
uri: 'https://raw.githubusercontent.com/test/devfile.yaml',
},
} as devfileApi.Devfile;
devfile.parent!.uri = 'https://raw.githubusercontent.com/test/devfile.yaml';
const _devfile = cloneDeep(devfile);

const newDevfile = prepareDevfile(devfile, factoryId, 'ephemeral', false, {
schemaVersion: '2.2.2',
metadata: {
generateName: 'nodejs',
},
} as devfileApi.Devfile);
const _parentDevfile = cloneDeep(parentDevfile);

const newDevfile = prepareDevfile(
_devfile,
factoryId,
'ephemeral',
false,
_parentDevfile,
);

expect(_devfile).toEqual(devfile);
expect(_parentDevfile).toEqual(parentDevfile);
expect(newDevfile.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR]).toEqual('ephemeral');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export function prepareDevfile(
factoryId: string,
storageType: che.WorkspaceStorageType | undefined,
appendSuffix: boolean,
parentDevfile?: devfileApi.Devfile | undefined,
_parentDevfile?: devfileApi.Devfile | undefined,
): devfileApi.Devfile {
const devfile = cloneDeep(_devfile);
const parentDevfile = cloneDeep(_parentDevfile);
const attributes = DevfileAdapter.getAttributes(devfile);
if (
!attributes[DEVWORKSPACE_METADATA_ANNOTATION] ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,9 @@ class CreatingStepFetchDevfile extends ProgressStep<Props, State> {
return true;
}

let resolveDone = false;
try {
// start resolving the devfile
resolveDone = await this.resolveDevfile(sourceUrl);
await this.resolveDevfile(sourceUrl);
} catch (e) {
const errorMessage = common.helpers.errors.getMessage(e);
// check if it is a scheme validation error
Expand All @@ -225,9 +224,6 @@ class CreatingStepFetchDevfile extends ProgressStep<Props, State> {
}
throw e;
}
if (!resolveDone) {
return false;
}

// wait for the devfile resolving to complete
return false;
Expand Down
13 changes: 7 additions & 6 deletions packages/dashboard-frontend/src/services/devfile/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ export class DevfileAdapter {
}

public static getAttributes(devfile: devfileApi.Devfile) {
const attributes = {};
const attributes = {} as any;

if (devfile.schemaVersion?.startsWith('2.0')) {
if (!devfile.metadata.attributes) {
devfile.metadata.attributes = attributes;
}
return devfile.metadata.attributes;
} else {
if (!devfile.attributes) {
devfile.attributes = attributes;
}
return devfile.attributes;
}
if (!devfile.attributes) {
devfile.attributes = attributes;
}

return devfile.attributes;
}

get attributes() {
Expand Down
Loading