2021-02-26 15:50:33 +00:00
|
|
|
import _ from 'lodash-es';
|
2020-08-12 23:30:23 +00:00
|
|
|
import * as JsonPatch from 'fast-json-patch';
|
|
|
|
|
|
|
|
import KubernetesCommonHelper from 'Kubernetes/helpers/commonHelper';
|
2021-04-27 17:51:13 +00:00
|
|
|
import {
|
|
|
|
KubernetesResourcePoolIngressClassAnnotationFormValue,
|
|
|
|
KubernetesResourcePoolIngressClassFormValue,
|
|
|
|
KubernetesResourcePoolIngressClassHostFormValue,
|
|
|
|
} from 'Kubernetes/models/resource-pool/formValues';
|
2021-11-30 04:14:52 +00:00
|
|
|
import { KubernetesApplicationPublishingTypes } from '../models/application/models';
|
2020-08-20 00:51:14 +00:00
|
|
|
import { KubernetesIngress, KubernetesIngressRule } from './models';
|
2020-08-12 23:30:23 +00:00
|
|
|
import { KubernetesIngressCreatePayload, KubernetesIngressRuleCreatePayload, KubernetesIngressRulePathCreatePayload } from './payloads';
|
2020-08-20 00:51:14 +00:00
|
|
|
import { KubernetesIngressClassAnnotation, KubernetesIngressClassRewriteTargetAnnotations } from './constants';
|
2020-07-14 20:45:19 +00:00
|
|
|
|
|
|
|
export class KubernetesIngressConverter {
|
|
|
|
static apiToModel(data) {
|
2020-08-12 23:30:23 +00:00
|
|
|
const paths = _.flatMap(data.spec.rules, (rule) => {
|
|
|
|
return !rule.http
|
|
|
|
? []
|
|
|
|
: _.map(rule.http.paths, (path) => {
|
|
|
|
const ingRule = new KubernetesIngressRule();
|
|
|
|
ingRule.IngressName = data.metadata.name;
|
2021-08-31 00:39:19 +00:00
|
|
|
ingRule.ServiceName = path.backend.service.name;
|
2020-08-12 23:30:23 +00:00
|
|
|
ingRule.Host = rule.host || '';
|
|
|
|
ingRule.IP = data.status.loadBalancer.ingress ? data.status.loadBalancer.ingress[0].ip : undefined;
|
2021-08-31 00:39:19 +00:00
|
|
|
ingRule.Port = path.backend.service.port.number;
|
2020-08-12 23:30:23 +00:00
|
|
|
ingRule.Path = path.path;
|
|
|
|
return ingRule;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = new KubernetesIngress();
|
|
|
|
res.Name = data.metadata.name;
|
|
|
|
res.Namespace = data.metadata.namespace;
|
|
|
|
res.Annotations = data.metadata.annotations || {};
|
|
|
|
res.IngressClassName =
|
|
|
|
data.metadata.annotations && data.metadata.annotations[KubernetesIngressClassAnnotation]
|
|
|
|
? data.metadata.annotations[KubernetesIngressClassAnnotation]
|
|
|
|
: data.spec.ingressClassName;
|
|
|
|
res.Paths = paths;
|
2021-04-27 17:51:13 +00:00
|
|
|
res.Hosts = _.uniq(_.map(data.spec.rules, 'host'));
|
|
|
|
const idx = _.findIndex(res.Hosts, (h) => h === undefined);
|
|
|
|
if (idx >= 0) {
|
|
|
|
res.Hosts.splice(idx, 1, '');
|
|
|
|
}
|
2020-08-12 23:30:23 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-11-30 04:14:52 +00:00
|
|
|
/**
|
|
|
|
* Converts Application Form Value (from Create Application View) to Ingresses
|
|
|
|
* @param {KubernetesApplicationFormValues} formValues
|
|
|
|
* @param {string} serviceName
|
|
|
|
* @returns {KubernetesIngressRule[]}
|
|
|
|
*/
|
2020-08-12 23:30:23 +00:00
|
|
|
static applicationFormValuesToIngresses(formValues, serviceName) {
|
2021-11-30 04:14:52 +00:00
|
|
|
const isPublishingToIngress = formValues.PublishingType === KubernetesApplicationPublishingTypes.INGRESS;
|
2020-08-12 23:30:23 +00:00
|
|
|
const ingresses = angular.copy(formValues.OriginalIngresses);
|
|
|
|
_.forEach(formValues.PublishedPorts, (p) => {
|
|
|
|
const ingress = _.find(ingresses, { Name: p.IngressName });
|
2021-11-30 04:14:52 +00:00
|
|
|
if (ingress) {
|
|
|
|
if (p.NeedsDeletion) {
|
|
|
|
_.remove(ingress.Paths, (path) => path.Port === p.ContainerPort && path.ServiceName === serviceName && path.Path === p.IngressRoute);
|
|
|
|
} else if (isPublishingToIngress && p.IsNew) {
|
|
|
|
const rule = new KubernetesIngressRule();
|
|
|
|
rule.IngressName = ingress.Name;
|
|
|
|
rule.ServiceName = serviceName;
|
|
|
|
rule.Port = p.ContainerPort;
|
|
|
|
if (p.IngressRoute) {
|
|
|
|
rule.Path = _.startsWith(p.IngressRoute, '/') ? p.IngressRoute : '/' + p.IngressRoute;
|
|
|
|
}
|
|
|
|
rule.Host = p.IngressHost;
|
|
|
|
ingress.Paths.push(rule);
|
2021-07-19 07:45:20 +00:00
|
|
|
}
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
2020-07-14 20:45:19 +00:00
|
|
|
});
|
2020-08-12 23:30:23 +00:00
|
|
|
return ingresses;
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:37:46 +00:00
|
|
|
static applicationFormValuesToDeleteIngresses(formValues, application) {
|
|
|
|
const ingresses = angular.copy(formValues.OriginalIngresses);
|
|
|
|
application.Services.forEach((service) => {
|
|
|
|
ingresses.forEach((ingress) => {
|
|
|
|
const path = _.find(ingress.Paths, { ServiceName: service.metadata.name });
|
|
|
|
if (path) {
|
|
|
|
_.remove(ingress.Paths, path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return ingresses;
|
|
|
|
}
|
|
|
|
|
2022-03-16 21:25:36 +00:00
|
|
|
static removeIngressesPaths(ingresses, services) {
|
|
|
|
const originalIngress = angular.copy(ingresses);
|
|
|
|
originalIngress.forEach((ingress) => {
|
|
|
|
services.forEach((service) => {
|
|
|
|
_.remove(ingress.Paths, { ServiceName: service.Name });
|
|
|
|
});
|
2022-01-16 19:37:46 +00:00
|
|
|
});
|
2022-03-16 21:25:36 +00:00
|
|
|
return originalIngress;
|
2022-01-16 19:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 21:25:36 +00:00
|
|
|
static generateNewIngresses(ingresses, services) {
|
|
|
|
const originalIngresses = angular.copy(ingresses);
|
|
|
|
services
|
|
|
|
.filter((s) => s.Ingress)
|
|
|
|
.forEach((service) => {
|
|
|
|
if (service.Ports.length !== 0) {
|
|
|
|
const matchedIngress = _.find(originalIngresses, { Name: service.Ports[0].ingress.IngressName });
|
|
|
|
if (matchedIngress) {
|
|
|
|
const rule = new KubernetesIngressRule();
|
|
|
|
rule.ServiceName = service.Name;
|
|
|
|
rule.IngressName = service.Ports[0].ingress.IngressName;
|
|
|
|
rule.Host = service.Ports[0].ingress.Host;
|
|
|
|
rule.Path = _.startsWith(service.Ports[0].ingress.Path, '/') ? service.Ports[0].ingress.Path : '/' + service.Ports[0].ingress.Path;
|
|
|
|
rule.Port = service.Ports[0].port;
|
|
|
|
|
|
|
|
matchedIngress.Paths.push(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return originalIngresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need this function for [ resource summary ] controller
|
2022-01-16 19:37:46 +00:00
|
|
|
static newApplicationFormValuesToIngresses(formValues, serviceName, servicePorts) {
|
|
|
|
const ingresses = angular.copy(formValues.OriginalIngresses);
|
|
|
|
servicePorts.forEach((port) => {
|
|
|
|
const ingress = _.find(ingresses, { Name: port.ingress.IngressName });
|
|
|
|
if (ingress) {
|
|
|
|
const rule = new KubernetesIngressRule();
|
|
|
|
rule.ServiceName = serviceName;
|
|
|
|
rule.IngressName = port.ingress.IngressName;
|
|
|
|
rule.Host = port.ingress.Host;
|
|
|
|
rule.Path = _.startsWith(port.ingress.Path, '/') ? port.ingress.Path : '/' + port.ingress.Path;
|
|
|
|
rule.Port = port.port;
|
|
|
|
|
|
|
|
ingress.Paths.push(rule);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ingresses;
|
|
|
|
}
|
|
|
|
|
2020-08-20 00:51:14 +00:00
|
|
|
/**
|
|
|
|
*
|
2021-11-30 04:14:52 +00:00
|
|
|
* @param {KubernetesResourcePoolIngressClassFormValue[]} formValues
|
|
|
|
* @returns {KubernetesIngress} Ingress
|
2020-08-20 00:51:14 +00:00
|
|
|
*/
|
|
|
|
static resourcePoolIngressClassFormValueToIngress(formValues) {
|
|
|
|
const res = new KubernetesIngress();
|
|
|
|
res.Name = formValues.IngressClass.Name;
|
|
|
|
res.Namespace = formValues.Namespace;
|
|
|
|
const pairs = _.map(formValues.Annotations, (a) => [a.Key, a.Value]);
|
|
|
|
res.Annotations = _.fromPairs(pairs);
|
|
|
|
if (formValues.RewriteTarget) {
|
|
|
|
_.extend(res.Annotations, KubernetesIngressClassRewriteTargetAnnotations[formValues.IngressClass.Type]);
|
|
|
|
}
|
|
|
|
res.Annotations[KubernetesIngressClassAnnotation] = formValues.IngressClass.Name;
|
2022-01-09 20:02:02 +00:00
|
|
|
res.IngressClassName = formValues.IngressClass.Name;
|
2021-04-27 17:51:13 +00:00
|
|
|
res.Hosts = formValues.Hosts;
|
2021-02-26 15:50:33 +00:00
|
|
|
res.Paths = formValues.Paths;
|
2020-08-20 00:51:14 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {KubernetesIngressClass} ics Ingress classes (saved in Portainer DB)
|
2021-02-26 15:50:33 +00:00
|
|
|
* @param {KubernetesIngress[]} ingresses Existing Kubernetes ingresses. Must be empty for RP CREATE VIEW and filled for RP EDIT VIEW
|
2020-08-20 00:51:14 +00:00
|
|
|
*/
|
|
|
|
static ingressClassesToFormValues(ics, ingresses) {
|
|
|
|
const res = _.map(ics, (ic) => {
|
2021-02-26 15:50:33 +00:00
|
|
|
const fv = new KubernetesResourcePoolIngressClassFormValue(ic);
|
2020-08-20 00:51:14 +00:00
|
|
|
const ingress = _.find(ingresses, { Name: ic.Name });
|
|
|
|
if (ingress) {
|
|
|
|
fv.Selected = true;
|
|
|
|
fv.WasSelected = true;
|
2021-04-27 17:51:13 +00:00
|
|
|
fv.Hosts = _.map(ingress.Hosts, (host) => {
|
|
|
|
const hfv = new KubernetesResourcePoolIngressClassHostFormValue();
|
|
|
|
hfv.Host = host;
|
|
|
|
hfv.PreviousHost = host;
|
|
|
|
hfv.IsNew = false;
|
|
|
|
return hfv;
|
|
|
|
});
|
2020-08-20 00:51:14 +00:00
|
|
|
const [[rewriteKey]] = _.toPairs(KubernetesIngressClassRewriteTargetAnnotations[ic.Type]);
|
|
|
|
const annotations = _.map(_.toPairs(ingress.Annotations), ([key, value]) => {
|
|
|
|
if (key === rewriteKey) {
|
|
|
|
fv.RewriteTarget = true;
|
|
|
|
} else if (key !== KubernetesIngressClassAnnotation) {
|
|
|
|
const annotation = new KubernetesResourcePoolIngressClassAnnotationFormValue();
|
|
|
|
annotation.Key = key;
|
|
|
|
annotation.Value = value;
|
|
|
|
return annotation;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fv.Annotations = _.without(annotations, undefined);
|
|
|
|
fv.AdvancedConfig = fv.Annotations.length > 0;
|
2021-02-26 15:50:33 +00:00
|
|
|
fv.Paths = ingress.Paths;
|
2020-08-20 00:51:14 +00:00
|
|
|
}
|
|
|
|
return fv;
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-08-12 23:30:23 +00:00
|
|
|
static createPayload(data) {
|
|
|
|
const res = new KubernetesIngressCreatePayload();
|
|
|
|
res.metadata.name = data.Name;
|
|
|
|
res.metadata.namespace = data.Namespace;
|
2022-01-09 20:02:02 +00:00
|
|
|
res.spec.ingressClassName = data.IngressClassName;
|
2020-08-12 23:30:23 +00:00
|
|
|
if (data.Paths && data.Paths.length) {
|
2020-08-20 00:51:14 +00:00
|
|
|
_.forEach(data.Paths, (p) => {
|
|
|
|
if (p.Host === 'undefined' || p.Host === undefined) {
|
|
|
|
p.Host = '';
|
|
|
|
}
|
|
|
|
});
|
2021-04-27 17:51:13 +00:00
|
|
|
const hostsWithRules = [];
|
2020-08-12 23:30:23 +00:00
|
|
|
const groups = _.groupBy(data.Paths, 'Host');
|
2021-04-27 17:51:13 +00:00
|
|
|
let rules = _.map(groups, (paths, host) => {
|
|
|
|
const updatedHost = _.find(data.Hosts, (h) => {
|
|
|
|
return h === host || h.PreviousHost === host;
|
|
|
|
});
|
|
|
|
host = updatedHost.Host || updatedHost;
|
|
|
|
if (updatedHost.NeedsDeletion) {
|
|
|
|
return;
|
2020-08-12 23:30:23 +00:00
|
|
|
}
|
2021-04-27 17:51:13 +00:00
|
|
|
const rule = new KubernetesIngressRuleCreatePayload();
|
2020-08-12 23:30:23 +00:00
|
|
|
KubernetesCommonHelper.assignOrDeleteIfEmpty(rule, 'host', host);
|
|
|
|
rule.http.paths = _.map(paths, (p) => {
|
|
|
|
const path = new KubernetesIngressRulePathCreatePayload();
|
|
|
|
path.path = p.Path;
|
2021-08-31 00:39:19 +00:00
|
|
|
path.backend.service.name = p.ServiceName;
|
|
|
|
path.backend.service.port.number = p.Port;
|
2020-08-12 23:30:23 +00:00
|
|
|
return path;
|
|
|
|
});
|
2021-04-27 17:51:13 +00:00
|
|
|
hostsWithRules.push(host);
|
2020-08-12 23:30:23 +00:00
|
|
|
return rule;
|
|
|
|
});
|
2021-04-27 17:51:13 +00:00
|
|
|
rules = _.without(rules, undefined);
|
|
|
|
const keptHosts = _.without(
|
|
|
|
_.map(data.Hosts, (h) => (h.NeedsDeletion ? undefined : h.Host || h)),
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
const hostsWithoutRules = _.without(keptHosts, ...hostsWithRules);
|
|
|
|
const emptyRules = _.map(hostsWithoutRules, (host) => {
|
|
|
|
return { host: host };
|
|
|
|
});
|
|
|
|
rules = _.concat(rules, emptyRules);
|
2020-08-12 23:30:23 +00:00
|
|
|
KubernetesCommonHelper.assignOrDeleteIfEmpty(res, 'spec.rules', rules);
|
2021-04-27 17:51:13 +00:00
|
|
|
} else if (data.Hosts) {
|
|
|
|
res.spec.rules = [];
|
|
|
|
_.forEach(data.Hosts, (host) => {
|
|
|
|
if (!host.NeedsDeletion) {
|
2021-08-31 00:39:19 +00:00
|
|
|
res.spec.rules.push({ host: host.Host || host });
|
2021-04-27 17:51:13 +00:00
|
|
|
}
|
|
|
|
});
|
2020-08-12 23:30:23 +00:00
|
|
|
} else {
|
|
|
|
delete res.spec.rules;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static patchPayload(oldData, newData) {
|
|
|
|
const oldPayload = KubernetesIngressConverter.createPayload(oldData);
|
|
|
|
const newPayload = KubernetesIngressConverter.createPayload(newData);
|
|
|
|
const payload = JsonPatch.compare(oldPayload, newPayload);
|
|
|
|
return payload;
|
2020-07-14 20:45:19 +00:00
|
|
|
}
|
|
|
|
}
|