From d67d0abbd3ff99b087e17b65463c2fb8bb9ecd83 Mon Sep 17 00:00:00 2001 From: Oleksii Orel Date: Fri, 4 Jan 2019 15:49:57 +0200 Subject: [PATCH] CHE-8909 fix add a new machine flow (#12324) * CHE-8909 fix add a new machine flow Signed-off-by: Oleksii Orel * fix machine name in WorkspaceDetailsMachineActionsTest selenium test * fix the regular expression for a container name Signed-off-by: Oleksii Orel --- .../edit-machine-dialog.controller.ts | 27 ++++++++++++------- .../edit-machine-dialog.html | 4 +-- .../api/environment/environment-manager.ts | 5 +++- .../kubernetes-environment-manager.ts | 24 ++++++++--------- .../kubernetes-machine-recipe-parser.ts | 2 +- .../WorkspaceDetailsMachineActionsTest.java | 2 +- 6 files changed, 36 insertions(+), 28 deletions(-) diff --git a/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.controller.ts b/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.controller.ts index a4612440ee..f5db768d22 100644 --- a/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.controller.ts +++ b/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.controller.ts @@ -38,6 +38,7 @@ export class EditMachineDialogController { private cheEnvironmentRegistry: CheEnvironmentRegistry; private environmentManager: EnvironmentManager; private isAdd: boolean; + private isKubernetes: boolean; private machineName: string; private usedMachinesNames: Array; private environment: che.IWorkspaceEnvironment; @@ -74,7 +75,7 @@ export class EditMachineDialogController { this.deepFreeze(this.originEnvironment); this.previousStateEnvironment = angular.copy(this.originEnvironment); this.currentStateEnvironment = angular.copy(this.originEnvironment); - + this.isKubernetes = this.cheRecipeService.isKubernetes(this.currentStateEnvironment.recipe); this.usedMachinesNames = Object.keys(this.currentStateEnvironment.machines).filter((machineName: string) => { return this.isAdd || machineName !== this.machineName; }); @@ -103,7 +104,7 @@ export class EditMachineDialogController { if (!this.machine || !this.machine.recipe) { return; } - this.machineName = angular.copy(this.environmentManager.getMachineName(this.machine)); + this.machineName = this.environmentManager.getMachineName(this.machine); this.machineRAM = this.environmentManager.getMemoryLimit(this.machine); // update memory limit this.environmentManager.setMemoryLimit(this.machine, this.machineRAM); @@ -144,19 +145,24 @@ export class EditMachineDialogController { if (this.machineRAM !== this.environmentManager.getMemoryLimit(this.originMachine)) { return true; } + if (this.isKubernetes) { + return !angular.equals(this.machine.recipe.spec, this.originMachine.recipe.spec); + } return !angular.equals(this.machine.recipe, this.originMachine.recipe); } /** * Update machine's name if it change. * @param {string} name + * @param {boolean} isValid */ - onNameChange(name: string): void { - const oldMachineName = this.isAdd ? this.machine.name : this.machineName; - this.machineName = name; + onNameChange(name: string, isValid: boolean): void { + if (!isValid) { + return; + } + const oldMachineName = this.isAdd ? this.machine.name : this.getFullName(this.machineName); const machineName = this.getFullName(name); - const oldEnvironment = this.isAdd ? this.currentStateEnvironment : this.previousStateEnvironment; - const environment = this.environmentManager.renameMachine(oldEnvironment, oldMachineName, machineName); + const environment = this.environmentManager.renameMachine(this.currentStateEnvironment, oldMachineName, machineName); const machines = this.environmentManager.getMachines(environment); const machineIndex = machines.findIndex((machine: IEnvironmentManagerMachine) => { return machine.name === machineName; @@ -167,6 +173,7 @@ export class EditMachineDialogController { this.machine.recipe = machines[machineIndex].recipe; this.currentStateEnvironment = this.environmentManager.getEnvironment(environment, machines); this.stringifyMachineRecipe(); + this.machine.name = this.getFullName(name); } /** @@ -176,7 +183,7 @@ export class EditMachineDialogController { isRecipeValid(): che.IValidation { try { this.machine.recipe = this.environmentManager.parseMachineRecipe(this.machineRecipeScript); - if (this.cheRecipeService.isOpenshift(this.currentStateEnvironment.recipe)) { + if (this.isKubernetes) { const newPod = this.machine.recipe.metadata.name; const oldPod = this.originMachine.recipe.metadata.name; if (newPod !== oldPod && this.usedMachinesNames.map((name: string) => { @@ -234,7 +241,7 @@ export class EditMachineDialogController { // checks machine name changes const newMachineName = this.environmentManager.getMachineName(this.machine); if (this.machineName !== newMachineName) { - this.onNameChange(newMachineName); + this.machineName = newMachineName; } // checks memory limit changes this.checkMemoryLimitChanges(); @@ -309,7 +316,7 @@ export class EditMachineDialogController { this.currentStateEnvironment = this.environmentManager.addMachine(this.currentStateEnvironment, this.machine); const name = this.environmentManager.getMachineName(this.machine); if (!this.currentStateEnvironment.machines[this.getFullName(name)]) { - this.onNameChange(name); + this.machineName = name; } } } diff --git a/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.html b/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.html index a8a8380783..2c2bd1660c 100644 --- a/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.html +++ b/dashboard/src/app/workspaces/workspace-details/workspace-machines/edit-machine-dialog/edit-machine-dialog.html @@ -12,12 +12,12 @@ che-form="editMachineForm" che-name="name" che-place-holder="enter machine's name" - che-on-change="editMachineDialogController.onNameChange($value)" + che-on-change="editMachineDialogController.onNameChange($value, editMachineForm.$valid)" custom-validator="editMachineDialogController.isUnique($value)" ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 500, 'blur': 0 } }" ng-model="editMachineDialogController.machineName" ng-maxlength="128" - ng-pattern="/^[A-Za-z0-9_\-\.]+$/" + ng-pattern="/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/" required>
diff --git a/dashboard/src/components/api/environment/environment-manager.ts b/dashboard/src/components/api/environment/environment-manager.ts index c56168ef61..361c6101e3 100644 --- a/dashboard/src/components/api/environment/environment-manager.ts +++ b/dashboard/src/components/api/environment/environment-manager.ts @@ -146,7 +146,10 @@ export abstract class EnvironmentManager { * @returns {string} */ getUniqueMachineName(environment: che.IWorkspaceEnvironment, namePrefix?: string): string { - let newMachineName = namePrefix ? namePrefix : 'new-machine'; + let newMachineName = 'new-machine'; + if (namePrefix) { + newMachineName = `${namePrefix}/${newMachineName}`; + } const usedMachinesNames: Array = environment && environment.machines ? Object.keys(environment.machines) : []; for (let pos: number = 1; pos < 1000; pos++) { if (usedMachinesNames.indexOf(newMachineName + pos.toString()) === -1) { diff --git a/dashboard/src/components/api/environment/kubernetes-environment-manager.ts b/dashboard/src/components/api/environment/kubernetes-environment-manager.ts index 59c463afa2..95b4f573f1 100644 --- a/dashboard/src/components/api/environment/kubernetes-environment-manager.ts +++ b/dashboard/src/components/api/environment/kubernetes-environment-manager.ts @@ -383,20 +383,18 @@ export class KubernetesEnvironmentManager extends EnvironmentManager { * @returns {string} */ getUniqueMachineName(environment: che.IWorkspaceEnvironment): string { - const usedMachinesNames: Array = environment && environment.machines ? Object.keys(environment.machines) : []; - const podNames = usedMachinesNames.map((name: string) => { - return this.splitName(name)[0]; - }); - let namePrefix = 'pod'; - for (let pos = 1; pos < 1000; pos++) { - if (podNames.indexOf(namePrefix + pos.toString()) === -1) { - namePrefix += pos.toString(); - break; - } + const envRecipe = this.parseRecipe(environment.recipe.content); + if (!envRecipe || !angular.isArray(envRecipe.items)) { + return super.getUniqueMachineName(environment); + } + const pod = envRecipe.items.find((podItem: IPodItem) => { + return podItem && podItem.kind === POD; + }); + if (!pod || !pod.metadata || !pod.metadata.name) { + return super.getUniqueMachineName(environment); } - namePrefix += '/machine'; - return super.getUniqueMachineName(environment, namePrefix); + return super.getUniqueMachineName(environment, pod.metadata.name); } /** @@ -445,7 +443,7 @@ export class KubernetesEnvironmentManager extends EnvironmentManager { if (!pod.kind || pod.kind !== POD || pod.metadata.name !== machineRecipe.metadata.name) { return false; } - return angular.equals(machineRecipePod, this.getMachinePod(pod)); + return machineRecipePod.metadata.name === this.getMachinePod(pod).metadata.name; }); if (usedPodIndex === -1) { recipe.items.push(machineRecipe); diff --git a/dashboard/src/components/api/environment/kubernetes-machine-recipe-parser.ts b/dashboard/src/components/api/environment/kubernetes-machine-recipe-parser.ts index 287d20fb90..8f83fe0eaf 100644 --- a/dashboard/src/components/api/environment/kubernetes-machine-recipe-parser.ts +++ b/dashboard/src/components/api/environment/kubernetes-machine-recipe-parser.ts @@ -137,6 +137,6 @@ export class KubernetesMachineRecipeParser implements IParser { * @returns {boolean} */ private testName(name: string): boolean { - return /^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/.test(name); + return /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/.test(name); } } diff --git a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/dashboard/workspaces/details/WorkspaceDetailsMachineActionsTest.java b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/dashboard/workspaces/details/WorkspaceDetailsMachineActionsTest.java index 90d9561f61..9028d369fb 100644 --- a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/dashboard/workspaces/details/WorkspaceDetailsMachineActionsTest.java +++ b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/dashboard/workspaces/details/WorkspaceDetailsMachineActionsTest.java @@ -47,7 +47,7 @@ public class WorkspaceDetailsMachineActionsTest { private static final String SPECIAL_CHARACTERS_ERRORS_MESSAGE = "The name should not contain special characters like space, dollar, etc."; private static final String NAME_WITH_SPECIAL_CHARACTERS = "@#$^&*(!"; - private static final String MAX_VALID_NAME = NameGenerator.generate("max_name", 120); + private static final String MAX_VALID_NAME = NameGenerator.generate("max-name", 120); private static final String TOO_BIG_NAME = NameGenerator.generate(MAX_VALID_NAME, 1); private static final String TOO_BIG_RAM_SIZE = "1000"; private static final String MAX_VALID_RAM_VALUE = "100";