@controlplane/cli
Version:
Control Plane Corporation CLI
80 lines • 4.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCplnAnnotation = getCplnAnnotation;
exports.isSharedPvc = isSharedPvc;
exports.convertsToDictionarySecret = convertsToDictionarySecret;
exports.ensureNumber = ensureNumber;
exports.ensurePropertyPresence = ensurePropertyPresence;
exports.raiseCustomK8sError = raiseCustomK8sError;
exports.getFormattedWarning = getFormattedWarning;
/**
* Reads a Control Plane annotation from a K8s object, preferring the object's own metadata over the pod template metadata.
*
* @param {string} annotationName - The full annotation name to look up (e.g. 'cpln.io/workload-type').
* @param {K8sObject} object - The top-level K8s object (Deployment, StatefulSet, CronJob, etc.).
* @param {K8sMetadata | undefined} templateMetadata - (Optional) The pod template metadata used as a fallback.
* @returns {string | undefined} The annotation value as a string, or undefined when the annotation is absent.
*/
function getCplnAnnotation(annotationName, object, templateMetadata) {
var _a, _b, _c, _d;
const value = (_c = (_b = (_a = object.metadata) === null || _a === void 0 ? void 0 : _a.annotations) === null || _b === void 0 ? void 0 : _b[annotationName]) !== null && _c !== void 0 ? _c : (_d = templateMetadata === null || templateMetadata === void 0 ? void 0 : templateMetadata.annotations) === null || _d === void 0 ? void 0 : _d[annotationName];
// The YAML loader coerces unquoted scalars (e.g. `true`, `123`) to non-string types, so normalize to the string an annotation always is
return value === undefined || value === null ? undefined : String(value);
}
/**
* Determines whether a persistent volume claim requires shared (multi-node) storage.
*
* @param {K8sPersistentVolumeClaim} pvc - The persistent volume claim to inspect.
* @returns {boolean} True when any access mode allows access from multiple nodes.
*/
function isSharedPvc(pvc) {
var _a, _b;
const accessModes = (_b = (_a = pvc.spec) === null || _a === void 0 ? void 0 : _a.accessModes) !== null && _b !== void 0 ? _b : [];
return accessModes.includes('ReadWriteMany') || accessModes.includes('ReadOnlyMany');
}
/**
* Determines whether a K8s Secret converts to a Control Plane dictionary secret, whose keys are
* individually addressable via `cpln://secret/<name>.<key>` references.
*
* Mirrors the type determination in K8sSecretHandler; keep the two in sync.
*
* @param {K8sSecret} k8sSecret - The K8s Secret to inspect.
* @returns {boolean} True when the secret converts to a dictionary secret.
*/
function convertsToDictionarySecret(k8sSecret) {
var _a, _b;
// A secret carrying both data and stringData always merges into a dictionary
if (k8sSecret.data && k8sSecret.stringData) {
return true;
}
// Docker config secrets hold a single registry-credentials blob
if (k8sSecret.type == 'kubernetes.io/dockerconfigjson') {
return false;
}
// Basic-auth secrets convert to a userpass secret, not a per-key dictionary
if (k8sSecret.type == 'kubernetes.io/basic-auth') {
return false;
}
// A 'payload' key converts to an opaque single-value secret
const data = (_b = (_a = k8sSecret.data) !== null && _a !== void 0 ? _a : k8sSecret.stringData) !== null && _b !== void 0 ? _b : {};
return !Object.prototype.hasOwnProperty.call(data, 'payload');
}
function ensureNumber(value) {
if (typeof value === 'string') {
return parseFloat(value);
}
return value;
}
function ensurePropertyPresence(property, filePath, object) {
if (!object) {
throw new Error(`ERROR: '${property}' is required for every object in the file '${filePath}'`);
}
throw new Error(`ERROR: '${property}' is required for object of kind '${object.kind}' and of metadata.name '${object.metadata.name}' in the file '${filePath}'`);
}
function raiseCustomK8sError(message, filePath, object) {
throw new Error(`ERROR: ${message}. Object of kind ${object.kind} and of metadata.name '${object.metadata.name}' in the file '${filePath}'`);
}
function getFormattedWarning(message, filePath, object) {
return `WARNING: ${message}. Object of kind ${object.kind} and of metadata.name '${object.metadata.name}' in the file '${filePath}'`;
}
//# sourceMappingURL=helper.js.map