2021-10-22 00:42:27 +02:00
|
|
|
const App = require('../../../models/App');
|
|
|
|
const k8s = require('@kubernetes/client-node');
|
|
|
|
const Logger = require('../../../utils/Logger');
|
|
|
|
const logger = new Logger();
|
|
|
|
const loadConfig = require('../../../utils/loadConfig');
|
|
|
|
|
2024-04-12 08:46:43 -04:00
|
|
|
|
|
|
|
const gatewayRoutes = [
|
|
|
|
{ type: 'httproutes', version: 'v1' },
|
2024-04-12 10:57:02 -04:00
|
|
|
{ type: 'httproutes', version: 'v1beta1' },
|
2024-04-12 08:46:43 -04:00
|
|
|
{ type: 'tcproutes', version: 'v1alpha2' },
|
|
|
|
{ type: 'grpcroutes', version: 'v1alpha2' },
|
|
|
|
{ type: 'tlsroutes', version: 'v1alpha2' },
|
|
|
|
{ type: 'udproutes', version: 'v1alpha2' }
|
|
|
|
];
|
|
|
|
|
2021-10-22 00:42:27 +02:00
|
|
|
const useKubernetes = async (apps) => {
|
|
|
|
const { useOrdering: orderType, unpinStoppedApps } = await loadConfig();
|
|
|
|
|
|
|
|
let ingresses = null;
|
2024-04-12 08:46:43 -04:00
|
|
|
let routeData = [];
|
2021-10-22 00:42:27 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const kc = new k8s.KubeConfig();
|
|
|
|
kc.loadFromCluster();
|
2024-04-12 08:46:43 -04:00
|
|
|
|
2021-10-22 00:42:27 +02:00
|
|
|
const k8sNetworkingV1Api = kc.makeApiClient(k8s.NetworkingV1Api);
|
|
|
|
await k8sNetworkingV1Api.listIngressForAllNamespaces().then((res) => {
|
|
|
|
ingresses = res.body.items;
|
|
|
|
});
|
2024-04-12 08:46:43 -04:00
|
|
|
|
|
|
|
const customObjectsApi = kc.makeApiClient(k8s.CustomObjectsApi);
|
|
|
|
for (let route of gatewayRoutes) {
|
|
|
|
await customObjectsApi.listClusterCustomObject('gateway.networking.k8s.io', route.version, route.type).then((res) => {
|
|
|
|
res.body.items.forEach(item => routeData.push({ ...item, routeType: route.type }));
|
|
|
|
}).catch(error => {
|
|
|
|
logger.log(`Error fetching ${route.type}: ${error.message}`, 'ERROR');
|
|
|
|
});
|
|
|
|
}
|
2021-10-22 00:42:27 +02:00
|
|
|
} catch {
|
|
|
|
logger.log("Can't connect to the Kubernetes API", 'ERROR');
|
2024-04-12 08:46:43 -04:00
|
|
|
logger.log(error.message, 'ERROR');
|
2021-10-22 00:42:27 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 08:46:43 -04:00
|
|
|
if (ingresses || routeData.length > 0) {
|
2021-10-22 00:42:27 +02:00
|
|
|
apps = await App.findAll({
|
|
|
|
order: [[orderType, 'ASC']],
|
|
|
|
});
|
|
|
|
|
|
|
|
ingresses = ingresses.filter(
|
|
|
|
(e) => Object.keys(e.metadata.annotations).length !== 0
|
|
|
|
);
|
|
|
|
|
2024-04-12 08:46:43 -04:00
|
|
|
routeData = routeData.filter(
|
|
|
|
item => item.metadata &&
|
|
|
|
item.metadata.annotations &&
|
|
|
|
Object.keys(item.metadata.annotations).length !== 0
|
|
|
|
);
|
|
|
|
|
2021-10-22 00:42:27 +02:00
|
|
|
const kubernetesApps = [];
|
|
|
|
|
|
|
|
for (const ingress of ingresses) {
|
|
|
|
const annotations = ingress.metadata.annotations;
|
|
|
|
|
|
|
|
if (
|
|
|
|
'flame.pawelmalak/name' in annotations &&
|
|
|
|
'flame.pawelmalak/url' in annotations &&
|
|
|
|
/^app/.test(annotations['flame.pawelmalak/type'])
|
|
|
|
) {
|
|
|
|
kubernetesApps.push({
|
|
|
|
name: annotations['flame.pawelmalak/name'],
|
|
|
|
url: annotations['flame.pawelmalak/url'],
|
|
|
|
icon: annotations['flame.pawelmalak/icon'] || 'kubernetes',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 08:46:43 -04:00
|
|
|
for (const item of routeData) {
|
|
|
|
const annotations = item.metadata.annotations || {};
|
|
|
|
|
|
|
|
if (/^app/.test(annotations['flame.pawelmalak/type'])) {
|
|
|
|
if (item.spec && item.spec.hostnames) {
|
|
|
|
item.spec.hostnames.forEach(hostname => {
|
|
|
|
kubernetesApps.push({
|
|
|
|
name: annotations['flame.pawelmalak/name'] || item.metadata.name,
|
|
|
|
url: annotations['flame.pawelmalak/url'] || hostname,
|
|
|
|
icon: annotations['flame.pawelmalak/icon'] || 'kubernetes',
|
|
|
|
type: item.routeType.toUpperCase()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uniqueApps = Array.from(new Set(kubernetesApps.map(app => JSON.stringify(app)))).map(item => JSON.parse(item));
|
|
|
|
|
2021-10-22 00:42:27 +02:00
|
|
|
if (unpinStoppedApps) {
|
|
|
|
for (const app of apps) {
|
|
|
|
await app.update({ isPinned: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 08:46:43 -04:00
|
|
|
for (const item of uniqueApps) {
|
2021-10-22 00:42:27 +02:00
|
|
|
if (apps.some((app) => app.name === item.name)) {
|
|
|
|
const app = apps.find((a) => a.name === item.name);
|
|
|
|
await app.update({ ...item, isPinned: true });
|
|
|
|
} else {
|
|
|
|
await App.create({
|
|
|
|
...item,
|
|
|
|
isPinned: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = useKubernetes;
|