@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
102 lines (101 loc) • 5.44 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.seedInitialClusters = void 0;
const js_yaml_1 = __importDefault(require("js-yaml"));
const lodash_1 = require("lodash");
const mongoose_1 = __importDefault(require("mongoose"));
const app_config_1 = require("../app.config");
const entities_1 = require("../entities");
const seed_clusters_1 = require("../seeds/seed-clusters");
const services_1 = require("../services");
const seedInitialClusters = async () => {
var _a;
const { WorkspaceService } = await Promise.resolve().then(() => __importStar(require("../services/WorkspaceService")));
const { ClusterService } = await Promise.resolve().then(() => __importStar(require("../services/ClusterService")));
const clusterSvc = new ClusterService();
const workspaceSvc = new WorkspaceService();
const userSvc = new services_1.UserService();
if (app_config_1.Config.grab("INITIAL_CLUSTER_KUBECONFIG")) {
// find all workspaces that don't have this initial cluster
const kubeConfig = app_config_1.Config.grab("INITIAL_CLUSTER_KUBECONFIG");
try {
const Cluster = mongoose_1.default.model("clusters", entities_1.clusterSchema, "clusters");
const Workspace = mongoose_1.default.model("workspaces", entities_1.workspaceSchema, "workspaces");
// validate YAML
const kubeConfigObject = js_yaml_1.default.load(kubeConfig);
const clusterServer = (_a = kubeConfigObject.clusters[0].cluster) === null || _a === void 0 ? void 0 : _a.server;
if (!clusterServer)
return;
// extract cluster server URL & IP address
const clusterServerURL = new URL(clusterServer);
const clusterIP = clusterServerURL === null || clusterServerURL === void 0 ? void 0 : clusterServerURL.hostname;
if (!clusterIP)
return;
// Tìm initial cluster
const initialCluster = await Cluster.find({
primaryIP: clusterIP,
kubeConfig: { $regex: kubeConfigObject.clusters[0].cluster["certificate-authority-data"] },
});
console.log("initialCluster :>> ", initialCluster);
if ((0, lodash_1.isEmpty)(initialCluster)) {
const cluster = await (0, seed_clusters_1.addInitialBareMetalCluster)(kubeConfig);
console.log("Added initial cluster!");
}
// // Tìm tất cả workspace có clusters với "kubeConfig" là "kubeConfig"
// const workspaceIdsWithInitialCluster = await Cluster.find({
// primaryIP: clusterIP,
// kubeConfig: { $regex: kubeConfigObject.clusters[0].cluster["certificate-authority-data"] },
// }).distinct("workspace");
// console.log("workspaceIdsWithInitialCluster :>> ", workspaceIdsWithInitialCluster.length);
// // Tìm các workspace không có trong danh sách trên
// const workspacesWithoutInitialCluster = await workspaceSvc.find({
// _id: { $nin: workspaceIdsWithInitialCluster },
// });
// // console.log("workspacesWithoutInitialCluster :>> ", workspacesWithoutInitialCluster);
// console.log("workspacesWithoutInitialCluster :>> ", workspacesWithoutInitialCluster.length);
// // Tạo cluster cho tất cả workspace
// workspacesWithoutInitialCluster.forEach(async (workspace) => {
// const owner = await userSvc.findOne({ _id: workspace.owner });
// if (!owner) {
// console.error(`Error: owner "${workspace.owner}" not found -> delete workspace!`);
// await Workspace.deleteOne({ _id: workspace._id });
// return;
// }
// const cluster = await addInitialBareMetalCluster(kubeConfig, workspace, owner);
// console.log("Added initial cluster to workspace :>> ", workspace.name, workspace._id);
// });
// return workspacesWithoutInitialCluster;
}
catch (error) {
console.error("Error:", error);
throw error;
}
}
};
exports.seedInitialClusters = seedInitialClusters;