@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
84 lines (83 loc) • 4.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeploymentPreparator = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const k8s_1 = __importDefault(require("../../../modules/k8s"));
class DeploymentPreparator {
constructor(cluster, namespace, appSlug, env) {
this.cluster = cluster;
this.namespace = namespace;
this.appSlug = appSlug;
this.env = env;
}
async prepareNamespace(onUpdate) {
const { contextName: context } = this.cluster;
// Check if namespace exists
const isNsExisted = await k8s_1.default.isNamespaceExisted(this.namespace, { context });
if (!isNsExisted) {
const message = `Namespace "${this.namespace}" not found, creating one...`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(message);
(0, log_1.logWarn)(message);
const createNsRes = await k8s_1.default.createNamespace(this.namespace, { context });
if (!createNsRes) {
const error = `Unable to create new namespace: ${this.namespace}`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(error);
throw new Error(error);
}
return true;
}
return false;
}
async createImagePullSecrets(onUpdate) {
const { contextName: context } = this.cluster;
try {
const { name: imagePullSecretName } = await k8s_1.default.createImagePullSecretsInNamespace(this.appSlug, this.env, this.cluster.slug, this.namespace);
const message = `Created "${imagePullSecretName}" imagePullSecrets in the "${this.namespace}" namespace`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(message);
return { name: imagePullSecretName };
}
catch (error) {
if (error.message.indexOf("already exists") > -1) {
const message = `ImagePullSecrets "${this.appSlug}" already exists in the "${this.namespace}" namespace`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(message);
return { name: this.appSlug };
}
(0, log_1.logError)(`[DeploymentPreparator > createImagePullSecrets]`, error);
const errorMessage = `Can't create imagePullSecrets: ${error.message}`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(errorMessage);
throw new Error(errorMessage);
}
}
async applyDeployment(processedYaml, message, onUpdate) {
const { contextName: context } = this.cluster;
try {
// Apply the deployment YAML
await k8s_1.default.kubectlApplyContent(processedYaml, { context });
const applyMessage = `Applied new deployment YAML successfully.`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(applyMessage);
// Annotate the deployment
const deploymentName = this.extractDeploymentName(processedYaml);
if (deploymentName) {
const annotation = `kubernetes.io/change-cause="${message}"`;
await k8s_1.default.kubectlAnnotateDeployment(annotation, deploymentName, this.namespace, {
context,
});
const annotateMessage = `Annotated deployment "${deploymentName}" with "${annotation}"`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(annotateMessage);
}
}
catch (error) {
const errorMessage = `Failed to apply deployment: ${error.message}`;
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(errorMessage);
throw new Error(errorMessage);
}
}
extractDeploymentName(yaml) {
const deploymentNameMatch = yaml.match(/metadata:\n\s*name:\s*([^\n]+)/);
return deploymentNameMatch ? deploymentNameMatch[1].trim() : null;
}
}
exports.DeploymentPreparator = DeploymentPreparator;