Skip to content

Commit

Permalink
Merge pull request #2981 from manaswinidas/workbench-name-only-numbers
Browse files Browse the repository at this point in the history
Handle only numbers workbench names
  • Loading branch information
openshift-merge-bot[bot] authored Jul 4, 2024
2 parents e48e6d3 + 2587cd2 commit 1d3463c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,44 @@ describe('Workbench page', () => {
verifyRelativeURL('/projects/test-project?section=workbenches');
});

it('Create workbench with numbers', () => {
initIntercepts({ isEmpty: true });
workbenchPage.visit('test-project');
workbenchPage.findCreateButton().click();
createSpawnerPage.findSubmitButton().should('be.disabled');
verifyRelativeURL('/projects/test-project/spawner');
createSpawnerPage.findNameInput().fill('1234');
createSpawnerPage.findDescriptionInput().fill('test-description');
//to check scrollable dropdown selection
createSpawnerPage.findNotebookImage('test-9').click();
createSpawnerPage.selectContainerSize(
'XSmall Limits: 0.5 CPU, 500Mi Memory Requests: 0.1 CPU, 100Mi Memory',
);
createSpawnerPage.findSubmitButton().should('be.enabled');

createSpawnerPage.findSubmitButton().click();

cy.wait('@createWorkbench').then((interception) => {
expect(interception.request.body).to.containSubset({
metadata: {
labels: {
app: 'wb-1234',
'opendatahub.io/dashboard': 'true',
'opendatahub.io/odh-managed': 'true',
'opendatahub.io/user': 'test-2duser',
},
annotations: {
'openshift.io/display-name': '1234',
'openshift.io/description': 'test-description',
},
name: 'wb-1234',
namespace: 'test-project',
},
});
});
verifyRelativeURL('/projects/test-project?section=workbenches');
});

it('list workbench and table sorting', () => {
initIntercepts({});
workbenchPage.visit('test-project');
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/api/k8s/notebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const assembleNotebook = (
existingTolerations,
existingResources,
} = data;
const notebookId = overrideNotebookId || translateDisplayNameForK8s(notebookName);
const notebookId = overrideNotebookId || translateDisplayNameForK8s(notebookName, 'wb-');
const imageUrl = `${image.imageStream?.status?.dockerImageRepository}:${image.imageVersion?.name}`;
const imageSelection = `${image.imageStream?.metadata.name}:${image.imageVersion?.name}`;

Expand Down
1 change: 1 addition & 0 deletions frontend/src/concepts/k8s/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('translateDisplayNameForK8s', () => {
expect(translateDisplayNameForK8s('$ymbols & Capitals and Spaces! (These are invalid!)')).toBe(
'ymbols--capitals-and-spaces-these-are-invalid',
);
expect(translateDisplayNameForK8s('1234', 'wb-')).toBe('wb-1234');
});
});

Expand Down
16 changes: 14 additions & 2 deletions frontend/src/concepts/k8s/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ export const getDisplayNameFromK8sResource = (resource: K8sDSGResource): string
export const getDescriptionFromK8sResource = (resource: K8sDSGResource): string =>
resource.metadata.annotations?.['openshift.io/description'] || '';

export const translateDisplayNameForK8s = (name: string): string =>
name
/**
* Converts a display name to a k8s safe variant, if needed a `safeK8sPrefix` can be provided to customize the prefix used to align with k8s standards if it is needed.
*/
export const translateDisplayNameForK8s = (name: string, safeK8sPrefix?: string): string => {
const translatedName: string = name
.trim()
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^A-Za-z0-9-]/g, '');

if (safeK8sPrefix) {
if (/^\d+$/.test(translatedName)) {
const translatedNameWithoutNumbers = `wb-${translatedName}`;
return translatedNameWithoutNumbers;
}
}
return translatedName;
};

export const isValidK8sName = (name?: string): boolean =>
name === undefined || (name.length > 0 && /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/.test(name));

0 comments on commit 1d3463c

Please sign in to comment.