ran pre-commit

This commit is contained in:
Rikpat 2024-09-03 13:30:45 +02:00
parent f4aff5180e
commit 8aafaf1fba
3 changed files with 74 additions and 63 deletions

View File

@ -161,18 +161,18 @@ metadata:
spec: spec:
description: TV Show Management Application description: TV Show Management Application
group: Media group: Media
href: 'https://sonarr.example.org/' href: "https://sonarr.example.org/"
icon: sonarr.svg icon: sonarr.svg
widget: widget:
type: sonarr type: sonarr
url: 'http://sonarr.media.svc.cluster.local:8989' url: "http://sonarr.media.svc.cluster.local:8989"
keyFrom: keyFrom:
secretKeyRef: secretKeyRef:
key: SONARR_API_KEY key: SONARR_API_KEY
name: arr-secrets name: arr-secrets
``` ```
Some attributes have values inferred from the definition itself, `app` is read from *app.kubernetes.io/name* annotation or metadata.name, namespace is also read from metadata. Some attributes have values inferred from the definition itself, `app` is read from _app.kubernetes.io/name_ annotation or metadata.name, namespace is also read from metadata.
For secrets and configMaps, if `namespace` is not specified in ref object, it assumes it's in the same namespace as CRD. If you want to specify a default namespace (for example for security reason when assigning secrets:read permission for only one namespace), you can specify `defaultSecretNamespace` and `defaultConfigMapNamespace` in `kubernetes.yaml` file. For secrets and configMaps, if `namespace` is not specified in ref object, it assumes it's in the same namespace as CRD. If you want to specify a default namespace (for example for security reason when assigning secrets:read permission for only one namespace), you can specify `defaultSecretNamespace` and `defaultConfigMapNamespace` in `kubernetes.yaml` file.

View File

