k8ts
Version:
Powerful framework for building Kubernetes manifests in TypeScript.
89 lines • 3.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResourceLoader = void 0;
const instruments_1 = require("@k8ts/instruments");
const doddle_1 = require("doddle");
const emittery_1 = __importDefault(require("emittery"));
const immutable_1 = require("immutable");
const error_1 = require("../../error");
const meta_1 = require("./meta");
class ResourceLoader extends emittery_1.default {
_options;
constructor(_options) {
super();
this._options = _options;
}
_attachSourceAnnotations(loadEvent) {
const { resource } = loadEvent;
resource.meta.add(meta_1.k8ts_namespace, {
"^declared-in": `(${resource.origin.root.name}) ${resource.origin.name}`
});
}
_checkNames(resources) {
let names = (0, immutable_1.Map)();
const nameRegexp = /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/;
for (const resource of resources) {
const name = [resource.kind.name, resource.namespace, resource.name]
.filter(Boolean)
.join("/");
const existing = names.get(name);
if (existing) {
throw new error_1.MakeError(`Duplicate resource designation ${name}. Existing: ${existing.format("source")}, new: ${resource.format("source")}`);
}
if (!nameRegexp.test(resource.name)) {
throw new error_1.MakeError(`Invalid resource name ${resource.name}. Must match ${nameRegexp}`);
}
names = names.set(name, resource);
}
}
async load(input) {
// TODO: Handle ORIGINS that are referenced but not passed to the runner
const parentOrigin = input.__node__;
const addResource = async (res) => {
if (resources.has(res)) {
return;
}
if (!res.isRoot) {
return;
}
const origin = res.origin;
if (!origin.isChildOf(parentOrigin)) {
return;
}
const event = {
isExported: res.meta.has(`#${meta_1.k8ts_namespace}is-exported`),
resource: res
};
this._attachSourceAnnotations(event);
await this.emit("load", event);
resources = resources.add(res);
};
let resources = (0, immutable_1.Set)();
// We execute the main FILE iterable to load all the resources attached to the origin
(0, doddle_1.seq)(input).toArray().pull();
for (let res of input.__node__.resources) {
if (instruments_1.ForwardRef.is(res)) {
throw new error_1.MakeError(`Resource ${res} is a forward reference`);
}
await addResource(res);
}
// Some resources might appear as dependencies to sub-resources that
// haven't loaded. We can acquire them by getting all the needed resources
for (const resource of resources.toArray()) {
for (const relation of resource.recursiveRelationsSubtree) {
await addResource(relation.needed);
}
}
// The lazy sections might have attached more resources to the origin
for (const resource of parentOrigin.resources) {
await addResource(resource);
}
this._checkNames(resources);
return resources.toArray();
}
}
exports.ResourceLoader = ResourceLoader;
//# sourceMappingURL=loader.js.map