useKubernetes.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const App = require('../../../models/App');
  2. const k8s = require('@kubernetes/client-node');
  3. const Logger = require('../../../utils/Logger');
  4. const logger = new Logger();
  5. const loadConfig = require('../../../utils/loadConfig');
  6. const useKubernetes = async (apps) => {
  7. const { useOrdering: orderType, unpinStoppedApps } = await loadConfig();
  8. let ingresses = null;
  9. try {
  10. const kc = new k8s.KubeConfig();
  11. kc.loadFromCluster();
  12. const k8sNetworkingV1Api = kc.makeApiClient(k8s.NetworkingV1Api);
  13. await k8sNetworkingV1Api.listIngressForAllNamespaces().then((res) => {
  14. ingresses = res.body.items;
  15. });
  16. } catch {
  17. logger.log("Can't connect to the Kubernetes API", 'ERROR');
  18. }
  19. if (ingresses) {
  20. apps = await App.findAll({
  21. order: [[orderType, 'ASC']],
  22. });
  23. ingresses = ingresses.filter(
  24. (e) => Object.keys(e.metadata.annotations).length !== 0
  25. );
  26. const kubernetesApps = [];
  27. for (const ingress of ingresses) {
  28. const annotations = ingress.metadata.annotations;
  29. if (
  30. 'flame.pawelmalak/name' in annotations &&
  31. 'flame.pawelmalak/url' in annotations &&
  32. /^app/.test(annotations['flame.pawelmalak/type'])
  33. ) {
  34. kubernetesApps.push({
  35. name: annotations['flame.pawelmalak/name'],
  36. url: annotations['flame.pawelmalak/url'],
  37. icon: annotations['flame.pawelmalak/icon'] || 'kubernetes',
  38. // Add description and visibility
  39. description: annotations['flame.pawelmalak/description'] || '',
  40. isPublic: (annotations['flame.pawelmalak/visible'] && /^true$/.test(annotations['flame.pawelmalak/visible']) ? 1 : 0),
  41. });
  42. }
  43. }
  44. if (unpinStoppedApps) {
  45. for (const app of apps) {
  46. await app.update({ isPinned: false });
  47. }
  48. }
  49. for (const item of kubernetesApps) {
  50. // Find by name or url
  51. if (apps.some((app) => app.name === item.name || app.url === item.url)) {
  52. const app = apps.find((a) => a.name === item.name || app.url === item.url);
  53. await app.update({ ...item, isPinned: true });
  54. } else {
  55. await App.create({
  56. ...item,
  57. isPinned: true,
  58. });
  59. }
  60. }
  61. }
  62. };
  63. module.exports = useKubernetes;