|
|
|
@ -13,7 +13,6 @@ import getKubeConfig from "utils/config/kubernetes";
|
|
|
|
|
|
|
|
|
|
const logger = createLogger("service-helpers");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function servicesFromConfig() {
|
|
|
|
|
checkAndCopyConfig("services.yaml");
|
|
|
|
|
|
|
|
|
@ -32,14 +31,14 @@ export async function servicesFromConfig() {
|
|
|
|
|
services: servicesGroup[Object.keys(servicesGroup)[0]].map((entries) => ({
|
|
|
|
|
name: Object.keys(entries)[0],
|
|
|
|
|
...entries[Object.keys(entries)[0]],
|
|
|
|
|
type: 'service'
|
|
|
|
|
type: "service",
|
|
|
|
|
})),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// add default weight to services based on their position in the configuration
|
|
|
|
|
servicesArray.forEach((group, groupIndex) => {
|
|
|
|
|
group.services.forEach((service, serviceIndex) => {
|
|
|
|
|
if(!service.weight) {
|
|
|
|
|
if (!service.weight) {
|
|
|
|
|
servicesArray[groupIndex].services[serviceIndex].weight = (serviceIndex + 1) * 100;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -66,7 +65,9 @@ export async function servicesFromDocker() {
|
|
|
|
|
const isSwarm = !!servers[serverName].swarm;
|
|
|
|
|
const docker = new Docker(getDockerArguments(serverName).conn);
|
|
|
|
|
const listProperties = { all: true };
|
|
|
|
|
const containers = await ((isSwarm) ? docker.listServices(listProperties) : docker.listContainers(listProperties));
|
|
|
|
|
const containers = await (isSwarm
|
|
|
|
|
? docker.listServices(listProperties)
|
|
|
|
|
: docker.listContainers(listProperties));
|
|
|
|
|
|
|
|
|
|
// bad docker connections can result in a <Buffer ...> object?
|
|
|
|
|
// in any case, this ensures the result is the expected array
|
|
|
|
@ -76,8 +77,8 @@ export async function servicesFromDocker() {
|
|
|
|
|
|
|
|
|
|
const discovered = containers.map((container) => {
|
|
|
|
|
let constructedService = null;
|
|
|
|
|
const containerLabels = isSwarm ? shvl.get(container, 'Spec.Labels') : container.Labels;
|
|
|
|
|
const containerName = isSwarm ? shvl.get(container, 'Spec.Name') : container.Names[0];
|
|
|
|
|
const containerLabels = isSwarm ? shvl.get(container, "Spec.Labels") : container.Labels;
|
|
|
|
|
const containerName = isSwarm ? shvl.get(container, "Spec.Name") : container.Names[0];
|
|
|
|
|
|
|
|
|
|
Object.keys(containerLabels).forEach((label) => {
|
|
|
|
|
if (label.startsWith("homepage.")) {
|
|
|
|
@ -85,10 +86,14 @@ export async function servicesFromDocker() {
|
|
|
|
|
constructedService = {
|
|
|
|
|
container: containerName.replace(/^\//, ""),
|
|
|
|
|
server: serverName,
|
|
|
|
|
type: 'service'
|
|
|
|
|
type: "service",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
shvl.set(constructedService, label.replace("homepage.", ""), substituteEnvironmentVars(containerLabels[label]));
|
|
|
|
|
shvl.set(
|
|
|
|
|
constructedService,
|
|
|
|
|
label.replace("homepage.", ""),
|
|
|
|
|
substituteEnvironmentVars(containerLabels[label])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -132,12 +137,12 @@ export async function servicesFromDocker() {
|
|
|
|
|
function getUrlFromIngress(ingress) {
|
|
|
|
|
const urlHost = ingress.spec.rules[0].host;
|
|
|
|
|
const urlPath = ingress.spec.rules[0].http.paths[0].path;
|
|
|
|
|
const urlSchema = ingress.spec.tls ? 'https' : 'http';
|
|
|
|
|
const urlSchema = ingress.spec.tls ? "https" : "http";
|
|
|
|
|
return `${urlSchema}://${urlHost}${urlPath}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function servicesFromKubernetes() {
|
|
|
|
|
const ANNOTATION_BASE = 'gethomepage.dev';
|
|
|
|
|
const ANNOTATION_BASE = "gethomepage.dev";
|
|
|
|
|
const ANNOTATION_WIDGET_BASE = `${ANNOTATION_BASE}/widget.`;
|
|
|
|
|
const ANNOTATION_POD_SELECTOR = `${ANNOTATION_BASE}/pod-selector`;
|
|
|
|
|
|
|
|
|
@ -151,39 +156,52 @@ export async function servicesFromKubernetes() {
|
|
|
|
|
const networking = kc.makeApiClient(NetworkingV1Api);
|
|
|
|
|
const crd = kc.makeApiClient(CustomObjectsApi);
|
|
|
|
|
|
|
|
|
|
const ingressList = await networking.listIngressForAllNamespaces(null, null, null, null)
|
|
|
|
|
const ingressList = await networking
|
|
|
|
|
.listIngressForAllNamespaces(null, null, null, null)
|
|
|
|
|
.then((response) => response.body)
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
logger.error("Error getting ingresses: %d %s %s", error.statusCode, error.body, error.response);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const traefikIngressListContaino = await crd.listClusterCustomObject("traefik.containo.us", "v1alpha1", "ingressroutes")
|
|
|
|
|
const traefikIngressListContaino = await crd
|
|
|
|
|
.listClusterCustomObject("traefik.containo.us", "v1alpha1", "ingressroutes")
|
|
|
|
|
.then((response) => response.body)
|
|
|
|
|
.catch(async (error) => {
|
|
|
|
|
if (error.statusCode !== 404) {
|
|
|
|
|
logger.error("Error getting traefik ingresses from traefik.containo.us: %d %s %s", error.statusCode, error.body, error.response);
|
|
|
|
|
logger.error(
|
|
|
|
|
"Error getting traefik ingresses from traefik.containo.us: %d %s %s",
|
|
|
|
|
error.statusCode,
|
|
|
|
|
error.body,
|
|
|
|
|
error.response
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const traefikIngressListIo = await crd.listClusterCustomObject("traefik.io", "v1alpha1", "ingressroutes")
|
|
|
|
|
const traefikIngressListIo = await crd
|
|
|
|
|
.listClusterCustomObject("traefik.io", "v1alpha1", "ingressroutes")
|
|
|
|
|
.then((response) => response.body)
|
|
|
|
|
.catch(async (error) => {
|
|
|
|
|
if (error.statusCode !== 404) {
|
|
|
|
|
logger.error("Error getting traefik ingresses from traefik.io: %d %s %s", error.statusCode, error.body, error.response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.error(
|
|
|
|
|
"Error getting traefik ingresses from traefik.io: %d %s %s",
|
|
|
|
|
error.statusCode,
|
|
|
|
|
error.body,
|
|
|
|
|
error.response
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const traefikIngressList = [...traefikIngressListContaino, ...traefikIngressListIo];
|
|
|
|
|
|
|
|
|
|
if (traefikIngressList && traefikIngressList.items.length > 0) {
|
|
|
|
|
const traefikServices = traefikIngressList.items
|
|
|
|
|
.filter((ingress) => ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/href`])
|
|
|
|
|
const traefikServices = traefikIngressList.items.filter(
|
|
|
|
|
(ingress) => ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/href`]
|
|
|
|
|
);
|
|
|
|
|
ingressList.items.push(...traefikServices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -191,43 +209,51 @@ export async function servicesFromKubernetes() {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const services = ingressList.items
|
|
|
|
|
.filter((ingress) => ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/enabled`] === 'true')
|
|
|
|
|
.filter(
|
|
|
|
|
(ingress) =>
|
|
|
|
|
ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/enabled`] === "true"
|
|
|
|
|
)
|
|
|
|
|
.map((ingress) => {
|
|
|
|
|
let constructedService = {
|
|
|
|
|
app: ingress.metadata.name,
|
|
|
|
|
namespace: ingress.metadata.namespace,
|
|
|
|
|
href: ingress.metadata.annotations[`${ANNOTATION_BASE}/href`] || getUrlFromIngress(ingress),
|
|
|
|
|
name: ingress.metadata.annotations[`${ANNOTATION_BASE}/name`] || ingress.metadata.name,
|
|
|
|
|
group: ingress.metadata.annotations[`${ANNOTATION_BASE}/group`] || "Kubernetes",
|
|
|
|
|
weight: ingress.metadata.annotations[`${ANNOTATION_BASE}/weight`] || '0',
|
|
|
|
|
icon: ingress.metadata.annotations[`${ANNOTATION_BASE}/icon`] || '',
|
|
|
|
|
description: ingress.metadata.annotations[`${ANNOTATION_BASE}/description`] || '',
|
|
|
|
|
external: false,
|
|
|
|
|
type: 'service'
|
|
|
|
|
};
|
|
|
|
|
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]) {
|
|
|
|
|
constructedService.external = String(ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]).toLowerCase() === "true"
|
|
|
|
|
}
|
|
|
|
|
if (ingress.metadata.annotations[ANNOTATION_POD_SELECTOR]) {
|
|
|
|
|
constructedService.podSelector = ingress.metadata.annotations[ANNOTATION_POD_SELECTOR];
|
|
|
|
|
}
|
|
|
|
|
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`]) {
|
|
|
|
|
constructedService.ping = ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`];
|
|
|
|
|
}
|
|
|
|
|
Object.keys(ingress.metadata.annotations).forEach((annotation) => {
|
|
|
|
|
if (annotation.startsWith(ANNOTATION_WIDGET_BASE)) {
|
|
|
|
|
shvl.set(constructedService, annotation.replace(`${ANNOTATION_BASE}/`, ""), ingress.metadata.annotations[annotation]);
|
|
|
|
|
let constructedService = {
|
|
|
|
|
app: ingress.metadata.name,
|
|
|
|
|
namespace: ingress.metadata.namespace,
|
|
|
|
|
href: ingress.metadata.annotations[`${ANNOTATION_BASE}/href`] || getUrlFromIngress(ingress),
|
|
|
|
|
name: ingress.metadata.annotations[`${ANNOTATION_BASE}/name`] || ingress.metadata.name,
|
|
|
|
|
group: ingress.metadata.annotations[`${ANNOTATION_BASE}/group`] || "Kubernetes",
|
|
|
|
|
weight: ingress.metadata.annotations[`${ANNOTATION_BASE}/weight`] || "0",
|
|
|
|
|
icon: ingress.metadata.annotations[`${ANNOTATION_BASE}/icon`] || "",
|
|
|
|
|
description: ingress.metadata.annotations[`${ANNOTATION_BASE}/description`] || "",
|
|
|
|
|
external: false,
|
|
|
|
|
type: "service",
|
|
|
|
|
};
|
|
|
|
|
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]) {
|
|
|
|
|
constructedService.external =
|
|
|
|
|
String(ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]).toLowerCase() === "true";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (ingress.metadata.annotations[ANNOTATION_POD_SELECTOR]) {
|
|
|
|
|
constructedService.podSelector = ingress.metadata.annotations[ANNOTATION_POD_SELECTOR];
|
|
|
|
|
}
|
|
|
|
|
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`]) {
|
|
|
|
|
constructedService.ping = ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`];
|
|
|
|
|
}
|
|
|
|
|
Object.keys(ingress.metadata.annotations).forEach((annotation) => {
|
|
|
|
|
if (annotation.startsWith(ANNOTATION_WIDGET_BASE)) {
|
|
|
|
|
shvl.set(
|
|
|
|
|
constructedService,
|
|
|
|
|
annotation.replace(`${ANNOTATION_BASE}/`, ""),
|
|
|
|
|
ingress.metadata.annotations[annotation]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
constructedService = JSON.parse(substituteEnvironmentVars(JSON.stringify(constructedService)));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error("Error attempting k8s environment variable substitution.");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
constructedService = JSON.parse(substituteEnvironmentVars(JSON.stringify(constructedService)));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error("Error attempting k8s environment variable substitution.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return constructedService;
|
|
|
|
|
});
|
|
|
|
|
return constructedService;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mappedServiceGroups = [];
|
|
|
|
|
|
|
|
|
@ -251,7 +277,6 @@ export async function servicesFromKubernetes() {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return mappedServiceGroups;
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error(e);
|
|
|
|
|
throw e;
|
|
|
|
@ -264,7 +289,7 @@ export function cleanServiceGroups(groups) {
|
|
|
|
|
services: serviceGroup.services.map((service) => {
|
|
|
|
|
const cleanedService = { ...service };
|
|
|
|
|
if (cleanedService.showStats !== undefined) cleanedService.showStats = JSON.parse(cleanedService.showStats);
|
|
|
|
|
if (typeof service.weight === 'string') {
|
|
|
|
|
if (typeof service.weight === "string") {
|
|
|
|
|
const weight = parseInt(service.weight, 10);
|
|
|
|
|
if (Number.isNaN(weight)) {
|
|
|
|
|
cleanedService.weight = 0;
|
|
|
|
@ -303,6 +328,7 @@ export function cleanServiceGroups(groups) {
|
|
|
|
|
userEmail, // azuredevops
|
|
|
|
|
repositoryId,
|
|
|
|
|
metric, // glances
|
|
|
|
|
chart, // glances
|
|
|
|
|
stream, // mjpeg
|
|
|
|
|
fit,
|
|
|
|
|
method, // openmediavault widget
|
|
|
|
@ -311,9 +337,10 @@ export function cleanServiceGroups(groups) {
|
|
|
|
|
} = cleanedService.widget;
|
|
|
|
|
|
|
|
|
|
let fieldsList = fields;
|
|
|
|
|
if (typeof fields === 'string') {
|
|
|
|
|
try { JSON.parse(fields) }
|
|
|
|
|
catch (e) {
|
|
|
|
|
if (typeof fields === "string") {
|
|
|
|
|
try {
|
|
|
|
|
JSON.parse(fields);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error("Invalid fields list detected in config for service '%s'", service.name);
|
|
|
|
|
fieldsList = null;
|
|
|
|
|
}
|
|
|
|
@ -373,6 +400,11 @@ export function cleanServiceGroups(groups) {
|
|
|
|
|
}
|
|
|
|
|
if (type === "glances") {
|
|
|
|
|
if (metric) cleanedService.widget.metric = metric;
|
|
|
|
|
if (chart !== undefined) {
|
|
|
|
|
cleanedService.widget.chart = chart;
|
|
|
|
|
} else {
|
|
|
|
|
cleanedService.widget.chart = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (type === "mjpeg") {
|
|
|
|
|
if (stream) cleanedService.widget.stream = stream;
|
|
|
|
|