@controlplane/cli
Version:
Control Plane Corporation CLI
96 lines • 4.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractSecretNames = exports.getWorkloadHealth = void 0;
function getWorkloadHealth(deployments, workload) {
var _a, _b;
// Timestamp to record when the health check is performed
const readyCheckTimestamp = new Date().toISOString();
// If there are no deployments or the deployments array is invalid, workload is not ready
if (!deployments || !Array.isArray(deployments) || deployments.length < 1) {
return { ready: false, readyLatest: false, readyCheckTimestamp };
}
// Initialize counters for ready deployments and total locations
let ready = 0;
let latestReady = 0;
let totalLocations = deployments.length;
// Iterate through each deployment to assess readiness
for (let deployment of deployments) {
// Skip if status information is missing
if (!deployment.status) {
continue;
}
// Check if deployment reports ready and has not failed synchronization
if (deployment.status.ready && !deployment.status.internal.syncFailed) {
// Increment count of ready deployments
ready += 1;
// Get the last processed version, defaulting to 0 if missing
const lastProcessedVersion = deployment.status.lastProcessedVersion || 0;
// Skip counting latest readiness if processed version is behind workload version
if (lastProcessedVersion < workload.version) {
continue;
}
// Find the version object corresponding to the expected deployment version
const latestVersion = (deployment.status.versions || []).find((v) => v.workload === deployment.status.expectedDeploymentVersion);
// If that version is marked ready, count it toward latestReady
if (latestVersion && latestVersion.ready) {
latestReady += 1;
}
}
}
// Exclude locations that are suspended or have autoscaling turned off
for (let localOption of workload.spec.localOptions || []) {
// If location is suspended or both minScale and maxScale are zero, do not count it
if (localOption.suspend || (((_a = localOption.autoscaling) === null || _a === void 0 ? void 0 : _a.minScale) === 0 && ((_b = localOption.autoscaling) === null || _b === void 0 ? void 0 : _b.maxScale) === 0)) {
totalLocations -= 1;
}
}
// If at least one deployment is ready, compute readiness flags
if (ready > 0) {
return {
ready: true,
readyLatest: workload.spec.type === 'cron' ? true : latestReady >= totalLocations,
readyCheckTimestamp,
};
}
// If no deployments are ready, report not ready
return { ready: false, readyLatest: false, readyCheckTimestamp };
}
exports.getWorkloadHealth = getWorkloadHealth;
function extractSecretNames(workload) {
var _a, _b;
// Initialize an empty set for secret names
const secretNames = new Set();
// Regex to match secret references
const secretRegex = /cpln:\/\/secret\/(.+)|\/org\/[^/]+\/secret\/(.+)/;
// Process containers in the workload
(_b = (_a = workload.spec) === null || _a === void 0 ? void 0 : _a.containers) === null || _b === void 0 ? void 0 : _b.forEach((container) => {
var _a, _b;
// Process environment variables
(_a = container.env) === null || _a === void 0 ? void 0 : _a.forEach((envVar) => addSecretName(envVar.value, secretNames, secretRegex));
// Process volumes
(_b = container.volumes) === null || _b === void 0 ? void 0 : _b.forEach((volume) => addSecretName(volume.uri, secretNames, secretRegex));
});
// Return the set of unique secret names
return secretNames;
}
exports.extractSecretNames = extractSecretNames;
function addSecretName(value, secretNames, secretRegex) {
var _a;
// Skip if the value is undefined or null
if (!value) {
return;
}
// Match secret reference in the value
const match = value.match(secretRegex);
// Skip if there was no match
if (!match) {
return;
}
// Extract the name before the dot
const secretName = (_a = (match[1] || match[2])) === null || _a === void 0 ? void 0 : _a.split('.')[0];
// Add the secret name to the set if found
if (secretName) {
secretNames.add(secretName);
}
}
//# sourceMappingURL=workload.js.map