@ -8,7 +8,7 @@ const logger = createLogger("kubernetes-widget");
export default async function handler(req, res) { export default async function handler(req, res) {
try { try {
const kc = makeKubeConfig(getKubernetesConfig()) const kc = makeKubeConfig(getKubernetesConfig());
if (!kc) { if (!kc) {
return res.status(500).send({ return res.status(500).send({
error: "No kubernetes configuration", error: "No kubernetes configuration",

View File

@ -183,7 +183,6 @@ export async function servicesFromKubernetes() {
const ANNOTATION_WIDGET_BASE = `${ANNOTATION_BASE}/widget.`; const ANNOTATION_WIDGET_BASE = `${ANNOTATION_BASE}/widget.`;
const { instanceName } = getSettings(); const { instanceName } = getSettings();
try { try {
const config = getKubernetesConfig(); const config = getKubernetesConfig();
const kc = makeKubeConfig(config); const kc = makeKubeConfig(config);
@ -204,7 +203,7 @@ export async function servicesFromKubernetes() {
const traefikContainoExists = await checkCRD(kc, "ingressroutes.traefik.containo.us"); const traefikContainoExists = await checkCRD(kc, "ingressroutes.traefik.containo.us");
const traefikExists = await checkCRD(kc, "ingressroutes.traefik.io"); const traefikExists = await checkCRD(kc, "ingressroutes.traefik.io");
const homepageServiceExists = await checkCRD(kc, "homepageservices.gethomepage.dev") const homepageServiceExists = await checkCRD(kc, "homepageservices.gethomepage.dev");
const traefikIngressListContaino = await crd const traefikIngressListContaino = await crd
.listClusterCustomObject("traefik.containo.us", "v1alpha1", "ingressroutes") .listClusterCustomObject("traefik.containo.us", "v1alpha1", "ingressroutes")
@ -251,7 +250,7 @@ export async function servicesFromKubernetes() {
return []; return [];
} }
let homepageServices = [] let homepageServices = [];
if (homepageServiceExists) { if (homepageServiceExists) {
homepageServices = await crd homepageServices = await crd
.listClusterCustomObject("gethomepage.dev", "v1", "homepageservices") .listClusterCustomObject("gethomepage.dev", "v1", "homepageservices")
@ -265,63 +264,75 @@ export async function servicesFromKubernetes() {
); );
return []; return [];
}); });
await Promise.all(homepageServices.items.map(async (service) => { await Promise.all(
const { spec, metadata } = service; homepageServices.items.map(async (service) => {
// Enter default values from metadata or annotations const { spec, metadata } = service;
spec.app = spec.app || metadata.annotations["app.kubernetes.io/name"] || metadata.name // Enter default values from metadata or annotations
spec.namespace = spec.namespace || metadata.namespace spec.app = spec.app || metadata.annotations["app.kubernetes.io/name"] || metadata.name;
spec.name = spec.name || metadata.name spec.namespace = spec.namespace || metadata.namespace;
spec.weight = spec.weight || "0" spec.name = spec.name || metadata.name;
spec.icon = spec.icon || "" spec.weight = spec.weight || "0";
spec.description = spec.description || "" spec.icon = spec.icon || "";
// Parse values from secrets and configmaps in widget spec.description = spec.description || "";
if (spec.widget) { // Parse values from secrets and configmaps in widget
const parsedWidget = {} if (spec.widget) {
await Promise.all(Object.keys(spec.widget).map(async key => { const parsedWidget = {};
// To keep up with kubernetes standard valueFrom await Promise.all(
if (key.endsWith("From")) { Object.keys(spec.widget).map(async (key) => {
if (spec.widget[key].secretKeyRef) { // To keep up with kubernetes standard valueFrom
const { secretKeyRef } = spec.widget[key] if (key.endsWith("From")) {
const secret = await core.readNamespacedSecret(secretKeyRef.name, secretKeyRef.namespace || config.defaultSecretNamespace || metadata.namespace) if (spec.widget[key].secretKeyRef) {
const base64secret = secret.body.data[secretKeyRef.key] const { secretKeyRef } = spec.widget[key];
if (!base64secret) { const secret = await core.readNamespacedSecret(
logger.error( secretKeyRef.name,
"Error getting secret value: Secret %s in namespace %s doesn't contain key %s", secretKeyRef.namespace || config.defaultSecretNamespace || metadata.namespace,
spec.widget[key].secretKeyRef.name, );
metadata.namespace, const base64secret = secret.body.data[secretKeyRef.key];
spec.widget[key].secretKeyRef.key, if (!base64secret) {
); logger.error(
return "Error getting secret value: Secret %s in namespace %s doesn't contain key %s",
spec.widget[key].secretKeyRef.name,
metadata.namespace,
spec.widget[key].secretKeyRef.key,
);
return;
}
parsedWidget[key.substring(0, key.length - 4)] = Buffer.from(base64secret, "base64").toString(
"utf8",
);
} else if (spec.widget[key].configMapKeyRef) {
const { configMapKeyRef } = spec.widget[key];
const configMap = await core.readNamespacedConfigMap(
configMapKeyRef.name,
configMapKeyRef.namespace || config.defaultConfigMapNamespace || metadata.namespace,
);
const configMapValue = configMap.body.data[configMapKeyRef.key];
if (!configMapValue) {
logger.error(
"Error getting configMap value: ConfigMap %s in namespace %s doesn't contain key %s",
spec.widget[key].configMapKey.name,
metadata.namespace,
spec.widget[key].configMapKey.key,
);
return;
}
parsedWidget[key.substring(0, key.length - 4)] = configMapValue;
}
} else {
parsedWidget[key] = spec.widget[key];
} }
parsedWidget[key.substring(0, key.length - 4)] = Buffer.from(base64secret, "base64").toString("utf8") }),
} else if (spec.widget[key].configMapKeyRef) { );
const { configMapKeyRef } = spec.widget[key] spec.widget = parsedWidget;
const configMap = await core.readNamespacedConfigMap(configMapKeyRef.name, configMapKeyRef.namespace || config.defaultConfigMapNamespace || metadata.namespace) }
const configMapValue = configMap.body.data[configMapKeyRef.key] }),
if (!configMapValue) { );
logger.error(
"Error getting configMap value: ConfigMap %s in namespace %s doesn't contain key %s",
spec.widget[key].configMapKey.name,
metadata.namespace,
spec.widget[key].configMapKey.key,
);
return
}
parsedWidget[key.substring(0, key.length - 4)] = configMapValue
}
} else {
parsedWidget[key] = spec.widget[key]
}
}))
spec.widget = parsedWidget
}
}))
} }
const services = [ const services = [
...homepageServices.items ...homepageServices.items
.filter(service => !service.spec.instances || service.spec.instances.includes(instanceName)) .filter((service) => !service.spec.instances || service.spec.instances.includes(instanceName))
.map(service => service.spec), .map((service) => service.spec),
...ingressList.items ...ingressList.items
.filter( .filter(
(ingress) => (ingress) =>
@ -377,7 +388,7 @@ export async function servicesFromKubernetes() {
} }
return constructedService; return constructedService;
}) }),
]; ];
const mappedServiceGroups = []; const mappedServiceGroups = [];
@ -403,7 +414,7 @@ export async function servicesFromKubernetes() {
return mappedServiceGroups; return mappedServiceGroups;
} catch (e) { } catch (e) {
console.log(e) console.log(e);
if (e) logger.error(e); if (e) logger.error(e);
throw e; throw e;
} }