2022-01-16 19:37:46 +00:00
|
|
|
import _ from 'lodash-es';
|
2022-10-11 21:06:57 +00:00
|
|
|
import { KubernetesServicePort } from 'Kubernetes/models/service/models';
|
2022-01-16 19:37:46 +00:00
|
|
|
import { KubernetesFormValidationReferences } from 'Kubernetes/models/application/formValues';
|
|
|
|
import KubernetesFormValidationHelper from 'Kubernetes/helpers/formValidationHelper';
|
|
|
|
import { KubernetesApplicationPublishingTypes } from 'Kubernetes/models/application/models/constants';
|
|
|
|
|
|
|
|
export default class KubeServicesItemViewController {
|
|
|
|
/* @ngInject */
|
|
|
|
constructor(EndpointProvider, Authentication) {
|
|
|
|
this.EndpointProvider = EndpointProvider;
|
|
|
|
this.Authentication = Authentication;
|
2022-01-27 06:37:46 +00:00
|
|
|
this.KubernetesApplicationPublishingTypes = KubernetesApplicationPublishingTypes;
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addPort() {
|
2022-01-27 06:37:46 +00:00
|
|
|
const port = new KubernetesServicePort();
|
|
|
|
port.nodePort = '';
|
|
|
|
port.port = '';
|
|
|
|
port.targetPort = '';
|
|
|
|
port.protocol = 'TCP';
|
2022-10-11 21:06:57 +00:00
|
|
|
this.service.Ports.push(port);
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removePort(index) {
|
2022-10-11 21:06:57 +00:00
|
|
|
this.service.Ports.splice(index, 1);
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
servicePort(index) {
|
2022-10-11 21:06:57 +00:00
|
|
|
const targetPort = this.service.Ports[index].targetPort;
|
|
|
|
this.service.Ports[index].port = targetPort;
|
|
|
|
this.onChangeServicePort();
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isAdmin() {
|
|
|
|
return this.Authentication.isAdmin();
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeContainerPort() {
|
|
|
|
const state = this.state.duplicates.targetPort;
|
2022-10-11 21:06:57 +00:00
|
|
|
const source = _.map(this.service.Ports, (sp) => sp.targetPort);
|
2022-01-16 19:37:46 +00:00
|
|
|
const duplicates = KubernetesFormValidationHelper.getDuplicates(source);
|
|
|
|
state.refs = duplicates;
|
|
|
|
state.hasRefs = Object.keys(duplicates).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeServicePort() {
|
|
|
|
const state = this.state.duplicates.servicePort;
|
2022-10-11 21:06:57 +00:00
|
|
|
const source = _.map(this.service.Ports, (sp) => sp.port);
|
2022-01-16 19:37:46 +00:00
|
|
|
const duplicates = KubernetesFormValidationHelper.getDuplicates(source);
|
|
|
|
state.refs = duplicates;
|
|
|
|
state.hasRefs = Object.keys(duplicates).length > 0;
|
2022-10-11 21:06:57 +00:00
|
|
|
|
|
|
|
this.service.servicePortError = state.hasRefs;
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onChangeNodePort() {
|
|
|
|
const state = this.state.duplicates.nodePort;
|
2022-10-11 21:06:57 +00:00
|
|
|
|
|
|
|
// create a list of all the node ports (number[]) in the cluster and current form
|
|
|
|
const clusterNodePortsWithoutCurrentService = this.nodePortServices
|
|
|
|
.filter((npService) => npService.Name !== this.service.Name)
|
|
|
|
.map((npService) => npService.Ports)
|
|
|
|
.flat()
|
|
|
|
.map((npServicePorts) => npServicePorts.NodePort);
|
|
|
|
const formNodePortsWithoutCurrentService = this.formServices
|
|
|
|
.filter((formService) => formService.Type === KubernetesApplicationPublishingTypes.NODE_PORT && formService.Name !== this.service.Name)
|
|
|
|
.map((formService) => formService.Ports)
|
|
|
|
.flat()
|
|
|
|
.map((formServicePorts) => formServicePorts.nodePort);
|
|
|
|
const serviceNodePorts = this.service.Ports.map((sp) => sp.nodePort);
|
|
|
|
// getDuplicates cares about the index, so put the serviceNodePorts at the start
|
|
|
|
const allNodePortsWithoutCurrentService = [...clusterNodePortsWithoutCurrentService, ...formNodePortsWithoutCurrentService];
|
|
|
|
|
|
|
|
const duplicates = KubernetesFormValidationHelper.getDuplicateNodePorts(serviceNodePorts, allNodePortsWithoutCurrentService);
|
2022-01-16 19:37:46 +00:00
|
|
|
state.refs = duplicates;
|
|
|
|
state.hasRefs = Object.keys(duplicates).length > 0;
|
2022-10-11 21:06:57 +00:00
|
|
|
|
|
|
|
this.service.nodePortError = state.hasRefs;
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
2022-10-11 21:06:57 +00:00
|
|
|
if (this.service.Ports.length === 0) {
|
2022-01-16 19:37:46 +00:00
|
|
|
this.addPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
duplicates: {
|
|
|
|
targetPort: new KubernetesFormValidationReferences(),
|
|
|
|
servicePort: new KubernetesFormValidationReferences(),
|
|
|
|
nodePort: new KubernetesFormValidationReferences(),
|
|
|
|
},
|
|
|
|
endpointId: this.EndpointProvider.endpointID(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|