@controlplane/cli
Version:
Control Plane Corporation CLI
77 lines • 3.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractSecretNames = exports.getWorkloadHealth = void 0;
function getWorkloadHealth(deployments, workload) {
var _a, _b;
const readyCheckTimestamp = new Date().toISOString();
if (deployments.length < 1) {
return { ready: false, readyLatest: false, readyCheckTimestamp };
}
let ready = 0;
let latestReady = 0;
for (let deployment of deployments) {
const expectedDeploymentVersion = deployment.status.expectedDeploymentVersion;
if (deployment.status.ready && !deployment.status.internal.syncFailed) {
ready += 1;
const latestVersion = deployment.status.versions.find((v) => v.workload === expectedDeploymentVersion);
if (latestVersion) {
if (latestVersion.ready) {
latestReady += 1;
}
}
}
}
let totalLocations = deployments.length;
for (let localOption of workload.spec.localOptions || []) {
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 (ready > 0) {
return {
ready: true,
readyLatest: workload.spec.type === 'cron' ? true : latestReady >= totalLocations,
readyCheckTimestamp,
};
}
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
;