fix(service): fix service name starting from zero EE-2807 (#6694)

* fix service name index
pull/6714/head
Richard Wei 2022-03-31 10:34:29 +13:00 committed by GitHub
parent fb7f24df9c
commit ec3d7026d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 10 deletions

View File

@ -21,26 +21,32 @@ export default class KubeServicesViewController {
p.Selector = this.formValues.Selector; p.Selector = this.formValues.Selector;
p.Name = this.getUniqName(); p.Name = this.getUniqName();
this.state.nameIndex += 1;
this.formValues.Services.push(p); this.formValues.Services.push(p);
} }
getUniqName() { getUniqName() {
let name = this.formValues.Name + '-' + this.state.nameIndex; //services name will follow thia patten: service, service-2, service-3...
let nameIndex = 2;
let UniqName = this.formValues.Name;
const services = this.formValues.Services; const services = this.formValues.Services;
services.forEach((service) => {
if (service.Name === name) { const sortServices = services.sort((a, b) => {
this.state.nameIndex += 1; return a.Name.localeCompare(b.Name);
name = this.formValues.Name + '-' + this.state.nameIndex; });
if (sortServices.length !== 0) {
sortServices.forEach((service) => {
if (service.Name === UniqName) {
UniqName = this.formValues.Name + '-' + nameIndex;
nameIndex += 1;
} }
}); });
const UniqName = this.formValues.Name + '-' + this.state.nameIndex; }
return UniqName; return UniqName;
} }
deleteService(index) { deleteService(index) {
this.formValues.Services.splice(index, 1); this.formValues.Services.splice(index, 1);
this.state.nameIndex -= 1;
} }
addPort(index) { addPort(index) {
@ -98,7 +104,6 @@ export default class KubeServicesViewController {
}, },
], ],
selected: KubernetesApplicationPublishingTypes.CLUSTER_IP, selected: KubernetesApplicationPublishingTypes.CLUSTER_IP,
nameIndex: this.formValues.Services.length,
endpointId: this.EndpointProvider.endpointID(), endpointId: this.EndpointProvider.endpointID(),
}; };
} }