1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00

feat(app): add ingress to app service form [EE-5569] (#9106)

This commit is contained in:
Ali 2023-06-26 16:21:19 +12:00 committed by GitHub
parent 8c16fbb8aa
commit 89c1d0e337
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 1929 additions and 1181 deletions

View file

@ -1,8 +1,10 @@
import { FormikErrors } from 'formik';
import { ServiceFormValues } from './types';
import { Ingress } from '@/react/kubernetes/ingresses/types';
export function isServicePortError<T>(
import { ServiceFormValues, ServicePort } from './types';
export function isErrorType<T>(
error: string | FormikErrors<T> | undefined
): error is FormikErrors<T> {
return error !== undefined && typeof error !== 'string';
@ -24,7 +26,7 @@ function generateIndexedName(appName: string, index: number) {
}
function isNameUnique(name: string, services: ServiceFormValues[]) {
return services.findIndex((service) => service.Name === name) === -1;
return !services.find((service) => service.Name === name);
}
export function generateUniqueName(
@ -57,3 +59,101 @@ export const serviceFormDefaultValues: ServiceFormValues = {
Ingress: false,
Selector: {},
};
/**
* Generates new Ingress objects from form path data
* @param {Ingress[]} oldIngresses - The old Ingress objects
* @param {ServicePort[]} newServicesPorts - The new ServicePort objects from the form
* @param {ServicePort[]} oldServicesPorts - The old ServicePort objects
* @returns {Ingress[]} The new Ingress objects
*/
export function generateNewIngressesFromFormPaths(
oldIngresses?: Ingress[],
newServicesPorts?: ServicePort[],
oldServicesPorts?: ServicePort[]
): Ingress[] {
// filter the ports to only the ones that have an ingress
const oldIngressPaths = oldServicesPorts
?.flatMap((port) => port.ingressPaths)
.filter((ingressPath) => ingressPath?.IngressName);
const newPortsWithIngress = newServicesPorts?.filter(
(port) => port.ingressPaths?.length
);
// return early if possible
if (!oldIngresses && !newPortsWithIngress) {
return [];
}
// remove the old paths from the newIngresses copy
const newIngresses = structuredClone(oldIngresses) ?? [];
oldIngressPaths?.forEach((oldIngressPath) => {
if (!oldIngressPath?.Path) return;
const newMatchingIng = newIngresses?.find(
(ingress) => ingress.Name === oldIngressPath.IngressName
);
if (!newMatchingIng) return;
// remove the old path from the new ingress
const oldPathIndex = newMatchingIng?.Paths?.findIndex(
(path) =>
path.Path === prependWithSlash(oldIngressPath.Path) &&
path.Host === oldIngressPath.Host
);
if (oldPathIndex === -1 || oldPathIndex === undefined) return;
if (newMatchingIng.Paths) {
newMatchingIng.Paths = [
...newMatchingIng.Paths.slice(0, oldPathIndex),
...newMatchingIng.Paths.slice(oldPathIndex + 1),
];
}
// update the new ingresses with the newMatchingIng
const newIngIndex = newIngresses.findIndex(
(ingress) => ingress.Name === newMatchingIng.Name
);
newIngresses[newIngIndex] = newMatchingIng;
});
// and add the new paths to return the updated ingresses
newPortsWithIngress?.forEach(
({ ingressPaths: newIngresspaths, ...servicePort }) => {
newIngresspaths?.forEach((newIngressPath) => {
if (!newIngressPath?.Path) return;
const newMatchingIng = newIngresses.find(
(ingress) => ingress.Name === newIngressPath?.IngressName
);
if (!newMatchingIng) return;
// add the new path to the new ingress
if (
newIngressPath.Host &&
newIngressPath.IngressName &&
servicePort.serviceName &&
servicePort.port
) {
newMatchingIng.Paths = [
...(newMatchingIng.Paths ?? []),
{
Path: prependWithSlash(newIngressPath.Path),
Host: newIngressPath.Host,
IngressName: newIngressPath.IngressName,
ServiceName: servicePort.serviceName,
Port: servicePort.port,
PathType: 'Prefix',
},
];
}
// update the new ingresses with the newMatchingIng
const newIngIndex = newIngresses.findIndex(
(ingress) => ingress.Name === newMatchingIng.Name
);
newIngresses[newIngIndex] = newMatchingIng;
});
}
);
return newIngresses;
}
export function prependWithSlash(path?: string) {
return path?.startsWith('/') ? path : `/${path}`;
}