CHE-8909 fix add a new machine flow (#12324)

* CHE-8909 fix add a new machine flow

Signed-off-by: Oleksii Orel <oorel@redhat.com>

* fix machine name in WorkspaceDetailsMachineActionsTest selenium test

* fix the regular expression for a container name

Signed-off-by: Oleksii Orel <oorel@redhat.com>
6.19.x
Oleksii Orel 2019-01-04 15:49:57 +02:00 committed by GitHub
parent ed9a5f5d02
commit d67d0abbd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 28 deletions

View File

@ -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<string>;
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;
}
}
}

View File

@ -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>
<che-error-messages che-message-scope="workspace-details-environment" che-message-name="Machine name">
<div ng-message></div>

View File

@ -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<string> = environment && environment.machines ? Object.keys(environment.machines) : [];
for (let pos: number = 1; pos < 1000; pos++) {
if (usedMachinesNames.indexOf(newMachineName + pos.toString()) === -1) {

View File

@ -383,20 +383,18 @@ export class KubernetesEnvironmentManager extends EnvironmentManager {
* @returns {string}
*/
getUniqueMachineName(environment: che.IWorkspaceEnvironment): string {
const usedMachinesNames: Array<string> = 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);

View File

@ -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);
}
}

View File

@ -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";