2022-11-07 06:03:11 +00:00
|
|
|
import { KubernetesService, KubernetesServicePort, KubernetesServiceTypes } from '@/kubernetes/models/service/models';
|
|
|
|
import { KubernetesApplicationPublishingTypes } from '@/kubernetes/models/application/models/constants';
|
2022-10-11 21:06:57 +00:00
|
|
|
import { notifyError } from '@/portainer/services/notifications';
|
2022-11-07 06:03:11 +00:00
|
|
|
import { getServices } from '@/react/kubernetes/networks/services/service';
|
2022-01-16 19:37:46 +00:00
|
|
|
|
|
|
|
export default class KubeServicesViewController {
|
|
|
|
/* @ngInject */
|
|
|
|
constructor($async, EndpointProvider, Authentication) {
|
|
|
|
this.$async = $async;
|
|
|
|
this.EndpointProvider = EndpointProvider;
|
|
|
|
this.Authentication = Authentication;
|
2022-10-11 21:06:57 +00:00
|
|
|
this.asyncOnInit = this.asyncOnInit.bind(this);
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addEntry(service) {
|
|
|
|
const p = new KubernetesService();
|
2022-09-23 04:11:35 +00:00
|
|
|
p.Type = service;
|
2022-01-16 19:37:46 +00:00
|
|
|
|
|
|
|
p.Selector = this.formValues.Selector;
|
|
|
|
|
|
|
|
p.Name = this.getUniqName();
|
|
|
|
this.formValues.Services.push(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUniqName() {
|
2022-03-30 21:34:29 +00:00
|
|
|
//services name will follow thia patten: service, service-2, service-3...
|
|
|
|
let nameIndex = 2;
|
|
|
|
let UniqName = this.formValues.Name;
|
2022-01-16 19:37:46 +00:00
|
|
|
const services = this.formValues.Services;
|
2022-03-30 21:34:29 +00:00
|
|
|
|
|
|
|
const sortServices = services.sort((a, b) => {
|
|
|
|
return a.Name.localeCompare(b.Name);
|
2022-01-16 19:37:46 +00:00
|
|
|
});
|
2022-03-30 21:34:29 +00:00
|
|
|
|
|
|
|
if (sortServices.length !== 0) {
|
|
|
|
sortServices.forEach((service) => {
|
|
|
|
if (service.Name === UniqName) {
|
|
|
|
UniqName = this.formValues.Name + '-' + nameIndex;
|
|
|
|
nameIndex += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-16 19:37:46 +00:00
|
|
|
return UniqName;
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteService(index) {
|
|
|
|
this.formValues.Services.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
addPort(index) {
|
|
|
|
const p = new KubernetesServicePort();
|
|
|
|
this.formValues.Services[index].Ports.push(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceType(type) {
|
|
|
|
switch (type) {
|
|
|
|
case KubernetesApplicationPublishingTypes.CLUSTER_IP:
|
|
|
|
return KubernetesServiceTypes.CLUSTER_IP;
|
|
|
|
case KubernetesApplicationPublishingTypes.NODE_PORT:
|
|
|
|
return KubernetesServiceTypes.NODE_PORT;
|
|
|
|
case KubernetesApplicationPublishingTypes.LOAD_BALANCER:
|
|
|
|
return KubernetesServiceTypes.LOAD_BALANCER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isAdmin() {
|
|
|
|
return this.Authentication.isAdmin();
|
|
|
|
}
|
|
|
|
|
2022-10-11 21:06:57 +00:00
|
|
|
async asyncOnInit() {
|
|
|
|
try {
|
|
|
|
// get all nodeport services in the cluster, to validate unique nodeports in the form
|
|
|
|
const allSettledServices = await Promise.allSettled(this.namespaces.map((namespace) => getServices(this.state.endpointId, namespace)));
|
|
|
|
const allServices = allSettledServices
|
|
|
|
.filter((settledService) => settledService.status === 'fulfilled' && settledService.value)
|
|
|
|
.map((fulfilledService) => fulfilledService.value)
|
|
|
|
.flat();
|
|
|
|
this.nodePortServices = allServices.filter((service) => service.Type === 'NodePort');
|
|
|
|
} catch (error) {
|
|
|
|
notifyError('Failure', error, 'Failed getting services');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:37:46 +00:00
|
|
|
$onInit() {
|
|
|
|
this.state = {
|
|
|
|
serviceType: [
|
|
|
|
{
|
|
|
|
typeName: KubernetesServiceTypes.CLUSTER_IP,
|
|
|
|
typeValue: KubernetesApplicationPublishingTypes.CLUSTER_IP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
typeName: KubernetesServiceTypes.NODE_PORT,
|
|
|
|
typeValue: KubernetesApplicationPublishingTypes.NODE_PORT,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
typeName: KubernetesServiceTypes.LOAD_BALANCER,
|
|
|
|
typeValue: KubernetesApplicationPublishingTypes.LOAD_BALANCER,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
selected: KubernetesApplicationPublishingTypes.CLUSTER_IP,
|
|
|
|
endpointId: this.EndpointProvider.endpointID(),
|
|
|
|
};
|
2022-10-11 21:06:57 +00:00
|
|
|
return this.$async(this.asyncOnInit);
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
}
|